More documentation.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-07-01 17:07:22 +02:00
parent 03f5e3bb5a
commit 8f6ecdd4ad
35 changed files with 721 additions and 262 deletions

View File

@@ -1,16 +1,24 @@
use crate::static_data::Statistic;
use crate::StringKey;
use hashbrown::HashMap;
use crate::static_data::Statistic;
use crate::StringKey;
/// A nature is an attribute on a Pokemon that modifies the effective base stats on a Pokemon. They
/// can have an increased statistic and a decreased statistic, or be neutral.
#[derive(Debug)]
pub struct Nature {
/// The stat that should receive the increased modifier.
increase_stat: Statistic,
/// The stat that should receive the decreased modifier.
decrease_stat: Statistic,
/// The amount by which the increased stat is multiplied.
increase_modifier: f32,
/// The amount by which the decreased stat is multiplied.
decrease_modifier: f32,
}
impl Nature {
/// Instantiates a new statistic.
pub fn new(
increase_stat: Statistic,
decrease_stat: Statistic,
@@ -25,14 +33,18 @@ impl Nature {
}
}
/// The stat that should receive the increased modifier.
pub fn increased_stat(&self) -> Statistic {
self.increase_stat
}
/// The stat that should receive the decreased modifier.
pub fn decreased_stat(&self) -> Statistic {
self.decrease_stat
}
/// Calculates the modifier for a given stat. If it's the increased stat, returns the increased
/// modifier, if it's the decreased stat, returns the decreased modifier. Otherwise returns 1.0
pub fn get_stat_modifier(&self, stat: Statistic) -> f32 {
if stat == self.increase_stat {
self.increase_modifier
@@ -44,26 +56,32 @@ impl Nature {
}
}
/// A library of all natures that can be used, stored by their names.
#[derive(Debug)]
pub struct NatureLibrary {
/// The underlying data structure.
map: HashMap<StringKey, Nature>,
}
impl NatureLibrary {
/// Creates a new nature library with a given capacity.
pub fn new(capacity: usize) -> Self {
NatureLibrary {
map: HashMap::with_capacity(capacity),
}
}
/// Adds a new nature with name to the library.
pub fn load_nature(&mut self, name: StringKey, nature: Nature) {
self.map.insert(name, nature);
}
/// Gets a nature by name.
pub fn get_nature(&self, key: &StringKey) -> Option<&Nature> {
self.map.get(key)
}
/// Finds a nature name by nature.
pub fn get_nature_name(&self, nature: &Nature) -> StringKey {
for kv in &self.map {
// As natures can't be copied, and should always be the same reference as the value