add kill boxes

This commit is contained in:
Evan Pratten 2021-10-03 20:25:06 -04:00
parent dea7984e2e
commit 61b72e903d
7 changed files with 39 additions and 11 deletions

View File

@ -26,6 +26,9 @@
"width": 56,
"height": 313
}
],
"kill":[
],
"win": {
"x": 12000,

View File

@ -44,6 +44,9 @@
"width": 704,
"height": 64
}
],
"kill":[
],
"win": {
"x": 12000,

View File

@ -13,6 +13,7 @@ pub const GRAVITY_PPS: f32 = 2.0;
pub fn modify_player_based_on_forces(
player: &mut MainCharacter,
colliders: &Vec<Rectangle>,
killers: &Vec<Rectangle>,
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(())
}

View File

@ -66,6 +66,7 @@ impl MainCharacter {
&mut self,
state: Option<CharacterState>,
colliders: &Vec<Rectangle>,
killers: &Vec<Rectangle>,
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) {

View File

@ -8,6 +8,7 @@ pub mod loader;
pub struct LevelZones {
pub appear: Vec<Rectangle>,
pub disappear: Vec<Rectangle>,
pub kill: Vec<Rectangle>,
pub win: Rectangle,
}

View File

@ -75,6 +75,7 @@ impl Action<Scenes, ScreenError, GameContext> 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<Scenes, ScreenError, GameContext> 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 {

View File

@ -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,
)
}