PkmnLib_rs/src/static_data/growth_rates.rs

42 lines
1.3 KiB
Rust
Raw Normal View History

use crate::defines::LevelInt;
2022-07-01 15:07:22 +00:00
/// A growth rate defines how much experience is required per level.
pub trait GrowthRate {
2022-07-01 15:07:22 +00:00
/// Calculate the level something with this growth rate would have at a certain experience.
fn calculate_level(&self, experience: u32) -> LevelInt;
2022-07-01 15:07:22 +00:00
/// Calculate the experience something with this growth rate would have at a certain level.
fn calculate_experience(&self, level: LevelInt) -> u32;
}
2022-07-01 15:07:22 +00:00
/// An implementation of the growth rate that uses a lookup table for experience.
pub struct LookupGrowthRate {
2022-07-01 15:07:22 +00:00
/// The lookup Vec.
experience: Vec<u32>,
}
impl LookupGrowthRate {
2022-07-01 15:07:22 +00:00
/// 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 {
2022-06-06 11:54:59 +00:00
if level >= self.experience.len() as LevelInt {
*self.experience.last().unwrap()
} else {
self.experience[(level - 1) as usize]
}
}
}