Passed
Pull Request — master (#206)
by Vincent
12:05
created

handle(CastScope,EffectScope)   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
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 1
    public SwitchPositionOnAttackHandler(Fight fight) {
41 1
        this.applier = new SwitchPositionApplier(fight);
42 1
    }
43
44
    @Override
45
    public void handle(CastScope cast, CastScope.EffectScope effect) {
46 1
        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 1
        final ActiveFighter caster = cast.caster();
52
53 1
        for (PassiveFighter target : effect.targets()) {
54 1
            if (!target.equals(caster)) {
55 1
                target.buffs().add(new Buff(effect.effect(), cast.action(), caster, target, this));
56
            }
57 1
        }
58 1
    }
59
60
    @Override
61
    public boolean onCastTarget(Buff buff, CastScope cast) {
62 1
        if (!isDamageCast(cast)) {
63 1
            return true;
64
        }
65
66 1
        final ActiveFighter buffCaster = buff.caster();
67 1
        final PassiveFighter target = buff.target();
68
69 1
        applier.apply(buffCaster, target);
70 1
        cast.replaceTarget(target, buffCaster);
71
72 1
        return false;
73
    }
74
75
    /**
76
     * Check if the action is direct damage attack
77
     *
78
     * @param cast The action to check
79
     *
80
     * @return true if the cast can be dodged
81
     */
82
    private boolean isDamageCast(CastScope cast) {
83 1
        return cast.effects().stream()
84 1
            .map(CastScope.EffectScope::effect)
85
            // Should return only direct damage effects
86 1
            .anyMatch(effect -> EffectsUtils.isDamageEffect(effect.effect()) && effect.duration() == 0)
87
        ;
88
    }
89
}
90