forked from Arakne/Araknemu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Arakne#27 Add effect handler for punishment
- Loading branch information
1 parent
901f29d
commit addf2f3
Showing
14 changed files
with
1,140 additions
and
8 deletions.
There are no files selected for viewing
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
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
153 changes: 153 additions & 0 deletions
153
...u/game/fight/castable/effect/handler/characteristic/AddCharacteristicOnDamageHandler.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,153 @@ | ||
/* | ||
* 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.characteristic; | ||
|
||
import fr.quatrevieux.araknemu.data.constant.Characteristic; | ||
import fr.quatrevieux.araknemu.game.fight.Fight; | ||
import fr.quatrevieux.araknemu.game.fight.castable.CastScope; | ||
import fr.quatrevieux.araknemu.game.fight.castable.effect.buff.Buff; | ||
import fr.quatrevieux.araknemu.game.fight.castable.effect.buff.BuffEffect; | ||
import fr.quatrevieux.araknemu.game.fight.castable.effect.buff.BuffHook; | ||
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; | ||
import fr.quatrevieux.araknemu.game.spell.effect.SpellEffect; | ||
import org.checkerframework.checker.index.qual.NonNegative; | ||
import org.checkerframework.checker.index.qual.Positive; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Effect handler for punishment | ||
* | ||
* Add a characteristic boost when the fight suffer from direct damage | ||
* Characteristics mapping must be added using {@link AddCharacteristicOnDamageHandler#register(int, Characteristic)} method | ||
* | ||
* Parameters: | ||
* - first (i.e. min) is the effect used as characteristic boost | ||
* - second (i.e. max) is the maximal boost value per turn | ||
* - third (i.e. special) is the boost duration | ||
* | ||
* Note: this buff cannot be dispelled, but the boosted characteristics can | ||
*/ | ||
public final class AddCharacteristicOnDamageHandler implements EffectHandler, BuffHook { | ||
private final Fight fight; | ||
|
||
/** | ||
* Map first effect parameter to related characteristic hook | ||
*/ | ||
private final Map<Integer, AlterCharacteristicHook> hooksMapping = new HashMap<>(); | ||
|
||
public AddCharacteristicOnDamageHandler(Fight fight) { | ||
this.fight = fight; | ||
} | ||
|
||
@Override | ||
public void handle(CastScope cast, CastScope.EffectScope effect) { | ||
throw new UnsupportedOperationException("Alter characteristic effect must be used as a buff"); | ||
} | ||
|
||
@Override | ||
public void buff(CastScope cast, CastScope.EffectScope effect) { | ||
final SpellEffect spellEffect = effect.effect(); | ||
final ActiveFighter caster = cast.caster(); | ||
|
||
for (PassiveFighter target : cast.targets()) { | ||
target.buffs().add(new Buff( | ||
spellEffect, | ||
cast.action(), | ||
caster, | ||
target, | ||
this, | ||
false | ||
)); | ||
} | ||
} | ||
|
||
/** | ||
* Register an handled characteristic | ||
* | ||
* @param effectId Handled effect id. This is the first parameter of the spell effect | ||
* @param hook Alter characteristic hook to use | ||
* | ||
* @return This instance | ||
*/ | ||
public AddCharacteristicOnDamageHandler register(int effectId, AlterCharacteristicHook hook) { | ||
hooksMapping.put(effectId, hook); | ||
|
||
return this; | ||
} | ||
|
||
/** | ||
* Register an handled characteristic | ||
* | ||
* @param effectId Handled effect id. This is the first parameter of the spell effect | ||
* @param characteristic The mapped characteristic | ||
* | ||
* @return This instance | ||
*/ | ||
public AddCharacteristicOnDamageHandler register(int effectId, Characteristic characteristic) { | ||
return register(effectId, AlterCharacteristicHook.add(fight, characteristic)); | ||
} | ||
|
||
@Override | ||
public void onDirectDamageApplied(Buff buff, ActiveFighter caster, @Positive int damage) { | ||
final PassiveFighter target = buff.target(); | ||
final int boostEffectId = buff.effect().min(); | ||
final AlterCharacteristicHook hook = hooksMapping.get(boostEffectId); | ||
|
||
if (hook == null) { | ||
throw new IllegalArgumentException("Unsupported effect " + boostEffectId + " for punishment effect"); | ||
} | ||
|
||
final int maximalValue = buff.effect().max() - currentBoostValue(buff, caster); | ||
|
||
if (maximalValue <= 0) { | ||
return; | ||
} | ||
|
||
target.buffs().add(new Buff( | ||
BuffEffect.withCustomEffectAndDuration(buff.effect(), boostEffectId, Math.min(maximalValue, damage), buff.effect().special()), | ||
buff.action(), | ||
caster, | ||
target, | ||
hook | ||
)); | ||
} | ||
|
||
private @NonNegative int currentBoostValue(Buff buff, ActiveFighter caster) { | ||
// Add 1 to duration in case of self damage | ||
final int expectedEffectDuration = buff.effect().special() + (caster.equals(buff.target()) ? 1 : 0); | ||
|
||
int boost = 0; | ||
|
||
for (Buff activeBuff : buff.target().buffs()) { | ||
if (activeBuff.effect().effect() == buff.effect().min() | ||
&& activeBuff.remainingTurns() == expectedEffectDuration | ||
&& activeBuff.action().equals(buff.action()) | ||
) { | ||
boost += activeBuff.effect().min(); | ||
} | ||
} | ||
|
||
return boost; | ||
} | ||
} |
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
Oops, something went wrong.