roguelike-game/src/inventory_system.rs

398 lines
14 KiB
Rust
Raw Normal View History

2021-11-15 09:19:22 -05:00
use crate::components::*;
2021-11-17 13:50:55 -05:00
use crate::particle_system::ParticleBuilder;
2021-11-15 09:19:22 -05:00
use crate::{game_log::GameLog, Map};
2021-11-17 13:50:55 -05:00
use rltk::RGB;
2021-11-03 15:11:19 -04:00
use specs::prelude::*;
pub struct ItemCollectionSystem {}
impl<'a> System<'a> for ItemCollectionSystem {
#[allow(clippy::type_complexity)]
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("Failed to add item to backpack");
if pickup.collected_by == *player_entity {
gamelog.entries.push(format!(
"You pick up the {}.",
names.get(pickup.item).unwrap().name
));
}
}
wants_pickup.clear();
}
}
2021-11-03 15:59:23 -04:00
pub struct ItemUseSystem {}
2021-11-03 15:59:23 -04:00
impl<'a> System<'a> for ItemUseSystem {
2021-11-03 15:59:23 -04:00
#[allow(clippy::type_complexity)]
type SystemData = (
ReadExpect<'a, Entity>,
WriteExpect<'a, GameLog>,
ReadExpect<'a, Map>,
2021-11-03 15:59:23 -04:00
Entities<'a>,
WriteStorage<'a, WantsToUseItem>,
2021-11-03 15:59:23 -04:00
ReadStorage<'a, Name>,
ReadStorage<'a, Consumable>,
ReadStorage<'a, ProvidesHealing>,
2021-11-05 10:42:44 -04:00
ReadStorage<'a, InflictsDamage>,
2021-11-03 15:59:23 -04:00
WriteStorage<'a, CombatStats>,
2021-11-05 10:42:44 -04:00
WriteStorage<'a, SufferDamage>,
2021-11-05 13:12:22 -04:00
ReadStorage<'a, AreaOfEffect>,
2021-11-05 14:32:14 -04:00
WriteStorage<'a, Confusion>,
2021-11-15 09:19:22 -05:00
ReadStorage<'a, Equippable>,
WriteStorage<'a, Equipped>,
WriteStorage<'a, InBackpack>,
2021-11-17 13:50:55 -05:00
WriteExpect<'a, ParticleBuilder>,
ReadStorage<'a, Position>,
2021-11-03 15:59:23 -04:00
);
#[allow(clippy::cognitive_complexity)]
2021-11-03 15:59:23 -04:00
fn run(&mut self, data: Self::SystemData) {
let (
player_entity,
mut gamelog,
map,
2021-11-03 15:59:23 -04:00
entities,
mut wants_use,
2021-11-03 15:59:23 -04:00
names,
consumables,
healing,
2021-11-05 10:42:44 -04:00
inflict_damage,
2021-11-03 15:59:23 -04:00
mut combat_stats,
2021-11-05 10:42:44 -04:00
mut suffer_damage,
2021-11-05 13:12:22 -04:00
aoe,
2021-11-05 14:32:14 -04:00
mut confused,
2021-11-15 09:19:22 -05:00
equippable,
mut equipped,
mut backpack,
2021-11-17 13:50:55 -05:00
mut particle_builder,
positions,
2021-11-03 15:59:23 -04:00
) = data;
2021-11-05 13:12:22 -04:00
for (entity, useitem) in (&entities, &wants_use).join() {
2021-11-05 10:42:44 -04:00
let mut used_item = true;
2021-11-05 13:12:22 -04:00
// Targeting
let mut targets: Vec<Entity> = Vec::new();
match useitem.target {
None => {
targets.push(*player_entity);
}
Some(target) => {
match aoe.get(useitem.item) {
None => {
// Single target in tile
let idx = map.xy_idx(target.x, target.y);
for mob in map.tile_content[idx].iter() {
targets.push(*mob);
}
}
Some(area_effect) => {
// AoE
let mut blast_tiles =
rltk::field_of_view(target, area_effect.radius, &*map);
blast_tiles.retain(|p| {
p.x > 0 && p.x < map.width - 1 && p.y > 0 && p.y < map.height - 1
});
for tile_idx in blast_tiles.iter() {
let idx = map.xy_idx(tile_idx.x, tile_idx.y);
for mob in map.tile_content[idx].iter() {
targets.push(*mob);
}
2021-11-17 13:50:55 -05:00
particle_builder.request(
tile_idx.x,
tile_idx.y,
RGB::named(rltk::ORANGE),
RGB::named(rltk::BLACK),
rltk::to_cp437('░'),
200.0,
);
2021-11-05 13:12:22 -04:00
}
}
}
}
}
2021-11-15 09:19:22 -05:00
// If it is equippable, then we want to equip it - and unequip whatever else was in that slot
match equippable.get(useitem.item) {
None => {}
Some(can_equip) => {
let target_slot = can_equip.slot;
let target = targets[0];
// Remove any items the target has in the item's slot
let mut to_unequip: Vec<Entity> = Vec::new();
for (item_entity, already_equipped, name) in
(&entities, &equipped, &names).join()
{
if already_equipped.owner == target && already_equipped.slot == target_slot
{
to_unequip.push(item_entity);
if target == *player_entity {
gamelog.entries.push(format!("You unequip {}.", name.name));
}
}
}
for item in to_unequip.iter() {
equipped.remove(*item);
backpack
.insert(*item, InBackpack { owner: target })
.expect("Unable to put unequipped item back in backpack");
}
// Wield the item
equipped
.insert(
useitem.item,
Equipped {
owner: target,
slot: target_slot,
},
)
.expect("Failed to equip item");
backpack.remove(useitem.item);
if target == *player_entity {
gamelog.entries.push(format!(
"You equip {}.",
names.get(useitem.item).unwrap().name
));
}
}
}
2021-11-05 10:42:44 -04:00
// If the item heals, apply the healing
match healing.get(useitem.item) {
2021-11-03 15:59:23 -04:00
None => {}
Some(healer) => {
2021-11-05 10:42:44 -04:00
used_item = false;
2021-11-05 13:12:22 -04:00
for target in targets.iter() {
let stats = combat_stats.get_mut(*target);
if let Some(stats) = stats {
stats.hp = i32::min(stats.max_hp, stats.hp + healer.heal_amount);
if entity == *player_entity {
gamelog.entries.push(format!(
"You drink the {}, healing {} hp.",
names.get(useitem.item).unwrap().name,
healer.heal_amount
));
}
used_item = true;
2021-11-17 13:50:55 -05:00
// Visually show healing
if let Some(pos) = positions.get(*target) {
particle_builder.request(
pos.x,
pos.y,
RGB::named(rltk::GREEN),
RGB::named(rltk::BLACK),
rltk::to_cp437('♥'),
200.0,
);
}
2021-11-05 13:12:22 -04:00
}
2021-11-03 15:59:23 -04:00
}
}
}
2021-11-05 10:42:44 -04:00
// If it inflicts damage, apply it to the target cell
match inflict_damage.get(useitem.item) {
None => {}
2021-11-05 10:42:44 -04:00
Some(damage) => {
used_item = false;
2021-11-05 13:12:22 -04:00
for mob in targets.iter() {
2021-11-05 10:42:44 -04:00
SufferDamage::new_damage(&mut suffer_damage, *mob, damage.damage);
if entity == *player_entity {
let mob_name = names.get(*mob).unwrap();
let item_name = names.get(useitem.item).unwrap();
gamelog.entries.push(format!(
"You use {} on {}, inflicting {} hp.",
item_name.name, mob_name.name, damage.damage
));
2021-11-17 13:50:55 -05:00
if let Some(pos) = positions.get(*mob) {
particle_builder.request(
pos.x,
pos.y,
RGB::named(rltk::RED),
RGB::named(rltk::BLACK),
rltk::to_cp437('‼'),
200.0,
);
}
2021-11-05 10:42:44 -04:00
}
used_item = true;
}
}
}
2021-11-05 14:32:14 -04:00
// Can it pass along confusion? Note the use of scopes
// to escape from the borrow checker!
let mut add_confusion = Vec::new();
{
match confused.get(useitem.item) {
None => {}
Some(confusion) => {
used_item = false;
for mob in targets.iter() {
add_confusion.push((*mob, confusion.turns));
if entity == *player_entity {
let mob_name = names.get(*mob).unwrap();
let item_name = names.get(useitem.item).unwrap();
gamelog.entries.push(format!(
"You use {} on {}, confusing them.",
item_name.name, mob_name.name
));
2021-11-17 13:50:55 -05:00
if let Some(pos) = positions.get(*mob) {
particle_builder.request(
pos.x,
pos.y,
RGB::named(rltk::MAGENTA),
RGB::named(rltk::BLACK),
rltk::to_cp437('?'),
200.0,
);
}
2021-11-05 14:32:14 -04:00
}
}
}
}
}
for mob in add_confusion.iter() {
confused
.insert(mob.0, Confusion { turns: mob.1 })
.expect("Unable to add confused status");
}
2021-11-05 10:42:44 -04:00
// If it's a consumable, delete it on use
if used_item {
let consumable = consumables.get(useitem.item);
match consumable {
None => {}
Some(_) => {
entities
.delete(useitem.item)
.expect("Failed to consume item");
}
2021-11-03 15:59:23 -04:00
}
}
}
wants_use.clear();
2021-11-03 15:59:23 -04:00
}
}
2021-11-04 09:40:58 -04:00
pub struct ItemDropSystem {}
impl<'a> System<'a> for ItemDropSystem {
#[allow(clippy::type_complexity)]
2021-11-04 09:40:58 -04:00
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 drop item to 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();
}
}
2021-11-15 11:32:09 -05:00
pub struct ItemRemoveSystem {}
impl<'a> System<'a> for ItemRemoveSystem {
#[allow(clippy::type_complexity)]
2021-11-15 11:32:09 -05:00
type SystemData = (
Entities<'a>,
WriteStorage<'a, WantsToRemoveItem>,
WriteStorage<'a, Equipped>,
2021-11-15 13:27:40 -05:00
WriteStorage<'a, InBackpack>,
2021-11-15 11:32:09 -05:00
);
fn run(&mut self, data: Self::SystemData) {
let (entities, mut wants_remove, mut equipped, mut backpack) = data;
for (entity, to_remove) in (&entities, &wants_remove).join() {
equipped.remove(to_remove.item);
2021-11-15 13:27:40 -05:00
backpack
.insert(to_remove.item, InBackpack { owner: entity })
2021-11-15 11:32:09 -05:00
.expect("Unable to remove item and put in backpack");
}
wants_remove.clear();
}
}