diff --git a/src/logging.rs b/src/logging.rs index 09ac054..da29444 100644 --- a/src/logging.rs +++ b/src/logging.rs @@ -24,8 +24,8 @@ pub fn log_positions( info!("Player not found or multiple players detected."); } if let Ok(goal_transform) = goals.get_single() { - info!("Player Position: {:?}", goal_transform.translation); + info!("Goal Position: {:?}", goal_transform.translation); } else { - info!("Player not found or multiple players detected."); + info!("Goal not found or multiple players detected."); } } diff --git a/src/player.rs b/src/player.rs index 29247aa..f4c86d6 100644 --- a/src/player.rs +++ b/src/player.rs @@ -2,27 +2,29 @@ use bevy::prelude::*; use bevy_ecs_ldtk::prelude::*; use bevy_rapier2d::dynamics::Velocity; -use crate::{colliders::ColliderBundle, ground_detection::GroundDetection, level_structure::Wall}; -use std::f32::consts::PI; +use crate::{colliders::ColliderBundle, ground_detection::GroundDetection}; -#[derive(Default)] -enum Facing { - LEFT, - #[default] - RIGHT, +pub struct PlayerPlugin; + +impl Plugin for PlayerPlugin { + fn build(&self, app: &mut App) { + app.add_systems(Update, player_movement) + .register_ldtk_entity::("Player"); + } } + +#[derive(Default, Component)] +enum Facing { + Left, + #[default] + Right, +} + #[derive(Default, Component)] pub(crate) struct Player { direction: Facing, } -impl Player { - fn swap_direction(&mut self) { - match self.direction { - Facing::RIGHT => self.direction = Facing::LEFT, - Facing::LEFT => self.direction = Facing::RIGHT - } - } -} + #[derive(Default, Bundle, LdtkEntity)] struct PlayerBundle { player: Player, @@ -35,6 +37,54 @@ struct PlayerBundle { pub collider_bundle: ColliderBundle, } +fn player_movement( + mut query: Query<(&mut Velocity, &GroundDetection, &mut Player, &mut Transform), With>, + input: Res>, +) { + // let movement_direction = + // if input.just_pressed(KeyCode::KeyW) || input.just_pressed(KeyCode::Space) { + // GridCoords::new(0, 1) + // } else if input.just_pressed(KeyCode::KeyA) { + // GridCoords::new(-1, 0) + // } else if input.just_pressed(KeyCode::KeyS) { + // GridCoords::new(0, -1) + // } else if input.just_pressed(KeyCode::KeyD) { + // GridCoords::new(1, 0) + // } else { + // return; + // }; + for (mut velocity, ground_detection, mut player, mut p_transform) in &mut query { + // let Right = if input.pressed(KeyCode::KeyD) { 1. } else { 0. }; + // let left = if input.pressed(KeyCode::KeyA) { 1. } else { 0. }; + + let right = if input.pressed(KeyCode::KeyD) { + 1. + } else { + 0. + }; + let left = if input.pressed(KeyCode::KeyA) { + 1. + } else { + 0. + }; + + // gotta query for sprite to control how the player "appears" to look in either direction + + velocity.linvel.x = (right - left) * 200.; + + if input.just_pressed(KeyCode::Space) && ground_detection.on_ground { + velocity.linvel.y = 500.; + } + } + + // for mut player_grid_coords in players.iter_mut() { + // let destination = *player_grid_coords + movement_direction; + // if !level_walls.in_wall(&destination) { + // *player_grid_coords = destination; + // } + // } +} + // #[derive(Default, Resource)] // struct LevelWalls { // wall_locations: HashSet, @@ -51,66 +101,6 @@ struct PlayerBundle { // } // } -fn player_movement( - mut query: Query<(&mut Velocity, &GroundDetection, &Player), With>, - input: Res>, -) { - // let movement_direction = - // if input.just_pressed(KeyCode::KeyW) || input.just_pressed(KeyCode::Space) { - // GridCoords::new(0, 1) - // } else if input.just_pressed(KeyCode::KeyA) { - // GridCoords::new(-1, 0) - // } else if input.just_pressed(KeyCode::KeyS) { - // GridCoords::new(0, -1) - // } else if input.just_pressed(KeyCode::KeyD) { - // GridCoords::new(1, 0) - // } else { - // return; - // }; - for (mut velocity, ground_detection, player) in &mut query { - // let right = if input.pressed(KeyCode::KeyD) { 1. } else { 0. }; - // let left = if input.pressed(KeyCode::KeyA) { 1. } else { 0. }; - - let right = if input.pressed(KeyCode::KeyD) { - // match player.direction { - // Facing::LEFT => player.direction = Facing::RIGHT, - // Facing::RIGHT => (), - - // } - 1. - - } else { - 0. - }; - let left = if input.pressed(KeyCode::KeyA) { - 1. - } else { - 0. - }; - velocity.linvel.x = (right - left) * 200.; - - if input.just_pressed(KeyCode::Space) && ground_detection.on_ground { - velocity.linvel.y = 500.; - } - } - - // for mut player_grid_coords in players.iter_mut() { - // let destination = *player_grid_coords + movement_direction; - // if !level_walls.in_wall(&destination) { - // *player_grid_coords = destination; - // } - // } -} - -pub struct PlayerPlugin; - -impl Plugin for PlayerPlugin { - fn build(&self, app: &mut App) { - app.add_systems(Update, player_movement) - .register_ldtk_entity::("Player"); - } -} - // fn cache_wall_locations( // mut level_walls: ResMut, // mut level_events: EventReader,