WHAT HAPPENED AFTER A LONG STREAM
Player movement :D Uh..miguel not paralysed Failed to flip miguel when turning Please help
This commit is contained in:
176
src/player.rs
176
src/player.rs
@@ -1,11 +1,28 @@
|
||||
use bevy::{prelude::*, utils::HashSet};
|
||||
use bevy::prelude::*;
|
||||
use bevy_ecs_ldtk::prelude::*;
|
||||
use bevy_rapier2d::dynamics::Velocity;
|
||||
|
||||
use crate::level_structure::Wall;
|
||||
use crate::{colliders::ColliderBundle, ground_detection::GroundDetection, level_structure::Wall};
|
||||
use std::f32::consts::PI;
|
||||
|
||||
#[derive(Default)]
|
||||
enum Facing {
|
||||
LEFT,
|
||||
#[default]
|
||||
RIGHT,
|
||||
}
|
||||
#[derive(Default, Component)]
|
||||
pub(crate) struct Player;
|
||||
|
||||
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,
|
||||
@@ -13,85 +30,112 @@ struct PlayerBundle {
|
||||
sprite_bundle: LdtkSpriteSheetBundle,
|
||||
#[grid_coords]
|
||||
grid_coords: GridCoords,
|
||||
pub ground_detection: GroundDetection,
|
||||
#[from_entity_instance]
|
||||
pub collider_bundle: ColliderBundle,
|
||||
}
|
||||
|
||||
#[derive(Default, Resource)]
|
||||
struct LevelWalls {
|
||||
wall_locations: HashSet<GridCoords>,
|
||||
level_width: i32,
|
||||
level_height: i32,
|
||||
}
|
||||
impl LevelWalls {
|
||||
fn in_wall(&self, grid_coords: &GridCoords) -> bool {
|
||||
grid_coords.x < 0
|
||||
|| grid_coords.y < 0
|
||||
|| grid_coords.x >= self.level_width
|
||||
|| grid_coords.y >= self.level_height
|
||||
|| self.wall_locations.contains(grid_coords)
|
||||
}
|
||||
}
|
||||
// #[derive(Default, Resource)]
|
||||
// struct LevelWalls {
|
||||
// wall_locations: HashSet<GridCoords>,
|
||||
// level_width: i32,
|
||||
// level_height: i32,
|
||||
// }
|
||||
// impl LevelWalls {
|
||||
// fn in_wall(&self, grid_coords: &GridCoords) -> bool {
|
||||
// grid_coords.x < 0
|
||||
// || grid_coords.y < 0
|
||||
// || grid_coords.x >= self.level_width
|
||||
// || grid_coords.y >= self.level_height
|
||||
// || self.wall_locations.contains(grid_coords)
|
||||
// }
|
||||
// }
|
||||
|
||||
fn move_player_from_input(
|
||||
mut players: Query<&mut GridCoords, With<Player>>,
|
||||
fn player_movement(
|
||||
mut query: Query<(&mut Velocity, &GroundDetection, &Player), With<Player>>,
|
||||
input: Res<ButtonInput<KeyCode>>,
|
||||
level_walls: Res<LevelWalls>,
|
||||
) {
|
||||
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;
|
||||
};
|
||||
// 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. };
|
||||
|
||||
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;
|
||||
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, (move_player_from_input, cache_wall_locations))
|
||||
.register_ldtk_entity::<PlayerBundle>("Player")
|
||||
.init_resource::<LevelWalls>();
|
||||
app.add_systems(Update, player_movement)
|
||||
.register_ldtk_entity::<PlayerBundle>("Player");
|
||||
}
|
||||
}
|
||||
|
||||
fn cache_wall_locations(
|
||||
mut level_walls: ResMut<LevelWalls>,
|
||||
mut level_events: EventReader<LevelEvent>,
|
||||
walls: Query<&GridCoords, With<Wall>>,
|
||||
ldtk_project_entities: Query<&Handle<LdtkProject>>,
|
||||
ldtk_project_assets: Res<Assets<LdtkProject>>,
|
||||
) {
|
||||
for level_event in level_events.read() {
|
||||
if let LevelEvent::Spawned(level_iid) = level_event {
|
||||
let ldtk_project = ldtk_project_assets
|
||||
.get(ldtk_project_entities.single())
|
||||
.expect("LdtkProject should be loaded when level is spawned");
|
||||
let level = ldtk_project
|
||||
.get_raw_level_by_iid(level_iid.get())
|
||||
.expect("spawned level should exist in project");
|
||||
// fn cache_wall_locations(
|
||||
// mut level_walls: ResMut<LevelWalls>,
|
||||
// mut level_events: EventReader<LevelEvent>,
|
||||
// walls: Query<&GridCoords, With<Wall>>,
|
||||
// ldtk_project_entities: Query<&Handle<LdtkProject>>,
|
||||
// ldtk_project_assets: Res<Assets<LdtkProject>>,
|
||||
// ) {
|
||||
// for level_event in level_events.read() {
|
||||
// if let LevelEvent::Spawned(level_iid) = level_event {
|
||||
// let ldtk_project = ldtk_project_assets
|
||||
// .get(ldtk_project_entities.single())
|
||||
// .expect("LdtkProject should be loaded when level is spawned");
|
||||
// let level = ldtk_project
|
||||
// .get_raw_level_by_iid(level_iid.get())
|
||||
// .expect("spawned level should exist in project");
|
||||
|
||||
let wall_locations = walls.iter().copied().collect();
|
||||
// let wall_locations = walls.iter().copied().collect();
|
||||
|
||||
let new_level_walls = LevelWalls {
|
||||
wall_locations,
|
||||
level_width: level.px_wid / crate::GRID_SIZE,
|
||||
level_height: level.px_hei / crate::GRID_SIZE,
|
||||
};
|
||||
// let new_level_walls = LevelWalls {
|
||||
// wall_locations,
|
||||
// level_width: level.px_wid / crate::GRID_SIZE,
|
||||
// level_height: level.px_hei / crate::GRID_SIZE,
|
||||
// };
|
||||
|
||||
*level_walls = new_level_walls;
|
||||
}
|
||||
}
|
||||
}
|
||||
// *level_walls = new_level_walls;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user