Allow player to pick up items

This commit is contained in:
Daniel Lynn 2021-07-10 19:15:31 -05:00
parent 4e639f9179
commit 4aecf6f7d6
2 changed files with 23 additions and 2 deletions

View File

@ -78,3 +78,6 @@ pub struct ProvidesHealing {
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ProvidesDungeonMap;
#[derive(Clone, PartialEq)]
pub struct Carried(pub Entity);

View File

@ -5,6 +5,8 @@ use crate::prelude::*;
#[read_component(Player)]
#[read_component(Enemy)]
#[write_component(Health)]
#[read_component(Item)]
#[read_component(Carried)]
pub fn player_input(
ecs: &mut SubWorld,
commands: &mut CommandBuffer,
@ -18,6 +20,23 @@ pub fn player_input(
VirtualKeyCode::Right | VirtualKeyCode::D => Point::new(1, 0),
VirtualKeyCode::Up | VirtualKeyCode::W => Point::new(0, -1),
VirtualKeyCode::Down | VirtualKeyCode::S => Point::new(0, 1),
VirtualKeyCode::G => {
let (player, player_pos) = players
.iter(ecs)
.find_map(|(entity, pos)| Some((*entity, *pos)))
.unwrap();
let mut items = <(Entity, &Item, &Point)>::query();
items
.iter(ecs)
.filter(|(_entity, _item, &item_pos)| item_pos == player_pos)
.for_each(|(entity, _item, _item_pos)| {
commands.remove_component::<Point>(*entity);
commands.add_component(*entity, Carried(player));
});
Point::new(0, 0)
}
_ => Point::new(0, 0),
};
@ -26,11 +45,10 @@ pub fn player_input(
.find_map(|(entity, pos)| Some((*entity, *pos + delta)))
.unwrap();
let mut enemies = <(Entity, &Point)>::query().filter(component::<Enemy>());
let mut did_something = false;
if delta.x != 0 || delta.y != 0 {
let mut hit_something = false;
let mut enemies = <(Entity, &Point)>::query().filter(component::<Enemy>());
enemies
.iter(ecs)
.filter(|(_, pos)| **pos == destination)