Test Failed
Pull Request — master (#206)
by Vincent
11:44
created

onCastTarget(Buff,CastScope)   A

Complexity

Conditions 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 11
rs 10
eloc 8
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.fighter.ActiveFighter;
29
import fr.quatrevieux.araknemu.game.fight.fighter.PassiveFighter;
30
31
/**
32
 * Buff effect for switch position between buff caster and target when the target will take damage
33
 * This effect will also replace the cast target
34
 *
35
 * @see CastScope#replaceTarget(PassiveFighter, PassiveFighter)
36
 */
37
public final class SwitchPositionOnAttackHandler implements EffectHandler, BuffHook {
38
    private final SwitchPositionApplier applier;
39
40
    public SwitchPositionOnAttackHandler(Fight fight) {
41
        this.applier = new SwitchPositionApplier(fight);
42
    }
43
44
    @Override
45
    public void handle(CastScope cast, CastScope.EffectScope effect) {
46
        throw new UnsupportedOperationException("Switch position on damage is a buff effect");
47
    }
48
49
    @Override
50
    public void buff(CastScope cast, CastScope.EffectScope effect) {
51
        final ActiveFighter caster = cast.caster();
52
53
        for (PassiveFighter target : effect.targets()) {
54
            if (!target.equals(caster)) {
55
                target.buffs().add(new Buff(effect.effect(), cast.action(), caster, target, this));
56
            }
57
        }
58
    }
59
60
    @Override
61
    public void onCastTarget(Buff buff, CastScope cast) {
62
        if (!isDamageCast(cast)) {
63
            return;
64
        }
65
66
        final ActiveFighter buffCaster = buff.caster();
67
        final PassiveFighter target = buff.target();
68
69
        applier.apply(buffCaster, target);
70
        cast.replaceTarget(target, buffCaster);
71
    }
72
73
    /**
74
     * Check if the action is direct damage attack
75
     *
76
     * @param cast The action to check
77
     *
78
     * @return true if the cast can be dodged
79
     */
80
    private boolean isDamageCast(CastScope cast) {
81
        return cast.effects().stream()
82
            .map(CastScope.EffectScope::effect)
83
            // Should return only direct damage effects
84
            .anyMatch(effect -> EffectsUtils.isDamageEffect(effect.effect()) && effect.duration() == 0)
85
        ;
86
    }
87
}
88