Passed
Pull Request — master (#229)
by Vincent
11:03
created

check(Turn,Castable,FightCell)   A

Complexity

Conditions 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 7
dl 0
loc 11
ccs 5
cts 5
cp 1
crap 2
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-2020 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.game.fight.castable.validator;
21
22
import fr.arakne.utils.value.Interval;
23
import fr.quatrevieux.araknemu.data.constant.Characteristic;
24
import fr.quatrevieux.araknemu.game.fight.castable.Castable;
25
import fr.quatrevieux.araknemu.game.fight.map.FightCell;
26
import fr.quatrevieux.araknemu.game.fight.turn.Turn;
27
import fr.quatrevieux.araknemu.network.game.out.info.Error;
28
29
/**
30
 * Validate the cast range
31
 */
32 1
public final class RangeValidator implements CastConstraintValidator {
33
    @Override
34
    public boolean check(Turn turn, Castable castable, FightCell target) {
35 1
        final int distance = turn.fighter().cell().coordinate().distance(target);
36
37 1
        Interval range = castable.constraints().range();
38
39 1
        if (castable.modifiableRange()) {
40 1
            range = range.modify(turn.fighter().characteristics().get(Characteristic.SIGHT_BOOST));
41
        }
42
43 1
        return range.contains(distance);
44
    }
45
46
    @Override
47
    public Error validate(Turn turn, Castable castable, FightCell target) {
48 1
        final int distance = turn.fighter().cell().coordinate().distance(target);
49
50 1
        Interval range = castable.constraints().range();
51
52 1
        if (castable.modifiableRange()) {
53 1
            range = range.modify(turn.fighter().characteristics().get(Characteristic.SIGHT_BOOST));
54
        }
55
56 1
        if (!range.contains(distance)) {
57 1
            return Error.cantCastBadRange(range, distance);
58
        }
59
60 1
        return null;
61
    }
62
}
63