try controlling the sprite for direction change, also your asset path still sucks

This commit is contained in:
2024-11-07 10:32:38 +02:00
parent 0e862aa71f
commit ef375b2da8
2 changed files with 67 additions and 77 deletions

View File

@@ -24,8 +24,8 @@ pub fn log_positions(
info!("Player not found or multiple players detected."); info!("Player not found or multiple players detected.");
} }
if let Ok(goal_transform) = goals.get_single() { if let Ok(goal_transform) = goals.get_single() {
info!("Player Position: {:?}", goal_transform.translation); info!("Goal Position: {:?}", goal_transform.translation);
} else { } else {
info!("Player not found or multiple players detected."); info!("Goal not found or multiple players detected.");
} }
} }

View File

@@ -2,27 +2,29 @@ use bevy::prelude::*;
use bevy_ecs_ldtk::prelude::*; use bevy_ecs_ldtk::prelude::*;
use bevy_rapier2d::dynamics::Velocity; use bevy_rapier2d::dynamics::Velocity;
use crate::{colliders::ColliderBundle, ground_detection::GroundDetection, level_structure::Wall}; use crate::{colliders::ColliderBundle, ground_detection::GroundDetection};
use std::f32::consts::PI;
#[derive(Default)] pub struct PlayerPlugin;
enum Facing {
LEFT, impl Plugin for PlayerPlugin {
#[default] fn build(&self, app: &mut App) {
RIGHT, app.add_systems(Update, player_movement)
.register_ldtk_entity::<PlayerBundle>("Player");
} }
}
#[derive(Default, Component)]
enum Facing {
Left,
#[default]
Right,
}
#[derive(Default, Component)] #[derive(Default, Component)]
pub(crate) struct Player { pub(crate) struct Player {
direction: Facing, 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)] #[derive(Default, Bundle, LdtkEntity)]
struct PlayerBundle { struct PlayerBundle {
player: Player, player: Player,
@@ -35,6 +37,54 @@ struct PlayerBundle {
pub collider_bundle: ColliderBundle, pub collider_bundle: ColliderBundle,
} }
fn player_movement(
mut query: Query<(&mut Velocity, &GroundDetection, &mut Player, &mut Transform), With<Player>>,
input: Res<ButtonInput<KeyCode>>,
) {
// 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)] // #[derive(Default, Resource)]
// struct LevelWalls { // struct LevelWalls {
// wall_locations: HashSet<GridCoords>, // wall_locations: HashSet<GridCoords>,
@@ -51,66 +101,6 @@ struct PlayerBundle {
// } // }
// } // }
fn player_movement(
mut query: Query<(&mut Velocity, &GroundDetection, &Player), With<Player>>,
input: Res<ButtonInput<KeyCode>>,
) {
// 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::<PlayerBundle>("Player");
}
}
// fn cache_wall_locations( // fn cache_wall_locations(
// mut level_walls: ResMut<LevelWalls>, // mut level_walls: ResMut<LevelWalls>,
// mut level_events: EventReader<LevelEvent>, // mut level_events: EventReader<LevelEvent>,