Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix experience being not calculated correctly (#142) #150

Merged
merged 1 commit into from
Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
11 changes: 4 additions & 7 deletions src/main/java/pl/uj/io/cuteanimals/model/fight/FightManager.java
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 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));
Expand Down
53 changes: 53 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,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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}