Pass commands to be executed on collision to extracted function

This commit is contained in:
Paul-Christian Volkmer 2023-05-18 23:22:55 +02:00
parent b3253490b9
commit d23dfb969b
2 changed files with 37 additions and 25 deletions

View File

@ -82,7 +82,7 @@ impl FromStr for BoxAreaContent {
impl Display for Command { impl Display for Command {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self { match self {
Command::SpawnPlayer(player_id, x, y) => write!(f, "Spawn {}", player_id), Command::SpawnPlayer(player_id, _x, _y) => write!(f, "Spawn {}", player_id),
Command::RemovePlayer(player_id) => write!(f, "Face {}", player_id), Command::RemovePlayer(player_id) => write!(f, "Face {}", player_id),
Command::FacePlayer(player_id, direction) => write!(f, "Face {} {}", player_id, direction), Command::FacePlayer(player_id, direction) => write!(f, "Face {} {}", player_id, direction),
Command::MovePlayer(player_id, direction) => write!(f, "Move {} {}", player_id, direction), Command::MovePlayer(player_id, direction) => write!(f, "Move {} {}", player_id, direction),

View File

@ -11,7 +11,7 @@ use crate::{Player, GLASS_SPACE};
pub struct World { pub struct World {
player: Player, player: Player,
remote_player: Option<Player>, _remote_player: Option<Player>,
right_top_box_area: BoxArea, right_top_box_area: BoxArea,
right_bottom_box_area: BoxArea, right_bottom_box_area: BoxArea,
left_bottom_box_area: BoxArea, left_bottom_box_area: BoxArea,
@ -25,7 +25,7 @@ impl World {
pub fn init() -> World { pub fn init() -> World {
World { World {
player: Player::init(), player: Player::init(),
remote_player: None, _remote_player: None,
right_top_box_area: BoxArea::new(BoxAreaPosition::RightTop, BoxAreaContent::EmptyGlass), right_top_box_area: BoxArea::new(BoxAreaPosition::RightTop, BoxAreaContent::EmptyGlass),
right_bottom_box_area: BoxArea::new( right_bottom_box_area: BoxArea::new(
BoxAreaPosition::RightBottom, BoxAreaPosition::RightBottom,
@ -62,52 +62,64 @@ impl World {
let player_id = self.player.id.clone(); let player_id = self.player.id.clone();
match event { match event {
Event::KeyDown { Event::KeyDown {
keycode: Some(Keycode::Up) | Some(Keycode::W), keycode: Some(Keycode::Up | Keycode::W),
.. ..
} => { } => {
self.execute_command(Command::MovePlayer(player_id.clone(), Direction::Up)); self.execute_command(Command::MovePlayer(player_id.clone(), Direction::Up));
if self.collides() { self.if_collides_execute(
self.execute_command(Command::MovePlayer(player_id.clone(), Direction::Down)); vec![
self.execute_command(Command::FacePlayer(player_id, Direction::Up)); Command::MovePlayer(player_id.clone(), Direction::Down),
} Command::FacePlayer(player_id, Direction::Up),
]
)
} }
Event::KeyDown { Event::KeyDown {
keycode: Some(Keycode::Down) | Some(Keycode::S), keycode: Some(Keycode::Down | Keycode::S),
.. ..
} => { } => {
self.execute_command(Command::MovePlayer(player_id.clone(), Direction::Down)); self.execute_command(Command::MovePlayer(player_id.clone(), Direction::Down));
if self.collides() { self.if_collides_execute(
self.execute_command(Command::MovePlayer(player_id.clone(), Direction::Up)); vec![
self.execute_command(Command::FacePlayer(player_id, Direction::Down)); Command::MovePlayer(player_id.clone(), Direction::Up),
} Command::FacePlayer(player_id, Direction::Down)
]
)
} }
Event::KeyDown { Event::KeyDown {
keycode: Some(Keycode::Left) | Some(Keycode::A), keycode: Some(Keycode::Left | Keycode::A),
.. ..
} => { } => {
self.execute_command(Command::MovePlayer(player_id.clone(), Direction::Left)); self.execute_command(Command::MovePlayer(player_id.clone(), Direction::Left));
if self.collides() { self.if_collides_execute(
self.execute_command(Command::MovePlayer(player_id.clone(), Direction::Right)); vec![
self.execute_command(Command::FacePlayer(player_id, Direction::Left)); Command::MovePlayer(player_id.clone(), Direction::Right),
} Command::FacePlayer(player_id, Direction::Left),
]
)
} }
Event::KeyDown { Event::KeyDown {
keycode: Some(Keycode::Right) | Some(Keycode::D), keycode: Some(Keycode::Right | Keycode::D),
.. ..
} => { } => {
self.execute_command(Command::MovePlayer(player_id.clone(), Direction::Right)); self.execute_command(Command::MovePlayer(player_id.clone(), Direction::Right));
if self.collides() { self.if_collides_execute(
self.execute_command(Command::MovePlayer(player_id.clone(), Direction::Left)); vec![
self.execute_command(Command::FacePlayer(player_id, Direction::Right)); Command::MovePlayer(player_id.clone(), Direction::Left),
} Command::FacePlayer(player_id, Direction::Right),
]
)
} }
Event::KeyUp { .. } => self.execute_command(Command::StopPlayer(player_id)), Event::KeyUp { .. } => self.execute_command(Command::StopPlayer(player_id)),
_ => {} _ => {}
} }
} }
fn collides(&mut self) -> bool { fn if_collides_execute(&mut self, commands: Vec<Command>) {
self.collides_with_stop() || !self.player.within_rect(&Self::playable_rect()) if self.collides_with_stop() || !self.player.within_rect(&Self::playable_rect()) {
commands.into_iter().for_each(|command| {
self.execute_command(command);
});
}
} }
/// Executes a command for world update. /// Executes a command for world update.