attribute exercising

This commit is contained in:
Llywelwyn 2023-09-27 22:12:53 +01:00
parent 4d614daad5
commit c757466df1
3 changed files with 33 additions and 2 deletions

View file

@ -354,6 +354,17 @@ impl Attributes {
charisma: Attribute::new(cha), charisma: Attribute::new(cha),
} }
} }
pub fn attr_from_index(&self, attr: i32) -> &Attribute {
match attr {
Self::STR => &self.strength,
Self::DEX => &self.dexterity,
Self::CON => &self.constitution,
Self::INT => &self.intelligence,
Self::WIS => &self.wisdom,
Self::CHA => &self.charisma,
_ => unreachable!("Tried to get an attribute that doesn't exist."),
}
}
pub fn exercise(&mut self, attr: i32, improve: bool) { pub fn exercise(&mut self, attr: i32, improve: bool) {
match attr { match attr {
Self::STR => { Self::STR => {

View file

@ -1,7 +1,11 @@
use specs::prelude::*; use specs::prelude::*;
use bracket_lib::prelude::*;
use super::{ EffectSpawner, EffectType }; use super::{ EffectSpawner, EffectType };
use crate::components::Attributes; use crate::components::Attributes;
const ATTRIBUTE_SOFTCAP: i32 = 20;
const ABUSE_CHANCE: i32 = 2; // 1 in this chance of abuse. 2 = 50%, 3 = 33%, etc.
pub(crate) fn exercise(ecs: &mut World, effect: &EffectSpawner, target: Entity) { pub(crate) fn exercise(ecs: &mut World, effect: &EffectSpawner, target: Entity) {
// Unwrap vars from the effect // Unwrap vars from the effect
let (attr, inc) = if let EffectType::Exercise { attribute, increment } = effect.effect_type { let (attr, inc) = if let EffectType::Exercise { attribute, increment } = effect.effect_type {
@ -13,6 +17,16 @@ pub(crate) fn exercise(ecs: &mut World, effect: &EffectSpawner, target: Entity)
// Get target attributes // Get target attributes
let mut attributes = ecs.write_storage::<Attributes>(); let mut attributes = ecs.write_storage::<Attributes>();
if let Some(has_attr) = attributes.get_mut(target) { if let Some(has_attr) = attributes.get_mut(target) {
has_attr.exercise(attr, inc); // Roll a d20. If we're trying to exercise a stat, we need to roll higher
// than the stat's current value. If we're abusing a stat, flip a coin.
let mut rng = ecs.write_resource::<RandomNumberGenerator>();
let success = if inc {
rng.roll_dice(1, ATTRIBUTE_SOFTCAP) > has_attr.attr_from_index(attr).current()
} else {
rng.roll_dice(1, ABUSE_CHANCE) == 1
};
if success {
has_attr.exercise(attr, inc);
}
} }
} }

View file

@ -28,7 +28,13 @@ macro_rules! serialize_individually {
} }
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
pub fn save_game(_ecs: &mut World) {} pub fn save_game(_ecs: &mut World) {
console::log(
"Unfortunately, saving isn't supported in any easy way on the web. Sorry!
You can, at least, save your morgue file after dying - it'll be written
to the log, and just needs saving into a text file."
)
}
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
pub fn save_game(ecs: &mut World) { pub fn save_game(ecs: &mut World) {