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.FighterHidden; |
29
|
|
|
import fr.quatrevieux.araknemu.game.fight.fighter.event.FighterInitialized; |
30
|
|
|
import fr.quatrevieux.araknemu.game.fight.fighter.event.FighterVisible; |
31
|
|
|
import fr.quatrevieux.araknemu.game.fight.map.FightCell; |
32
|
|
|
import fr.quatrevieux.araknemu.game.fight.turn.FightTurn; |
33
|
|
|
import org.checkerframework.checker.nullness.qual.MonotonicNonNull; |
34
|
|
|
import org.checkerframework.checker.nullness.qual.Nullable; |
35
|
|
|
|
36
|
|
|
import java.util.HashMap; |
37
|
|
|
import java.util.Map; |
38
|
|
|
import java.util.function.Consumer; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Base class for implements a fighter |
42
|
|
|
* Provide commons attributes and methods |
43
|
|
|
*/ |
44
|
1 |
|
public abstract class AbstractFighter implements Fighter { |
45
|
1 |
|
private final ListenerAggregate dispatcher = new DefaultListenerAggregate(); |
46
|
1 |
|
@SuppressWarnings({"assignment", "argument"}) |
47
|
|
|
private final BuffList buffs = new BuffList(this); |
48
|
1 |
|
@SuppressWarnings({"assignment", "argument"}) |
49
|
|
|
private final States states = new FighterStates(this); |
50
|
1 |
|
private final Map<Object, Object> attachments = new HashMap<>(); |
51
|
|
|
|
52
|
|
|
// Mutable attributes |
53
|
|
|
private @Nullable FightCell cell; |
54
|
|
|
private @MonotonicNonNull Fight fight; |
55
|
|
|
private @Nullable FightTurn turn; |
56
|
1 |
|
private Direction orientation = Direction.SOUTH_EAST; |
57
|
1 |
|
private boolean hidden = false; |
58
|
|
|
|
59
|
|
|
@Override |
60
|
|
|
public void init() { |
61
|
1 |
|
if (fight == null) { |
62
|
1 |
|
throw new IllegalStateException("The fighter should join the fight before initialisation"); |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
fight.dispatch(new FighterInitialized(this)); |
66
|
1 |
|
} |
67
|
|
|
|
68
|
|
|
@Override |
69
|
|
|
public final FightCell cell() { |
70
|
1 |
|
if (cell == null) { |
71
|
1 |
|
throw new IllegalStateException("The fighter is not on a cell"); |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
return cell; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
@Override |
78
|
|
|
public final Direction orientation() { |
79
|
1 |
|
return orientation; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
@Override |
83
|
|
|
public final void setOrientation(Direction orientation) { |
84
|
1 |
|
this.orientation = orientation; |
85
|
1 |
|
} |
86
|
|
|
|
87
|
|
|
@Override |
88
|
|
|
public final void move(@Nullable FightCell cell) { |
89
|
1 |
|
if (this.cell != null) { |
90
|
1 |
|
this.cell.removeFighter(); |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
if (cell != null) { |
94
|
1 |
|
cell.set(this); |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
this.cell = cell; |
98
|
1 |
|
} |
99
|
|
|
|
100
|
|
|
@Override |
101
|
|
|
public void dispatch(Object event) { |
102
|
1 |
|
dispatcher.dispatch(event); |
103
|
1 |
|
} |
104
|
|
|
|
105
|
|
|
public final ListenerAggregate dispatcher() { |
106
|
1 |
|
return dispatcher; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
@Override |
110
|
|
|
public final BuffList buffs() { |
111
|
1 |
|
return buffs; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
@Override |
115
|
|
|
public final States states() { |
116
|
1 |
|
return states; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
@Override |
120
|
|
|
public final Fight fight() { |
121
|
1 |
|
if (fight == null) { |
122
|
1 |
|
throw new IllegalStateException("The fighter has not join a fight"); |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
return fight; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
@Override |
129
|
|
|
public final void joinFight(Fight fight, FightCell startCell) { |
130
|
1 |
|
if (this.fight != null) { |
131
|
1 |
|
throw new IllegalStateException("A fight is already defined"); |
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
this.fight = fight; |
135
|
1 |
|
this.cell = startCell; |
136
|
1 |
|
startCell.set(this); |
137
|
1 |
|
} |
138
|
|
|
|
139
|
|
|
@Override |
140
|
|
|
public void play(FightTurn turn) { |
141
|
1 |
|
this.turn = turn; |
142
|
1 |
|
} |
143
|
|
|
|
144
|
|
|
@Override |
145
|
|
|
public void stop() { |
146
|
1 |
|
turn = null; |
147
|
1 |
|
} |
148
|
|
|
|
149
|
|
|
@Override |
150
|
|
|
public final FightTurn turn() { |
151
|
1 |
|
if (turn == null) { |
152
|
1 |
|
throw new FightException("It's not your turn"); |
153
|
|
|
} |
154
|
|
|
|
155
|
1 |
|
return turn; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
@Override |
159
|
|
|
public final void perform(Consumer<FightTurn> action) { |
160
|
1 |
|
final FightTurn turn = this.turn; |
|
|
|
|
161
|
|
|
|
162
|
1 |
|
if (turn != null) { |
163
|
1 |
|
action.accept(turn); |
164
|
|
|
} |
165
|
1 |
|
} |
166
|
|
|
|
167
|
|
|
@Override |
168
|
|
|
public boolean isPlaying() { |
169
|
1 |
|
return turn != null && turn.active(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
@Override |
173
|
|
|
public final void attach(Object key, Object value) { |
174
|
1 |
|
attachments.put(key, value); |
175
|
1 |
|
} |
176
|
|
|
|
177
|
|
|
@Override |
178
|
|
|
public final @Nullable Object attachment(Object key) { |
179
|
1 |
|
return attachments.get(key); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
@Override |
183
|
|
|
public final boolean isOnFight() { |
184
|
1 |
|
return fight != null && cell != null; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
@Override |
188
|
|
|
public boolean hidden() { |
189
|
1 |
|
return hidden; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
@Override |
193
|
|
|
public void setHidden(Fighter caster, boolean hidden) { |
194
|
1 |
|
if (this.hidden == hidden) { |
195
|
1 |
|
return; |
196
|
|
|
} |
197
|
|
|
|
198
|
1 |
|
this.hidden = hidden; |
199
|
|
|
|
200
|
1 |
|
if (fight != null) { |
201
|
1 |
|
fight.dispatch(hidden ? new FighterHidden(this, caster) : new FighterVisible(this, caster)); |
202
|
|
|
} |
203
|
1 |
|
} |
204
|
|
|
|
205
|
|
|
@Override |
206
|
|
|
public final boolean equals(@Nullable Object obj) { |
207
|
1 |
|
if (obj == this) { |
208
|
1 |
|
return true; |
209
|
|
|
} |
210
|
|
|
|
211
|
1 |
|
if (!(obj instanceof FighterData)) { |
212
|
1 |
|
return false; |
213
|
|
|
} |
214
|
|
|
|
215
|
1 |
|
return id() == ((FighterData) obj).id(); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
@Override |
219
|
|
|
public final int hashCode() { |
220
|
1 |
|
return id(); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|