Passed
Push — master ( 99d37b...c9f44c )
by Vincent
20:33 queued 06:16
created

perform(Consumer)   A

Complexity

Conditions 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 6
ccs 3
cts 3
cp 1
c 0
b 0
f 0
cc 2
crap 2
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-2020 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.game.fight.fighter;
21
22
import fr.arakne.utils.maps.constant.Direction;
23
import fr.quatrevieux.araknemu.core.event.DefaultListenerAggregate;
24
import fr.quatrevieux.araknemu.core.event.ListenerAggregate;
25
import fr.quatrevieux.araknemu.game.fight.Fight;
26
import fr.quatrevieux.araknemu.game.fight.castable.effect.buff.BuffList;
27
import fr.quatrevieux.araknemu.game.fight.exception.FightException;
28
import fr.quatrevieux.araknemu.game.fight.fighter.event.FighterInitialized;
29
import fr.quatrevieux.araknemu.game.fight.map.FightCell;
30
import fr.quatrevieux.araknemu.game.fight.turn.FightTurn;
31
32
import java.util.HashMap;
33
import java.util.Map;
34
import java.util.function.Consumer;
35
36
/**
37
 * Base class for implements a fighter
38
 * Provide commons attributes and methods
39
 */
40 1
public abstract class AbstractFighter implements Fighter {
41 1
    private final ListenerAggregate dispatcher = new DefaultListenerAggregate();
42 1
    private final BuffList buffs = new BuffList(this);
43 1
    private final States states = new FighterStates(this);
44 1
    private final Map<Object, Object> attachments = new HashMap<>();
45
46
    // Mutable attributes
47
    private FightCell cell;
48
    private Fight fight;
49
    private FightTurn turn;
50 1
    private Direction orientation = Direction.SOUTH_EAST;
51
52
    @Override
53
    public void init() {
54 1
        fight.dispatch(new FighterInitialized(this));
55 1
    }
56
57
    @Override
58
    public final FightCell cell() {
59 1
        return cell;
60
    }
61
62
    @Override
63
    public final Direction orientation() {
64 1
        return orientation;
65
    }
66
67
    @Override
68
    public final void setOrientation(Direction orientation) {
69 1
        this.orientation = orientation;
70 1
    }
71
72
    @Override
73
    public final void move(FightCell cell) {
74 1
        if (this.cell != null) {
75 1
            this.cell.removeFighter();
76
        }
77
78 1
        if (cell != null) {
79 1
            cell.set(this);
80
        }
81
82 1
        this.cell = cell;
83 1
    }
84
85
    @Override
86
    public void dispatch(Object event) {
87 1
        dispatcher.dispatch(event);
88 1
    }
89
90
    public final ListenerAggregate dispatcher() {
91 1
        return dispatcher;
92
    }
93
94
    @Override
95
    public final BuffList buffs() {
96 1
        return buffs;
97
    }
98
99
    @Override
100
    public final States states() {
101 1
        return states;
102
    }
103
104
    @Override
105
    public final Fight fight() {
106 1
        return fight;
107
    }
108
109
    @Override
110
    public final void joinFight(Fight fight, FightCell startCell) {
111 1
        if (this.fight != null) {
112 1
            throw new IllegalStateException("A fight is already defined");
113
        }
114
115 1
        this.fight = fight;
116 1
        this.cell = startCell;
117 1
        startCell.set(this);
118 1
    }
119
120
    @Override
121
    public void play(FightTurn turn) {
122 1
        this.turn = turn;
123 1
    }
124
125
    @Override
126
    public void stop() {
127 1
        turn = null;
128 1
    }
129
130
    @Override
131
    public final FightTurn turn() {
132 1
        if (turn == null) {
133 1
            throw new FightException("It's not your turn");
134
        }
135
136 1
        return turn;
137
    }
138
139
    @Override
140
    public final void perform(Consumer<FightTurn> action) {
141 1
        final FightTurn turn = this.turn;
0 ignored issues
show
Comprehensibility introduced by
The variable turnshadows a field with the same name declared in line 49. Consider renaming it.
Loading history...
142
143 1
        if (turn != null) {
144 1
            action.accept(turn);
145
        }
146 1
    }
147
148
    @Override
149
    public final void attach(Object key, Object value) {
150 1
        attachments.put(key, value);
151 1
    }
152
153
    @Override
154
    public final Object attachment(Object key) {
155 1
        return attachments.get(key);
156
    }
157
158
    @Override
159
    public final boolean isOnFight() {
160 1
        return fight != null && cell != null;
161
    }
162
163
    @Override
164
    public final boolean equals(Object obj) {
165 1
        if (obj == this) {
166 1
            return true;
167
        }
168
169 1
        if (!(obj instanceof PassiveFighter)) {
170
            return false;
171
        }
172
173 1
        return id() == ((PassiveFighter) obj).id();
174
    }
175
176
    @Override
177
    public final int hashCode() {
178 1
        return id();
179
    }
180
}
181