roguelike/src/player.rs
2022-01-29 16:07:01 +01:00

186 lines
6.4 KiB
Rust

use std::cmp::{max, min};
use rltk::{Point, Rltk, VirtualKeyCode};
use specs::prelude::*;
use crate::gamelog::GameLog;
use crate::{
components::{CombatStats, Player, Position, Viewshed, WantsToMelee},
map::Map,
EntityMoved, HungerClock, HungerState, Item, Monster, RunState, State, TileType,
WantsToPickupItem,
};
pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
let mut positions = ecs.write_storage::<Position>();
let mut players = ecs.write_storage::<Player>();
let mut viewsheds = ecs.write_storage::<Viewshed>();
let combat_stats = ecs.read_storage::<CombatStats>();
let entities = ecs.entities();
let mut wants_to_melee = ecs.write_storage::<WantsToMelee>();
let map = ecs.fetch::<Map>();
let mut entity_moved = ecs.write_storage::<EntityMoved>();
for (entity, _player, pos, viewshed) in
(&entities, &mut 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() {
let target = combat_stats.get(*potential_target);
if let Some(_target) = target {
wants_to_melee
.insert(
entity,
WantsToMelee {
target: *potential_target,
},
)
.expect("Add target failed");
}
}
if !map.blocked[destination_idx] {
pos.x = min(79, max(0, pos.x + delta_x));
pos.y = min(49, max(0, pos.y + delta_y));
viewshed.dirty = true;
let mut ppos = ecs.write_resource::<Point>();
ppos.x = pos.x;
ppos.y = pos.y;
entity_moved
.insert(entity, EntityMoved {})
.expect("Unable to insert marker");
}
}
}
fn get_item(ecs: &mut World) {
let player_pos = ecs.fetch::<Point>();
let player_entity = ecs.fetch::<Entity>();
let entities = ecs.entities();
let items = ecs.read_storage::<Item>();
let positions = ecs.read_storage::<Position>();
let mut gamelog = ecs.fetch_mut::<GameLog>();
let mut target_item: Option<Entity> = 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::<WantsToPickupItem>();
pickup
.insert(
*player_entity,
WantsToPickupItem {
collected_by: *player_entity,
item,
},
)
.expect("Unable to insert want to pickup");
}
}
}
pub fn player_input(gs: &mut State, ctx: &mut Rltk) -> RunState {
match ctx.key {
None => return RunState::AwaitingInput,
Some(key) => match key {
VirtualKeyCode::Left | VirtualKeyCode::H => try_move_player(-1, 0, &mut gs.ecs),
VirtualKeyCode::Right | VirtualKeyCode::L => try_move_player(1, 0, &mut gs.ecs),
VirtualKeyCode::Up | VirtualKeyCode::K => try_move_player(0, -1, &mut gs.ecs),
VirtualKeyCode::Down | VirtualKeyCode::J => try_move_player(0, 1, &mut gs.ecs),
VirtualKeyCode::Space => return skip_turn(&mut gs.ecs),
VirtualKeyCode::Y => try_move_player(1, -1, &mut gs.ecs),
VirtualKeyCode::U => try_move_player(-1, -1, &mut gs.ecs),
VirtualKeyCode::N => try_move_player(1, 1, &mut gs.ecs),
VirtualKeyCode::B => try_move_player(-1, 1, &mut gs.ecs),
VirtualKeyCode::G => get_item(&mut gs.ecs),
VirtualKeyCode::I => return RunState::ShowInventory,
VirtualKeyCode::D => return RunState::ShowDropItem,
VirtualKeyCode::Escape => return RunState::SaveGame,
VirtualKeyCode::R => return RunState::ShowRemoveItem,
VirtualKeyCode::Period => {
if try_next_level(&mut gs.ecs) {
return RunState::NextLevel;
}
}
_ => return RunState::AwaitingInput,
},
}
RunState::PlayerTurn
}
fn skip_turn(ecs: &mut World) -> RunState {
let player_entity = ecs.fetch::<Entity>();
let viewshed_components = ecs.read_storage::<Viewshed>();
let monsters = ecs.read_storage::<Monster>();
let worldmap_resource = ecs.fetch::<Map>();
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() {
if let Some(_mob) = monsters.get(*entity_id) {
can_heal = true;
}
}
}
let hunger_clock = ecs.read_storage::<HungerClock>();
let mut heal_bonus = 0;
if let Some(hc) = hunger_clock.get(*player_entity) {
match hc.state {
HungerState::Hungry | HungerState::Starving => {
can_heal = false;
}
HungerState::WellFed => {
heal_bonus = 1;
}
_ => {}
}
}
if can_heal {
let mut health_component = ecs.write_storage::<CombatStats>();
let player_hp = health_component.get_mut(*player_entity).unwrap();
player_hp.hp = i32::min(player_hp.hp + 1 + heal_bonus, player_hp.max_hp);
}
RunState::PlayerTurn
}
fn try_next_level(ecs: &mut World) -> bool {
let player_pos = ecs.fetch::<Point>();
let map = ecs.fetch::<Map>();
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>();
gamelog
.entries
.push("There is no way down from here".to_string());
false
}
}