diff --git a/src/main.rs b/src/main.rs index cbf3c54..84512d9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,24 +1,18 @@ -use bevy::{prelude::*, utils::HashSet}; +use bevy::prelude::*; use bevy_ecs_ldtk::prelude::*; +use player::{Player, PlayerPlugin}; + +mod player; fn main() { App::new() .add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest())) .add_plugins(LdtkPlugin) + .add_plugins(PlayerPlugin) + .add_systems(Update, (translate_grid_coords_entities, check_goal)) .insert_resource(LevelSelection::index(0)) - .register_ldtk_entity::("Player") - .init_resource::() .register_ldtk_entity::("Goal") .register_ldtk_int_cell::(1) - .add_systems( - Update, - ( - move_player_from_input, - translate_grid_coords_entities, - cache_wall_locations, - check_goal, - ), - ) .add_systems(Startup, setup) .run(); } @@ -35,17 +29,6 @@ fn setup(mut commands: Commands, asset_server: Res) { ..Default::default() }); } -#[derive(Default, Component)] -struct Player; - -#[derive(Default, Bundle, LdtkEntity)] -struct PlayerBundle { - player: Player, - #[sprite_sheet_bundle] - sprite_bundle: LdtkSpriteSheetBundle, - #[grid_coords] - grid_coords: GridCoords, -} #[derive(Default, Component)] struct Goal; @@ -58,32 +41,8 @@ struct GoalBundle { #[grid_coords] grid_coords: GridCoords, } -fn move_player_from_input( - mut players: Query<&mut GridCoords, With>, - input: Res>, - level_walls: Res, -) { - let movement_direction = if input.just_pressed(KeyCode::KeyW) { - 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 player_grid_coords in players.iter_mut() { - let destination = *player_grid_coords + movement_direction; - if !level_walls.in_wall(&destination) { - *player_grid_coords = destination; - } - } -} -const GRID_SIZE: i32 = 16; +pub const GRID_SIZE: i32 = 16; fn translate_grid_coords_entities( mut grid_coords_entities: Query<(&mut Transform, &GridCoords), Changed>, ) { @@ -102,50 +61,6 @@ struct WallBundle { wall: Wall, } -#[derive(Default, Resource)] -struct LevelWalls { - wall_locations: HashSet, - 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 cache_wall_locations( - mut level_walls: ResMut, - mut level_events: EventReader, - walls: Query<&GridCoords, With>, - ldtk_project_entities: Query<&Handle>, - ldtk_project_assets: Res>, -) { - 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 new_level_walls = LevelWalls { - wall_locations, - level_width: level.px_wid / GRID_SIZE, - level_height: level.px_hei / GRID_SIZE, - }; - - *level_walls = new_level_walls; - } - } -} - fn check_goal( level_selection: ResMut, players: Query<&GridCoords, (With, Changed)>, diff --git a/src/player.rs b/src/player.rs index 527736b..bc7e56a 100644 --- a/src/player.rs +++ b/src/player.rs @@ -1,60 +1,57 @@ -use bevy::prelude::*; +use bevy::{prelude::*, utils::HashSet}; use bevy_ecs_ldtk::prelude::*; -use bevy_rapier2d::dynamics::Velocity; -use crate::{climbing::Climber, inventory::Inventory}; -use crate::{colliders::ColliderBundle, ground_detection::GroundDetection}; +use crate::{Wall, GRID_SIZE}; -#[derive(Copy, Clone, Eq, PartialEq, Debug, Default, Component)] -pub struct Player; +#[derive(Default, Component)] +pub(crate) struct Player; -#[derive(Clone, Default, Bundle, LdtkEntity)] -pub struct PlayerBundle { - #[sprite_bundle("player.png")] - pub sprite_bundle: SpriteBundle, - #[from_entity_instance] - pub collider_bundle: ColliderBundle, - pub player: Player, - #[worldly] - pub worldly: Worldly, - pub climber: Climber, - pub ground_detection: GroundDetection, - - // Build Items Component manually by using `impl From<&EntityInstance>` - #[from_entity_instance] - items: Inventory, - - // The whole EntityInstance can be stored directly as an EntityInstance component - #[from_entity_instance] - entity_instance: EntityInstance, +#[derive(Default, Bundle, LdtkEntity)] +struct PlayerBundle { + player: Player, + #[sprite_sheet_bundle] + sprite_bundle: LdtkSpriteSheetBundle, + #[grid_coords] + grid_coords: GridCoords, } -pub fn player_movement( +#[derive(Default, Resource)] +struct LevelWalls { + wall_locations: HashSet, + 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>, input: Res>, - mut query: Query<(&mut Velocity, &mut Climber, &GroundDetection), With>, + level_walls: Res, ) { - for (mut velocity, mut climber, ground_detection) in &mut query { - let right = if input.pressed(KeyCode::KeyD) { 1. } else { 0. }; - let left = if input.pressed(KeyCode::KeyA) { 1. } else { 0. }; + let movement_direction = if input.just_pressed(KeyCode::KeyW) { + 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; + }; - velocity.linvel.x = (right - left) * 200.; - - if climber.intersecting_climbables.is_empty() { - climber.climbing = false; - } else if input.just_pressed(KeyCode::KeyW) || input.just_pressed(KeyCode::KeyS) { - climber.climbing = true; - } - - if climber.climbing { - let up = if input.pressed(KeyCode::KeyW) { 1. } else { 0. }; - let down = if input.pressed(KeyCode::KeyS) { 1. } else { 0. }; - - velocity.linvel.y = (up - down) * 200.; - } - - if input.just_pressed(KeyCode::Space) && (ground_detection.on_ground || climber.climbing) { - velocity.linvel.y = 500.; - climber.climbing = false; + 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; } } } @@ -63,7 +60,37 @@ pub struct PlayerPlugin; impl Plugin for PlayerPlugin { fn build(&self, app: &mut App) { - app.add_systems(Update, player_movement) - .register_ldtk_entity::("Player"); + app.add_systems(Update, (move_player_from_input, cache_wall_locations)) + .register_ldtk_entity::("Player") + .init_resource::(); + } +} + +fn cache_wall_locations( + mut level_walls: ResMut, + mut level_events: EventReader, + walls: Query<&GridCoords, With>, + ldtk_project_entities: Query<&Handle>, + ldtk_project_assets: Res>, +) { + 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 new_level_walls = LevelWalls { + wall_locations, + level_width: level.px_wid / GRID_SIZE, + level_height: level.px_hei / GRID_SIZE, + }; + + *level_walls = new_level_walls; + } } }