Add chapter 2.6
This commit is contained in:
@@ -1,35 +1,67 @@
|
||||
use crate::{
|
||||
components::{Player, Position},
|
||||
map::{xy_idx, TileType},
|
||||
State,
|
||||
components::{CombatStats, Player, Position, Viewshed, WantsToMelee},
|
||||
map::{Map, TileType},
|
||||
RunState, State,
|
||||
};
|
||||
use rltk::Rltk;
|
||||
use rltk::{console, Point, Rltk, VirtualKeyCode};
|
||||
use specs::prelude::*;
|
||||
use std::cmp::{max, min};
|
||||
|
||||
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 map = ecs.fetch::<Vec<TileType>>();
|
||||
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>();
|
||||
|
||||
for (_player, pos) in (&mut players, &mut positions).join() {
|
||||
let destination_idx = xy_idx(pos.x + delta_x, pos.y + delta_y);
|
||||
if map[destination_idx] != TileType::Wall {
|
||||
for (entity, _player, pos, viewshed) in
|
||||
(&entities, &mut players, &mut positions, &mut viewsheds).join()
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn player_input(gs: &mut State, ctx: &mut Rltk) {
|
||||
pub fn player_input(gs: &mut State, ctx: &mut Rltk) -> RunState {
|
||||
match ctx.key {
|
||||
None => {}
|
||||
None => return RunState::AwaitingInput,
|
||||
Some(key) => match key {
|
||||
rltk::VirtualKeyCode::Left => try_move_player(-1, 0, &mut gs.ecs),
|
||||
rltk::VirtualKeyCode::Right => try_move_player(1, 0, &mut gs.ecs),
|
||||
rltk::VirtualKeyCode::Up => try_move_player(0, -1, &mut gs.ecs),
|
||||
rltk::VirtualKeyCode::Down => try_move_player(0, 1, &mut gs.ecs),
|
||||
_ => {}
|
||||
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::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),
|
||||
_ => return RunState::AwaitingInput,
|
||||
},
|
||||
}
|
||||
|
||||
RunState::PlayerTurn
|
||||
}
|
||||
|
Reference in New Issue
Block a user