From b189589c1eac8d5f6327bd05bd73d9cce61c257d Mon Sep 17 00:00:00 2001 From: Hubert Jaremko Date: Tue, 9 Jun 2020 01:30:19 +0200 Subject: [PATCH] Fix experience being not calculated correctly (#142) Fix focus ability damage (#140) Add some tests (#84) --- .../io/cuteanimals/action/ability/Focus.java | 2 +- .../cuteanimals/model/fight/FightManager.java | 11 ++-- .../cuteanimals/action/ability/FocusTest.java | 53 +++++++++++++++++++ .../model/fight/FightManagerTest.java | 49 +++++++++++++++++ 4 files changed, 107 insertions(+), 8 deletions(-) create mode 100644 src/test/java/pl/uj/io/cuteanimals/action/ability/FocusTest.java create mode 100644 src/test/java/pl/uj/io/cuteanimals/model/fight/FightManagerTest.java diff --git a/src/main/java/pl/uj/io/cuteanimals/action/ability/Focus.java b/src/main/java/pl/uj/io/cuteanimals/action/ability/Focus.java index 6263460..a900e11 100644 --- a/src/main/java/pl/uj/io/cuteanimals/action/ability/Focus.java +++ b/src/main/java/pl/uj/io/cuteanimals/action/ability/Focus.java @@ -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) { diff --git a/src/main/java/pl/uj/io/cuteanimals/model/fight/FightManager.java b/src/main/java/pl/uj/io/cuteanimals/model/fight/FightManager.java index bfb5dd5..741a0ce 100644 --- a/src/main/java/pl/uj/io/cuteanimals/model/fight/FightManager.java +++ b/src/main/java/pl/uj/io/cuteanimals/model/fight/FightManager.java @@ -55,13 +55,10 @@ public IResult endBattle() { var playerAttrs = (PlayerAttributes) player.getAttributes(); var playerLevel = playerAttrs.getLevel(); var enemyLevel = fightingWith.getAttributes().getLevel(); - var gainedExperience = - (int) - Math.round( - ((playerAttrs.getRequiredExperience() / 6.0) - * (playerLevel < enemyLevel - ? 0.5 - : 1 + Math.abs(playerLevel - enemyLevel) * 0.5))); + var expBonus = playerLevel < enemyLevel ? Math.abs(playerLevel - enemyLevel) / 2 : 0; + var baseExp = playerAttrs.getRequiredExperience() / 6; + var levelPenalty = playerLevel < enemyLevel ? 2 : 1; + var gainedExperience = baseExp / levelPenalty + expBonus; ((PlayerAttributes) player.getAttributes()).addExperience(gainedExperience); player.getFightManager().setState(new ExplorationState(player)); diff --git a/src/test/java/pl/uj/io/cuteanimals/action/ability/FocusTest.java b/src/test/java/pl/uj/io/cuteanimals/action/ability/FocusTest.java new file mode 100644 index 0000000..483bd32 --- /dev/null +++ b/src/test/java/pl/uj/io/cuteanimals/action/ability/FocusTest.java @@ -0,0 +1,53 @@ +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()).isEqualTo(manaBefore - 20); + } + + @Test + void atLeast20ManaNeeded() { + attrs.addMana(-200); + var result = (new Focus()).execute(player); + assertThat(result.getMessage()) + .isEqualTo("* 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(); + var expected = -(playerDamage + 2 * playerAttack); + // getDamage is randomized + assertThat(dummy.getAttributes().getHealth()).isBetween(expected - 1, expected); + } +} diff --git a/src/test/java/pl/uj/io/cuteanimals/model/fight/FightManagerTest.java b/src/test/java/pl/uj/io/cuteanimals/model/fight/FightManagerTest.java new file mode 100644 index 0000000..4d05367 --- /dev/null +++ b/src/test/java/pl/uj/io/cuteanimals/model/fight/FightManagerTest.java @@ -0,0 +1,49 @@ +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; + private PlayerAttributes attrs; + + @BeforeEach + void setUp() { + player = new Player(); + attrs = (PlayerAttributes) player.getAttributes(); + } + + @Test + void endBattleEqualLevelExperienceTest() { + var dummy = new Monster("dummy", new NPCAttributes(1, 1, 1, 1)); + + player.getFightManager().beginFight(dummy); + player.getFightManager().endBattle(); + assertThat(attrs.getExperience() == 1); + } + + @Test + void endBattleMonsterHigherLevelExperienceTest() { + var dummy = new Monster("dummy", new NPCAttributes(1, 1, 11, 1)); + + player.getFightManager().beginFight(dummy); + player.getFightManager().endBattle(); + assertThat(attrs.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(attrs.getExperience() == 1); + } +}