Merge pull request #28 from Ewpratten/collider_fixes

Make the player die a lot
This commit is contained in:
Evan Pratten 2021-10-02 16:11:56 -07:00 committed by GitHub
commit 921b6dba80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 82 additions and 27 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 46 KiB

After

Width:  |  Height:  |  Size: 35 KiB

View File

@ -8,6 +8,7 @@ use super::{CharacterState, MainCharacter};
pub const GRAVITY_PPS: f32 = 2.0;
#[must_use]
pub fn modify_player_based_on_forces(
player: &mut MainCharacter,
colliders: &Vec<Rectangle>,
@ -56,16 +57,18 @@ pub fn modify_player_based_on_forces(
if player.current_state == CharacterState::Jumping
|| player.current_state == CharacterState::Dashing
{
player.update_player(
return player.update_player(
Some(CharacterState::Running),
colliders,
level_height_offset,
);
return Ok(());
}
}
// Check sideways collisions
if player.velocity.y == 0.0 && check_player_colliding_with_colliders(){
return Err(());
}
// Finally apply the velocity to the player
player.position += player.velocity;

View File

@ -38,7 +38,7 @@ impl MainCharacter {
movement_force: Vector2::zero(),
velocity: Vector2::zero(),
base_velocity: Vector2::new(0.0, GRAVITY_PPS),
size: Vector2::new(100.0, 100.0),
size: Vector2::new(80.0, 100.0),
sprite_sheet: AnimatedSpriteSheet::new(
sprite_sheet,
Vector2::new(300.0, 300.0),
@ -51,12 +51,13 @@ impl MainCharacter {
}
}
#[must_use]
pub fn update_player(
&mut self,
state: Option<CharacterState>,
colliders: &Vec<Rectangle>,
level_height_offset: f32,
) {
) -> Result<(), ()> {
if let Some(state) = state {
// Update the internal state
if state != self.current_state {
@ -73,6 +74,14 @@ impl MainCharacter {
}
// Update the player based on the new velocity
modify_player_based_on_forces(self, colliders, level_height_offset).unwrap();
modify_player_based_on_forces(self, colliders, level_height_offset)
}
pub fn reset(&mut self) {
self.position = Vector2::new(0.0, 0.0);
self.velocity = Vector2::zero();
self.movement_force = Vector2::zero();
self.current_state = CharacterState::default();
self.state_set_timestamp = Utc::now();
}
}

View File

@ -74,7 +74,7 @@ use std::{cell::RefCell, sync::mpsc::TryRecvError};
use discord_sdk::activity::ActivityBuilder;
use raylib::prelude::*;
use tracing::{error, info};
use tracing::{error, info, warn};
use utilities::discord::DiscordConfig;
use crate::{
@ -224,6 +224,7 @@ pub async fn game_begin(game_config: &mut GameConfig) -> Result<(), Box<dyn std:
.is_key_pressed(KeyboardKey::KEY_F3)
{
game_config.debug_view = !game_config.debug_view;
warn!("Debug view set: {}", game_config.debug_view);
}
}

View File

@ -1,9 +1,6 @@
use raylib::{RaylibHandle, RaylibThread};
use crate::{
utilities::datastore::{load_texture_from_internal_data, ResourceLoadError},
StaticGameData,
};
use crate::{StaticGameData, utilities::{datastore::{load_texture_from_internal_data, ResourceLoadError}, world_paint_texture::WorldPaintTexture}};
use super::Level;
@ -26,13 +23,15 @@ pub fn load_all_levels(
let mut levels = Vec::new();
for level_name in &level_names {
levels.push(Level {
name: level_name.to_string(),
background_tex: load_texture_from_internal_data(
background_tex: WorldPaintTexture::new(load_texture_from_internal_data(
raylib_handle,
thread,
&format!("levels/{}/background.png", level_name),
)?,
)?),
platform_tex: load_texture_from_internal_data(
raylib_handle,
thread,

View File

@ -1,11 +1,13 @@
use raylib::{math::Rectangle, texture::Texture2D};
use crate::utilities::world_paint_texture::WorldPaintTexture;
pub mod loader;
#[derive(Debug)]
pub struct Level {
pub name: String,
pub background_tex: Texture2D,
pub background_tex: WorldPaintTexture,
pub platform_tex: Texture2D,
pub colliders: Vec<Rectangle>
}

View File

@ -28,6 +28,7 @@ pub struct InGameScreen {
world_background: WorldPaintTexture,
levels: Vec<Level>,
current_level_idx: usize,
player_dead: bool,
}
impl InGameScreen {
@ -48,6 +49,7 @@ impl InGameScreen {
world_background: WorldPaintTexture::new(background_texture),
levels,
current_level_idx: 0,
player_dead: false,
}
}
}
@ -63,7 +65,7 @@ impl Action<Scenes, ScreenError, GameContext> for InGameScreen {
// Set the player to running
let cur_level = self.levels.get(self.current_level_idx).unwrap();
self.player.update_player(
let _ = self.player.update_player(
Some(CharacterState::Running),
&cur_level.colliders,
-cur_level.platform_tex.height as f32,
@ -112,6 +114,10 @@ impl Action<Scenes, ScreenError, GameContext> for InGameScreen {
if renderer.is_key_pressed(KeyboardKey::KEY_ESCAPE) {
Ok(ActionFlag::SwitchState(Scenes::PauseScreen))
} else if self.player_dead {
// TODO: (luna) make this switch to the death screen plz
Ok(ActionFlag::SwitchState(Scenes::FsmErrorScreen))
} else {
Ok(ActionFlag::Continue)
}
@ -119,6 +125,13 @@ impl Action<Scenes, ScreenError, GameContext> for InGameScreen {
fn on_finish(&mut self, _interrupted: bool) -> Result<(), ScreenError> {
debug!("Finished InGameScreen");
// Handle resetting if the player dies
if self.player_dead {
self.player_dead = false;
self.player.reset();
}
Ok(())
}
}

View File

@ -21,7 +21,6 @@ impl FrameUpdate for InGameScreen {
// Get the current level
let cur_level = self.levels.get(self.current_level_idx).unwrap();
// Set the camera's offset based on screen size
self.camera.offset = raylib.get_screen_size().div(Vector2::new(2.0, 1.05));
self.camera.target = Vector2::new(self.player.position.x, self.camera.target.y);
@ -32,22 +31,39 @@ impl FrameUpdate for InGameScreen {
let is_dash = raylib.is_key_pressed(KeyboardKey::KEY_LEFT_SHIFT)
&& !(self.player.current_state == CharacterState::Dashing);
if is_jump {
self.player.update_player(Some(CharacterState::Jumping), &cur_level.colliders,
-cur_level.platform_tex.height as f32,);
let collision_result = if is_jump {
self.player.update_player(
Some(CharacterState::Jumping),
&cur_level.colliders,
-cur_level.platform_tex.height as f32,
)
} else if is_dash {
self.player.update_player(Some(CharacterState::Dashing), &cur_level.colliders,
-cur_level.platform_tex.height as f32,);
self.player.update_player(
Some(CharacterState::Dashing),
&cur_level.colliders,
-cur_level.platform_tex.height as f32,
)
} else {
if self.player.current_state != CharacterState::Jumping
&& self.player.current_state != CharacterState::Dashing
{
self.player.update_player(Some(CharacterState::Running), &cur_level.colliders,
-cur_level.platform_tex.height as f32,);
self.player.update_player(
Some(CharacterState::Running),
&cur_level.colliders,
-cur_level.platform_tex.height as f32,
)
} else {
self.player.update_player(None, &cur_level.colliders,
-cur_level.platform_tex.height as f32,);
self.player.update_player(
None,
&cur_level.colliders,
-cur_level.platform_tex.height as f32,
)
}
};
// Handle running into a wall
if let Err(_) = collision_result {
self.player_dead = true;
}
}
}

View File

@ -22,10 +22,23 @@ impl WorldSpaceRender for InGameScreen {
let cur_level = self.levels.get(self.current_level_idx).unwrap();
// Render the world background
// self.world_background.render(raylib, Vector2::new(0.0, -1080.0), &self.camera);
cur_level.background_tex.render(raylib, Vector2::new(0.0, -1080.0), &self.camera);
// Render the platform layer
raylib.draw_texture_v(&cur_level.platform_tex, Vector2::new(WORLD_LEVEL_X_OFFSET, -cur_level.platform_tex.height as f32), Color::WHITE);
raylib.draw_texture_v(
&cur_level.platform_tex,
Vector2::new(WORLD_LEVEL_X_OFFSET, -cur_level.platform_tex.height as f32),
Color::WHITE,
);
if config.debug_view {
for collider in &cur_level.colliders {
let mut translated_collider = collider.clone();
translated_collider.y += -cur_level.platform_tex.height as f32;
translated_collider.x += WORLD_LEVEL_X_OFFSET;
raylib.draw_rectangle_lines_ex(translated_collider, 5, Color::RED);
}
}
// Render the floor as a line
let screen_world_zero = raylib.get_screen_to_world2D(Vector2::zero(), self.camera);
@ -40,7 +53,6 @@ impl WorldSpaceRender for InGameScreen {
config.colors.white,
);
// Render the player
render_character_in_camera_space(raylib, &self.player, &config);
}