Complete section 5.12

This commit is contained in:
Timothy Warren 2022-01-06 13:37:23 -05:00
parent 6cb4406080
commit 542c05cb6c

View File

@ -10,7 +10,7 @@ use crate::components::{
use crate::game_log::GameLog; use crate::game_log::GameLog;
use crate::{Bystander, Map, RunState, State, TileType}; 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::<Position>(); let mut positions = ecs.write_storage::<Position>();
let players = ecs.read_storage::<Player>(); let players = ecs.read_storage::<Player>();
let mut viewsheds = ecs.write_storage::<Viewshed>(); let mut viewsheds = ecs.write_storage::<Viewshed>();
@ -25,6 +25,7 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
let mut renderables = ecs.write_storage::<Renderable>(); let mut renderables = ecs.write_storage::<Renderable>();
let bystanders = ecs.read_storage::<Bystander>(); let bystanders = ecs.read_storage::<Bystander>();
let vendors = ecs.read_storage::<Vendor>(); let vendors = ecs.read_storage::<Vendor>();
let mut result = RunState::AwaitingInput;
let mut swap_entities: Vec<(Entity, i32, i32)> = Vec::new(); 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 < 1
|| pos.y + delta_y > map.height - 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); 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; viewshed.dirty = true;
let mut ppos = ecs.write_resource::<Point>(); let mut ppos = ecs.write_resource::<Point>();
ppos.x = pos.x; ppos.x = pos.x;
ppos.y = pos.y ppos.y = pos.y;
result = RunState::PlayerTurn;
} else if combat_stats.get(*potential_target).is_some() { } else if combat_stats.get(*potential_target).is_some() {
wants_to_melee wants_to_melee
.insert( .insert(
@ -67,7 +69,8 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
}, },
) )
.expect("Add target failed"); .expect("Add target failed");
return;
return RunState::PlayerTurn;
} }
if let Some(door) = doors.get_mut(*potential_target) { 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::<Point>(); let mut ppos = ecs.write_resource::<Point>();
ppos.x = pos.x; ppos.x = pos.x;
ppos.y = pos.y; 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; their_pos.y = m.2;
} }
} }
result
} }
pub fn try_next_level(ecs: &mut World) -> bool { 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::Up
| VirtualKeyCode::Numpad8 | VirtualKeyCode::Numpad8
| VirtualKeyCode::Key8 | 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::Down
| VirtualKeyCode::Numpad2 | VirtualKeyCode::Numpad2
| VirtualKeyCode::Key2 | 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::Left
| VirtualKeyCode::Numpad4 | VirtualKeyCode::Numpad4
| VirtualKeyCode::Key4 | 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::Right
| VirtualKeyCode::Numpad6 | VirtualKeyCode::Numpad6
| VirtualKeyCode::Key6 | VirtualKeyCode::Key6
| VirtualKeyCode::L => try_move_player(1, 0, &mut gs.ecs), | VirtualKeyCode::L => return try_move_player(1, 0, &mut gs.ecs),
// Diagonals // Diagonals
VirtualKeyCode::Numpad7 VirtualKeyCode::Numpad7
| VirtualKeyCode::Key7 | VirtualKeyCode::Key7
| VirtualKeyCode::U | 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::Numpad9
| VirtualKeyCode::Key9 | VirtualKeyCode::Key9
| VirtualKeyCode::Y | 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::Numpad1
| VirtualKeyCode::Key1 | VirtualKeyCode::Key1
| VirtualKeyCode::B | 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::Numpad3
| VirtualKeyCode::Key3 | VirtualKeyCode::Key3
| VirtualKeyCode::N | 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 // Skip Turn
VirtualKeyCode::Numpad5 | VirtualKeyCode::Key5 | VirtualKeyCode::Space => { VirtualKeyCode::Numpad5 | VirtualKeyCode::Key5 | VirtualKeyCode::Space => {