Update dagger and shield to have attack/defense bonuses

This commit is contained in:
Timothy Warren 2021-11-15 09:45:12 -05:00
parent 4322b250cb
commit b2ed5e7758
4 changed files with 60 additions and 7 deletions

View File

@ -165,3 +165,13 @@ pub struct Equipped {
pub owner: Entity, pub owner: Entity,
pub slot: EquipmentSlot, pub slot: EquipmentSlot,
} }
#[derive(Component, ConvertSaveload, Clone)]
pub struct MeleePowerBonus {
pub power: i32,
}
#[derive(Component, ConvertSaveload, Clone)]
pub struct DefenseBonus {
pub defense: i32,
}

View File

@ -402,6 +402,9 @@ fn main() -> rltk::BError {
SimpleMarker<SerializeMe>, SimpleMarker<SerializeMe>,
SerializationHelper, SerializationHelper,
Equippable, Equippable,
Equipped,
MeleePowerBonus,
DefenseBonus,
); );
gs.ecs.insert(SimpleMarkerAllocator::<SerializeMe>::new()); gs.ecs.insert(SimpleMarkerAllocator::<SerializeMe>::new());

View File

@ -1,4 +1,7 @@
use crate::{game_log::GameLog, CombatStats, Name, SufferDamage, WantsToMelee}; use crate::components::{
CombatStats, DefenseBonus, Equipped, MeleePowerBonus, Name, SufferDamage, WantsToMelee,
};
use crate::game_log::GameLog;
use specs::prelude::*; use specs::prelude::*;
pub struct MeleeCombatSystem {} pub struct MeleeCombatSystem {}
@ -11,19 +14,54 @@ impl<'a> System<'a> for MeleeCombatSystem {
ReadStorage<'a, Name>, ReadStorage<'a, Name>,
ReadStorage<'a, CombatStats>, ReadStorage<'a, CombatStats>,
WriteStorage<'a, SufferDamage>, WriteStorage<'a, SufferDamage>,
ReadStorage<'a, MeleePowerBonus>,
ReadStorage<'a, DefenseBonus>,
ReadStorage<'a, Equipped>,
); );
fn run(&mut self, data: Self::SystemData) { fn run(&mut self, data: Self::SystemData) {
let (entities, mut log, mut wants_melee, names, combat_stats, mut inflict_damage) = data; let (
entities,
mut log,
mut wants_melee,
names,
combat_stats,
mut inflict_damage,
melee_power_bonuses,
defense_bonuses,
equipped,
) = data;
for (_entity, wants_melee, name, stats) in for (entity, wants_melee, name, stats) in
(&entities, &wants_melee, &names, &combat_stats).join() (&entities, &wants_melee, &names, &combat_stats).join()
{ {
if stats.hp > 0 { if stats.hp > 0 {
let mut offensive_bonus = 0;
for (_item_entity, power_bonus, equipped_by) in
(&entities, &melee_power_bonuses, &equipped).join()
{
if equipped_by.owner == entity {
offensive_bonus += power_bonus.power;
}
}
let target_stats = combat_stats.get(wants_melee.target).unwrap(); let target_stats = combat_stats.get(wants_melee.target).unwrap();
if target_stats.hp > 0 { if target_stats.hp > 0 {
let target_name = names.get(wants_melee.target).unwrap(); let target_name = names.get(wants_melee.target).unwrap();
let damage = i32::max(0, stats.power - target_stats.defense);
let mut defensive_bonus = 0;
for (_item_entity, defense_bonus, equipped_by) in
(&entities, &defense_bonuses, &equipped).join()
{
if equipped_by.owner == wants_melee.target {
defensive_bonus += defense_bonus.defense;
}
}
let damage = i32::max(
0,
(stats.power + offensive_bonus) - (target_stats.defense + defensive_bonus),
);
if damage == 0 { if damage == 0 {
log.entries.push(format!( log.entries.push(format!(

View File

@ -1,7 +1,7 @@
use crate::components::{ use crate::components::{
AreaOfEffect, BlocksTile, CombatStats, Confusion, Consumable, EquipmentSlot, Equippable, AreaOfEffect, BlocksTile, CombatStats, Confusion, Consumable, DefenseBonus, EquipmentSlot,
InflictsDamage, Item, Monster, Name, Player, Position, ProvidesHealing, Ranged, Renderable, Equippable, InflictsDamage, Item, MeleePowerBonus, Monster, Name, Player, Position,
SerializeMe, Viewshed, ProvidesHealing, Ranged, Renderable, SerializeMe, Viewshed,
}; };
use crate::{random_table::RandomTable, Rect, MAP_WIDTH}; use crate::{random_table::RandomTable, Rect, MAP_WIDTH};
use rltk::{RandomNumberGenerator, RGB}; use rltk::{RandomNumberGenerator, RGB};
@ -214,6 +214,7 @@ fn dagger(ecs: &mut World, x: i32, y: i32) {
.with(Equippable { .with(Equippable {
slot: EquipmentSlot::Melee, slot: EquipmentSlot::Melee,
}) })
.with(MeleePowerBonus { power: 2 })
.marked::<SimpleMarker<SerializeMe>>() .marked::<SimpleMarker<SerializeMe>>()
.build(); .build();
} }
@ -232,6 +233,7 @@ fn shield(ecs: &mut World, x: i32, y: i32) {
.with(Equippable { .with(Equippable {
slot: EquipmentSlot::Shield, slot: EquipmentSlot::Shield,
}) })
.with(DefenseBonus { defense: 1 })
.marked::<SimpleMarker<SerializeMe>>() .marked::<SimpleMarker<SerializeMe>>()
.build(); .build();
} }