PkmnLib_rs/src/static_data/growth_rates.rs

42 lines
1.3 KiB
Rust
Executable File

use crate::defines::LevelInt;
/// A growth rate defines how much experience is required per level.
pub trait GrowthRate {
/// Calculate the level something with this growth rate would have at a certain experience.
fn calculate_level(&self, experience: u32) -> LevelInt;
/// Calculate the experience something with this growth rate would have at a certain level.
fn calculate_experience(&self, level: LevelInt) -> u32;
}
/// An implementation of the growth rate that uses a lookup table for experience.
pub struct LookupGrowthRate {
/// The lookup Vec.
experience: Vec<u32>,
}
impl LookupGrowthRate {
/// Instantiates a new lookup growth rate.
pub fn new(experience: Vec<u32>) -> LookupGrowthRate {
LookupGrowthRate { experience }
}
}
impl GrowthRate for LookupGrowthRate {
fn calculate_level(&self, experience: u32) -> LevelInt {
for (i, v) in self.experience.iter().enumerate() {
if *v > experience {
return i as LevelInt;
}
}
self.experience.len() as LevelInt
}
fn calculate_experience(&self, level: LevelInt) -> u32 {
if level >= self.experience.len() as LevelInt {
*self.experience.last().unwrap()
} else {
self.experience[(level - 1) as usize]
}
}
}