Passed
Pull Request — master (#207)
by Vincent
10:53
created

onReflectedDamage(Buff,ReflectedDamage)   A

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
/*
2
 * This file is part of Araknemu.
3
 *
4
 * Araknemu is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * Araknemu is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with Araknemu.  If not, see <https://www.gnu.org/licenses/>.
16
 *
17
 * Copyright (c) 2017-2021 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.game.fight.castable.effect.handler.shifting;
21
22
import fr.quatrevieux.araknemu.game.fight.Fight;
23
import fr.quatrevieux.araknemu.game.fight.castable.CastScope;
24
import fr.quatrevieux.araknemu.game.fight.castable.effect.EffectsUtils;
25
import fr.quatrevieux.araknemu.game.fight.castable.effect.buff.Buff;
26
import fr.quatrevieux.araknemu.game.fight.castable.effect.buff.BuffHook;
27
import fr.quatrevieux.araknemu.game.fight.castable.effect.handler.EffectHandler;
28
import fr.quatrevieux.araknemu.game.fight.castable.effect.handler.damage.ReflectedDamage;
29
import fr.quatrevieux.araknemu.game.fight.fighter.ActiveFighter;
30
import fr.quatrevieux.araknemu.game.fight.fighter.PassiveFighter;
31
32
/**
33
 * Buff effect for switch position between buff caster and target when the target will take damage
34
 * This effect will also replace the cast target
35
 *
36
 * @see CastScope#replaceTarget(PassiveFighter, PassiveFighter)
37
 */
38
public final class SwitchPositionOnAttackHandler implements EffectHandler, BuffHook {
39
    private final SwitchPositionApplier applier;
40
41 1
    public SwitchPositionOnAttackHandler(Fight fight) {
42 1
        this.applier = new SwitchPositionApplier(fight);
43 1
    }
44
45
    @Override
46
    public void handle(CastScope cast, CastScope.EffectScope effect) {
47 1
        throw new UnsupportedOperationException("Switch position on damage is a buff effect");
48
    }
49
50
    @Override
51
    public void buff(CastScope cast, CastScope.EffectScope effect) {
52 1
        final ActiveFighter caster = cast.caster();
53
54 1
        for (PassiveFighter target : effect.targets()) {
55 1
            if (!target.equals(caster)) {
56 1
                target.buffs().add(new Buff(effect.effect(), cast.action(), caster, target, this));
57
            }
58 1
        }
59 1
    }
60
61
    @Override
62
    public boolean onCastTarget(Buff buff, CastScope cast) {
63 1
        if (!isDamageCast(cast)) {
64 1
            return true;
65
        }
66
67 1
        final ActiveFighter buffCaster = buff.caster();
68 1
        final PassiveFighter target = buff.target();
69
70 1
        applier.apply(buffCaster, target);
71 1
        cast.replaceTarget(target, buffCaster);
72
73 1
        return false;
74
    }
75
76
    @Override
77
    public void onReflectedDamage(Buff buff, ReflectedDamage damage) {
78 1
        final ActiveFighter buffCaster = buff.caster();
79 1
        final PassiveFighter target = buff.target();
80
81 1
        applier.apply(buffCaster, target);
82 1
        damage.changeTarget(buffCaster);
83 1
    }
84
85
    /**
86
     * Check if the action is direct damage attack
87
     *
88
     * @param cast The action to check
89
     *
90
     * @return true if the cast can be dodged
91
     */
92
    private boolean isDamageCast(CastScope cast) {
93 1
        return cast.effects().stream()
94 1
            .map(CastScope.EffectScope::effect)
95
            // Should return only direct damage effects
96 1
            .anyMatch(effect -> EffectsUtils.isDamageEffect(effect.effect()) && effect.duration() == 0)
97
        ;
98
    }
99
}
100