Skip to content

Commit

Permalink
Merge pull request Arakne#205 from vincent4vx/fight-effects-shifting
Browse files Browse the repository at this point in the history
[fight] Arakne#27 Add move back and front effects
  • Loading branch information
vincent4vx authored Nov 24, 2021
2 parents 7ff43a4 + f992de9 commit 4ddcd32
Show file tree
Hide file tree
Showing 29 changed files with 1,878 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ public FighterLife life() {
return fighter.life();
}

@Override
public int level() {
return fighter.level();
}

@Override
public Buffs buffs() {
return fighter.buffs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ public States states() {
return fighter.states();
}

@Override
public int level() {
return fighter.level();
}

@Override
public FighterCharacteristics characteristics() {
return fighter.characteristics();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ public void replaceTarget(PassiveFighter originalTarget, PassiveFighter newTarge
targetMapping.put(originalTarget, newTarget);
}

/**
* Remove a target of the cast
*/
public void removeTarget(PassiveFighter target) {
targetMapping.remove(target);
}

/**
* Get list of effects to apply
*/
Expand Down Expand Up @@ -218,7 +225,7 @@ public SpellEffect effect() {
public Collection<PassiveFighter> targets() {
return targets.stream()
.map(targetMapping::get)
.filter(fighter -> !fighter.dead())
.filter(fighter -> fighter != null && !fighter.dead())
.collect(Collectors.toList())
;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import fr.quatrevieux.araknemu.game.fight.castable.CastScope;
import fr.quatrevieux.araknemu.game.fight.castable.effect.handler.damage.Damage;
import fr.quatrevieux.araknemu.game.fight.fighter.ActiveFighter;

/**
* Hook action for apply buff effects
Expand Down Expand Up @@ -60,6 +61,24 @@ public default void onCastTarget(Buff buff, CastScope cast) {}
*/
public default void onDamage(Buff buff, Damage value) {}

/**
* The fighter will take damages
*/
public default void onDirectDamage(Buff buff, ActiveFighter caster, Damage value) {
onDamage(buff, value);
}

/**
* The fighter will take damages by a buff (i.e. poison)
*
* @param buff The current buff
* @param poison The poison buff
* @param value The damage to apply
*/
public default void onBuffDamage(Buff buff, Buff poison, Damage value) {
onDamage(buff, value);
}

/**
* The fighter life has been altered
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import fr.quatrevieux.araknemu.game.fight.castable.CastScope;
import fr.quatrevieux.araknemu.game.fight.castable.effect.handler.damage.Damage;
import fr.quatrevieux.araknemu.game.fight.fighter.ActiveFighter;
import fr.quatrevieux.araknemu.game.fight.fighter.Fighter;
import fr.quatrevieux.araknemu.game.fight.fighter.PassiveFighter;
import fr.quatrevieux.araknemu.network.game.out.fight.AddBuff;
Expand Down Expand Up @@ -98,9 +99,16 @@ public void onCastTarget(CastScope cast) {
}

@Override
public void onDamage(Damage value) {
public void onDirectDamage(ActiveFighter caster, Damage value) {
for (Buff buff : buffs) {
buff.hook().onDamage(buff, value);
buff.hook().onDirectDamage(buff, caster, value);
}
}

@Override
public void onBuffDamage(Buff poison, Damage value) {
for (Buff buff : buffs) {
buff.hook().onBuffDamage(buff, poison, value);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import fr.quatrevieux.araknemu.game.fight.castable.CastScope;
import fr.quatrevieux.araknemu.game.fight.castable.effect.handler.damage.Damage;
import fr.quatrevieux.araknemu.game.fight.fighter.ActiveFighter;
import fr.quatrevieux.araknemu.game.fight.fighter.PassiveFighter;

/**
Expand All @@ -38,9 +39,14 @@ public interface Buffs extends Iterable<Buff> {
public void onCastTarget(CastScope cast);

/**
* @see BuffHook#onDamage(Buff, Damage)
* @see BuffHook#onDirectDamage(Buff, ActiveFighter, Damage)
*/
public void onDamage(Damage value);
public void onDirectDamage(ActiveFighter caster, Damage value);

/**
* @see BuffHook#onBuffDamage(Buff, Buff, Damage)
*/
public void onBuffDamage(Buff poison, Damage value);

/**
* @see BuffHook#onLifeAltered(Buff, int)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,67 @@ public DamageApplier(Element element, Fight fight) {
}

/**
* Apply a damage effect to a fighter
* Apply a direct damage effect to a fighter
*
* Note: do not use this method for a buff, it will call the invalid buff hook
*
* @param caster The spell caster
* @param effect The effect to apply
* @param target The target
*
* @return The real damage value
*
* @see DamageApplier#apply(Buff) For apply a buff damage (i.e. poison)
* @see fr.quatrevieux.araknemu.game.fight.castable.effect.buff.Buffs#onDirectDamage(ActiveFighter, Damage) The called buff hook
*/
public int apply(ActiveFighter caster, SpellEffect effect, PassiveFighter target) {
final Damage damage = computeDamage(caster, effect, target);
target.buffs().onDirectDamage(caster, damage);

return applyDamage(caster, damage, target);
}

/**
* Apply a damage buff effect
*
* @param buff Buff to apply
*
* @return The real damage value
*
* @see fr.quatrevieux.araknemu.game.fight.castable.effect.buff.Buffs#onBuffDamage(Buff, Damage) The called buff hook
*/
public int apply(Buff buff) {
final PassiveFighter target = buff.target();
final ActiveFighter caster = buff.caster();

final Damage damage = computeDamage(caster, buff.effect(), target);
target.buffs().onBuffDamage(buff, damage);

return applyDamage(caster, damage, target);
}

/**
* Create the damage object
*/
private Damage computeDamage(ActiveFighter caster, SpellEffect effect, PassiveFighter target) {
final EffectValue value = new EffectValue(effect)
.percent(caster.characteristics().get(element.boost()))
.percent(caster.characteristics().get(Characteristic.PERCENT_DAMAGE))
.fixed(caster.characteristics().get(Characteristic.FIXED_DAMAGE))
;

final Damage damage = new Damage(value.value(), element)
return new Damage(value.value(), element)
.percent(target.characteristics().get(element.percentResistance()))
.fixed(target.characteristics().get(element.fixedResistance()))
;
}

target.buffs().onDamage(damage);

/**
* Apply the damage object to the target
*
* @return The life change value. Negative for damage, positive for heal.
*/
private int applyDamage(ActiveFighter caster, Damage damage, PassiveFighter target) {
if (damage.reducedDamage() > 0) {
fight.send(ActionEffect.reducedDamage(target, damage.reducedDamage()));
}
Expand All @@ -77,15 +116,4 @@ public int apply(ActiveFighter caster, SpellEffect effect, PassiveFighter target

return target.life().alter(caster, -damage.value());
}

/**
* Apply a damage buff effect
*
* @param buff Buff to apply
*
* @return The realm damage value
*/
public int apply(Buff buff) {
return apply(buff.caster(), buff.effect(), buff.target());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* 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.shifting;

import fr.arakne.utils.value.helper.RandomUtil;
import fr.quatrevieux.araknemu.game.fight.Fight;
import fr.quatrevieux.araknemu.game.fight.castable.CastScope;
import fr.quatrevieux.araknemu.game.fight.castable.effect.EffectsUtils;
import fr.quatrevieux.araknemu.game.fight.castable.effect.buff.Buff;
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.PassiveFighter;

/**
* Buff effect which permit to "cancel" a damage effect by moving back
*
* This effect is hooked before cast the spell, and remove the target if the effect is a damage,
* and it's launch in close combat (i.e. distance = 1)
*
* Note: this effect is only applied on direct damage
*/
public final class AvoidDamageByMovingBackHandler implements EffectHandler, BuffHook {
private final MoveBackApplier applier;
private final RandomUtil random = new RandomUtil();

public AvoidDamageByMovingBackHandler(Fight fight) {
applier = new MoveBackApplier(fight);
}

@Override
public void handle(CastScope cast, CastScope.EffectScope effect) {
throw new UnsupportedOperationException("Avoid damage by moving back is a buff effect");
}

@Override
public void buff(CastScope cast, CastScope.EffectScope effect) {
for (PassiveFighter target : effect.targets()) {
target.buffs().add(new Buff(effect.effect(), cast.action(), cast.caster(), target, this));
}
}

@Override
public void onCastTarget(Buff buff, CastScope cast) {
if (!isDamageCast(cast) || buff.target().cell().coordinate().distance(cast.caster().cell()) != 1) {
return;
}

if (!random.bool(buff.effect().min())) {
return;
}

applier.apply(cast.caster(), buff.target(), buff.effect().max());
cast.removeTarget(buff.target());
}

/**
* Check if the action is direct damage attack
*
* @param cast The action to check
*
* @return true if the cast can be dodged
*/
private boolean isDamageCast(CastScope cast) {
return cast.effects().stream()
.map(CastScope.EffectScope::effect)
// Should return only direct damage effects
.anyMatch(effect -> EffectsUtils.isDamageEffect(effect.effect()) && effect.duration() == 0)
;
}
}
Loading

0 comments on commit 4ddcd32

Please sign in to comment.