From 542c05cb6c742b91113599f1f327d897fd3ac25f Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 6 Jan 2022 13:37:23 -0500 Subject: [PATCH] Complete section 5.12 --- src/player.rs | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/src/player.rs b/src/player.rs index b3def79..5289eb1 100644 --- a/src/player.rs +++ b/src/player.rs @@ -10,7 +10,7 @@ use crate::components::{ use crate::game_log::GameLog; use crate::{Bystander, Map, RunState, State, TileType}; -pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) { +pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) -> RunState { let mut positions = ecs.write_storage::(); let players = ecs.read_storage::(); let mut viewsheds = ecs.write_storage::(); @@ -25,6 +25,7 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) { let mut renderables = ecs.write_storage::(); let bystanders = ecs.read_storage::(); let vendors = ecs.read_storage::(); + let mut result = RunState::AwaitingInput; let mut swap_entities: Vec<(Entity, i32, i32)> = Vec::new(); @@ -36,7 +37,7 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) { || pos.y + delta_y < 1 || pos.y + delta_y > map.height - 1 { - return; + return RunState::AwaitingInput; } let destination_idx = map.xy_idx(pos.x + delta_x, pos.y + delta_y); @@ -57,7 +58,8 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) { viewshed.dirty = true; let mut ppos = ecs.write_resource::(); ppos.x = pos.x; - ppos.y = pos.y + ppos.y = pos.y; + result = RunState::PlayerTurn; } else if combat_stats.get(*potential_target).is_some() { wants_to_melee .insert( @@ -67,7 +69,8 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) { }, ) .expect("Add target failed"); - return; + + return RunState::PlayerTurn; } if let Some(door) = doors.get_mut(*potential_target) { @@ -92,6 +95,13 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) { let mut ppos = ecs.write_resource::(); ppos.x = pos.x; ppos.y = pos.y; + + // Change levels by running onto a set of stairs + result = match map.tiles[destination_idx] { + TileType::DownStairs => RunState::NextLevel, + TileType::UpStairs => RunState::PreviousLevel, + _ => RunState::PlayerTurn, + } } } @@ -101,6 +111,8 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) { their_pos.y = m.2; } } + + result } pub fn try_next_level(ecs: &mut World) -> bool { @@ -283,43 +295,43 @@ pub fn player_input(gs: &mut State, ctx: &mut Rltk) -> RunState { VirtualKeyCode::Up | VirtualKeyCode::Numpad8 | VirtualKeyCode::Key8 - | VirtualKeyCode::K => try_move_player(0, -1, &mut gs.ecs), + | VirtualKeyCode::K => return try_move_player(0, -1, &mut gs.ecs), VirtualKeyCode::Down | VirtualKeyCode::Numpad2 | VirtualKeyCode::Key2 - | VirtualKeyCode::J => try_move_player(0, 1, &mut gs.ecs), + | VirtualKeyCode::J => return try_move_player(0, 1, &mut gs.ecs), VirtualKeyCode::Left | VirtualKeyCode::Numpad4 | VirtualKeyCode::Key4 - | VirtualKeyCode::H => try_move_player(-1, 0, &mut gs.ecs), + | VirtualKeyCode::H => return try_move_player(-1, 0, &mut gs.ecs), VirtualKeyCode::Right | VirtualKeyCode::Numpad6 | VirtualKeyCode::Key6 - | VirtualKeyCode::L => try_move_player(1, 0, &mut gs.ecs), + | VirtualKeyCode::L => return try_move_player(1, 0, &mut gs.ecs), // Diagonals VirtualKeyCode::Numpad7 | VirtualKeyCode::Key7 | VirtualKeyCode::U - | VirtualKeyCode::Q => try_move_player(-1, -1, &mut gs.ecs), + | VirtualKeyCode::Q => return try_move_player(-1, -1, &mut gs.ecs), VirtualKeyCode::Numpad9 | VirtualKeyCode::Key9 | VirtualKeyCode::Y - | VirtualKeyCode::W => try_move_player(1, -1, &mut gs.ecs), + | VirtualKeyCode::W => return try_move_player(1, -1, &mut gs.ecs), VirtualKeyCode::Numpad1 | VirtualKeyCode::Key1 | VirtualKeyCode::B - | VirtualKeyCode::A => try_move_player(-1, 1, &mut gs.ecs), + | VirtualKeyCode::A => return try_move_player(-1, 1, &mut gs.ecs), VirtualKeyCode::Numpad3 | VirtualKeyCode::Key3 | VirtualKeyCode::N - | VirtualKeyCode::S => try_move_player(1, 1, &mut gs.ecs), + | VirtualKeyCode::S => return try_move_player(1, 1, &mut gs.ecs), // Skip Turn VirtualKeyCode::Numpad5 | VirtualKeyCode::Key5 | VirtualKeyCode::Space => {