123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- extern crate specs;
- use specs::prelude::*;
- use super::{WantsToPickupItem, Name, InBackpack, Position, gamelog::GameLog, WantsToUseItem,
- Consumable, ProvidesHealing, CombatStats, WantsToDropItem, InflictsDamage, Map, SufferDamage,
- AreaOfEffect, Confusion};
- 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("Unable to insert backpack entry");
- if pickup.collected_by == *player_entity {
- gamelog.entries.insert(0, format!("You pick up the {}.", names.get(pickup.item).unwrap().name));
- }
- }
- wants_pickup.clear();
- }
- }
- pub struct ItemUseSystem {}
- impl<'a> System<'a> for ItemUseSystem {
- #[allow(clippy::type_complexity)]
- type SystemData = ( ReadExpect<'a, Entity>,
- WriteExpect<'a, GameLog>,
- ReadExpect<'a, Map>,
- Entities<'a>,
- WriteStorage<'a, WantsToUseItem>,
- ReadStorage<'a, Name>,
- ReadStorage<'a, Consumable>,
- ReadStorage<'a, ProvidesHealing>,
- ReadStorage<'a, InflictsDamage>,
- WriteStorage<'a, CombatStats>,
- WriteStorage<'a, SufferDamage>,
- ReadStorage<'a, AreaOfEffect>,
- WriteStorage<'a, Confusion>
- );
- fn run(&mut self, data : Self::SystemData) {
- let (player_entity, mut gamelog, map, entities, mut wants_use, names,
- consumables, healing, inflict_damage, mut combat_stats, mut suffer_damage,
- aoe, mut confused) = data;
- for (entity, useitem) in (&entities, &wants_use).join() {
- let mut used_item = true;
-
- let mut targets : Vec<Entity> = Vec::new();
- match useitem.target {
- None => { targets.push( *player_entity ); }
- Some(target) => {
- let area_effect = aoe.get(useitem.item);
- match area_effect {
- None => {
-
- let idx = map.xy_idx(target.x, target.y);
- for mob in map.tile_content[idx].iter() {
- targets.push(*mob);
- }
- }
- Some(area_effect) => {
-
- let blast_tiles = rltk::field_of_view(target, area_effect.radius, &*map);
- 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);
- }
- }
- }
- }
- }
- }
-
- let item_heals = healing.get(useitem.item);
- match item_heals {
- None => {}
- Some(healer) => {
- used_item = false;
- for target in targets.iter() {
- let stats = combat_stats.get_mut(*target);
- if let Some(stats) = stats {
- stats.hp = i32::max(stats.max_hp, stats.hp + healer.heal_amount);
- if entity == *player_entity {
- gamelog.entries.insert(0, format!("You use the {}, healing {} hp.", names.get(useitem.item).unwrap().name, healer.heal_amount));
- }
- used_item = true;
- }
- }
- }
- }
-
- let item_damages = inflict_damage.get(useitem.item);
- match item_damages {
- None => {}
- Some(damage) => {
- used_item = false;
- for mob in targets.iter() {
- suffer_damage.insert(*mob, SufferDamage{ amount : damage.damage }).expect("Unable to insert");
- if entity == *player_entity {
- let mob_name = names.get(*mob).unwrap();
- let item_name = names.get(useitem.item).unwrap();
- gamelog.entries.insert(0, format!("You use {} on {}, inflicting {} hp.", item_name.name, mob_name.name, damage.damage));
- }
- used_item = true;
- }
- }
- }
-
- let mut add_confusion = Vec::new();
- {
- let causes_confusion = confused.get(useitem.item);
- match causes_confusion {
- 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.insert(0, format!("You use {} on {}, confusing them.", item_name.name, mob_name.name));
- }
- }
- }
- }
- }
- for mob in add_confusion.iter() {
- confused.insert(mob.0, Confusion{ turns: mob.1 }).expect("Unable to insert status");
- }
-
- if used_item {
- let consumable = consumables.get(useitem.item);
- match consumable {
- None => {}
- Some(_) => {
- entities.delete(useitem.item).expect("Delete failed");
- }
- }
- }
- }
- wants_use.clear();
- }
- }
- pub struct ItemDropSystem {}
- impl<'a> System<'a> for ItemDropSystem {
- #[allow(clippy::type_complexity)]
- 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.insert(0, format!("You drop up the {}.", names.get(to_drop.item).unwrap().name));
- }
- }
- wants_drop.clear();
- }
- }
|