MoveToTargetCellHandler(Fight)   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
dl 0
loc 3
ccs 3
cts 3
cp 1
crap 1
rs 10
c 1
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.arakne.utils.maps.CoordinateCell;
23
import fr.arakne.utils.maps.constant.Direction;
24
import fr.arakne.utils.maps.path.Decoder;
25
import fr.quatrevieux.araknemu.game.fight.Fight;
26
import fr.quatrevieux.araknemu.game.fight.castable.FightCastScope;
27
import fr.quatrevieux.araknemu.game.fight.castable.effect.handler.EffectHandler;
28
import fr.quatrevieux.araknemu.game.fight.fighter.Fighter;
29
import fr.quatrevieux.araknemu.game.fight.map.BattlefieldCell;
30
import fr.quatrevieux.araknemu.game.fight.map.FightCell;
31
32
/**
33
 * Move the adjacent fighter to the target cell
34
 *
35
 * - Get the direction between caster and target cell
36
 * - Get the adjacent fighter for the given direction
37
 * - Compute the distance between the target fighter and the target cell
38
 * - Perform a move back on the target fighter with the computed distance
39
 */
40
public final class MoveToTargetCellHandler implements EffectHandler {
41
    private final Decoder<FightCell> decoder;
42
    private final MoveBackApplier applier;
43
44 1
    public MoveToTargetCellHandler(Fight fight) {
45 1
        this.decoder = fight.map().decoder();
46 1
        this.applier = new MoveBackApplier(fight);
47 1
    }
48
49
    @Override
50
    public void handle(FightCastScope cast, FightCastScope.EffectScope effect) {
51 1
        final Fighter caster = cast.caster();
52 1
        final FightCell casterCell = caster.cell();
53 1
        final CoordinateCell<BattlefieldCell> casterCellCoordinate = casterCell.coordinate();
54
55
        // Remove 1 because the distance should be computed from the target fighter cell
56 1
        final int distance = casterCellCoordinate.distance(cast.target()) - 1;
57 1
        final Direction direction = casterCellCoordinate.directionTo(cast.target());
58
59 1
        if (distance < 1) {
60 1
            return;
61
        }
62
63 1
        decoder.nextCellByDirection(casterCell, direction)
64 1
            .map(FightCell::fighter)
65 1
            .ifPresent(target -> applier.apply(caster, target, distance))
66
        ;
67 1
    }
68
69
    @Override
70
    public void buff(FightCastScope cast, FightCastScope.EffectScope effect) {
71 1
        throw new UnsupportedOperationException("Cannot use move back as buff effect");
72
    }
73
}
74