Passed
Push — master ( bbdd9e...bfd11a )
by Vincent
05:16 queued 12s
created

coordinate()   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 0
Metric Value
cc 1
eloc 3
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
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.map;
21
22
import fr.arakne.utils.maps.CoordinateCell;
23
import fr.arakne.utils.maps.serializer.CellData;
24
import fr.quatrevieux.araknemu.game.fight.exception.FightMapException;
25
import fr.quatrevieux.araknemu.game.fight.fighter.PassiveFighter;
26
27
import java.util.Optional;
28
29
/**
30
 * Base fight cell
31
 */
32
public final class WalkableFightCell implements FightCell {
33
    private final FightMap map;
34
    private final CellData template;
35
    private final int id;
36
    private final CoordinateCell<FightCell> coordinate;
37
38
    private PassiveFighter fighter;
39
40 1
    public WalkableFightCell(FightMap map, CellData template, int id) {
41 1
        this.map = map;
42 1
        this.template = template;
43 1
        this.id = id;
44 1
        this.coordinate = new CoordinateCell<>(this);
45 1
    }
46
47
    @Override
48
    public int id() {
49 1
        return id;
50
    }
51
52
    @Override
53
    public FightMap map() {
54 1
        return map;
55
    }
56
57
    @Override
58
    public boolean walkable() {
59 1
        return fighter == null;
60
    }
61
62
    @Override
63
    public boolean walkableIgnoreFighter() {
64 1
        return true;
65
    }
66
67
    @Override
68
    public boolean sightBlocking() {
69 1
        return !template.lineOfSight() || fighter != null;
70
    }
71
72
    @Override
73
    public CoordinateCell<FightCell> coordinate() {
74 1
        return coordinate;
75
    }
76
77
    @Override
78
    public Optional<PassiveFighter> fighter() {
79 1
        return Optional.ofNullable(fighter);
80
    }
81
82
    @Override
83
    public void set(PassiveFighter fighter) {
84 1
        if (this.fighter != null) {
85 1
            throw new FightMapException("A fighter is already set on this cell (" + id + ")");
86
        }
87
88 1
        this.fighter = fighter;
89 1
    }
90
91
    @Override
92
    public void removeFighter() {
93 1
        if (this.fighter == null) {
94 1
            throw new FightMapException("No fighter found on cell " + id);
95
        }
96
97 1
        this.fighter = null;
98 1
    }
99
100
    @Override
101
    public int hashCode() {
102 1
        return id;
103
    }
104
105
    @Override
106
    public boolean equals(Object o) {
107 1
        if (this == o) {
108 1
            return true;
109
        }
110
111 1
        if (o == null || getClass() != o.getClass()) {
112
            return false;
113
        }
114
115 1
        final WalkableFightCell that = (WalkableFightCell) o;
116
117 1
        return id == that.id && map == that.map;
118
    }
119
}
120