roguelike-game/src/player.rs

150 lines
5.0 KiB
Rust
Raw Normal View History

2021-11-03 15:11:19 -04:00
use crate::{
2021-11-04 10:44:52 -04:00
game_log::GameLog, CombatStats, Item, Map, Player, Position, RunState, State, Viewshed,
2021-11-03 15:11:19 -04:00
WantsToMelee, WantsToPickupItem,
};
2021-10-26 15:43:59 -04:00
use rltk::{Point, Rltk, VirtualKeyCode};
2021-10-22 10:05:06 -04:00
use specs::prelude::*;
2021-10-25 15:26:39 -04:00
use std::cmp::{max, min};
2021-10-22 10:05:06 -04:00
pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) {
let mut positions = ecs.write_storage::<Position>();
2021-10-29 15:15:22 -04:00
let players = ecs.read_storage::<Player>();
2021-10-26 14:23:08 -04:00
let mut viewsheds = ecs.write_storage::<Viewshed>();
2021-10-29 15:15:22 -04:00
let entities = ecs.entities();
let combat_stats = ecs.read_storage::<CombatStats>();
2021-10-25 14:23:19 -04:00
let map = ecs.fetch::<Map>();
2021-10-29 15:15:22 -04:00
let mut wants_to_melee = ecs.write_storage::<WantsToMelee>();
2021-10-22 10:05:06 -04:00
2021-10-29 15:15:22 -04:00
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;
}
2021-10-25 14:23:19 -04:00
let destination_idx = map.xy_idx(pos.x + delta_x, pos.y + delta_y);
2021-10-29 15:15:22 -04:00
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");
return;
}
}
2021-10-29 11:11:17 -04:00
if !map.blocked[destination_idx] {
2021-10-22 10:05:06 -04:00
pos.x = min(79, max(0, pos.x + delta_x));
pos.y = min(49, max(0, pos.y + delta_y));
2021-10-26 14:23:08 -04:00
2021-10-29 15:15:22 -04:00
viewshed.dirty = true;
2021-10-26 15:43:59 -04:00
let mut ppos = ecs.write_resource::<Point>();
ppos.x = pos.x;
ppos.y = pos.y;
2021-10-22 10:05:06 -04:00
}
}
}
2021-10-26 15:43:59 -04:00
pub fn player_input(gs: &mut State, ctx: &mut Rltk) -> RunState {
2021-10-22 10:05:06 -04:00
// Player movement
match ctx.key {
2021-10-29 15:15:22 -04:00
None => return RunState::AwaitingInput, // Nothing happened
2021-10-22 10:05:06 -04:00
Some(key) => match key {
2021-11-03 15:11:19 -04:00
// Cardinal directions
2021-10-29 11:11:17 -04:00
VirtualKeyCode::Left
| VirtualKeyCode::Numpad4
| VirtualKeyCode::H
2021-11-04 09:25:07 -04:00
| VirtualKeyCode::Key4 => try_move_player(-1, 0, &mut gs.ecs),
2021-10-22 14:50:04 -04:00
2021-10-29 11:11:17 -04:00
VirtualKeyCode::Right
| VirtualKeyCode::Numpad6
| VirtualKeyCode::L
2021-11-04 09:25:07 -04:00
| VirtualKeyCode::Key6 => try_move_player(1, 0, &mut gs.ecs),
2021-10-22 14:50:04 -04:00
2021-10-29 11:11:17 -04:00
VirtualKeyCode::Up
| VirtualKeyCode::Numpad8
| VirtualKeyCode::K
2021-11-04 09:25:07 -04:00
| VirtualKeyCode::Key8 => try_move_player(0, -1, &mut gs.ecs),
2021-10-29 11:11:17 -04:00
VirtualKeyCode::Down
| VirtualKeyCode::Numpad2
| VirtualKeyCode::J
| VirtualKeyCode::S
2021-11-04 09:25:07 -04:00
| VirtualKeyCode::Key2 => try_move_player(0, 1, &mut gs.ecs),
2021-10-22 14:50:04 -04:00
2021-10-29 11:11:17 -04:00
// Diagonals
2021-11-04 09:25:07 -04:00
VirtualKeyCode::Numpad9 | VirtualKeyCode::Y | VirtualKeyCode::Key9 => {
2021-10-29 11:11:17 -04:00
try_move_player(1, -1, &mut gs.ecs)
}
2021-11-04 09:25:07 -04:00
VirtualKeyCode::Numpad7 | VirtualKeyCode::U | VirtualKeyCode::Key7 => {
2021-10-29 11:11:17 -04:00
try_move_player(-1, -1, &mut gs.ecs)
}
2021-11-04 09:25:07 -04:00
VirtualKeyCode::Numpad3 | VirtualKeyCode::N | VirtualKeyCode::Key3 => {
2021-10-29 11:11:17 -04:00
try_move_player(1, 1, &mut gs.ecs)
}
2021-11-04 09:25:07 -04:00
VirtualKeyCode::Numpad1 | VirtualKeyCode::B | VirtualKeyCode::Key1 => {
2021-10-29 11:11:17 -04:00
try_move_player(-1, 1, &mut gs.ecs)
2021-10-25 15:26:39 -04:00
}
2021-10-22 14:50:04 -04:00
2021-11-03 15:11:19 -04:00
// Pick up item
VirtualKeyCode::G => get_item(&mut gs.ecs),
// Show inventory
VirtualKeyCode::I => return RunState::ShowInventory,
2021-11-04 09:40:58 -04:00
// Show item drop screen
VirtualKeyCode::D => return RunState::ShowDropItem,
2021-11-08 13:58:40 -05:00
// Save and Quit
VirtualKeyCode::Escape => return RunState::SaveGame,
2021-10-29 15:15:22 -04:00
_ => return RunState::AwaitingInput,
2021-10-22 10:05:06 -04:00
},
}
2021-10-26 15:43:59 -04:00
2021-10-29 15:15:22 -04:00
RunState::PlayerTurn
2021-10-25 15:26:39 -04:00
}
2021-11-03 15:11:19 -04:00
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 pick up item?!");
}
}
}