skill cooldowns working

This commit is contained in:
ntr 2018-10-26 19:59:14 +11:00
parent 12d30fbc82
commit f9966e1c2b
3 changed files with 116 additions and 62 deletions

View File

@ -15,8 +15,8 @@ type Cooldown = Option<u8>;
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)] #[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
pub struct CrypSkill { pub struct CrypSkill {
skill: Skill, pub skill: Skill,
cd: Cooldown, pub cd: Cooldown,
} }
impl CrypSkill { impl CrypSkill {
@ -200,6 +200,32 @@ impl Cryp {
self.skills.iter().any(|s| s.skill == skill && s.can_cast(self)) self.skills.iter().any(|s| s.skill == skill && s.can_cast(self))
} }
pub fn skill_set_cd(&mut self, skill: Skill) -> &mut Cryp {
let i = self.skills.iter().position(|s| s.skill == skill).unwrap();
self.skills.remove(i);
self.skills.push(CrypSkill::new(skill));
self
}
pub fn decrease_cooldowns(&mut self) -> &mut Cryp {
for skill in self.skills.iter_mut() {
if let Some(cd) = skill.cd {
// if the cd is 1 it becomes none
if cd == 1 {
println!("{:?} is now off cd", skill.skill);
skill.cd = None;
continue;
}
// otherwise decrement it
skill.cd = Some(cd.saturating_sub(1));
}
}
self
}
pub fn rez(&mut self) -> &mut Cryp { pub fn rez(&mut self) -> &mut Cryp {
self.hp.set(self.stam.value); self.hp.set(self.stam.value);
self self

View File

@ -60,6 +60,28 @@ impl Team {
None => panic!("abiltity not in game"), None => panic!("abiltity not in game"),
} }
} }
pub fn progress_cooldowns(&mut self) -> &mut Team {
// get a copy of the used skills
for skill in self.skills.clone().iter() {
// copy the creep cause we will replace it
let mut cryp = self.cryp_by_id(skill.cryp_id).unwrap().clone();
if skill.used_cooldown() {
cryp.skill_set_cd(skill.skill);
self.update_cryp(cryp);
continue;
}
cryp.decrease_cooldowns();
println!("{:?}", cryp);
self.update_cryp(cryp);
}
self
}
} }
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)] #[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
@ -333,12 +355,20 @@ impl Game {
let mut cryp = self.cryp_by_id(incoming.cryp_id).clone(); let mut cryp = self.cryp_by_id(incoming.cryp_id).clone();
let mut target_cryp = self.cryp_by_id(incoming.target_cryp_id.unwrap()).clone(); let mut target_cryp = self.cryp_by_id(incoming.target_cryp_id.unwrap()).clone();
let resolution = incoming.resolve(&mut cryp, &mut target_cryp); let resolution = incoming.resolve(&mut cryp, &mut target_cryp);
// push the resolved spell on
team.resolved.push(*resolution); team.resolved.push(*resolution);
team.update_cryp(target_cryp); team.update_cryp(target_cryp);
self.update_team(team); self.update_team(team);
} }
} }
// now damage has all been assigned
// handle cooldowns
for team in self.teams.iter_mut() {
team.progress_cooldowns();
}
self self
} }
@ -648,8 +678,6 @@ mod tests {
.learn(Skill::Block) .learn(Skill::Block)
.create(); .create();
let _x_id = x.id;
let y = Cryp::new() let y = Cryp::new()
.named(&"lemongrass tea".to_string()) .named(&"lemongrass tea".to_string())
.level(8) .level(8)
@ -658,8 +686,6 @@ mod tests {
.learn(Skill::Block) .learn(Skill::Block)
.create(); .create();
let _y_id = y.id;
let mut game = Game::new(); let mut game = Game::new();
game game
@ -688,69 +714,62 @@ mod tests {
return game; return game;
} }
#[test] // #[test]
fn game_test() { // fn phase_test() {
let x = Cryp::new() // let mut game = create_test_game();
.named(&"pronounced \"creeep\"".to_string())
.level(8)
.create();
let x_id = x.id; // let x_team = game.teams[0].clone();
// let y_team = game.teams[1].clone();
let y = Cryp::new() // let x_cryp = x_team.cryps[0].clone();
.named(&"lemongrass tea".to_string()) // let y_cryp = y_team.cryps[0].clone();
.level(8)
.create();
let y_id = y.id; // let x_attack_id = game.add_skill(x_team.id, x_cryp.id, y_team.id, Skill::Attack).unwrap();
// let y_attack_id = game.add_skill(y_team.id, y_cryp.id, x_team.id, Skill::Attack).unwrap();
let mut game = Game::new(); // assert!(game.skill_phase_finished());
game // game.target_phase_start();
.set_team_num(2)
.set_team_size(1)
.set_pve(false);
let x_team_id = Uuid::new_v4(); // game.add_target(x_team.id, x_cryp.id, y_attack_id).unwrap();
let mut x_team = Team::new(x_team_id); // game.add_target(y_team.id, y_cryp.id, x_attack_id).unwrap();
x_team
.set_cryps(vec![x]);
let y_team_id = Uuid::new_v4(); // assert!(game.target_phase_finished());
let mut y_team = Team::new(y_team_id);
y_team
.set_cryps(vec![y]);
game // game.damage_phase_start();
.add_team(x_team).unwrap()
.add_team(y_team).unwrap();
assert!(game.can_start()); // assert!([Phase::Skill, Phase::Finish].contains(&game.phase));
game.start(); // return;
// }
let x_attack_id = game.add_skill(x_team_id, x_id, y_team_id, Skill::Attack); // #[test]
let y_attack_id = game.add_skill(y_team_id, y_id, x_team_id, Skill::Attack); // fn stun_test() {
// let mut game = create_test_game();
assert!(game.skill_phase_finished()); // let x_team = game.teams[0].clone();
// let y_team = game.teams[1].clone();
game.target_phase_start(); // let x_cryp = x_team.cryps[0].clone();
// let y_cryp = y_team.cryps[0].clone();
println!("{:?}", game); // let x_stun_id = game.add_skill(x_team.id, x_cryp.id, y_team.id, Skill::TestStun).unwrap();
// let y_attack_id = game.add_skill(y_team.id, y_cryp.id, x_team.id, Skill::TestTouch).unwrap();
game.add_target(x_team_id, x_id, y_attack_id.unwrap()).unwrap(); // assert!(game.skill_phase_finished());
game.add_target(y_team_id, y_id, x_attack_id.unwrap()).unwrap(); // game.target_phase_start();
assert!(game.target_phase_finished()); // game.add_target(x_team.id, x_cryp.id, y_attack_id).unwrap();
// game.add_target(y_team.id, y_cryp.id, x_stun_id).unwrap();
game.damage_phase_start(); // assert!(game.target_phase_finished());
// game.damage_phase_start();
assert!([Phase::Skill, Phase::Finish].contains(&game.phase)); // // should auto progress back to skill phase
// assert!(game.phase == Phase::Skill);
println!("{:?}", game); // assert!(game.team_by_id(y_team.id).cryps[0].is_stunned());
// assert!(game.team_by_id(y_team.id).skills_required() == 0);
return; // }
}
#[test] #[test]
fn cooldown_test() { fn cooldown_test() {
@ -762,28 +781,32 @@ mod tests {
let x_cryp = x_team.cryps[0].clone(); let x_cryp = x_team.cryps[0].clone();
let y_cryp = y_team.cryps[0].clone(); let y_cryp = y_team.cryps[0].clone();
let x_stun_id = game.add_skill(x_team.id, x_cryp.id, y_team.id, Skill::TestStun).unwrap(); let x_stun_id = game.add_skill(x_team.id, x_cryp.id, y_team.id, Skill::TestTouch).unwrap();
let y_attack_id = game.add_skill(y_team.id, y_cryp.id, x_team.id, Skill::TestTouch).unwrap(); let y_attack_id = game.add_skill(y_team.id, y_cryp.id, x_team.id, Skill::TestTouch).unwrap();
assert!(game.skill_phase_finished());
game.target_phase_start(); game.target_phase_start();
game.add_target(x_team.id, x_cryp.id, y_attack_id).unwrap(); game.add_target(x_team.id, x_cryp.id, y_attack_id).unwrap();
game.add_target(y_team.id, y_cryp.id, x_stun_id).unwrap(); game.add_target(y_team.id, y_cryp.id, x_stun_id).unwrap();
assert!(game.target_phase_finished());
game.damage_phase_start(); game.damage_phase_start();
// should auto progress back to skill phase // should auto progress back to skill phase
assert!(game.phase == Phase::Skill); assert!(game.phase == Phase::Skill);
assert!(game.team_by_id(y_team.id).cryps[0].is_stunned());
assert!(game.team_by_id(y_team.id).skills_required() == 0);
// should be on cooldown now
assert!(game.add_skill(x_team.id, x_cryp.id, y_team.id, Skill::Stun).is_err());
println!("{:#?}", game);
// after 1 turn block should be off cooldown // after 1 turn block should be off cooldown
assert!(game.team_by_id(y_team.id).cryps[0].skill_on_cd(Skill::Block).is_none()); assert!(game.team_by_id(y_team.id).cryps[0].skill_on_cd(Skill::Block).is_none());
// second round
// now we block and it should go back on cd
let x_block_id = game.add_skill(x_team.id, x_cryp.id, y_team.id, Skill::Block).unwrap();
let y_block_id = game.add_skill(y_team.id, y_cryp.id, x_team.id, Skill::Block).unwrap();
game.target_phase_start();
game.add_target(x_team.id, x_cryp.id, y_block_id).unwrap();
game.add_target(y_team.id, y_cryp.id, x_block_id).unwrap();
game.damage_phase_start();
assert!(game.team_by_id(y_team.id).cryps[0].skill_on_cd(Skill::Block).is_some());
println!("{:#?}", game);
} }
} }

View File

@ -1,7 +1,7 @@
use rand::prelude::*; use rand::prelude::*;
use uuid::Uuid; use uuid::Uuid;
use cryp::{Cryp}; use cryp::{Cryp, CrypSkill};
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)] #[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
pub struct Roll { pub struct Roll {
@ -72,6 +72,11 @@ impl Cast {
self.target_cryp_id = Some(cryp_id); self.target_cryp_id = Some(cryp_id);
self self
} }
pub fn used_cooldown(self) -> bool {
let cs = CrypSkill::new(self.skill);
return cs.cd.is_some();
}
} }
// #[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)] // #[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]