forked from Arakne/Araknemu
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fight] Arakne#27 Add give percent life effect
- Loading branch information
1 parent
396f302
commit db2ba8d
Showing
5 changed files
with
305 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
.../quatrevieux/araknemu/game/fight/castable/effect/handler/heal/GivePercentLifeHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* This file is part of Araknemu. | ||
* | ||
* Araknemu is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Araknemu is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with Araknemu. If not, see <https://www.gnu.org/licenses/>. | ||
* | ||
* Copyright (c) 2017-2021 Vincent Quatrevieux | ||
*/ | ||
|
||
package fr.quatrevieux.araknemu.game.fight.castable.effect.handler.heal; | ||
|
||
import fr.quatrevieux.araknemu.game.fight.castable.CastScope; | ||
import fr.quatrevieux.araknemu.game.fight.castable.effect.EffectValue; | ||
import fr.quatrevieux.araknemu.game.fight.castable.effect.handler.EffectHandler; | ||
import fr.quatrevieux.araknemu.game.fight.fighter.ActiveFighter; | ||
import fr.quatrevieux.araknemu.game.fight.fighter.PassiveFighter; | ||
|
||
/** | ||
* Heal fighters by giving its own life, in percent of current life | ||
*/ | ||
public final class GivePercentLifeHandler implements EffectHandler { | ||
@Override | ||
public void handle(CastScope cast, CastScope.EffectScope effect) { | ||
final ActiveFighter caster = cast.caster(); | ||
final int heal = new EffectValue(effect.effect()).value() * caster.life().current() / 100; | ||
|
||
caster.life().alter(caster, -heal); | ||
|
||
for (PassiveFighter target : effect.targets()) { | ||
if (!target.equals(caster)) { | ||
target.life().alter(caster, heal); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void buff(CastScope cast, CastScope.EffectScope effect) { | ||
throw new UnsupportedOperationException("Cannot use give percent life effect as buff"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
235 changes: 235 additions & 0 deletions
235
...trevieux/araknemu/game/fight/castable/effect/handler/heal/GivePercentLifeHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,235 @@ | ||
/* | ||
* This file is part of Araknemu. | ||
* | ||
* Araknemu is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Araknemu is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with Araknemu. If not, see <https://www.gnu.org/licenses/>. | ||
* | ||
* Copyright (c) 2017-2022 Vincent Quatrevieux | ||
*/ | ||
|
||
package fr.quatrevieux.araknemu.game.fight.castable.effect.handler.heal; | ||
|
||
import fr.quatrevieux.araknemu.data.value.EffectArea; | ||
import fr.quatrevieux.araknemu.game.fight.Fight; | ||
import fr.quatrevieux.araknemu.game.fight.FightBaseCase; | ||
import fr.quatrevieux.araknemu.game.fight.castable.CastScope; | ||
import fr.quatrevieux.araknemu.game.fight.fighter.player.PlayerFighter; | ||
import fr.quatrevieux.araknemu.game.spell.Spell; | ||
import fr.quatrevieux.araknemu.game.spell.SpellConstraints; | ||
import fr.quatrevieux.araknemu.game.spell.effect.SpellEffect; | ||
import fr.quatrevieux.araknemu.game.spell.effect.area.CellArea; | ||
import fr.quatrevieux.araknemu.game.spell.effect.area.CircleArea; | ||
import fr.quatrevieux.araknemu.game.spell.effect.target.SpellEffectTarget; | ||
import fr.quatrevieux.araknemu.network.game.out.fight.action.ActionEffect; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
class GivePercentLifeHandlerTest extends FightBaseCase { | ||
private Fight fight; | ||
private PlayerFighter caster; | ||
private PlayerFighter target; | ||
private GivePercentLifeHandler handler; | ||
private int lastTargetLife; | ||
|
||
@Override | ||
@BeforeEach | ||
public void setUp() throws Exception { | ||
super.setUp(); | ||
|
||
fight = createFight(); | ||
fight.nextState(); | ||
|
||
caster = player.fighter(); | ||
target = other.fighter(); | ||
|
||
target.move(fight.map().get(123)); | ||
target.life().alter(target, -30); | ||
lastTargetLife = target.life().current(); | ||
|
||
handler = new GivePercentLifeHandler(); | ||
|
||
requestStack.clear(); | ||
} | ||
|
||
@Test | ||
void applyRandomEffect() { | ||
SpellEffect effect = Mockito.mock(SpellEffect.class); | ||
Spell spell = Mockito.mock(Spell.class); | ||
SpellConstraints constraints = Mockito.mock(SpellConstraints.class); | ||
|
||
Mockito.when(effect.min()).thenReturn(5); | ||
Mockito.when(effect.max()).thenReturn(10); | ||
Mockito.when(effect.area()).thenReturn(new CellArea()); | ||
Mockito.when(effect.target()).thenReturn(SpellEffectTarget.DEFAULT); | ||
Mockito.when(spell.constraints()).thenReturn(constraints); | ||
Mockito.when(constraints.freeCell()).thenReturn(false); | ||
|
||
CastScope scope = makeCastScope(caster, spell, effect, target.cell()); | ||
handler.handle(scope, scope.effects().get(0)); | ||
|
||
assertBetween(14, 29, computeHeal()); | ||
|
||
requestStack.assertAll( | ||
ActionEffect.alterLifePoints(caster, caster, -computeHeal()), | ||
ActionEffect.alterLifePoints(caster, target, computeHeal()) | ||
); | ||
} | ||
|
||
@Test | ||
void applyFixedEffect() { | ||
SpellEffect effect = Mockito.mock(SpellEffect.class); | ||
Spell spell = Mockito.mock(Spell.class); | ||
SpellConstraints constraints = Mockito.mock(SpellConstraints.class); | ||
|
||
Mockito.when(effect.min()).thenReturn(10); | ||
Mockito.when(effect.area()).thenReturn(new CellArea()); | ||
Mockito.when(effect.target()).thenReturn(SpellEffectTarget.DEFAULT); | ||
Mockito.when(spell.constraints()).thenReturn(constraints); | ||
Mockito.when(constraints.freeCell()).thenReturn(false); | ||
|
||
CastScope scope = makeCastScope(caster, spell, effect, target.cell()); | ||
handler.handle(scope, scope.effects().get(0)); | ||
|
||
assertEquals(29, computeHeal()); | ||
requestStack.assertAll( | ||
ActionEffect.alterLifePoints(caster, caster, -29), | ||
ActionEffect.alterLifePoints(caster, target, 29) | ||
); | ||
} | ||
|
||
@Test | ||
void applyToEmptyCellShouldRemoveCasterLife() { | ||
SpellEffect effect = Mockito.mock(SpellEffect.class); | ||
Spell spell = Mockito.mock(Spell.class); | ||
SpellConstraints constraints = Mockito.mock(SpellConstraints.class); | ||
|
||
Mockito.when(effect.min()).thenReturn(10); | ||
Mockito.when(effect.area()).thenReturn(new CellArea()); | ||
Mockito.when(effect.target()).thenReturn(SpellEffectTarget.DEFAULT); | ||
Mockito.when(spell.constraints()).thenReturn(constraints); | ||
Mockito.when(constraints.freeCell()).thenReturn(false); | ||
|
||
CastScope scope = makeCastScope(caster, spell, effect, fight.map().get(5)); | ||
handler.handle(scope, scope.effects().get(0)); | ||
|
||
requestStack.assertLast(ActionEffect.alterLifePoints(caster, caster, -29)); | ||
} | ||
|
||
@Test | ||
void applyToEmptyCellWithArea() { | ||
SpellEffect effect = Mockito.mock(SpellEffect.class); | ||
Spell spell = Mockito.mock(Spell.class); | ||
SpellConstraints constraints = Mockito.mock(SpellConstraints.class); | ||
|
||
Mockito.when(effect.min()).thenReturn(10); | ||
Mockito.when(effect.area()).thenReturn(new CircleArea(new EffectArea(EffectArea.Type.CIRCLE, 2))); | ||
Mockito.when(effect.target()).thenReturn(SpellEffectTarget.DEFAULT); | ||
Mockito.when(spell.constraints()).thenReturn(constraints); | ||
Mockito.when(constraints.freeCell()).thenReturn(false); | ||
|
||
CastScope scope = makeCastScope(caster, spell, effect, fight.map().get(122)); | ||
handler.handle(scope, scope.effects().get(0)); | ||
|
||
assertEquals(29, computeHeal()); | ||
|
||
requestStack.assertAll( | ||
ActionEffect.alterLifePoints(caster, caster, -29), | ||
ActionEffect.alterLifePoints(caster, target, 29) | ||
); | ||
} | ||
|
||
@Test | ||
void applyShouldIgnoreSelfTarget() { | ||
SpellEffect effect = Mockito.mock(SpellEffect.class); | ||
Spell spell = Mockito.mock(Spell.class); | ||
SpellConstraints constraints = Mockito.mock(SpellConstraints.class); | ||
|
||
caster.life().alter(caster, -30); | ||
requestStack.clear(); | ||
|
||
Mockito.when(effect.min()).thenReturn(10); | ||
Mockito.when(effect.area()).thenReturn(new CircleArea(new EffectArea(EffectArea.Type.CIRCLE, 20))); | ||
Mockito.when(effect.target()).thenReturn(SpellEffectTarget.DEFAULT); | ||
Mockito.when(spell.constraints()).thenReturn(constraints); | ||
Mockito.when(constraints.freeCell()).thenReturn(false); | ||
|
||
CastScope scope = makeCastScope(caster, spell, effect, fight.map().get(122)); | ||
handler.handle(scope, scope.effects().get(0)); | ||
|
||
requestStack.assertAll( | ||
ActionEffect.alterLifePoints(caster, caster, -26), | ||
ActionEffect.alterLifePoints(caster, target, 26) | ||
); | ||
} | ||
|
||
@Test | ||
void withMultipleAlliesShouldOnlyRemoveCasterLifeOnce() { | ||
fight = fightBuilder() | ||
.addSelf(fb -> fb.currentLife(250).maxLife(250).cell(298)) | ||
.addAlly(fb -> fb.currentLife(50).maxLife(100).cell(312)) | ||
.addAlly(fb -> fb.currentLife(50).maxLife(100).cell(313)) | ||
.addEnemy(fb -> fb.cell(293)) | ||
.build(true) | ||
; | ||
|
||
fight.nextState(); | ||
|
||
caster = (PlayerFighter) fight.fighters().get(0); | ||
|
||
SpellEffect effect = Mockito.mock(SpellEffect.class); | ||
Spell spell = Mockito.mock(Spell.class); | ||
SpellConstraints constraints = Mockito.mock(SpellConstraints.class); | ||
|
||
requestStack.clear(); | ||
|
||
Mockito.when(effect.min()).thenReturn(10); | ||
Mockito.when(effect.area()).thenReturn(new CircleArea(new EffectArea(EffectArea.Type.CIRCLE, 1))); | ||
Mockito.when(effect.target()).thenReturn(SpellEffectTarget.DEFAULT); | ||
Mockito.when(spell.constraints()).thenReturn(constraints); | ||
Mockito.when(constraints.freeCell()).thenReturn(false); | ||
|
||
CastScope scope = makeCastScope(caster, spell, effect, fight.map().get(298)); | ||
handler.handle(scope, scope.effects().get(0)); | ||
|
||
requestStack.assertAll( | ||
ActionEffect.alterLifePoints(caster, caster, -25), | ||
ActionEffect.alterLifePoints(caster, fight.fighters().get(1), 25), | ||
ActionEffect.alterLifePoints(caster, fight.fighters().get(2), 25) | ||
); | ||
} | ||
|
||
@Test | ||
void buffNotSupported() { | ||
SpellEffect effect = Mockito.mock(SpellEffect.class); | ||
Spell spell = Mockito.mock(Spell.class); | ||
SpellConstraints constraints = Mockito.mock(SpellConstraints.class); | ||
|
||
Mockito.when(effect.min()).thenReturn(10); | ||
Mockito.when(effect.area()).thenReturn(new CellArea()); | ||
Mockito.when(effect.target()).thenReturn(SpellEffectTarget.DEFAULT); | ||
Mockito.when(effect.duration()).thenReturn(5); | ||
Mockito.when(spell.constraints()).thenReturn(constraints); | ||
Mockito.when(constraints.freeCell()).thenReturn(false); | ||
|
||
CastScope scope = makeCastScope(caster, spell, effect, target.cell()); | ||
assertThrows(UnsupportedOperationException.class, () -> handler.buff(scope, scope.effects().get(0))); | ||
} | ||
|
||
private int computeHeal() { | ||
return target.life().current() - lastTargetLife; | ||
} | ||
} |