1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
package ch.asynk.gdx.boardgame.animations;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.ObjectMap;
import com.badlogic.gdx.utils.Pool;
import ch.asynk.gdx.boardgame.FramedSprite;
import ch.asynk.gdx.boardgame.Piece;
public class FireAnimation implements Animation, Pool.Poolable
{
public static class Config
{
public int burstCount;
public float burstDelay;
public float maxFireDelay;
public float maxFireScattering;
public float shellSpeed;
public float smokeDuration;
public float explosionDuration;
public FramedSprite shellSprites;
public FramedSprite explosionSprites;
public Sound shellFireSnd;
public Sound explosionSnd;
public Config(int burstCount, float burstDelay,
float maxFireDelay, float maxFireScattering,
float shellSpeed, float smokeDuration, float explosionDuration,
FramedSprite shellSprites, FramedSprite explosionSprites,
Sound shellFireSnd, Sound explosionSnd)
{
this.burstCount = burstCount;
this.burstDelay = burstDelay;
this.maxFireDelay = maxFireDelay;
this.maxFireScattering = maxFireScattering;
this.shellSpeed = shellSpeed;
this.smokeDuration = smokeDuration;
this.explosionDuration = explosionDuration;
this.shellSprites = shellSprites;
this.explosionSprites = explosionSprites;
this.shellFireSnd = shellFireSnd;
this.explosionSnd = explosionSnd;
}
}
private static ObjectMap<String, Config> configs = new ObjectMap<String, Config>();
public static void register(final String name,
int burstCount,
float burstDelay,
float maxFireDelay,
float maxFireScattering,
float shellSpeed,
float smokeDuration,
float explosionDuration,
final Texture shellTexture, int shellR, int shellC,
final Texture explosionTexture, int explosionR, int explosionC,
final Sound shellFireSnd,
final Sound explosionSnd
)
{
Config cfg = new Config(burstCount, burstDelay,
maxFireDelay, maxFireScattering,
shellSpeed, smokeDuration, (explosionTexture == null ? 0f : explosionDuration),
(shellTexture == null ? null : new FramedSprite(shellTexture, shellR, shellC)),
(explosionTexture == null ? null : new FramedSprite(explosionTexture, explosionR, explosionC)),
shellFireSnd, explosionSnd
);
configs.put(name, cfg);
}
public static void free()
{
// for(String key : configs.keySet()) {
// Config cfg = configs.get(key);
// }
configs.clear();
}
private static final Pool<FireAnimation> shellFireAnimationPool = new Pool<FireAnimation>()
{
@Override protected FireAnimation newObject()
{
return new FireAnimation();
}
};
public static Vector2 sv = new Vector2();
public static Vector2 tv = new Vector2();
public static FireAnimation obtain(final String configName, Piece shooter, Piece target)
{
shooter.getFireingPoint(sv, target);
target.getImpactPoint(tv);
return obtain(configName, sv.x, sv.y, tv.x, tv.y);
}
public static FireAnimation obtain(final String configName, float x0, float y0, float x1, float y1)
{
FireAnimation a = shellFireAnimationPool.obtain();
Config cfg = configs.get(configName);
if (cfg == null) {
throw new RuntimeException("FireAnimation : no configuration named : '" + configName + "'");
}
a.compute(cfg, x0, y0, x1, y1);
return a;
}
@Override public void reset()
{
this.shot = null;
this.shots = null;
}
@Override public void dispose()
{
if (this.shot != null) {
this.shot.dispose();
}
if (this.shots != null) {
for (ShotAnimation shot : shots) {
shot.dispose();
}
}
shellFireAnimationPool.free(this);
}
private boolean single;
private ShotAnimation shot;
private ShotAnimation[] shots;
private FireAnimation()
{
}
private void compute(Config cfg, float x0, float y0, float x1, float y1)
{
this.single = (cfg.burstCount == 1);
if (single) {
this.shot = ShotAnimation.obtain();
this.shot.compute(0, cfg, x0, y0, x1, y1);
} else {
this.shots = new ShotAnimation[cfg.burstCount];
for (int i = 0; i < cfg.burstCount; i++) {
this.shots[i] = ShotAnimation.obtain();
this.shots[i].compute(i, cfg, x0, y0, x1, y1);
}
}
}
@Override public boolean completed()
{
if (single) {
return this.shot.completed();
} else {
boolean completed = true;
for (ShotAnimation shot : this.shots) {
completed &= shot.completed();
}
return completed;
}
}
@Override public boolean animate(float delta)
{
if (single) {
return this.shot.animate(delta);
} else {
boolean completed = true;
for (ShotAnimation shot : this.shots) {
completed &= shot.animate(delta);
}
return completed;
}
}
@Override public void draw(Batch batch)
{
if (single) {
this.shot.draw(batch);
} else {
for (ShotAnimation shot : this.shots) {
shot.draw(batch);
}
}
}
@Override public void drawDebug(ShapeRenderer shapeRenderer)
{
if (single) {
this.shot.drawDebug(shapeRenderer);
} else {
for (ShotAnimation shot : this.shots) {
shot.drawDebug(shapeRenderer);
}
}
}
}
|