Skip to content

Commit

Permalink
Fix experience being not calculated correctly (#142)
Browse files Browse the repository at this point in the history
Fix focus ability damage (#140)

Add some tests (#84)
  • Loading branch information
hjaremko committed Jun 8, 2020
1 parent 9ab8e14 commit cdb696d
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public IResult attack() {
player.getFightManager()
.getEnemy()
.getAttributes()
.addHealth(damageDone - additionalDamage);
.addHealth(-damageDone - additionalDamage);
var mobHealthLeft = player.getFightManager().getEnemy().getAttributes().getHealth();

if (mobHealthLeft <= 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,10 @@ public IResult endBattle() {
var playerAttrs = (PlayerAttributes) player.getAttributes();
var playerLevel = playerAttrs.getLevel();
var enemyLevel = fightingWith.getAttributes().getLevel();
var expBonus = playerLevel < enemyLevel ? Math.abs(playerLevel - enemyLevel) / 2 : 0;
var gainedExperience =
(int)
Math.round(
((playerAttrs.getRequiredExperience() / 6.0)
* (playerLevel < enemyLevel
? 0.5
: 1 + Math.abs(playerLevel - enemyLevel) * 0.5)));
playerAttrs.getRequiredExperience() / 6 / (playerLevel < enemyLevel ? 2 : 1)
+ expBonus;

((PlayerAttributes) player.getAttributes()).addExperience(gainedExperience);
player.getFightManager().setState(new ExplorationState(player));
Expand Down
52 changes: 52 additions & 0 deletions src/test/java/pl/uj/io/cuteanimals/action/ability/FocusTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package pl.uj.io.cuteanimals.action.ability;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import pl.uj.io.cuteanimals.model.Monster;
import pl.uj.io.cuteanimals.model.NPCAttributes;
import pl.uj.io.cuteanimals.model.Player;
import pl.uj.io.cuteanimals.model.PlayerAttributes;

class FocusTest {
private Player player;
private Monster dummy;
private PlayerAttributes attrs;

@BeforeEach
void setUp() {
player = new Player();
attrs = (PlayerAttributes) player.getAttributes();
player.getAttributes().addLevel(4);

dummy = new Monster("dummy", new NPCAttributes(0, 1, 1, 0));
player.getFightManager().beginFight(dummy);
var focus = new Focus();
focus.execute(player);
}

@Test
void shouldDrain20Mana() {
var manaBefore = attrs.getMana();
player.getFightManager().attack();
assertThat(attrs.getMana() == manaBefore - 20);
}

@Test
void atLeast20ManaNeeded() {
attrs.addMana(-200);
var result = (new Focus()).execute(player);
assertThat(
result.getMessage()
.equals("Not enough mana! You need at least 20 mana to use this ability."));
}

@Test
void attackShouldInflictAdditionalDamage() {
player.getFightManager().attack();
var playerAttack = player.getAttributes().getAttack();
var playerDamage = attrs.getDamage();
assertThat(dummy.getAttributes().getHealth() == -(playerDamage + 2 * playerAttack));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package pl.uj.io.cuteanimals.model.fight;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import pl.uj.io.cuteanimals.model.Monster;
import pl.uj.io.cuteanimals.model.NPCAttributes;
import pl.uj.io.cuteanimals.model.Player;
import pl.uj.io.cuteanimals.model.PlayerAttributes;

class FightManagerTest {
private Player player;

@BeforeEach
void setUp() {
player = new Player();
}

@Test
void endBattleEqualLevelExperienceTest() {
var dummy = new Monster("dummy", new NPCAttributes(1, 1, 1, 1));

player.getFightManager().beginFight(dummy);
player.getFightManager().endBattle();
assertThat(((PlayerAttributes) player.getAttributes()).getExperience() == 1);
}

@Test
void endBattleMonsterHigherLevelExperienceTest() {
var dummy = new Monster("dummy", new NPCAttributes(1, 1, 11, 1));

player.getFightManager().beginFight(dummy);
player.getFightManager().endBattle();
assertThat(((PlayerAttributes) player.getAttributes()).getExperience() == 1 + 5);
}

@Test
void endBattleMonsterLowerLevelExperienceTest() {
player.getAttributes().addLevel(1);
var dummy = new Monster("dummy", new NPCAttributes(1, 1, 11, 1));

player.getFightManager().beginFight(dummy);
player.getFightManager().endBattle();
assertThat(((PlayerAttributes) player.getAttributes()).getExperience() == 1);
}
}

0 comments on commit cdb696d

Please sign in to comment.