Skip to content

Commit

Permalink
refactor: Use the movement system
Browse files Browse the repository at this point in the history
Keeps the same underlying functionality, but simplifies the API,
standardizing the movement of players and monsters via the movement
system.
  • Loading branch information
yngtdd committed Mar 30, 2022
1 parent 1a8aac4 commit b67faad
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 44 deletions.
29 changes: 10 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl State {
resources,
input_systems: build_input_scheduler(),
player_systems: build_player_schedule(),
monster_systems: build_monster_scheduler()
monster_systems: build_monster_scheduler(),
}
}
}
Expand All @@ -75,24 +75,15 @@ impl GameState for State {

let current_state = self.resources.get::<TurnState>().unwrap().clone();
match current_state {
TurnState::AwaitingInput => {
self.input_systems.execute(
&mut self.ecs,
&mut self.resources
)
}
TurnState::PlayerTurn => {
self.player_systems.execute(
&mut self.ecs,
&mut self.resources
)
}
TurnState::MonsterTurn => {
self.monster_systems.execute(
&mut self.ecs,
&mut self.resources
)
}
TurnState::AwaitingInput => self
.input_systems
.execute(&mut self.ecs, &mut self.resources),
TurnState::PlayerTurn => self
.player_systems
.execute(&mut self.ecs, &mut self.resources),
TurnState::MonsterTurn => self
.monster_systems
.execute(&mut self.ecs, &mut self.resources),
}

render_draw_buffer(ctx).expect("Render error");
Expand Down
7 changes: 6 additions & 1 deletion src/systems/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
mod collisions;
mod end_turn;
mod entity_render;
mod map_render;
mod movement;
mod player_input;
mod random_move;
mod end_turn;

use crate::prelude::*;

Expand All @@ -18,6 +19,8 @@ pub fn build_input_scheduler() -> Schedule {

pub fn build_player_schedule() -> Schedule {
Schedule::builder()
.add_system(movement::movement_system())
.flush()
.add_system(collisions::collisions_system())
.flush()
.add_system(map_render::map_render_system())
Expand All @@ -28,6 +31,8 @@ pub fn build_player_schedule() -> Schedule {

pub fn build_monster_scheduler() -> Schedule {
Schedule::builder()
.add_system(movement::movement_system())
.flush()
.add_system(random_move::random_move_system())
.flush()
.add_system(collisions::collisions_system())
Expand Down
33 changes: 16 additions & 17 deletions src/systems/player_input.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use crate::prelude::*;

#[system]
#[write_component(Point)]
#[read_component(Point)]
#[read_component(Player)]
pub fn player_input(
ecs: &mut SubWorld,
#[resource] map: &Map,
commands: &mut CommandBuffer,
#[resource] key: &Option<VirtualKeyCode>,
#[resource] camera: &mut Camera,
#[resource] turn_state: &mut TurnState
#[resource] turn_state: &mut TurnState,
) {
let mut players = <(Entity, &Point)>::query().filter(component::<Player>());

if let Some(key) = key {
let delta = match key {
VirtualKeyCode::Left => Point::new(-1, 0),
Expand All @@ -19,18 +20,16 @@ pub fn player_input(
_ => Point::new(0, 0),
};

if delta.x != 0 || delta.y != 0 {
let mut players = <&mut Point>::query().filter(component::<Player>());

players.iter_mut(ecs).for_each(|pos| {
let destination = *pos + delta;

if map.can_enter_tile(destination) {
*pos = destination;
camera.on_player_move(destination);
*turn_state = TurnState::PlayerTurn;
}
})
}
players.iter(ecs).for_each(|(entity, pos)| {
let destination = *pos + delta;
commands.push((
(),
WantsToMove {
entity: *entity,
destination,
},
));
});
*turn_state = TurnState::PlayerTurn;
}
}
18 changes: 11 additions & 7 deletions src/systems/random_move.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::prelude::*;

#[system]
#[write_component(Point)]
#[read_component(Point)]
#[read_component(MovingRandomly)]
pub fn random_move(ecs: &mut SubWorld, #[resource] map: &Map) {
let mut movers = <(&mut Point, &MovingRandomly)>::query();
pub fn random_move(ecs: &mut SubWorld, commands: &mut CommandBuffer) {
let mut movers = <(Entity, &Point, &MovingRandomly)>::query();

movers.iter_mut(ecs).for_each(|(pos, _)| {
movers.iter_mut(ecs).for_each(|(entity, pos, _)| {
let mut rng = RandomNumberGenerator::new();
let destination = match rng.range::<u8>(0, 4) {
0 => Point::new(-1, 0),
Expand All @@ -15,8 +15,12 @@ pub fn random_move(ecs: &mut SubWorld, #[resource] map: &Map) {
_ => Point::new(0, 1),
} + *pos;

if map.can_enter_tile(destination) {
*pos = destination;
}
commands.push((
(),
WantsToMove {
entity: *entity,
destination,
},
));
});
}

0 comments on commit b67faad

Please sign in to comment.