use std::cmp::{max, min}; use ::rltk::{Point, Rltk, VirtualKeyCode}; use ::specs::prelude::*; use crate::components::{ BlocksTile, BlocksVisibility, CombatStats, Door, EntityMoved, HungerClock, HungerState, Item, Monster, Player, Position, Renderable, Viewshed, WantsToMelee, WantsToPickupItem, }; use crate::game_log::GameLog; use crate::{Map, RunState, State, TileType}; pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) { let mut positions = ecs.write_storage::(); let players = ecs.read_storage::(); let mut viewsheds = ecs.write_storage::(); let entities = ecs.entities(); let combat_stats = ecs.read_storage::(); let map = ecs.fetch::(); let mut wants_to_melee = ecs.write_storage::(); let mut entity_moved = ecs.write_storage::(); let mut doors = ecs.write_storage::(); let mut blocks_visibility = ecs.write_storage::(); let mut blocks_movement = ecs.write_storage::(); let mut renderables = ecs.write_storage::(); for (entity, _player, pos, viewshed) in (&entities, &players, &mut positions, &mut viewsheds).join() { if pos.x + delta_x < 1 || pos.x + delta_x > map.width - 1 || pos.y + delta_y < 1 || pos.y + delta_y > map.height - 1 { return; } let destination_idx = map.xy_idx(pos.x + delta_x, pos.y + delta_y); for potential_target in map.tile_content[destination_idx].iter() { if let Some(_target) = combat_stats.get(*potential_target) { wants_to_melee .insert( entity, WantsToMelee { target: *potential_target, }, ) .expect("Add target failed"); return; } if let Some(door) = doors.get_mut(*potential_target) { door.open = true; blocks_visibility.remove(*potential_target); blocks_movement.remove(*potential_target); let glyph = renderables.get_mut(*potential_target).unwrap(); glyph.glyph = rltk::to_cp437('/'); viewshed.dirty = true; } } if !map.blocked[destination_idx] { pos.x = min(map.width - 1, max(0, pos.x + delta_x)); pos.y = min(map.height - 1, max(0, pos.y + delta_y)); entity_moved .insert(entity, EntityMoved {}) .expect("Failed to add EntityMoved flag to player"); viewshed.dirty = true; let mut ppos = ecs.write_resource::(); ppos.x = pos.x; ppos.y = pos.y; } } } pub fn try_next_level(ecs: &mut World) -> bool { let player_pos = ecs.fetch::(); let map = ecs.fetch::(); let player_idx = map.xy_idx(player_pos.x, player_pos.y); if map.tiles[player_idx] == TileType::DownStairs { true } else { let mut gamelog = ecs.fetch_mut::(); gamelog .entries .push("There is no way down from here.".to_string()); false } } fn get_item(ecs: &mut World) { let player_pos = ecs.fetch::(); let player_entity = ecs.fetch::(); let entities = ecs.entities(); let items = ecs.read_storage::(); let positions = ecs.read_storage::(); let mut gamelog = ecs.fetch_mut::(); let mut target_item: Option = None; for (item_entity, _item, position) in (&entities, &items, &positions).join() { if position.x == player_pos.x && position.y == player_pos.y { target_item = Some(item_entity); } } match target_item { None => gamelog .entries .push("There is nothing here to pick up.".to_string()), Some(item) => { let mut pickup = ecs.write_storage::(); pickup .insert( *player_entity, WantsToPickupItem { collected_by: *player_entity, item, }, ) .expect("Unable to pick up item?!"); } } } fn skip_turn(ecs: &mut World) -> RunState { let player_entity = ecs.fetch::(); let viewshed_components = ecs.read_storage::(); let monsters = ecs.read_storage::(); let worldmap_resource = ecs.fetch::(); let mut can_heal = true; let viewshed = viewshed_components.get(*player_entity).unwrap(); for tile in viewshed.visible_tiles.iter() { let idx = worldmap_resource.xy_idx(tile.x, tile.y); for entity_id in worldmap_resource.tile_content[idx].iter() { match monsters.get(*entity_id) { None => {} Some(_) => { can_heal = false; } } } } // Don't allow healing when hungry or starving let hunger_clocks = ecs.read_storage::(); if let Some(hc) = hunger_clocks.get(*player_entity) { match hc.state { HungerState::Hungry => can_heal = false, HungerState::Starving => can_heal = false, _ => {} } } if can_heal { let mut health_components = ecs.write_storage::(); let player_hp = health_components.get_mut(*player_entity).unwrap(); player_hp.hp = i32::min(player_hp.hp + 1, player_hp.max_hp); } RunState::PlayerTurn } pub fn player_input(gs: &mut State, ctx: &mut Rltk) -> RunState { // Mac OS is special when it comes to the numpad. Instead of reporting // the keys as Numpad-specific numbers, it reports the number row scan // codes. The quick fix is to match on both types of number scan codes. match ctx.key { None => return RunState::AwaitingInput, // Nothing happened Some(key) => match key { // Cardinal directions VirtualKeyCode::Up | VirtualKeyCode::Numpad8 | VirtualKeyCode::Key8 | VirtualKeyCode::K => 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::Left | VirtualKeyCode::Numpad4 | VirtualKeyCode::Key4 | VirtualKeyCode::H => try_move_player(-1, 0, &mut gs.ecs), VirtualKeyCode::Right | VirtualKeyCode::Numpad6 | VirtualKeyCode::Key6 | VirtualKeyCode::L => 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::Numpad9 | VirtualKeyCode::Key9 | VirtualKeyCode::Y | VirtualKeyCode::W => 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::Numpad3 | VirtualKeyCode::Key3 | VirtualKeyCode::N | VirtualKeyCode::S => try_move_player(1, 1, &mut gs.ecs), // Skip Turn VirtualKeyCode::Numpad5 | VirtualKeyCode::Key5 | VirtualKeyCode::Space => { return skip_turn(&mut gs.ecs) } // Level changes VirtualKeyCode::Period => { if try_next_level(&mut gs.ecs) { return RunState::NextLevel; } } // Item management VirtualKeyCode::G => get_item(&mut gs.ecs), VirtualKeyCode::I => return RunState::ShowInventory, VirtualKeyCode::D => return RunState::ShowDropItem, VirtualKeyCode::R => return RunState::ShowRemoveItem, // Save and Quit VirtualKeyCode::Escape => return RunState::SaveGame, _ => return RunState::AwaitingInput, }, } RunState::PlayerTurn }