-
Notifications
You must be signed in to change notification settings - Fork 2
/
Game.dart
208 lines (181 loc) · 5.93 KB
/
Game.dart
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
208
/**
* Author: Jimmy Pettersson.
*
* Game class containing the overall logic of the game.
*
* Known issues: Vertical sync issues.
*
* */
class Game {
CanvasRenderingContext2D context;
Player player;
List<Rocket> playerRockets, enemyRockets;
List<EnemyRow> enemyRows;
int score, level, tickId, movePlayerId, moveEnemyDownId, actionFlag;
static ImageElement playerImage, enemyImage, rocketImage;
static final int WIDTH = 800;
static final int HEIGHT = 800;
static final int LEFT = 37;
static final int RIGHT = 39;
static final int FIRE = 32;
static final int NONE = 0;
static final int DELAY = 20; // 50fps
static final int MOVE_DOWN_DELAY = 100;
Game(CanvasElement canvas, ImageElement playerImage, ImageElement enemyImage, ImageElement rocketImage) {
context = canvas.context2d;
Game.playerImage = playerImage;
Game.enemyImage = enemyImage;
Game.rocketImage = rocketImage;
playerRockets = new List<Rocket>();
enemyRockets = new List<Rocket>();
enemyRows = new List<EnemyRow>();
player = new Player(context);
score = 0;
level = 1;
window.on.keyDown.add(handleKeyDown);
window.on.keyUp.add(handleKeyUp);
}
/** Sets up the initial game state and draws the game board. */
void setup() {
drawStatics();
initializeLevel();
}
/** Add enemies to the level. */
void initializeLevel() {
enemyRows.add(new EnemyRow(context, 60, 50));
enemyRows.add(new EnemyRow(context, 60, 100));
enemyRows.add(new EnemyRow(context, 60, 150));
enemyRows.add(new EnemyRow(context, 60, 200));
}
/** Event handler for when a key is pressed. */
void handleKeyDown(KeyboardEvent event) {
if (event.keyCode == LEFT) {
actionFlag = LEFT;
} else if (event.keyCode == RIGHT) {
actionFlag = RIGHT;
} else if (event.keyCode == FIRE && playerRockets.length < 5) {
playerRockets.add(new Rocket(context, player.rocketCenterX, player.rocketTop));
}
}
/** Event handler for when a key is relased. */
void handleKeyUp(KeyboardEvent event) {
if (event.keyCode == LEFT || event.keyCode == RIGHT) actionFlag = NONE;
}
/** Starts the game by starting the different timers. */
void start() {
tickId = window.setInterval(tick, DELAY);
movePlayerId = window.setInterval(movePlayer, DELAY);
int moveDelay = MOVE_DOWN_DELAY - 5 * level;
moveEnemyDownId = window.setInterval(moveEnemyDown, moveDelay);
}
/** Stops the current game. */
void stop() {
clearIntervals();
drawStatics();
}
/**
* A game "tick" invoked each DELAY ms.
* Updates all the states of the game and checks for collisions.
*/
void tick() {
drawStatics();
player.draw();
updateRocketPositions();
updateEnemyPositions();
checkCollisions();
}
/** Updates the positions on all rockets in play. */
void updateRocketPositions() {
for (Rocket r in playerRockets) {
r.updatePosition(0, Directions.UP * Rocket.DY);
if (r.invalid) playerRockets.removeRange(playerRockets.indexOf(r), 1);
}
for (Rocket r in enemyRockets) {
r.updatePosition(0, Directions.DOWN * Rocket.DY);
if (r.invalid) enemyRockets.removeRange(enemyRockets.indexOf(r), 1);
}
}
/** Updates the position of all enemies and checks for game over. */
void updateEnemyPositions() {
for (EnemyRow er in enemyRows) {
er.updateEnemyPositions();
if (er.reachedBottom()) gameOver();
}
}
/** Collision detection between rockets and enemies/player. */
void checkCollisions() {
/* Player rockets hitting enemies */
for (EnemyRow er in enemyRows) {
for (Enemy e in er.enemies) {
for (Rocket r in playerRockets) {
if (e.checkCollision(r)) {
score++;
playerRockets.removeRange(playerRockets.indexOf(r), 1);
er.removeEnemy(e);
if (er.empty) enemyRows.removeRange(enemyRows.indexOf(er), 1);
if (enemyRows.isEmpty()) advanceLevel();
// This enemy has already been hit by a rocket, no need to loop over the rest.
break;
}
}
}
}
/* Enemy rockets hitting player */
for (Rocket r in enemyRockets) {
if (player.checkCollision(r)) {
gameOver();
break;
}
}
}
/** Moves the player. */
void movePlayer() {
if (actionFlag == LEFT && player.x > 0) {
player.updatePosition(Directions.LEFT * Player.DX, 0);
} else if (actionFlag == RIGHT && player.x < Game.WIDTH - Player.SIZE) {
player.updatePosition(Directions.RIGHT * Player.DX, 0);
}
}
/** Moves the enemies downward slowly. Acts on a separate timer. */
void moveEnemyDown() {
for (EnemyRow er in enemyRows) {
er.moveEnemyDown();
}
/* Fire rockets at random on this timer */
if (enemyRockets.length < 3 && Math.random() > 0.95) {
EnemyRow er = enemyRows[(Math.random() * enemyRows.length).toInt()];
Enemy e = er.enemies[(Math.random() * er.enemies.length).toInt()];
enemyRockets.add(new Rocket(context, e.rocketCenterX, e.rocketBottom));
}
}
/** Advances the game one level. */
void advanceLevel() {
level++;
clearIntervals();
drawStatics();
playerRockets.clear();
enemyRockets.clear();
setup();
start();
}
/** Clear all timers */
void clearIntervals() {
window.clearInterval(tickId);
window.clearInterval(movePlayerId);
window.clearInterval(moveEnemyDownId);
}
/** Draws the background gradient, the score and level texts and the bottom line */
void drawStatics() {
GameDrawer.drawBackground(context);
GameDrawer.updateScoreText(context, score);
GameDrawer.updateLevelText(context, level);
GameDrawer.drawBottomLine(context);
}
void gameOver() {
clearIntervals();
drawStatics();
GameDrawer.drawGameOver(context);
startButton.disabled = false;
stopButton.disabled = true;
}
}