skillz
This commit is contained in:
parent
b3423ad439
commit
8de7d1be20
58
src/cryp.rs
58
src/cryp.rs
@ -1,6 +1,8 @@
|
||||
use uuid::Uuid;
|
||||
use rand::prelude::*;
|
||||
|
||||
use skill::Skill;
|
||||
|
||||
#[derive(Debug,Clone,Copy,PartialEq)]
|
||||
pub enum StatKind {
|
||||
Dmg,
|
||||
@ -9,16 +11,9 @@ pub enum StatKind {
|
||||
Stam,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone)]
|
||||
pub struct Mod {
|
||||
pub name: String,
|
||||
pub value: u64,
|
||||
pub affects: Vec<StatKind>,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone)]
|
||||
pub struct Roll {
|
||||
pub roll: u64,
|
||||
pub base: u64,
|
||||
pub result: u64,
|
||||
pub kind: StatKind,
|
||||
}
|
||||
@ -37,30 +32,25 @@ impl Stat {
|
||||
|
||||
fn roll(&self, c: &Cryp) -> Roll {
|
||||
let mut rng = thread_rng();
|
||||
let roll: u64 = rng.gen();
|
||||
let base: u64 = rng.gen();
|
||||
|
||||
let mut roll = Roll { kind: self.kind, base, result: base };
|
||||
|
||||
println!("{:?}", self.kind);
|
||||
println!("{:064b} <- base roll", roll);
|
||||
println!("{:064b} <- base roll", base);
|
||||
|
||||
let active_mods = c.mods.iter().filter(|m|
|
||||
m.affects.iter().any(|a| *a == self.kind)
|
||||
).collect::<Vec<&Mod>>();
|
||||
// apply skills
|
||||
roll = c.skills.iter().fold(roll, |roll, s| s.apply(roll));
|
||||
|
||||
// println!("{:?}", active_mods);
|
||||
let mod_roll = active_mods.iter().fold(roll, |roll, m| {
|
||||
let roll_with_mod = roll | m.value;
|
||||
println!("{:064b} | <- {:?}", m.value, m.name);
|
||||
roll_with_mod
|
||||
});
|
||||
// finally combine with stat
|
||||
println!("{:064b} <- finalised", roll.result);
|
||||
roll.result = roll.result & self.value;
|
||||
|
||||
println!("{:064b} <- modified roll", mod_roll);
|
||||
|
||||
let result = self.value & mod_roll;
|
||||
println!("{:064b} & <- attribute roll", self.value);
|
||||
println!("{:064b} = {:?}", result, result);
|
||||
println!("{:064b} = {:?}", roll.result, roll.result);
|
||||
println!("");
|
||||
|
||||
return Roll { kind: self.kind, roll, result };
|
||||
return roll;
|
||||
}
|
||||
|
||||
fn reduce(&mut self, dmg: u64) -> &mut Stat {
|
||||
@ -85,7 +75,7 @@ pub struct Cryp {
|
||||
pub hp: Stat,
|
||||
pub xp: u64,
|
||||
pub lvl: u8,
|
||||
pub mods: Vec<Mod>,
|
||||
pub skills: Vec<Skill>,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
@ -105,7 +95,7 @@ impl Cryp {
|
||||
hp: Stat { value: 0, kind: StatKind::Hp },
|
||||
lvl: 0,
|
||||
xp: 0,
|
||||
mods: vec![],
|
||||
skills: vec![],
|
||||
name: String::new()
|
||||
};
|
||||
}
|
||||
@ -120,8 +110,8 @@ impl Cryp {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn add_mod(&mut self, m: Mod) -> &mut Cryp {
|
||||
self.mods.push(m);
|
||||
pub fn learn(mut self, s: Skill) -> Cryp {
|
||||
self.skills.push(s);
|
||||
self
|
||||
}
|
||||
|
||||
@ -188,26 +178,20 @@ impl Cryp {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use cryp::{StatKind,Mod};
|
||||
use cryp::{StatKind,Skill};
|
||||
use *;
|
||||
|
||||
#[test]
|
||||
fn create_cryp_test() {
|
||||
let newborn = Mod {
|
||||
name: "newborn".to_string(),
|
||||
value: 0,
|
||||
affects: vec![StatKind::Def],
|
||||
};
|
||||
|
||||
let mut level_two = Cryp::new()
|
||||
.named("hatchling".to_string())
|
||||
.level(2)
|
||||
.learn(Skill::Stoney)
|
||||
.create();
|
||||
|
||||
level_two.add_mod(newborn);
|
||||
// assert!(level_two.dmg <= 2_u64.pow(2));
|
||||
// assert!(level_two.def <= 2_u64.pow(2));
|
||||
assert_eq!(level_two.mods.len(), 1);
|
||||
assert_eq!(level_two.skills.len(), 1);
|
||||
println!("{:?}", level_two);
|
||||
return;
|
||||
}
|
||||
|
||||
13
src/lib.rs
13
src/lib.rs
@ -4,25 +4,20 @@ extern crate uuid;
|
||||
mod cryp;
|
||||
mod combat;
|
||||
mod battle;
|
||||
mod skill;
|
||||
|
||||
use combat::{battle, levelling};
|
||||
use battle::{Battle};
|
||||
use cryp::{Cryp, Mod, StatKind};
|
||||
use cryp::{Cryp, StatKind};
|
||||
use skill::{Skill};
|
||||
|
||||
pub fn main() {
|
||||
let mut a = Cryp::new()
|
||||
.named("pronounced \"creeep\"".to_string())
|
||||
.level(8)
|
||||
.learn(Skill::Stoney)
|
||||
.create();
|
||||
|
||||
let stoney = Mod {
|
||||
name: "stoney".to_string(),
|
||||
value: 127,
|
||||
affects: vec![StatKind::Def],
|
||||
};
|
||||
|
||||
a.add_mod(stoney);
|
||||
|
||||
let b = Cryp::new()
|
||||
.named("lemongrass tea".to_string())
|
||||
.level(8)
|
||||
|
||||
41
src/skill.rs
Executable file
41
src/skill.rs
Executable file
@ -0,0 +1,41 @@
|
||||
use cryp::{StatKind, Roll};
|
||||
|
||||
#[derive(Debug,Clone,Copy,PartialEq)]
|
||||
pub enum Skill {
|
||||
Stoney,
|
||||
Evasive,
|
||||
}
|
||||
|
||||
impl Skill {
|
||||
pub fn apply(&self, mut roll: Roll) -> Roll {
|
||||
match self {
|
||||
Skill::Stoney => stoney(self, roll),
|
||||
Skill::Evasive => evasive(self, roll),
|
||||
_ => panic!("missing skill"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn stoney(s: &Skill, mut roll: Roll) -> Roll {
|
||||
let effect = 0b11110000;
|
||||
match roll.kind {
|
||||
StatKind::Def => {
|
||||
println!("{:064b} | <- {:?}", effect, s);
|
||||
roll.result = roll.result | effect;
|
||||
roll
|
||||
},
|
||||
_ => roll,
|
||||
}
|
||||
}
|
||||
|
||||
fn evasive(s: &Skill, mut roll: Roll) -> Roll {
|
||||
match roll.kind {
|
||||
StatKind::Def => {
|
||||
if roll.result.is_power_of_two() {
|
||||
roll.result = u64::max_value()
|
||||
}
|
||||
roll
|
||||
},
|
||||
_ => roll,
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user