Skip to content

Commit

Permalink
Make sure the beast do not go to negative number
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandoDrRobot committed Aug 21, 2024
1 parent 809ac4e commit 2177d04
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
33 changes: 33 additions & 0 deletions src/models.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,37 @@ mod tests {
assert_eq!(player.player_name, 'Hero', "Player name should be 'Hero'");
assert_eq!(player.potions, 5, "Player should have 5 potions");
}

#[test]
#[available_gas(200000)]
fn test_beast_defeat() {
let mut beast = Beast {
beast_id: 1,
beast_name: 'Dragon',
beast_type: WorldElements::Draconic,
beast_description: 'A mighty dragon',
player_id: 1,
hp: 100,
current_hp: 10, // Bestia cerca de ser derrotada
attack: 50,
defense: 40,
mt1: 1,
mt2: 2,
mt3: 3,
mt4: 4,
level: 5,
experience_to_next_level: 1000,
};

// Simulate an attack that would defeat the beast
let damage = 20;
if damage >= beast.current_hp {
beast.current_hp = 0;
} else {
beast.current_hp -= damage;
}

assert_eq!(beast.current_hp, 0, "Beast should have 0 HP after being defeated");
// assert!(!beast.exist(), "Beast should be defeated");
}
}
7 changes: 6 additions & 1 deletion src/systems/battle.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,12 @@ mod battle_system {
let mt = get!(world, mt_id, (Mt));

let damage = self.calculate_damage(mt, player_beast, opponent_beast);
opponent_beast.current_hp -= damage;

if damage >= opponent_beast.current_hp {
opponent_beast.current_hp = 0;
} else {
opponent_beast.current_hp -= damage;
}

if opponent_beast.current_hp <= 0_u32 {
let message = 'Opponent\'s Beast Knocked Out!';
Expand Down

0 comments on commit 2177d04

Please sign in to comment.