-
Notifications
You must be signed in to change notification settings - Fork 15
/
game.rs
449 lines (392 loc) · 13.4 KB
/
game.rs
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
use ::math::{Vec2D};
use ::ship::{Ship};
pub use ::input::{Inputs, InputIndex};
use ::geom::{test_circle_point, test_circle_triangle};
use ::rng::{StdRng, Rng, new_rng};
pub struct Config {
pub acceleration: f64,
pub speed_limit: f64,
pub drag: f64,
pub explosion_life: f64,
pub angular_accel: f64,
pub angular_limit: f64,
pub angular_drag: f64,
pub bullet_interval: f64,
pub bullet_speed: f64,
pub bullet_lifetime: f64,
pub delta_t: f64,
pub asteroid_min_size: f64,
pub field_size: Vec2D,
pub key_binds: Vec<(u32, InputIndex)>,
}
const DEFAULT_KEYBINDS: &[(u32, InputIndex)] = &[
(90, InputIndex::Shoot), // Z
(32, InputIndex::Shoot), // Space
(38, InputIndex::Forward), // Up
(40, InputIndex::Backward), // Down
(37, InputIndex::Left), // Left
(39, InputIndex::Right), // Right
];
impl Config {
pub fn new() -> Config {
Config {
acceleration: 250.0,
speed_limit: 1000.0,
drag: 0.000005,
explosion_life: 0.3,
angular_accel: 30.0,
angular_limit: 4.0,
angular_drag: 8.0,
bullet_interval: 0.15,
bullet_speed: 400.0,
bullet_lifetime: 1.7,
asteroid_min_size: 20.0,
delta_t: 1.0 / 60.0,
field_size: Vec2D { x: 1280.0, y: 720.0 },
key_binds: DEFAULT_KEYBINDS.to_vec(),
}
}
pub fn lookup_input_key(&self, code: u32) -> Option<InputIndex> {
for &(key, val) in self.key_binds.iter() {
if key == code {
return Some(val);
}
}
None
}
}
#[derive(PartialEq)]
pub enum BulletSource {
Player,
UFO,
}
pub struct Bullet {
pub pos: Vec2D,
pub speed: Vec2D,
pub lifetime: u64,
pub dead: bool,
pub source: BulletSource,
}
impl Bullet {
pub fn tick(&mut self, config: &Config) {
self.pos += self.speed.scale(config.delta_t);
self.pos.clip(&config.field_size);
}
}
impl Bullet {
pub fn new(game: &Game, source: BulletSource) -> Bullet {
let ship = &game.ship;
let config = &game.config;
let direction = Vec2D::one().rotate(ship.angle);
Bullet {
pos: ship.pos + direction.scale(20.0),
speed: direction.scale(config.bullet_speed),
lifetime: game.tick + (config.bullet_lifetime / config.delta_t) as u64,
dead: false,
source: source,
}
}
}
#[derive(Clone)]
pub struct Asteroid {
pub pos: Vec2D,
pub speed: Vec2D,
pub angle: f64,
pub angle_speed: f64,
pub size: f64,
pub style: usize,
pub dead: bool,
}
impl Asteroid {
pub fn tick(&mut self, config: &Config) {
self.pos += self.speed.scale(config.delta_t);
self.pos.clip(&config.field_size);
self.angle += self.angle_speed * config.delta_t;
}
pub fn split_off(&self, config: &Config) -> Vec<Asteroid> {
let mut rv = Vec::new();
if self.size > config.asteroid_min_size {
let mut copy0 = self.clone();
let mut copy1 = self.clone();
let offset = Vec2D::one().rotate(self.angle).scale(self.size / 2.0);
copy0.size /= 2.0;
copy1.size /= 2.0;
copy0.pos += offset;
copy1.pos -= offset;
copy0.speed += offset;
copy1.speed -= offset;
rv.push(copy0);
rv.push(copy1);
}
rv
}
}
pub struct UFO {}
impl UFO {
pub fn tick(&mut self) {}
}
pub struct Explosion {
pub pos: Vec2D,
pub start_tick: u64,
pub lifetime: u64,
}
impl Explosion {
pub fn new(pos: Vec2D, tick: u64, config: &Config) -> Explosion {
Explosion {
pos: pos,
start_tick: tick,
lifetime: tick + ((config.explosion_life / config.delta_t) as u64),
}
}
//pub fn tick() { }
}
#[derive(Eq, PartialEq)]
pub enum GameState {
Running,
Respawning,
GameOver,
}
pub struct Game {
pub game_state: GameState,
pub ship: Ship,
pub ufo: Option<UFO>,
pub ufo_spawn_tick: u64,
pub lives: u64,
pub level: usize,
pub score: u64,
pub tick: u64,
pub next_bullet_tick: u64,
pub explosions: Vec<Explosion>,
pub bullets: Vec<Bullet>,
pub asteroids: Vec<Asteroid>,
pub inputs: Inputs,
pub config: Config,
pub rng: StdRng,
}
fn collide_asteroid_bullet(asteroid: &Asteroid, bullet: &Bullet) -> bool {
test_circle_point(asteroid.pos, asteroid.size, bullet.pos)
}
fn collide_asteroid_ship(asteroid: &Asteroid, ship: &Ship) -> bool {
let tr: Vec<Vec2D> = [
Vec2D { x: 10.0, y: 0.0 },
Vec2D { x: -10.0, y: -5.0 },
Vec2D { x: -10.0, y: 5.0 },
].iter().map(|p| ship.pos + p.scale(2.2).rotate(ship.angle)).collect();
test_circle_triangle(asteroid.pos, asteroid.size, tr[0], tr[1], tr[2])
}
use ::std::f64::consts::PI;
impl Game {
pub fn new() -> Game {
let mut game = Game {
tick: 0,
lives: 4,
level: 2,
score: 0,
game_state: GameState::Running,
next_bullet_tick: 0,
ship: Ship::new(),
ufo: None,
ufo_spawn_tick: ::std::u64::MAX,
explosions: Vec::new(),
bullets: Vec::new(),
asteroids: Vec::new(),
inputs: Inputs::new(),
config: Config::new(),
rng: new_rng().expect("could not seed rng"),
};
game.spawn_level();
game
}
pub fn spawn_level(&mut self) {
let field_size = self.config.field_size;
self.ship.pos = field_size.scale(0.5);
self.ship.speed = Vec2D::zero();
self.ship.angle = PI * -0.5;
for _ in 0..self.level {
let mut pos;
loop {
pos = Vec2D {
x: field_size.x * self.rng.next_f64(),
y: field_size.y * self.rng.next_f64(),
};
if (pos - self.ship.pos).len() > 300.0 { break; }
}
let angle = PI * 2.0 * self.rng.next_f64();
self.asteroids.push(Asteroid {
pos: pos,
speed: Vec2D { x: 100.0, y: 0.0 }.rotate(angle),
angle: angle,
angle_speed: 0.6,
size: 50.0,
style: 5,
dead: false,
});
}
}
pub fn reset(&mut self) {
*self = Game::new();
}
pub fn tick(&mut self) {
match self.game_state {
GameState::GameOver => {
if self.inputs.is_down(InputIndex::Shoot) {
self.reset();
}
return;
},
GameState::Respawning => {
if self.lives == 0 {
self.game_state = GameState::GameOver;
return;
}
let ship = &mut self.ship;
if self.inputs.is_down(InputIndex::Shoot) {
self.game_state = GameState::Running;
self.lives -= 1;
ship.speed = Vec2D::zero();
ship.pos = self.config.field_size.scale(0.5);
ship.angle = ::std::f64::consts::PI * -0.5;
ship.dead = false;
}
},
GameState::Running => {
if self.asteroids.len() == 0 {
self.level += 1;
self.spawn_level();
}
},
}
self.tick += 1;
let tick = self.tick;
// decay bullets
self.bullets.retain(|b| b.lifetime > tick);
// decay explosions
self.explosions.retain(|e| e.lifetime > tick);
{
// move entities
let inputs = &self.inputs;
let config = &self.config;
let ship = &mut self.ship;
ship.tick(inputs, config);
for asteroid in self.asteroids.iter_mut() {
asteroid.tick(config);
}
for bullet in self.bullets.iter_mut() {
bullet.tick(config);
}
for ufo in self.ufo.iter_mut() {
ufo.tick();
}
}
if !self.ship.dead {
// shoot
let inputs = &self.inputs;
let config = &self.config;
if inputs.been_pressed(InputIndex::Shoot) && self.tick >= self.next_bullet_tick {
self.next_bullet_tick = self.tick + (config.bullet_interval / config.delta_t) as u64;
let bullet = Bullet::new(self, BulletSource::Player);
self.bullets.push(bullet);
}
}
// COLLISIONS
{
// collide asteroids with bullets
let asteroids = &mut self.asteroids;
let explosions = &mut self.explosions;
let bullets = &mut self.bullets;
let config = &self.config;
let mut new_asteroids = Vec::new();
let mut new_explosions = Vec::new();
let mut score_change = 0;
for asteroid in asteroids.iter_mut() {
for bullet in bullets.iter_mut().filter(|bullet| bullet.source == BulletSource::Player) {
// bullets and asteroids may collide multiple times
// the alternative is having order-dependent logic
if collide_asteroid_bullet(asteroid, bullet) {
if !asteroid.dead {
score_change += 100;
new_asteroids.append(&mut asteroid.split_off(&config));
new_explosions.push(Explosion::new(asteroid.pos, tick, &config));
}
asteroid.dead = true;
bullet.dead = true;
}
}
}
self.score += score_change;
bullets.retain(|bullet| !bullet.dead);
asteroids.retain(|asteroid| !asteroid.dead);
asteroids.append(&mut new_asteroids);
explosions.append(&mut new_explosions);
}
{
// collide bullets with ship & ufo
let bullets = &mut self.bullets;
let explosions = &mut self.explosions;
let ship = &mut self.ship;
let ufo = &mut self.ufo;
let config = &self.config;
let collide_ship_bullet = |_: &Ship, _: &Bullet| false;
let collide_ufo_bullet = |_: &UFO, _: &Bullet| false;
for bullet in bullets.iter_mut() {
match bullet.source {
BulletSource::UFO => {
if !ship.dead && collide_ship_bullet(ship, bullet) {
self.game_state = GameState::Respawning;
explosions.push(Explosion::new(ship.pos, tick, config));
ship.dead = true;
bullet.dead = true;
}
},
BulletSource::Player => {
if ufo.as_ref().map_or(false, |ufo| collide_ufo_bullet(ufo, bullet)) {
*ufo = None;
bullet.dead = true;
}
},
}
}
bullets.retain(|bullet| !bullet.dead);
}
{
// collide asteroids with ship & ufo
let asteroids = &mut self.asteroids;
let explosions = &mut self.explosions;
let mut new_asteroids = Vec::new();
let config = &self.config;
let ship = &mut self.ship;
let ufo = &mut self.ufo;
let collide_asteroid_ufo = |_: &Asteroid, _: &UFO| false;
for asteroid in asteroids.iter_mut() {
let mut collided = false;
if !ship.dead && collide_asteroid_ship(asteroid, ship) {
self.game_state = GameState::Respawning;
explosions.push(Explosion::new(ship.pos, tick, &config));
explosions.push(Explosion::new(asteroid.pos, tick, &config));
ship.dead = true;
collided = true;
}
if ufo.as_ref().map_or(false, |ufo| collide_asteroid_ufo(asteroid, ufo)) {
*ufo = None;
collided = true;
}
if collided {
if !asteroid.dead {
new_asteroids.append(&mut asteroid.split_off(&config));
asteroid.dead = true;
}
}
}
asteroids.retain(|asteroid| !asteroid.dead);
asteroids.append(&mut new_asteroids);
}
{
let collide_ship_ufo = |_, _| false;
if collide_ship_ufo(&self.ship, &self.ufo) {
}
}
// END COLLISIONS
// forget pressed inputs
self.inputs.tick();
}
}