diff --git a/src/components.rs b/src/components.rs index 5477d06..126a77e 100644 --- a/src/components.rs +++ b/src/components.rs @@ -354,6 +354,17 @@ impl Attributes { 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) { match attr { Self::STR => { diff --git a/src/effects/attr.rs b/src/effects/attr.rs index a76baea..65b4b2e 100644 --- a/src/effects/attr.rs +++ b/src/effects/attr.rs @@ -1,7 +1,11 @@ use specs::prelude::*; +use bracket_lib::prelude::*; use super::{ EffectSpawner, EffectType }; 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) { // Unwrap vars from the effect 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 let mut attributes = ecs.write_storage::(); 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::(); + 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); + } } } diff --git a/src/saveload_system.rs b/src/saveload_system.rs index 894e4ff..dce6f76 100644 --- a/src/saveload_system.rs +++ b/src/saveload_system.rs @@ -28,7 +28,13 @@ macro_rules! serialize_individually { } #[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"))] pub fn save_game(ecs: &mut World) {