From b9a6da7ccabb972074f09d686dc78f9643accfac Mon Sep 17 00:00:00 2001 From: Raymond Hogenson Date: Wed, 22 Aug 2018 16:54:43 -0400 Subject: Make taking injuries more flexible I've just moved it into the Enemy trait, because I was planning on having Bill move around when he got hit, but it didn't look good. --- src/assets/introduction.png | Bin 972031 -> 972993 bytes src/assets/introduction.xcf | Bin 1593811 -> 1593413 bytes src/gates.rs | 15 ++++++++++++--- src/main.rs | 3 +-- src/state.rs | 1 + 5 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/assets/introduction.png b/src/assets/introduction.png index b087f26..5e14519 100644 Binary files a/src/assets/introduction.png and b/src/assets/introduction.png differ diff --git a/src/assets/introduction.xcf b/src/assets/introduction.xcf index 007bd6f..c12e25b 100644 Binary files a/src/assets/introduction.xcf and b/src/assets/introduction.xcf differ diff --git a/src/gates.rs b/src/gates.rs index 2c8da82..def618a 100644 --- a/src/gates.rs +++ b/src/gates.rs @@ -95,6 +95,14 @@ fn shorten(x: f64, y: f64, maxlen: f64) -> (f64, f64) { } } +impl Gates { + fn choose_position(&mut self) { + let (x, y) = random_position(); + self.target_x = x; + self.target_y = y; + } +} + impl state::Enemy for Gates { fn new() -> Gates { Gates { x: 200., y: 40., v_x: 0., v_y: 0., a_x: 0., a_y: 0., target_x: 200., target_y: 40., health: 1. } @@ -107,9 +115,7 @@ impl state::Enemy for Gates { /* If we've arrived (close and stopped) */ if (dx*dx + dy*dy).sqrt() < max_velocity / max_accel && (self.v_x*self.v_x + self.v_y*self.v_y).sqrt() < 0.01 { /* Then pick a new target */ - let (x, y) = random_position(); - self.target_x = x; - self.target_y = y; + self.choose_position(); } let dx = self.target_x - self.x; @@ -170,6 +176,9 @@ impl state::Enemy for Gates { fn set_health(&mut self, health: f64) { self.health = health; } + fn injure(&mut self, damage: f64) { + self.health -= damage; + } } impl state::Drawable for GatesBullet { diff --git a/src/main.rs b/src/main.rs index e2638b7..43c0c22 100644 --- a/src/main.rs +++ b/src/main.rs @@ -53,9 +53,8 @@ fn frame, U: state::Bullet>(canvas: &mut sdl2::render::Window for enemy in state.enemies.iter_mut() { for bullet in state.player_bullets.iter_mut() { if enemy.overlaps(bullet) { + enemy.injure(state::PBullet::damage()); bullet.delete(); - let health = enemy.health(); - enemy.set_health(health - state::PBullet::damage()); } } } diff --git a/src/state.rs b/src/state.rs index 820c1e4..bf62f51 100644 --- a/src/state.rs +++ b/src/state.rs @@ -76,6 +76,7 @@ pub trait Enemy: Drawable { fn act(&mut self, count: u64) -> Vec; fn health(&self) -> f64; fn set_health(&mut self, health: f64); + fn injure(&mut self, damage: f64); } pub struct State { -- cgit v1.3.1