mnml/src/cryp.rs
2018-08-11 21:23:14 +10:00

78 lines
1.4 KiB
Rust

use uuid::Uuid;
use rand::prelude::*;
#[derive(Debug,Clone)]
pub struct Cryp {
pub id: Uuid,
// todo
// make attributes hold this value
pub dmg: u64,
pub def: u64,
pub stam: u64,
pub xp: u64,
pub lvl: u8,
pub name: String,
}
fn check_lvl(lvl: u8) -> u8 {
if lvl > 64 { return 64; }
if lvl == 0 { return 0; }
return lvl;
}
impl Cryp {
pub fn new() -> Cryp {
let id = Uuid::new_v4();
return Cryp {
id,
dmg: 0,
def: 0,
stam: 0,
lvl: 0,
xp: 0,
name: String::new()
};
}
pub fn named(mut self, name: String) -> Cryp {
self.name = name.clone();
self
}
pub fn level(mut self, lvl: u8) -> Cryp {
self.lvl = check_lvl(lvl);
self
}
pub fn add_xp(mut self) -> Cryp {
self.xp = self.xp.saturating_add(1);
if self.xp.is_power_of_two() {
return self.level_up();
}
self
}
pub fn level_up(mut self) -> Cryp {
self.lvl = self.lvl.saturating_add(1);
self.finish()
}
pub fn finish(mut self) -> Cryp {
let mut rng = thread_rng();
let lvl_as_two_pwr = 2_u64.pow(self.lvl.into());
let max = match self.lvl == 64 {
true => u64::max_value(),
false => lvl_as_two_pwr,
};
self.xp = lvl_as_two_pwr;
self.dmg = rng.gen_range(1, max);
self.def = rng.gen_range(1, max);
self.stam = rng.gen_range(1, max);
self
}
}