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.fight.castable.effect.buff; |
21
|
|
|
|
22
|
|
|
import fr.quatrevieux.araknemu.game.fight.castable.CastScope; |
23
|
|
|
import fr.quatrevieux.araknemu.game.fight.castable.effect.handler.damage.Damage; |
24
|
|
|
import fr.quatrevieux.araknemu.game.fight.fighter.ActiveFighter; |
25
|
|
|
import fr.quatrevieux.araknemu.game.fight.fighter.Fighter; |
26
|
|
|
import fr.quatrevieux.araknemu.game.fight.fighter.PassiveFighter; |
27
|
|
|
import fr.quatrevieux.araknemu.network.game.out.fight.AddBuff; |
28
|
|
|
|
29
|
|
|
import java.util.Collection; |
30
|
|
|
import java.util.Iterator; |
31
|
|
|
import java.util.LinkedList; |
32
|
|
|
import java.util.function.Predicate; |
33
|
|
|
import java.util.stream.Stream; |
34
|
|
|
import java.util.stream.StreamSupport; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Handle buff list for a fighter |
38
|
|
|
*/ |
39
|
|
|
public final class BuffList implements Iterable<Buff>, Buffs { |
40
|
|
|
private final Fighter fighter; |
41
|
1 |
|
private final Collection<Buff> buffs = new LinkedList<>(); |
42
|
|
|
|
43
|
1 |
|
public BuffList(Fighter fighter) { |
44
|
1 |
|
this.fighter = fighter; |
45
|
1 |
|
} |
46
|
|
|
|
47
|
|
|
@Override |
48
|
|
|
public Iterator<Buff> iterator() { |
49
|
1 |
|
return buffs.iterator(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get the Buff stream |
54
|
|
|
*/ |
55
|
|
|
public Stream<Buff> stream() { |
56
|
1 |
|
return StreamSupport.stream(spliterator(), false); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
@Override |
60
|
|
|
public void add(Buff buff) { |
61
|
1 |
|
buffs.add(buff); |
62
|
1 |
|
buff.hook().onBuffStarted(buff); |
63
|
|
|
|
64
|
1 |
|
if (buff.remainingTurns() == 0) { |
65
|
1 |
|
buff.incrementRemainingTurns(); |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
fighter.fight().send(new AddBuff(buff)); |
69
|
|
|
|
70
|
|
|
// Add one turn on self-buff |
71
|
1 |
|
if (fighter.equals(buff.caster())) { |
72
|
1 |
|
buff.incrementRemainingTurns(); |
73
|
|
|
} |
74
|
1 |
|
} |
75
|
|
|
|
76
|
|
|
@Override |
77
|
|
|
public boolean onStartTurn() { |
78
|
1 |
|
boolean result = true; |
79
|
|
|
|
80
|
1 |
|
for (Buff buff : buffs) { |
81
|
1 |
|
result &= buff.hook().onStartTurn(buff); |
82
|
1 |
|
} |
83
|
|
|
|
84
|
1 |
|
return result; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
@Override |
88
|
|
|
public void onEndTurn() { |
89
|
1 |
|
for (Buff buff : buffs) { |
90
|
1 |
|
buff.hook().onEndTurn(buff); |
91
|
1 |
|
} |
92
|
1 |
|
} |
93
|
|
|
|
94
|
|
|
@Override |
95
|
|
|
public void onCastTarget(CastScope cast) { |
96
|
1 |
|
for (Buff buff : buffs) { |
97
|
1 |
|
buff.hook().onCastTarget(buff, cast); |
98
|
1 |
|
} |
99
|
1 |
|
} |
100
|
|
|
|
101
|
|
|
@Override |
102
|
|
|
public void onDirectDamage(ActiveFighter caster, Damage value) { |
103
|
1 |
|
for (Buff buff : buffs) { |
104
|
1 |
|
buff.hook().onDirectDamage(buff, caster, value); |
105
|
1 |
|
} |
106
|
1 |
|
} |
107
|
|
|
|
108
|
|
|
@Override |
109
|
|
|
public void onBuffDamage(Buff poison, Damage value) { |
110
|
1 |
|
for (Buff buff : buffs) { |
111
|
1 |
|
buff.hook().onBuffDamage(buff, poison, value); |
112
|
1 |
|
} |
113
|
1 |
|
} |
114
|
|
|
|
115
|
|
|
@Override |
116
|
|
|
public void onLifeAltered(int value) { |
117
|
1 |
|
for (Buff buff : buffs) { |
118
|
1 |
|
buff.hook().onLifeAltered(buff, value); |
119
|
1 |
|
} |
120
|
1 |
|
} |
121
|
|
|
|
122
|
|
|
@Override |
123
|
|
|
public void refresh() { |
124
|
1 |
|
removeIf(buff -> { |
125
|
1 |
|
buff.decrementRemainingTurns(); |
126
|
|
|
|
127
|
1 |
|
return !buff.valid(); |
128
|
|
|
}); |
129
|
1 |
|
} |
130
|
|
|
|
131
|
|
|
@Override |
132
|
|
|
public boolean removeAll() { |
133
|
1 |
|
return removeIf(Buff::canBeDispelled); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
@Override |
137
|
|
|
public boolean removeByCaster(PassiveFighter caster) { |
138
|
1 |
|
return removeIf(buff -> buff.caster().equals(caster)); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Remove buff by a predicate |
143
|
|
|
* |
144
|
|
|
* @param predicate Takes the buff as parameter, and return true to delete (and terminate) the buff |
145
|
|
|
* |
146
|
|
|
* @return true if there is a change (i.e. a buff is terminated) |
147
|
|
|
*/ |
148
|
|
|
private boolean removeIf(Predicate<Buff> predicate) { |
149
|
1 |
|
final Iterator<Buff> iterator = buffs.iterator(); |
150
|
|
|
|
151
|
1 |
|
boolean hasChanged = false; |
152
|
|
|
|
153
|
1 |
|
while (iterator.hasNext()) { |
154
|
1 |
|
final Buff buff = iterator.next(); |
155
|
|
|
|
156
|
1 |
|
if (predicate.test(buff)) { |
157
|
1 |
|
iterator.remove(); |
158
|
1 |
|
buff.hook().onBuffTerminated(buff); |
159
|
1 |
|
hasChanged = true; |
160
|
|
|
} |
161
|
1 |
|
} |
162
|
|
|
|
163
|
1 |
|
return hasChanged; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|