Passed
Pull Request — master (#280)
by Vincent
13:25
created

checkSelf(ActiveFighter,PassiveFighter)   A

Complexity

Conditions 1

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
eloc 2
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-2019 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.game.spell.effect.target;
21
22
import fr.quatrevieux.araknemu.game.fight.fighter.ActiveFighter;
23
import fr.quatrevieux.araknemu.game.fight.fighter.PassiveFighter;
24
import fr.quatrevieux.araknemu.game.fight.team.Team;
25
import org.checkerframework.checker.nullness.qual.Nullable;
26
27
import java.util.Objects;
28
29
/**
30
 * Implementation of effect target using spell flags
31
 */
32
public final class SpellEffectTarget implements EffectTarget {
33 1
    public static final SpellEffectTarget DEFAULT = new SpellEffectTarget(0);
34
35
    public static final int NOT_TEAM    = 1;
36
    public static final int NOT_SELF    = 2;
37
    public static final int NOT_ENEMY   = 4;
38
    public static final int ONLY_INVOC  = 8;
39
    public static final int NOT_INVOC   = 16;
40
    public static final int ONLY_CASTER = 32;
41
42
    private final int flags;
43
44 1
    public SpellEffectTarget(int flags) {
45 1
        this.flags = flags;
46 1
    }
47
48
    @Override
49
    public boolean onlyCaster() {
50 1
        return check(ONLY_CASTER);
51
    }
52
53
    @Override
54
    public boolean test(ActiveFighter caster, PassiveFighter fighter) {
55 1
        return checkSelf(caster, fighter)
56 1
            && checkTeam(caster.team(), fighter.team())
57 1
            && checkInvocation(fighter)
58
        ;
59
    }
60
61
    private boolean check(int flag) {
62 1
        return (flags & flag) == flag;
63
    }
64
65
    @Override
66
    public boolean equals(@Nullable Object obj) {
67 1
        if (obj == this) {
68 1
            return true;
69
        }
70
71 1
        if (obj == null) {
72 1
            return false;
73
        }
74
75 1
        return getClass().equals(obj.getClass()) && flags == ((SpellEffectTarget) obj).flags;
76
    }
77
78
    @Override
79
    public int hashCode() {
80 1
        return Objects.hash(flags);
81
    }
82
83
    private boolean checkTeam(Team<?> casterTeam, Team<?> targetTeam) {
84 1
        if (check(NOT_TEAM) && casterTeam.equals(targetTeam)) {
85 1
            return false;
86
        }
87
88 1
        if (check(NOT_ENEMY) && !casterTeam.equals(targetTeam)) {
89 1
            return false;
90
        }
91
92 1
        return true;
93
    }
94
95
    private boolean checkInvocation(PassiveFighter target) {
96 1
        final boolean isInvocation = target.invoked();
97
98 1
        if (check(ONLY_INVOC) && !isInvocation) {
99 1
            return false;
100
        }
101
102 1
        if (check(NOT_INVOC) && isInvocation) {
103 1
            return false;
104
        }
105
106 1
        return true;
107
    }
108
109
    private boolean checkSelf(ActiveFighter caster, PassiveFighter target) {
110 1
        return !check(NOT_SELF) || !caster.equals(target);
111
    }
112
}
113