forked from Pomax/Pjs-2D-Game-Engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sprite.pde
executable file
·378 lines (332 loc) · 9.43 KB
/
Sprite.pde
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/**
* Sprites are fixed dimension animated 2D graphics,
* with separate images for each frame of animation.
*
* Sprites can be positioned, transformed (translated,
* scaled, rotated and flipped), and moved along a
* path either manually or automatically.
*/
class Sprite extends Positionable {
State state;
SpritePath path;
float halign=0, valign=0;
float halfwidth, halfheight;
// Sprites have a specific coordinate that acts as "anchor" when multiple
// sprites are used for a single actor. When swapping sprite A for sprite
// B, the two coordinates A(h/vanchor) and B(h/vanchor) line up to the
// same screen pixel.
float hanchor=0, vanchor=0;
// frame data
PImage[] frames; // sprite frames
int numFrames=0; // frames.length cache
float frameFactor=1; // determines that frame serving compression/dilation
boolean hflip = false; // draw horizontall flipped?
boolean vflip = false; // draw vertically flipped?
// animation properties
boolean visible = true; // should the sprite be rendered when draw() is called?
boolean animated = true; // is this sprite "alive"?
int frameOffset = 0; // this determines which frame the sprite synchronises on
int framesServed = 0;
/**
* Shortcut constructor, if the sprite has one frame
*/
Sprite(String spritefile) {
this(spritefile, 1, 1, 0, 0);
}
/**
* Shortcut constructor, to build a Sprite directly off of a sprite map image.
*/
Sprite(String spritefile, int rows, int columns) {
this(spritefile, rows, columns, 0, 0);
}
/**
* Shortcut constructor, to build a Sprite directly off of a sprite map image.
*/
Sprite(String spritefile, int rows, int columns, float xpos, float ypos) {
this(SpriteMapHandler.cutTiledSpritesheet(spritefile, columns, rows, true), xpos, ypos, true);
}
/**
* Full constructor.
*/
private Sprite(PImage[] _frames, float _xpos, float _ypos, boolean _visible) {
path = new SpritePath();
setFrames(_frames);
visible = _visible;
}
/**
* bind a state to this sprite
*/
void setState(State _state) {
state = _state;
}
/**
* Bind sprite frames and record the sprite dimensions
*/
void setFrames(PImage[] _frames) {
frames = _frames;
width = _frames[0].width;
halfwidth = width/2.0;
height = _frames[0].height;
halfheight = height/2.0;
numFrames = _frames.length;
}
/**
* Get the alpha channel for a pixel
*/
float getAlpha(float _x, float _y) {
int x = (int) _x;
int y = (int) _y;
PImage frame = frames[currentFrame];
return alpha(frame.get(x,y));
}
/**
* Get the number of frames for this sprite
*/
int getFrameCount() {
return numFrames;
}
/**
* Check whether this sprite is animated
* (i.e. still has frames left to draw).
*/
boolean isAnimated() {
return animated;
}
/**
* Align this sprite, by treating its
* indicated x/y align values as
* center point.
*/
void align(int _halign, int _valign) {
if(_halign == LEFT) { halign = halfwidth; }
else if(_halign == CENTER) { halign = 0; }
else if(_halign == RIGHT) { halign = -halfwidth; }
ox = halign;
if(_valign == TOP) { valign = halfheight; }
else if(_valign == CENTER) { valign = 0; }
else if(_valign == BOTTOM) { valign = -halfheight; }
oy = valign;
}
/**
* explicitly set the alignment
*/
void setAlignment(float x, float y) {
halign = x;
valign = y;
ox = x;
oy = y;
}
/**
* Indicate the sprite's anchor point
*/
void anchor(int _hanchor, int _vanchor) {
if(_hanchor == LEFT) { hanchor = 0; }
else if(_hanchor == CENTER) { hanchor = halfwidth; }
else if(_hanchor == RIGHT) { hanchor = width; }
if(_vanchor == TOP) { vanchor = 0; }
else if(_vanchor == CENTER) { vanchor = halfheight; }
else if(_vanchor == BOTTOM) { vanchor = height; }
}
/**
* explicitly set the anchor point
*/
void setAnchor(float x, float y) {
hanchor = x;
vanchor = y;
}
/**
* if set, sprites are not rotation-interpolated
* on paths, even if they're curved.
*/
void setNoRotation(boolean _noRotation) {
path.setNoRotation(_noRotation);
}
/**
* Flip all frames for this sprite horizontally.
*/
void flipHorizontal() {
hflip = !hflip;
for(PImage img: frames) {
img.loadPixels();
int[] pxl = new int[img.pixels.length];
int w = int(width), h = int(height);
for(int x=0; x<w; x++) {
for(int y=0; y<h; y++) {
pxl[x + y*w] = img.pixels[((w-1)-x) + y*w]; }}
img.pixels = pxl;
img.updatePixels();
}
}
/**
* Flip all frames for this sprite vertically.
*/
void flipVertical() {
vflip = !vflip;
for(PImage img: frames) {
img.loadPixels();
int[] pxl = new int[img.pixels.length];
int w = int(width), h = int(height);
for(int x=0; x<w; x++) {
for(int y=0; y<h; y++) {
pxl[x + y*w] = img.pixels[x + ((h-1)-y)*w]; }}
img.pixels = pxl;
img.updatePixels();
}
}
// -- draw methods
/**
* Set the frame offset to sync the sprite animation
* based on a different frame in the frame set.
*/
void setFrameOffset(int offset) {
frameOffset = offset;
}
/**
* Set the frame factor for compressing or dilating
* frame serving. Default is '1'. 0.5 will make the
* frame serving twice as fast, 2 will make it twice
* as slow.
*/
void setAnimationSpeed(float factor) {
frameFactor = factor;
}
/**
* Set the frame offset to sync the sprite animation
* based on a different frame in the frame set.
*/
void setPathOffset(int offset) {
path.setOffset(offset);
}
// private current frame counter
private int currentFrame;
/**
* Get the 'current' sprite frame.
*/
PImage getFrame() {
// update sprite based on path frames
currentFrame = getCurrentFrameNumber();
if (path.size()>0) {
float[] pathdata = path.getNextFrameInformation();
setScale(pathdata[2], pathdata[3]);
setRotation(pathdata[4]);
if(state!=null) {
state.setActorOffsets(pathdata[0], pathdata[1]);
state.setActorDimensions(width*sx, height*sy, halign*sx, valign*sy);
} else {
ox = pathdata[0];
oy = pathdata[1];
}
}
return frames[currentFrame];
}
/**
* Get the 'current' frame number. If this
* sprite is no longer alive (i.e. it reached
* the end of its path), it will return the
* number for the last frame in the set.
*/
int getCurrentFrameNumber() {
if(path.size() > 0 && !path.looping && framesServed == path.size()) {
if(state!=null) { state.finished(); }
animated = false;
return numFrames-1;
}
float frame = ((frameCount+frameOffset)*frameFactor) % numFrames;
framesServed++;
return (int)frame;
}
/**
* Set up the coordinate transformations
* and draw the sprite's "current" frame
* at the correct location.
*/
void draw() {
draw(0,0);
}
void draw(float px, float py) {
if (visible) {
PImage img = getFrame();
float imx = x + px + ox - halfwidth,
imy = y + py + oy - halfheight;
image(img, imx, imy);
}
}
// pass-through/unused
void draw(float _a, float _b, float _c, float _d) {
this.draw();
}
boolean drawableFor(float _a, float _b, float _c, float _d) {
return true;
}
void drawObject() {
println("ERROR: something called Sprite.drawObject instead of Sprite.draw.");
}
// check if coordinate overlaps the sprite.
boolean over(float _x, float _y) {
_x -= ox - halfwidth;
_y -= oy - halfheight;
return x <= _x && _x <= x+width && y <= _y && _y <= y+height;
}
// -- pathing informmation
void reset() {
if(path.size()>0) {
path.reset();
animated = true; }
framesServed = 0;
frameOffset = -frameCount;
}
void stop() {
animated=false;
}
/**
* this sprite may be swapped out if it
* finished running through its path animation.
*/
boolean mayChange() {
if(path.size()==0) {
return true;
}
return !animated; }
/**
* Set the path loop property.
*/
void setLooping(boolean v) {
path.setLooping(v);
}
/**
* Clear path information
*/
void clearPath() {
path = new SpritePath();
}
/**
* Add a path point
*/
void addPathPoint(float x, float y, float sx, float sy, float r, int duration)
{
path.addPoint(x, y, sx, sy, r, duration);
}
/**
* Set up a (linear interpolated) path from point [1] to point [2]
*/
void addPathLine(float x1, float y1, float sx1, float sy1, float r1,
float x2, float y2, float sx2, float sy2, float r2,
float duration)
// pointless comment to make the 11 arg functor look slightly less horrible
{
path.addLine(x1,y1,sx1,sy1,r1, x2,y2,sx2,sy2,r2, duration);
}
/**
* Set up a (linear interpolated) path from point [1] to point [2]
*/
void addPathCurve(float x1, float y1, float sx1, float sy1, float r1,
float cx1, float cy1,
float cx2, float cy2,
float x2, float y2, float sx2, float sy2, float r2,
float duration,
float slowdown_ratio)
// pointless comment to make the 16 arg functor look slightly less horrible
{
path.addCurve(x1,y1,sx1,sy1,r1, cx1,cy1,cx2,cy2, x2,y2,sx2,sy2,r2, duration, slowdown_ratio);
}
}