EncumbranceSystem {}
This commit is contained in:
parent
0ef3a51e56
commit
0344f87da8
14 changed files with 282 additions and 30 deletions
84
src/ai/encumbrance_system.rs
Normal file
84
src/ai/encumbrance_system.rs
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
use crate::{gamelog, Attributes, Burden, EquipmentChanged, Equipped, InBackpack, Item, Pools};
|
||||
use rltk::prelude::*;
|
||||
use specs::prelude::*;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub struct EncumbranceSystem {}
|
||||
|
||||
impl<'a> System<'a> for EncumbranceSystem {
|
||||
#[allow(clippy::type_complexity)]
|
||||
type SystemData = (
|
||||
WriteStorage<'a, EquipmentChanged>,
|
||||
Entities<'a>,
|
||||
ReadStorage<'a, Item>,
|
||||
ReadStorage<'a, InBackpack>,
|
||||
ReadStorage<'a, Equipped>,
|
||||
WriteStorage<'a, Pools>,
|
||||
ReadStorage<'a, Attributes>,
|
||||
ReadExpect<'a, Entity>,
|
||||
WriteStorage<'a, Burden>,
|
||||
);
|
||||
|
||||
fn run(&mut self, data: Self::SystemData) {
|
||||
let (mut equip_dirty, entities, items, backpacks, wielded, mut pools, attributes, player, mut burdened) = data;
|
||||
if equip_dirty.is_empty() {
|
||||
return;
|
||||
}
|
||||
// Build update map
|
||||
let mut to_update: HashMap<Entity, f32> = HashMap::new();
|
||||
for (entity, _dirty) in (&entities, &equip_dirty).join() {
|
||||
to_update.insert(entity, 0.0);
|
||||
}
|
||||
equip_dirty.clear();
|
||||
// Total up equipped items
|
||||
for (item, equipped) in (&items, &wielded).join() {
|
||||
if to_update.contains_key(&equipped.owner) {
|
||||
let totals = to_update.get_mut(&equipped.owner).unwrap();
|
||||
*totals += item.weight;
|
||||
}
|
||||
}
|
||||
// Total carried items
|
||||
for (item, carried) in (&items, &backpacks).join() {
|
||||
if to_update.contains_key(&carried.owner) {
|
||||
let totals = to_update.get_mut(&carried.owner).unwrap();
|
||||
*totals += item.weight;
|
||||
}
|
||||
}
|
||||
// Apply to pools
|
||||
for (entity, weight) in to_update.iter() {
|
||||
if let Some(pool) = pools.get_mut(*entity) {
|
||||
pool.weight = *weight;
|
||||
if let Some(attr) = attributes.get(*entity) {
|
||||
let carry_capacity_lbs = (attr.strength.base + attr.strength.modifiers) * 10;
|
||||
if pool.weight as i32 > 3 * carry_capacity_lbs {
|
||||
// Overloaded
|
||||
burdened
|
||||
.insert(*entity, Burden { level: crate::BurdenLevel::Overloaded })
|
||||
.expect("Failed to insert Burden");
|
||||
if *entity == *player {
|
||||
gamelog::Logger::new().append("You're overloaded!").log();
|
||||
}
|
||||
} else if pool.weight as i32 > 2 * carry_capacity_lbs {
|
||||
// Strained
|
||||
burdened
|
||||
.insert(*entity, Burden { level: crate::BurdenLevel::Strained })
|
||||
.expect("Failed to insert Burden");
|
||||
if *entity == *player {
|
||||
gamelog::Logger::new().append("You're strained.").log();
|
||||
}
|
||||
} else if pool.weight as i32 > carry_capacity_lbs {
|
||||
// Burdened
|
||||
burdened
|
||||
.insert(*entity, Burden { level: crate::BurdenLevel::Burdened })
|
||||
.expect("Failed to insert Burden");
|
||||
if *entity == *player {
|
||||
gamelog::Logger::new().append("You're burdened.").log();
|
||||
}
|
||||
} else {
|
||||
burdened.remove(*entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
use crate::{Clock, Energy, Name, Position, RunState, TakingTurn, LOG_TICKS};
|
||||
use crate::{Burden, BurdenLevel, Clock, Energy, Name, Position, RunState, TakingTurn, LOG_TICKS};
|
||||
use rltk::prelude::*;
|
||||
use specs::prelude::*;
|
||||
|
||||
|
|
@ -12,6 +12,7 @@ impl<'a> System<'a> for EnergySystem {
|
|||
type SystemData = (
|
||||
ReadStorage<'a, Clock>,
|
||||
WriteStorage<'a, Energy>,
|
||||
ReadStorage<'a, Burden>,
|
||||
ReadStorage<'a, Position>,
|
||||
WriteStorage<'a, TakingTurn>,
|
||||
Entities<'a>,
|
||||
|
|
@ -22,7 +23,7 @@ impl<'a> System<'a> for EnergySystem {
|
|||
);
|
||||
|
||||
fn run(&mut self, data: Self::SystemData) {
|
||||
let (clock, mut energies, positions, mut turns, entities, mut rng, mut runstate, player, names) = data;
|
||||
let (clock, mut energies, burdens, positions, mut turns, entities, mut rng, mut runstate, player, names) = data;
|
||||
// If not ticking, do nothing.
|
||||
if *runstate != RunState::Ticking {
|
||||
return;
|
||||
|
|
@ -44,8 +45,17 @@ impl<'a> System<'a> for EnergySystem {
|
|||
}
|
||||
// EVERYTHING ELSE
|
||||
for (entity, energy, _pos) in (&entities, &mut energies, &positions).join() {
|
||||
let burden_modifier = if let Some(burden) = burdens.get(entity) {
|
||||
match burden.level {
|
||||
BurdenLevel::Burdened => 0.75,
|
||||
BurdenLevel::Strained => 0.5,
|
||||
BurdenLevel::Overloaded => 0.25,
|
||||
}
|
||||
} else {
|
||||
1.0
|
||||
};
|
||||
// Every entity has a POTENTIAL equal to their speed.
|
||||
let mut energy_potential: i32 = energy.speed;
|
||||
let mut energy_potential: i32 = (energy.speed as f32 * burden_modifier) as i32;
|
||||
// Increment current energy by NORMAL_SPEED for every
|
||||
// whole number of NORMAL_SPEEDS in their POTENTIAL.
|
||||
while energy_potential >= NORMAL_SPEED {
|
||||
|
|
|
|||
|
|
@ -6,3 +6,5 @@ mod quip_system;
|
|||
pub use quip_system::QuipSystem;
|
||||
mod regen_system;
|
||||
pub use regen_system::RegenSystem;
|
||||
mod encumbrance_system;
|
||||
pub use encumbrance_system::EncumbranceSystem;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue