2022-01-05 14:59:45 -05:00
|
|
|
use ::rltk::{Point, RandomNumberGenerator};
|
2021-12-24 10:38:44 -05:00
|
|
|
use ::specs::prelude::*;
|
2022-01-05 14:59:45 -05:00
|
|
|
use rltk::RGB;
|
2021-10-29 15:15:22 -04:00
|
|
|
|
2022-01-05 10:01:05 -05:00
|
|
|
use crate::components::{
|
2022-01-05 14:59:45 -05:00
|
|
|
Attributes, Equipped, InBackpack, LootTable, Name, Player, Pools, Position, SufferDamage,
|
2022-01-05 10:01:05 -05:00
|
|
|
};
|
2021-12-10 20:16:48 -05:00
|
|
|
use crate::game_log::GameLog;
|
2022-01-05 14:59:45 -05:00
|
|
|
use crate::gamesystem::{mana_at_level, player_hp_at_level};
|
|
|
|
use crate::particle_system::ParticleBuilder;
|
2022-01-12 11:12:59 -05:00
|
|
|
use crate::raws::{self, SpawnType, RAWS};
|
|
|
|
use crate::{spatial, Map, RunState};
|
2021-12-10 20:16:48 -05:00
|
|
|
|
2021-10-29 15:15:22 -04:00
|
|
|
pub struct DamageSystem {}
|
|
|
|
|
|
|
|
impl<'a> System<'a> for DamageSystem {
|
2022-01-05 14:59:45 -05:00
|
|
|
#[allow(clippy::type_complexity)]
|
2021-10-29 15:15:22 -04:00
|
|
|
type SystemData = (
|
2022-01-03 16:30:14 -05:00
|
|
|
WriteStorage<'a, Pools>,
|
2021-10-29 15:15:22 -04:00
|
|
|
WriteStorage<'a, SufferDamage>,
|
2021-11-16 10:28:05 -05:00
|
|
|
ReadStorage<'a, Position>,
|
|
|
|
WriteExpect<'a, Map>,
|
|
|
|
Entities<'a>,
|
2022-01-05 14:59:45 -05:00
|
|
|
ReadExpect<'a, Entity>,
|
|
|
|
ReadStorage<'a, Attributes>,
|
|
|
|
WriteExpect<'a, GameLog>,
|
|
|
|
WriteExpect<'a, ParticleBuilder>,
|
|
|
|
ReadExpect<'a, Point>,
|
2021-10-29 15:15:22 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
fn run(&mut self, data: Self::SystemData) {
|
2022-01-05 14:59:45 -05:00
|
|
|
let (
|
|
|
|
mut stats,
|
|
|
|
mut damage,
|
|
|
|
positions,
|
|
|
|
mut map,
|
|
|
|
entities,
|
|
|
|
player,
|
|
|
|
attributes,
|
|
|
|
mut log,
|
|
|
|
mut particles,
|
|
|
|
player_pos,
|
|
|
|
) = data;
|
|
|
|
let mut xp_gain = 0;
|
2022-01-13 10:42:02 -05:00
|
|
|
let mut gold_gain = 0.0_f32;
|
2021-10-29 15:15:22 -04:00
|
|
|
|
2021-11-16 10:28:05 -05:00
|
|
|
for (entity, mut stats, damage) in (&entities, &mut stats, &damage).join() {
|
2022-01-12 11:12:59 -05:00
|
|
|
let pos = positions.get(entity);
|
2022-01-05 14:59:45 -05:00
|
|
|
for dmg in damage.amount.iter() {
|
|
|
|
stats.hit_points.current -= dmg.0;
|
2022-01-12 11:12:59 -05:00
|
|
|
if let Some(pos) = pos {
|
2022-01-05 14:59:45 -05:00
|
|
|
let idx = map.xy_idx(pos.x, pos.y);
|
|
|
|
map.bloodstains.insert(idx);
|
|
|
|
}
|
2021-11-16 10:28:05 -05:00
|
|
|
|
2022-01-05 14:59:45 -05:00
|
|
|
if stats.hit_points.current < 1 && dmg.1 {
|
|
|
|
xp_gain += stats.level * 100;
|
2022-01-13 10:42:02 -05:00
|
|
|
gold_gain += stats.gold;
|
2022-01-12 11:12:59 -05:00
|
|
|
if let Some(pos) = pos {
|
|
|
|
let idx = map.xy_idx(pos.x, pos.y);
|
|
|
|
spatial::remove_entity(entity, idx);
|
|
|
|
}
|
2022-01-05 14:59:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-13 10:42:02 -05:00
|
|
|
if xp_gain != 0 || gold_gain != 0.0 {
|
2022-01-05 14:59:45 -05:00
|
|
|
let mut player_stats = stats.get_mut(*player).unwrap();
|
|
|
|
let player_attributes = attributes.get(*player).unwrap();
|
2022-01-13 10:42:02 -05:00
|
|
|
|
2022-01-05 14:59:45 -05:00
|
|
|
player_stats.xp += xp_gain;
|
2022-01-13 10:42:02 -05:00
|
|
|
player_stats.gold += gold_gain;
|
|
|
|
|
2022-01-05 14:59:45 -05:00
|
|
|
if player_stats.xp >= player_stats.level * 1000 {
|
|
|
|
// We've gone up a level!
|
|
|
|
player_stats.level += 1;
|
|
|
|
log.append(format!(
|
|
|
|
"Congratulations, you are now level {}",
|
|
|
|
player_stats.level
|
|
|
|
));
|
|
|
|
player_stats.hit_points.max = player_hp_at_level(
|
|
|
|
player_attributes.fitness.base + player_attributes.fitness.modifiers,
|
|
|
|
player_stats.level,
|
|
|
|
);
|
|
|
|
player_stats.hit_points.current = player_stats.hit_points.max;
|
|
|
|
player_stats.mana.max = mana_at_level(
|
|
|
|
player_attributes.intelligence.base + player_attributes.intelligence.modifiers,
|
|
|
|
player_stats.level,
|
|
|
|
);
|
|
|
|
player_stats.mana.current = player_stats.mana.max;
|
|
|
|
|
|
|
|
for i in 0..10 {
|
|
|
|
if player_pos.y - i > 1 {
|
|
|
|
particles.request(
|
|
|
|
player_pos.x,
|
|
|
|
player_pos.y - 1,
|
|
|
|
RGB::named(rltk::GOLD),
|
|
|
|
RGB::named(rltk::BLACK),
|
|
|
|
rltk::to_cp437('░'),
|
|
|
|
400.0,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-11-16 10:28:05 -05:00
|
|
|
}
|
2021-10-29 15:15:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
damage.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn delete_the_dead(ecs: &mut World) {
|
|
|
|
let mut dead: Vec<Entity> = Vec::new();
|
|
|
|
|
|
|
|
// Scope for the sake of the borrow checker
|
|
|
|
{
|
2022-01-03 16:30:14 -05:00
|
|
|
let combat_stats = ecs.read_storage::<Pools>();
|
2021-10-29 15:15:22 -04:00
|
|
|
let players = ecs.read_storage::<Player>();
|
2021-11-01 14:46:45 -04:00
|
|
|
let names = ecs.read_storage::<Name>();
|
2021-10-29 15:15:22 -04:00
|
|
|
let entities = ecs.entities();
|
2021-11-01 14:46:45 -04:00
|
|
|
let mut log = ecs.write_resource::<GameLog>();
|
|
|
|
|
2021-10-29 15:15:22 -04:00
|
|
|
for (entity, stats) in (&entities, &combat_stats).join() {
|
2022-01-03 16:30:14 -05:00
|
|
|
if stats.hit_points.current < 1 {
|
|
|
|
match players.get(entity) {
|
2021-11-01 14:46:45 -04:00
|
|
|
None => {
|
2021-11-19 19:52:15 -05:00
|
|
|
if let Some(victim_name) = names.get(entity) {
|
2021-11-19 11:30:25 -05:00
|
|
|
log.append(format!("{} is dead", &victim_name.name));
|
2021-11-01 14:46:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2022-01-05 10:01:05 -05:00
|
|
|
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>();
|
2022-01-05 10:01:05 -05:00
|
|
|
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() {
|
2022-01-05 10:01:05 -05:00
|
|
|
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
|
2022-01-05 10:01:05 -05:00
|
|
|
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
|
2022-01-05 10:01:05 -05:00
|
|
|
if let Some(pos) = pos {
|
2022-01-04 16:17:45 -05:00
|
|
|
to_drop.push((entity, *pos));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-05 10:01:05 -05:00
|
|
|
|
|
|
|
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);
|
2022-01-05 10:01:05 -05:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 10:01:05 -05:00
|
|
|
{
|
|
|
|
for drop in to_spawn.iter() {
|
2022-01-12 11:12:59 -05:00
|
|
|
raws::spawn_named_item(
|
2022-01-05 10:01:05 -05:00
|
|
|
&RAWS.lock().unwrap(),
|
|
|
|
ecs,
|
|
|
|
&drop.0,
|
|
|
|
SpawnType::AtPosition {
|
|
|
|
x: drop.1.x,
|
|
|
|
y: drop.1.y,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-29 15:15:22 -04:00
|
|
|
for victim in dead {
|
|
|
|
ecs.delete_entity(victim)
|
|
|
|
.expect("Unable to delete the dead");
|
|
|
|
}
|
|
|
|
}
|