2022-01-28 12:05:49 -05:00
|
|
|
use ::rltk::{Point, RandomNumberGenerator};
|
2022-01-20 11:48:58 -05:00
|
|
|
use ::specs::prelude::*;
|
2022-01-24 10:58:37 -05:00
|
|
|
use ::specs::saveload::{MarkedBuilder, SimpleMarker};
|
2022-01-20 11:48:58 -05:00
|
|
|
|
|
|
|
use super::{add_effect, entity_position, EffectSpawner, EffectType, Targets};
|
2022-01-25 14:02:40 -05:00
|
|
|
use crate::components::{
|
2022-01-28 12:05:49 -05:00
|
|
|
Attributes, Confusion, DamageOverTime, Duration, Name, Player, Pools, SerializeMe, Skills, Slow,
|
2022-01-25 14:02:40 -05:00
|
|
|
};
|
2022-01-20 11:48:58 -05:00
|
|
|
use crate::gamesystem::{mana_at_level, player_hp_at_level};
|
2022-02-01 10:39:46 -05:00
|
|
|
use crate::{colors, gamelog, EquipmentChanged, Map, StatusEffect};
|
2022-01-20 11:48:58 -05:00
|
|
|
|
|
|
|
pub fn inflict_damage(ecs: &mut World, damage: &EffectSpawner, target: Entity) {
|
|
|
|
let mut pools = ecs.write_storage::<Pools>();
|
2022-02-01 11:59:44 -05:00
|
|
|
let player_entity = ecs.fetch::<Entity>();
|
2022-01-20 11:48:58 -05:00
|
|
|
if let Some(pool) = pools.get_mut(target) {
|
|
|
|
if !pool.god_mode {
|
2022-01-28 11:48:25 -05:00
|
|
|
if let Some(creator) = damage.creator {
|
|
|
|
if creator == target {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2022-01-20 11:48:58 -05:00
|
|
|
if let EffectType::Damage { amount } = damage.effect_type {
|
|
|
|
pool.hit_points.current -= amount;
|
|
|
|
add_effect(None, EffectType::Bloodstain, Targets::Single { target });
|
|
|
|
add_effect(
|
|
|
|
None,
|
|
|
|
EffectType::Particle {
|
|
|
|
glyph: rltk::to_cp437('‼'),
|
|
|
|
fg: colors::ORANGE,
|
|
|
|
bg: colors::BLACK,
|
|
|
|
lifespan: 200.0,
|
|
|
|
},
|
|
|
|
Targets::Single { target },
|
|
|
|
);
|
2022-02-01 11:59:44 -05:00
|
|
|
if target == *player_entity {
|
|
|
|
gamelog::record_event("Damage Taken", amount);
|
|
|
|
}
|
|
|
|
if let Some(creator) = damage.creator {
|
|
|
|
if creator == *player_entity {
|
|
|
|
gamelog::record_event("Damage Inflicted", amount);
|
|
|
|
}
|
|
|
|
}
|
2022-01-20 11:48:58 -05:00
|
|
|
|
|
|
|
if pool.hit_points.current < 1 {
|
|
|
|
add_effect(
|
|
|
|
damage.creator,
|
|
|
|
EffectType::EntityDeath,
|
|
|
|
Targets::Single { target },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn bloodstain(ecs: &mut World, tile_idx: i32) {
|
|
|
|
let mut map = ecs.fetch_mut::<Map>();
|
|
|
|
map.bloodstains.insert(tile_idx as usize);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn death(ecs: &mut World, effect: &EffectSpawner, target: Entity) {
|
|
|
|
let mut xp_gain = 0;
|
|
|
|
let mut gold_gain = 0.0f32;
|
|
|
|
|
|
|
|
let mut pools = ecs.write_storage::<Pools>();
|
2022-01-28 12:05:49 -05:00
|
|
|
let mut attributes = ecs.write_storage::<Attributes>();
|
2022-01-20 19:41:16 -05:00
|
|
|
let map = ecs.fetch::<Map>();
|
2022-01-20 11:48:58 -05:00
|
|
|
|
|
|
|
if let Some(pos) = entity_position(ecs, target) {
|
|
|
|
crate::spatial::remove_entity(target, pos as usize);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(source) = effect.creator {
|
|
|
|
if ecs.read_storage::<Player>().get(source).is_some() {
|
|
|
|
if let Some(stats) = pools.get(target) {
|
|
|
|
xp_gain += stats.level * 100;
|
|
|
|
gold_gain += stats.gold;
|
|
|
|
}
|
|
|
|
|
|
|
|
if xp_gain != 0 || gold_gain != 0.0 {
|
|
|
|
let mut player_stats = pools.get_mut(source).unwrap();
|
2022-01-28 12:05:49 -05:00
|
|
|
let mut player_attributes = attributes.get_mut(source).unwrap();
|
2022-01-20 11:48:58 -05:00
|
|
|
player_stats.xp += xp_gain;
|
|
|
|
player_stats.gold += gold_gain;
|
|
|
|
if player_stats.xp >= player_stats.level * 1000 {
|
|
|
|
// We've gone up a level!
|
|
|
|
player_stats.level += 1;
|
2022-02-01 10:39:46 -05:00
|
|
|
gamelog::color_line(colors::MAGENTA, "Congratulations, you are now level")
|
|
|
|
.append(format!("{}", player_stats.level))
|
|
|
|
.log();
|
2022-01-28 12:05:49 -05:00
|
|
|
|
|
|
|
// Improve a random attribute
|
|
|
|
let mut rng = ecs.fetch_mut::<RandomNumberGenerator>();
|
|
|
|
match rng.roll_dice(1, 4) {
|
|
|
|
1 => {
|
|
|
|
player_attributes.might.base += 1;
|
2022-02-01 10:39:46 -05:00
|
|
|
gamelog::log_color_line(colors::GREEN, "You feel stronger!");
|
2022-01-28 12:05:49 -05:00
|
|
|
}
|
|
|
|
2 => {
|
|
|
|
player_attributes.fitness.base += 1;
|
2022-02-01 10:39:46 -05:00
|
|
|
gamelog::log_color_line(colors::GREEN, "You feel healthier!");
|
2022-01-28 12:05:49 -05:00
|
|
|
}
|
|
|
|
3 => {
|
|
|
|
player_attributes.quickness.base += 1;
|
2022-02-01 10:39:46 -05:00
|
|
|
gamelog::log_color_line(colors::GREEN, "You feel quicker!");
|
2022-01-28 12:05:49 -05:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
player_attributes.intelligence.base += 1;
|
2022-02-01 10:39:46 -05:00
|
|
|
gamelog::log_color_line(colors::GREEN, "You feel smarter!");
|
2022-01-28 12:05:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Improve all skills
|
|
|
|
let mut skills = ecs.write_storage::<Skills>();
|
|
|
|
let player_skills = skills.get_mut(*ecs.fetch::<Entity>()).unwrap();
|
|
|
|
for sk in player_skills.skills.iter_mut() {
|
|
|
|
*sk.1 += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ecs.write_storage::<EquipmentChanged>()
|
|
|
|
.insert(*ecs.fetch::<Entity>(), EquipmentChanged {})
|
|
|
|
.expect("Failed to insert EquipmentChanged tag");
|
|
|
|
|
2022-01-20 11:48:58 -05:00
|
|
|
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;
|
|
|
|
|
|
|
|
let player_pos = ecs.fetch::<Point>();
|
|
|
|
for i in 0..10 {
|
|
|
|
if player_pos.y - i > 1 {
|
|
|
|
add_effect(
|
|
|
|
None,
|
|
|
|
EffectType::Particle {
|
|
|
|
glyph: ::rltk::to_cp437('░'),
|
|
|
|
fg: colors::GOLD,
|
|
|
|
bg: colors::BLACK,
|
|
|
|
lifespan: 400.0,
|
|
|
|
},
|
|
|
|
Targets::Tile {
|
|
|
|
tile_idx: map.xy_idx(player_pos.x, player_pos.y - i) as i32,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-20 15:57:22 -05:00
|
|
|
|
|
|
|
pub fn heal_damage(ecs: &mut World, heal: &EffectSpawner, target: Entity) {
|
|
|
|
let mut pools = ecs.write_storage::<Pools>();
|
|
|
|
if let Some(pool) = pools.get_mut(target) {
|
|
|
|
if let EffectType::Healing { amount } = heal.effect_type {
|
|
|
|
pool.hit_points.current =
|
|
|
|
i32::min(pool.hit_points.max, pool.hit_points.current + amount);
|
|
|
|
add_effect(
|
|
|
|
None,
|
|
|
|
EffectType::Particle {
|
|
|
|
glyph: ::rltk::to_cp437('‼'),
|
|
|
|
fg: colors::GREEN,
|
|
|
|
bg: colors::BLACK,
|
|
|
|
lifespan: 200.0,
|
|
|
|
},
|
|
|
|
Targets::Single { target },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 11:42:02 -05:00
|
|
|
pub fn restore_mana(ecs: &mut World, mana: &EffectSpawner, target: Entity) {
|
|
|
|
let mut pools = ecs.write_storage::<Pools>();
|
|
|
|
if let Some(pool) = pools.get_mut(target) {
|
|
|
|
if let EffectType::Mana { amount } = mana.effect_type {
|
|
|
|
pool.hit_points.current = i32::min(pool.mana.max, pool.mana.current + amount);
|
|
|
|
add_effect(
|
|
|
|
None,
|
|
|
|
EffectType::Particle {
|
|
|
|
glyph: ::rltk::to_cp437('‼'),
|
|
|
|
fg: colors::BLUE,
|
|
|
|
bg: colors::BLACK,
|
|
|
|
lifespan: 200.0,
|
|
|
|
},
|
|
|
|
Targets::Single { target },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-20 15:57:22 -05:00
|
|
|
pub fn add_confusion(ecs: &mut World, effect: &EffectSpawner, target: Entity) {
|
|
|
|
if let EffectType::Confusion { turns } = &effect.effect_type {
|
2022-01-24 10:58:37 -05:00
|
|
|
ecs.create_entity()
|
|
|
|
.with(StatusEffect { target })
|
|
|
|
.with(Confusion {})
|
|
|
|
.with(Duration { turns: *turns })
|
|
|
|
.with(Name::from("Confusion"))
|
|
|
|
.marked::<SimpleMarker<SerializeMe>>()
|
|
|
|
.build();
|
2022-01-20 15:57:22 -05:00
|
|
|
}
|
|
|
|
}
|
2022-01-24 11:19:31 -05:00
|
|
|
|
|
|
|
pub fn attribute_effect(ecs: &mut World, effect: &EffectSpawner, target: Entity) {
|
|
|
|
if let EffectType::AttributeEffect {
|
|
|
|
bonus,
|
|
|
|
name,
|
|
|
|
duration,
|
|
|
|
} = &effect.effect_type
|
|
|
|
{
|
|
|
|
ecs.create_entity()
|
|
|
|
.with(StatusEffect { target })
|
|
|
|
.with(bonus.clone())
|
|
|
|
.with(Duration { turns: *duration })
|
|
|
|
.with(Name::from(name.clone()))
|
|
|
|
.marked::<SimpleMarker<SerializeMe>>()
|
|
|
|
.build();
|
|
|
|
|
|
|
|
ecs.write_storage::<EquipmentChanged>()
|
|
|
|
.insert(target, EquipmentChanged {})
|
|
|
|
.expect("Failed to insert EquipmentChanged tag");
|
|
|
|
}
|
|
|
|
}
|
2022-01-25 14:02:40 -05:00
|
|
|
|
|
|
|
pub fn slow(ecs: &mut World, effect: &EffectSpawner, target: Entity) {
|
|
|
|
if let EffectType::Slow { initiative_penalty } = &effect.effect_type {
|
|
|
|
ecs.create_entity()
|
|
|
|
.with(StatusEffect { target })
|
|
|
|
.with(Slow {
|
|
|
|
initiative_penalty: *initiative_penalty,
|
|
|
|
})
|
|
|
|
.with(Duration { turns: 5 })
|
|
|
|
.with(if *initiative_penalty > 0.0 {
|
|
|
|
Name::from("Slowed")
|
|
|
|
} else {
|
|
|
|
Name::from("Hasted")
|
|
|
|
})
|
|
|
|
.marked::<SimpleMarker<SerializeMe>>()
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn damage_over_time(ecs: &mut World, effect: &EffectSpawner, target: Entity) {
|
|
|
|
if let EffectType::DamageOverTime { damage } = &effect.effect_type {
|
|
|
|
ecs.create_entity()
|
|
|
|
.with(StatusEffect { target })
|
|
|
|
.with(DamageOverTime { damage: *damage })
|
|
|
|
.with(Duration { turns: 5 })
|
|
|
|
.with(Name::from("Damage Over Time"))
|
|
|
|
.marked::<SimpleMarker<SerializeMe>>()
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
}
|