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