Add chapter 2.9
This commit is contained in:
116
src/inventory_system.rs
Normal file
116
src/inventory_system.rs
Normal file
@@ -0,0 +1,116 @@
|
||||
use rltk::Rltk;
|
||||
use specs::prelude::*;
|
||||
|
||||
use crate::{CombatStats, InBackpack, Name, Position, Potion, State, WantsToDrinkPotion, WantsToDropItem, WantsToPickupItem};
|
||||
use crate::gamelog::GameLog;
|
||||
use crate::gui::ItemMenuResult;
|
||||
|
||||
pub struct ItemCollectionSystem {}
|
||||
|
||||
impl<'a> System<'a> for ItemCollectionSystem {
|
||||
type SystemData = (
|
||||
ReadExpect<'a, Entity>,
|
||||
WriteExpect<'a, GameLog>,
|
||||
WriteStorage<'a, WantsToPickupItem>,
|
||||
WriteStorage<'a, Position>,
|
||||
ReadStorage<'a, Name>,
|
||||
WriteStorage<'a, InBackpack>,
|
||||
);
|
||||
|
||||
fn run(&mut self, data: Self::SystemData) {
|
||||
let (player_entity, mut gamelog, mut wants_pickup, mut positions, names, mut backpack) =
|
||||
data;
|
||||
|
||||
for pickup in wants_pickup.join() {
|
||||
positions.remove(pickup.item);
|
||||
backpack
|
||||
.insert(
|
||||
pickup.item,
|
||||
InBackpack {
|
||||
owner: pickup.collected_by,
|
||||
},
|
||||
)
|
||||
.expect("Unable to insert backpack entry");
|
||||
|
||||
if pickup.collected_by == *player_entity {
|
||||
gamelog.entries.push(format!(
|
||||
"You pick up the {}.",
|
||||
names.get(pickup.item).unwrap().name
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
wants_pickup.clear();
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PotionUseSystem {}
|
||||
|
||||
impl<'a> System<'a> for PotionUseSystem {
|
||||
type SystemData = (
|
||||
ReadExpect<'a, Entity>,
|
||||
WriteExpect<'a, GameLog>,
|
||||
Entities<'a>,
|
||||
WriteStorage<'a, WantsToDrinkPotion>,
|
||||
ReadStorage<'a, Name>,
|
||||
ReadStorage<'a, Potion>,
|
||||
WriteStorage<'a, CombatStats>
|
||||
);
|
||||
|
||||
fn run(&mut self, data: Self::SystemData) {
|
||||
let (player_entity, mut gamelog, entities, mut wants_drink, names, potions, mut combat_stats) = data;
|
||||
|
||||
for (entity, drink, stats) in (&entities, &wants_drink, &mut combat_stats).join() {
|
||||
let potion = potions.get(drink.potion);
|
||||
match potion {
|
||||
None => {}
|
||||
Some(potion) => {
|
||||
stats.hp = i32::min(stats.max_hp, stats.hp + potion.heal_amount);
|
||||
if entity == *player_entity {
|
||||
gamelog.entries.push(format!("You drink the {}, healing {} hp.", names.get(drink.potion).unwrap().name, potion.heal_amount));
|
||||
}
|
||||
entities.delete(drink.potion).expect("Delete failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
wants_drink.clear();
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ItemDropSystem {}
|
||||
|
||||
impl<'a> System<'a> for ItemDropSystem {
|
||||
type SystemData = (
|
||||
ReadExpect<'a, Entity>,
|
||||
WriteExpect<'a, GameLog>,
|
||||
Entities<'a>,
|
||||
WriteStorage<'a, WantsToDropItem>,
|
||||
ReadStorage<'a, Name>,
|
||||
WriteStorage<'a, Position>,
|
||||
WriteStorage<'a, InBackpack>
|
||||
);
|
||||
|
||||
fn run(&mut self, data: Self::SystemData) {
|
||||
let (player_entity, mut gamelog, entities, mut wants_drop, names, mut positions, mut backpack) = data;
|
||||
|
||||
for (entity, to_drop) in (&entities, &wants_drop).join() {
|
||||
let mut dropper_pos: Position = Position { x: 0, y: 0 };
|
||||
{
|
||||
let dropped_pos = positions.get(entity).unwrap();
|
||||
dropper_pos.x = dropped_pos.x;
|
||||
dropper_pos.y = dropped_pos.y;
|
||||
}
|
||||
positions.insert(to_drop.item, Position { x: dropper_pos.x, y: dropper_pos.y }).expect("Unable to insert position");
|
||||
backpack.remove(to_drop.item);
|
||||
|
||||
if entity == *player_entity {
|
||||
gamelog.entries.push(format!("You drop the {}.", names.get(to_drop.item).unwrap().name));
|
||||
}
|
||||
}
|
||||
|
||||
wants_drop.clear();
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user