From c9b35e27106f16dafb65b2bed5e116309a473be3 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 4 Jan 2022 16:17:45 -0500 Subject: [PATCH] Drop items from defeated characters --- src/damage_system.rs | 41 ++++++++++++++++++++++++++++++++++++-- src/map_builders/forest.rs | 6 +++--- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/src/damage_system.rs b/src/damage_system.rs index 1f4ee17..f5de8d8 100644 --- a/src/damage_system.rs +++ b/src/damage_system.rs @@ -1,8 +1,8 @@ use ::specs::prelude::*; -use crate::components::{Name, Player, Pools, SufferDamage}; +use crate::components::{Equipped, InBackpack, Name, Player, Pools, Position, SufferDamage}; use crate::game_log::GameLog; -use crate::{Map, Position, RunState}; +use crate::{Map, RunState}; pub struct DamageSystem {} @@ -61,6 +61,43 @@ pub fn delete_the_dead(ecs: &mut World) { } } + // Drop everything held by dead people + { + // To avoid hold of borrowed entires, use a scope + let mut to_drop: Vec<(Entity, Position)> = Vec::new(); + let entities = ecs.entities(); + let mut equipped = ecs.write_storage::(); + let mut carried = ecs.write_storage::(); + let mut positions = ecs.write_storage::(); + + for victim in dead.iter() { + for (entity, equipped) in (&entities, &equipped).join() { + if equipped.owner == *victim { + // Drop their stuff + if let Some(pos) = positions.get(*victim) { + to_drop.push((entity, *pos)); + } + } + } + for (entity, backpack) in (&entities, &carried).join() { + if backpack.owner == *victim { + // Drop their stuff + if let Some(pos) = positions.get(*victim) { + to_drop.push((entity, *pos)); + } + } + } + } + + for drop in to_drop.iter() { + equipped.remove(drop.0); + carried.remove(drop.0); + positions + .insert(drop.0, drop.1) + .expect("Unable to insert position"); + } + } + for victim in dead { ecs.delete_entity(victim) .expect("Unable to delete the dead"); diff --git a/src/map_builders/forest.rs b/src/map_builders/forest.rs index 858ec28..f73df89 100644 --- a/src/map_builders/forest.rs +++ b/src/map_builders/forest.rs @@ -81,7 +81,7 @@ impl YellowBrickRoad { } fn build(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { - let starting_pos = build_data.starting_position.as_ref().unwrap().clone(); + let starting_pos = *build_data.starting_position.as_ref().unwrap(); let start_idx = build_data.map.xy_idx(starting_pos.x, starting_pos.y); let (end_x, end_y) = self.find_exit( @@ -92,7 +92,7 @@ impl YellowBrickRoad { let end_idx = build_data.map.xy_idx(end_x, end_y); build_data.map.populate_blocked(); - let path = rltk::a_star_search(start_idx, end_idx, &mut build_data.map); + let path = rltk::a_star_search(start_idx, end_idx, &build_data.map); for idx in path.steps.iter() { let x = *idx as i32 % build_data.map.width; @@ -125,7 +125,7 @@ impl YellowBrickRoad { let (stream_x, stream_y) = self.find_exit(build_data, stream_startx, stream_starty); let stream_idx = build_data.map.xy_idx(stream_x, stream_y) as usize; - let stream = rltk::a_star_search(stairs_idx, stream_idx, &mut build_data.map); + let stream = rltk::a_star_search(stairs_idx, stream_idx, &build_data.map); for tile in stream.steps.iter() { if build_data.map.tiles[*tile as usize] == TileType::Floor { build_data.map.tiles[*tile as usize] = TileType::ShallowWater;