roguelike-game/src/damage_system.rs

148 lines
5.3 KiB
Rust
Raw Normal View History

use ::rltk::RandomNumberGenerator;
2021-12-24 10:38:44 -05:00
use ::specs::prelude::*;
2021-10-29 15:15:22 -04:00
2022-01-28 13:47:16 -05:00
use crate::components::{
AreaOfEffect, Equipped, InBackpack, LootTable, Name, OnDeath, Player, Pools, Position,
};
use crate::effects::*;
2022-01-12 11:12:59 -05:00
use crate::raws::{self, SpawnType, RAWS};
use crate::{colors, gamelog, Map, RunState};
2021-10-29 15:15:22 -04:00
pub fn delete_the_dead(ecs: &mut World) {
let mut dead: Vec<Entity> = Vec::new();
// Scope for the sake of the borrow checker
{
let combat_stats = ecs.read_storage::<Pools>();
2021-10-29 15:15:22 -04:00
let players = ecs.read_storage::<Player>();
let names = ecs.read_storage::<Name>();
2021-10-29 15:15:22 -04:00
let entities = ecs.entities();
2021-10-29 15:15:22 -04:00
for (entity, stats) in (&entities, &combat_stats).join() {
if stats.hit_points.current < 1 {
match players.get(entity) {
None => {
if let Some(victim_name) = names.get(entity) {
gamelog::color_line(colors::RED, &victim_name.name)
.append("is dead!")
.log();
}
dead.push(entity)
}
2021-11-15 13:27:40 -05:00
Some(_) => {
let mut runstate = ecs.write_resource::<RunState>();
*runstate = RunState::GameOver;
}
2021-10-29 15:15:22 -04:00
}
}
}
}
2022-01-04 16:17:45 -05:00
// Drop everything held by dead people
let mut to_spawn: Vec<(String, Position)> = Vec::new();
2022-01-04 16:17:45 -05:00
{
// 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>();
let loot_tables = ecs.read_storage::<LootTable>();
let mut rng = ecs.write_resource::<RandomNumberGenerator>();
2022-01-04 16:17:45 -05:00
for victim in dead.iter() {
let pos = positions.get(*victim);
2022-01-04 16:17:45 -05:00
for (entity, equipped) in (&entities, &equipped).join() {
if equipped.owner == *victim {
// Drop their stuff
if let Some(pos) = pos {
2022-01-04 16:17:45 -05:00
to_drop.push((entity, *pos));
}
}
}
for (entity, backpack) in (&entities, &carried).join() {
if backpack.owner == *victim {
// Drop their stuff
if let Some(pos) = pos {
2022-01-04 16:17:45 -05:00
to_drop.push((entity, *pos));
}
}
}
if let Some(table) = loot_tables.get(*victim) {
let drop_finder =
2022-01-12 11:12:59 -05:00
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));
}
}
}
2022-01-04 16:17:45 -05:00
}
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 drop in to_spawn.iter() {
2022-01-12 11:12:59 -05:00
raws::spawn_named_item(
&RAWS.lock().unwrap(),
ecs,
&drop.0,
SpawnType::AtPosition {
x: drop.1.x,
y: drop.1.y,
},
);
}
}
2022-01-28 13:47:16 -05:00
// Fire death events
for victim in dead.iter() {
let death_effects = ecs.read_storage::<OnDeath>();
if let Some(death_effect) = death_effects.get(*victim) {
let mut rng = ecs.fetch_mut::<RandomNumberGenerator>();
for effect in death_effect.abilities.iter() {
if rng.roll_dice(1, 100) <= (effect.chance * 100.0) as i32 {
let map = ecs.fetch::<Map>();
if let Some(pos) = ecs.read_storage::<Position>().get(*victim) {
let spell_entity =
crate::raws::find_spell_entity(ecs, &effect.spell).unwrap();
let tile_idx = map.xy_idx(pos.x, pos.y);
let target = if let Some(aoe) =
ecs.read_storage::<AreaOfEffect>().get(spell_entity)
{
Targets::Tiles {
tiles: aoe_tiles(&map, rltk::Point::new(pos.x, pos.y), aoe.radius),
}
} else {
Targets::Tile {
tile_idx: tile_idx as i32,
}
};
add_effect(
None,
EffectType::SpellUse {
spell: crate::raws::find_spell_entity(ecs, &effect.spell).unwrap(),
},
target,
);
}
}
}
}
}
2021-10-29 15:15:22 -04:00
for victim in dead {
ecs.delete_entity(victim)
.expect("Unable to delete the dead");
}
}