-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGame.pde
207 lines (166 loc) · 5.81 KB
/
Game.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
class Game {
Ship ship;
InputController controller = new InPlayController();
AsteroidFactory asteroidFactory = new AsteroidFactory();
DisplayList displayList = new DisplayList();
private int score = 0;
int roids = 2;
int ships = 3;
int level = 0;
int timeOnLevel = 0;
int levelFrameStart;
boolean waiting = false;
EnemyShip enemy = null;
int nextEnemy = 0;
void init(boolean home) {
sounds.killAllSounds();
if (home) {
println("Home Screen");
controller = new NewGameController();
addObject(new HomeScreen());
for (int i = 0 ; i < 6 ; i++)
addObject(asteroidFactory.createLargeEx(null));
return;
}
// Real game
addObject(new ScoreBoard());
// Create the ship
tryAddingShip();
newLevel();
}
void startNewLevel(ListIterator it) {
sounds.startLevel();
waiting = false;
println("Starting level " + level + " with " + roids + " asteroids.");
// Add asteroids
for (int i = 0 ; i < roids ; i++)
it.add(asteroidFactory.createLargeEx(null));
}
void newLevel() {
waiting = true;
level++;
roids = min(roids + 2, 11);
timeOnLevel = 0;
levelFrameStart = frameCount;
println("Next Level : " + level);
// Wait one second before starting the level.
addObject(new NewGame());
}
void addObject(DisplayObject o) {
displayList.add(o);
}
void shipDied() {
ships--;
println("Ship died, you have " + ships + " left!");
}
void gameOver(ListIterator it) {
println("Game over!");
sounds.stopGame();
controller = new GameOverController();
it.add(new GameOver());
}
int getScore() {
return score;
}
int addPoints(int points) {
if (ships > 0)
score += points;
return score;
}
void newShip(ListIterator it) {
if (ships == 0) {
gameOver(it);
return;
}
tryNewShip = true;
}
boolean tryNewShip = false;
final Dimension safeWH = new Dimension(100, 100);
void tryAddingShip() {
// Wait until center is clear
Point center = new Point(width/2, height/2);
if (!isSafeZone(center, safeWH)) {
println("Center not clear!");
return;
}
println("Found a clearing!");
ship = new Ship(width/2, height/2);
addObject(ship);
tryNewShip = false;
}
public Point findSafeZone(Dimension size) {
println("Finding safe zone of size " + size.toString());
int MAX_TRIES = 20;
Point center;
for (int i = 0 ; i < MAX_TRIES ; i++) {
center = new Point((int)random(width * 0.15, width * 0.85), (int)random(height * 0.15, height * 0.85));
println("..." + center.toString());
if (isSafeZone(center, size))
return center;
}
return null;
}
public boolean isSafeZone(Point center, Dimension wh) {
Rectangle zone = new Rectangle(center.x - wh.width/2, center.y - wh.height/2, wh.width, wh.height);
println("Checking safe zone " + zone.toString());
Iterator it = displayList.iterator();
while (it.hasNext()) {
DisplayObject fo = (DisplayObject)it.next();
if (!(fo instanceof FlyingObject))
continue;
if (zone.intersects(((FlyingObject)fo).rect())) {
return false;
}
}
return true;
}
// Game Loop
void draw() {
if (tryNewShip)
tryAddingShip();
sounds.playBoop();
int last = timeOnLevel;
timeOnLevel = (int)((frameCount - levelFrameStart) / frameRate);
if (timeOnLevel != last) {
//println("Time on level: " + timeOnLevel + " seconds.");
//println("" + (frameCount - levelFrameStart) + ", " + (frameRate * 3));
}
// We add our first ship after 20 seconds for levels 1-2, 15 for 3-5
// then 10 for all those after that.
int time_until_enemy_l12 = 20; // 20
int time_until_enemy_l345 = 15;
int time_until_enemy_l6up = 10;
if (enemy == null && timeOnLevel > ((level < 3) ? time_until_enemy_l12 : ((level < 6) ? time_until_enemy_l345 : time_until_enemy_l6up)) ) {
if (nextEnemy == 0) {
nextEnemy = (int)(frameCount + (int)random(10,20) * frameRate);
println("Next enemy at " + nextEnemy);
}
else if (frameCount >= nextEnemy) {
println("Adding enemy ship");
nextEnemy = 0;
// <= 10K always show big ship, above randomly choose
enemy = new EnemyShip(score > 10000 ? (int)random(0, 10) % 2 == 1 : true);
addObject(enemy);
}
}
// Check for keyboard action then update the display list.
controller.checkKeyboard();
displayList.update();
displayList.hitTest();
// Draw
background(0);
Iterator it = displayList.iterator();
int asteroids = 0;
while (it.hasNext()) {
DisplayObject fo = (DisplayObject)it.next();
if (fo instanceof Asteroid)
asteroids++;
pushMatrix();
fo.draw();
popMatrix();
}
if (asteroids == 0 && enemy == null && !waiting) {
newLevel();
}
}
}