From 61b72e903dc05a712f8a85c2567d0af80c485a51 Mon Sep 17 00:00:00 2001 From: Evan Pratten Date: Sun, 3 Oct 2021 20:25:06 -0400 Subject: [PATCH] add kill boxes --- game/assets/levels/level_0/zones.json | 3 ++ game/assets/levels/level_1/zones.json | 3 ++ game/src/character/collisions.rs | 34 +++++++++++++++++------ game/src/character/mod.rs | 3 +- game/src/scenes/ingame_scene/level/mod.rs | 1 + game/src/scenes/ingame_scene/mod.rs | 2 +- game/src/scenes/ingame_scene/update.rs | 4 +++ 7 files changed, 39 insertions(+), 11 deletions(-) diff --git a/game/assets/levels/level_0/zones.json b/game/assets/levels/level_0/zones.json index 10fb295..88d0901 100644 --- a/game/assets/levels/level_0/zones.json +++ b/game/assets/levels/level_0/zones.json @@ -26,6 +26,9 @@ "width": 56, "height": 313 } + ], + "kill":[ + ], "win": { "x": 12000, diff --git a/game/assets/levels/level_1/zones.json b/game/assets/levels/level_1/zones.json index b787b50..15e2e30 100644 --- a/game/assets/levels/level_1/zones.json +++ b/game/assets/levels/level_1/zones.json @@ -44,6 +44,9 @@ "width": 704, "height": 64 } + ], + "kill":[ + ], "win": { "x": 12000, diff --git a/game/src/character/collisions.rs b/game/src/character/collisions.rs index 5775489..e4efdbe 100644 --- a/game/src/character/collisions.rs +++ b/game/src/character/collisions.rs @@ -13,6 +13,7 @@ pub const GRAVITY_PPS: f32 = 2.0; pub fn modify_player_based_on_forces( player: &mut MainCharacter, colliders: &Vec, + killers: &Vec, level_height_offset: f32, ) -> Result<(), ()> { trace!("Player state: {:?}", player.current_state); @@ -25,7 +26,7 @@ pub fn modify_player_based_on_forces( let predicted_player_position = player.position + player.velocity; // Calculate a bounding rect around the player both now, and one frame in the future - let mut player_rect = Rectangle::new( + let player_rect = Rectangle::new( predicted_player_position.x - (player.size.x / 2.0), predicted_player_position.y - (player.size.x / 2.0), player.size.x, @@ -48,6 +49,15 @@ pub fn modify_player_based_on_forces( || translated_rect.check_collision_recs(&predicted_player_rect) }) }; + let check_player_colliding_with_killers = || { + killers.iter().any(|rect| { + let mut translated_rect = rect.clone(); + translated_rect.y += level_height_offset; + translated_rect.x += WORLD_LEVEL_X_OFFSET; + translated_rect.check_collision_recs(&player_rect.clone()) + || translated_rect.check_collision_recs(&predicted_player_rect) + }) + }; let check_player_colliding_with_colliders_forwards = || { colliders.iter().any(|rect| { let mut translated_rect = rect.clone(); @@ -76,10 +86,11 @@ pub fn modify_player_based_on_forces( return player.update_player( Some(CharacterState::Running), colliders, + killers, level_height_offset, ); } - }else if player.current_state == CharacterState::Running { + } else if player.current_state == CharacterState::Running { player.override_state(CharacterState::Jumping); } @@ -87,22 +98,27 @@ pub fn modify_player_based_on_forces( player.position += player.velocity; // Re-calculate the player rect - player_rect = Rectangle::new( + let player_rect = Rectangle::new( player.position.x - (player.size.x / 2.0), player.position.y - (player.size.x / 2.0), player.size.x, player.size.y, ); - if player.position.y > 50.0 || colliders.iter().any(|rect| { - let mut translated_rect = rect.clone(); - translated_rect.y += level_height_offset; - translated_rect.x += WORLD_LEVEL_X_OFFSET; - translated_rect.check_collision_recs(&player_rect) - }) { + if player.position.y > 50.0 + || colliders.iter().any(|rect| { + let mut translated_rect = rect.clone(); + translated_rect.y += level_height_offset; + translated_rect.x += WORLD_LEVEL_X_OFFSET; + translated_rect.check_collision_recs(&player_rect) + }) + { return Err(()); } + if check_player_colliding_with_killers() { + return Err(()); + } Ok(()) } diff --git a/game/src/character/mod.rs b/game/src/character/mod.rs index 2c014cd..600b3c9 100644 --- a/game/src/character/mod.rs +++ b/game/src/character/mod.rs @@ -66,6 +66,7 @@ impl MainCharacter { &mut self, state: Option, colliders: &Vec, + killers: &Vec, level_height_offset: f32, ) -> Result<(), ()> { if let Some(state) = state { @@ -81,7 +82,7 @@ impl MainCharacter { } // Update the player based on the new velocity - modify_player_based_on_forces(self, colliders, level_height_offset) + modify_player_based_on_forces(self, colliders, killers, level_height_offset) } pub fn reset(&mut self) { diff --git a/game/src/scenes/ingame_scene/level/mod.rs b/game/src/scenes/ingame_scene/level/mod.rs index 35bc6e0..68f91b3 100644 --- a/game/src/scenes/ingame_scene/level/mod.rs +++ b/game/src/scenes/ingame_scene/level/mod.rs @@ -8,6 +8,7 @@ pub mod loader; pub struct LevelZones { pub appear: Vec, pub disappear: Vec, + pub kill: Vec, pub win: Rectangle, } diff --git a/game/src/scenes/ingame_scene/mod.rs b/game/src/scenes/ingame_scene/mod.rs index 8577c83..25b98e2 100644 --- a/game/src/scenes/ingame_scene/mod.rs +++ b/game/src/scenes/ingame_scene/mod.rs @@ -75,6 +75,7 @@ impl Action for InGameScreen { let _ = self.player.update_player( Some(CharacterState::Running), &cur_level.colliders, + &cur_level.zones.kill, -cur_level.platform_tex.height as f32, ); @@ -133,7 +134,6 @@ impl Action for InGameScreen { // Render the HUD self.render_screen_space(&mut renderer, &context.config); - // Check if the player won let cur_level = self.levels.get(context.current_level).unwrap(); if self.player.position.x > cur_level.zones.win.x { diff --git a/game/src/scenes/ingame_scene/update.rs b/game/src/scenes/ingame_scene/update.rs index 2b16ea5..520f78b 100644 --- a/game/src/scenes/ingame_scene/update.rs +++ b/game/src/scenes/ingame_scene/update.rs @@ -35,12 +35,14 @@ impl FrameUpdate for InGameScreen { self.player.update_player( Some(CharacterState::Jumping), &cur_level.colliders, + &cur_level.zones.kill, -cur_level.platform_tex.height as f32, ) } else if is_dash { self.player.update_player( Some(CharacterState::Dashing), &cur_level.colliders, + &cur_level.zones.kill, -cur_level.platform_tex.height as f32, ) } else { @@ -50,12 +52,14 @@ impl FrameUpdate for InGameScreen { self.player.update_player( Some(CharacterState::Running), &cur_level.colliders, + &cur_level.zones.kill, -cur_level.platform_tex.height as f32, ) } else { self.player.update_player( None, &cur_level.colliders, + &cur_level.zones.kill, -cur_level.platform_tex.height as f32, ) }