Drop items from defeated characters

This commit is contained in:
Timothy Warren 2022-01-04 16:17:45 -05:00
parent dad9b1de33
commit c9b35e2710
2 changed files with 42 additions and 5 deletions

View File

@ -1,8 +1,8 @@
use ::specs::prelude::*; 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::game_log::GameLog;
use crate::{Map, Position, RunState}; use crate::{Map, RunState};
pub struct DamageSystem {} 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::<Equipped>();
let mut carried = ecs.write_storage::<InBackpack>();
let mut positions = ecs.write_storage::<Position>();
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 { for victim in dead {
ecs.delete_entity(victim) ecs.delete_entity(victim)
.expect("Unable to delete the dead"); .expect("Unable to delete the dead");

View File

@ -81,7 +81,7 @@ impl YellowBrickRoad {
} }
fn build(&mut self, rng: &mut RandomNumberGenerator, build_data: &mut BuilderMap) { 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 start_idx = build_data.map.xy_idx(starting_pos.x, starting_pos.y);
let (end_x, end_y) = self.find_exit( 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); let end_idx = build_data.map.xy_idx(end_x, end_y);
build_data.map.populate_blocked(); 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() { for idx in path.steps.iter() {
let x = *idx as i32 % build_data.map.width; 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_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_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() { for tile in stream.steps.iter() {
if build_data.map.tiles[*tile as usize] == TileType::Floor { if build_data.map.tiles[*tile as usize] == TileType::Floor {
build_data.map.tiles[*tile as usize] = TileType::ShallowWater; build_data.map.tiles[*tile as usize] = TileType::ShallowWater;