-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathscript.js
More file actions
221 lines (189 loc) · 4.44 KB
/
script.js
File metadata and controls
221 lines (189 loc) · 4.44 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
// Key codes
var CODES = {
37: 'left',
38: 'up',
39: 'right',
40: 'down'
};
// Input state
var pressed = {
left: false,
up: false,
right: false,
down: false
};
// Game configuration
var cfg = {
fps: 60,
width: 640,
height: 480
};
var $container = $('#container');
// Game entities
var player;
var enemies = [];
var bullets = [];
function Player (x, y) {
this.position = {
x: x,
y: y
};
this.velocity = {
x: 0,
y: 0
};
this.speed = 5;
this.health = 3;
this.width = 50;
this.height = 50;
this.element = $('<div class="player">').appendTo($container);
}
function Enemy (x, y) {
this.position = {
x: x,
y: y
};
this.velocity = {
x: 0,
y: 0
};
this.health = 1;
this.width = 50;
this.height = 50;
this.element = $('<div class="enemy">').appendTo($container);
this.isAttacking = false;
this.timeWhenAttackStarted = 0;
}
function Bullet (x, y) {
this.position = {
x: x,
y: y
};
this.velocity = {
x: 0,
y: 10
};
this.health = 1;
this.width = 5;
this.height = 20;
this.element = $('<div class="bullet">').appendTo($container);
}
function setup () {
// Reset state
$container.empty();
player = new Player(320, 50);
enemies = [];
bullets = [];
// Spawn enemies
enemies.push(new Enemy(100, 360));
enemies.push(new Enemy(210, 360));
enemies.push(new Enemy(320, 360));
enemies.push(new Enemy(430, 360));
enemies.push(new Enemy(540, 360));
}
// Game logic
function update () {
// Spawn bullet
if (pressed.up) {
bullets.push(new Bullet(player.position.x, player.position.y + player.height));
}
// Left-right movement
if (pressed.left) {
player.velocity.x = -player.speed;
} else if (pressed.right) {
player.velocity.x = player.speed;
} else {
player.velocity.x = 0;
}
// Enemy attack movement
if (enemies.length) {
runEnemyAI(enemies);
}
// Amazing physics simulation
var entities = [player].concat(enemies, bullets);
entities.forEach(function(entity) {
entity.position.x += entity.velocity.x;
entity.position.y += entity.velocity.y;
});
// TODO: Collision detection & health adjustment
// Player bounds checking
if (player.position.x < 0) {
player.position.x = 0;
} else if (player.position.x > cfg.width) {
player.position.x = cfg.width;
}
// Bullet bounds/health checking
for (var i = bullets.length - 1; i >= 0; i--) {
var bullet = bullets[i];
// Remove bullet if dead or out of bounds
if (bullet.health <= 0 || bullet.position.y > cfg.height + bullet.height) {
bullet.element.remove();
bullets.splice(i, 1);
}
}
// Enemy health checking
for (var j = enemies.length - 1; j >= 0; j--) {
var enemy = enemies[j];
if (enemy.health <= 0) {
enemy.element.remove();
enemies.splice(j, 1);
}
}
// TODO: Lose condition (i.e. if player is dead)
// TODO: Win condition (i.e. if all enemies are dead)
}
// Enemy attack movement
function runEnemyAI (enemies) {
var attackingEnemy;
// Find enemy that is currently attacking
enemies.forEach(function (enemy) {
if (enemy.isAttacking) {
attackingEnemy = enemy;
}
});
// Choose 1 enemy to attack the player, if none currently are
if (!attackingEnemy) {
attackingEnemy = enemies[Math.floor(Math.random() * enemies.length)];
attackingEnemy.isAttacking = true;
attackingEnemy.timeWhenAttackStarted = Date.now();
}
var attackTime = Date.now() - attackingEnemy.timeWhenAttackStarted;
var epsilon = 0.3;
if (attackTime/540 > 2*Math.PI - epsilon) {
attackingEnemy.isAttacking = false;
attackingEnemy.velocity.x = 0;
attackingEnemy.velocity.y = 0;
return;
}
attackingEnemy.velocity.x = Math.sin(attackTime/180) * 5;
attackingEnemy.velocity.y = -Math.sin(attackTime/540)* 5;
}
// Rendering logic
function draw () {
$container.width(cfg.width);
$container.height(cfg.height);
var entities = [player].concat(enemies, bullets);
entities.forEach(function (entity) {
entity.element.css({
height: entity.height,
width: entity.width,
left: entity.position.x,
bottom: entity.position.y
});
});
}
// Input handling
$(document).bind('keydown keyup', function (evt) {
var direction = CODES[evt.which];
if (evt.type === 'keydown') {
pressed[direction] = true;
} else {
pressed[direction] = false;
}
});
setup();
// Kick off the game loop
setInterval(function () {
update();
draw();
}, 1000/cfg.fps);