From d5c48247f37ab7d905f64498e2826bfac3f647cd Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 12 Jan 2022 11:12:59 -0500 Subject: [PATCH] Complete Section 5.15 --- src/damage_system.rs | 15 ++++++++++----- src/map_indexing_system.rs | 19 +++++++++++++++---- src/player.rs | 3 +-- src/spatial.rs | 12 ++++++++++++ 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/src/damage_system.rs b/src/damage_system.rs index b11c606..f4ecd1d 100644 --- a/src/damage_system.rs +++ b/src/damage_system.rs @@ -8,8 +8,8 @@ use crate::components::{ use crate::game_log::GameLog; use crate::gamesystem::{mana_at_level, player_hp_at_level}; use crate::particle_system::ParticleBuilder; -use crate::raws::{get_item_drop, spawn_named_item, SpawnType, RAWS}; -use crate::{Map, RunState}; +use crate::raws::{self, SpawnType, RAWS}; +use crate::{spatial, Map, RunState}; pub struct DamageSystem {} @@ -44,15 +44,20 @@ impl<'a> System<'a> for DamageSystem { let mut xp_gain = 0; for (entity, mut stats, damage) in (&entities, &mut stats, &damage).join() { + let pos = positions.get(entity); for dmg in damage.amount.iter() { stats.hit_points.current -= dmg.0; - if let Some(pos) = positions.get(entity) { + if let Some(pos) = pos { let idx = map.xy_idx(pos.x, pos.y); map.bloodstains.insert(idx); } if stats.hit_points.current < 1 && dmg.1 { xp_gain += stats.level * 100; + if let Some(pos) = pos { + let idx = map.xy_idx(pos.x, pos.y); + spatial::remove_entity(entity, idx); + } } } } @@ -161,7 +166,7 @@ pub fn delete_the_dead(ecs: &mut World) { if let Some(table) = loot_tables.get(*victim) { let drop_finder = - get_item_drop(&crate::raws::RAWS.lock().unwrap(), &mut rng, &table.table); + raws::get_item_drop(&crate::raws::RAWS.lock().unwrap(), &mut rng, &table.table); if let Some(tag) = drop_finder { if let Some(pos) = pos { to_spawn.push((tag, *pos)); @@ -181,7 +186,7 @@ pub fn delete_the_dead(ecs: &mut World) { { for drop in to_spawn.iter() { - spawn_named_item( + raws::spawn_named_item( &RAWS.lock().unwrap(), ecs, &drop.0, diff --git a/src/map_indexing_system.rs b/src/map_indexing_system.rs index c3fd676..11bdaf7 100644 --- a/src/map_indexing_system.rs +++ b/src/map_indexing_system.rs @@ -1,6 +1,7 @@ use ::specs::prelude::*; -use crate::{spatial, BlocksTile, Map, Position}; +use crate::components::{BlocksTile, Pools, Position}; +use crate::{spatial, Map}; pub struct MapIndexingSystem {} @@ -9,18 +10,28 @@ impl<'a> System<'a> for MapIndexingSystem { ReadExpect<'a, Map>, ReadStorage<'a, Position>, ReadStorage<'a, BlocksTile>, + ReadStorage<'a, Pools>, Entities<'a>, ); fn run(&mut self, data: Self::SystemData) { - let (map, position, blockers, entities) = data; + let (map, position, blockers, pools, entities) = data; spatial::clear(); spatial::populate_blocked_from_map(&*map); for (entity, position) in (&entities, &position).join() { - let idx = map.xy_idx(position.x, position.y); - spatial::index_entity(entity, idx, blockers.get(entity).is_some()); + let mut alive = true; + if let Some(pools) = pools.get(entity) { + if pools.hit_points.current < 1 { + alive = false; + } + } + + if alive { + let idx = map.xy_idx(position.x, position.y); + spatial::index_entity(entity, idx, blockers.get(entity).is_some()); + } } } } diff --git a/src/player.rs b/src/player.rs index 2bc7218..77971d8 100644 --- a/src/player.rs +++ b/src/player.rs @@ -124,8 +124,7 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, ecs: &mut World) -> RunState } for m in swap_entities.iter() { - let their_pos = positions.get_mut(m.0); - if let Some(their_pos) = their_pos { + if let Some(their_pos) = positions.get_mut(m.0) { let old_idx = map.xy_idx(their_pos.x, their_pos.y); their_pos.x = m.1; their_pos.y = m.2; diff --git a/src/spatial.rs b/src/spatial.rs index f6eb854..45a1eed 100644 --- a/src/spatial.rs +++ b/src/spatial.rs @@ -119,3 +119,15 @@ pub fn move_entity(entity: Entity, moving_from: usize, moving_to: usize) { lock.blocked[moving_from].1 = from_blocked; lock.blocked[moving_to].1 = to_blocked; } + +pub fn remove_entity(entity: Entity, idx: usize) { + let mut lock = SPATIAL_MAP.lock().unwrap(); + lock.tile_content[idx].retain(|(e, _)| *e != entity); + let mut from_blocked = false; + lock.tile_content[idx].iter().for_each(|(_, blocks)| { + if *blocks { + from_blocked = true; + } + }); + lock.blocked[idx].1 = from_blocked; +}