-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSprite.java
More file actions
789 lines (675 loc) · 18.6 KB
/
Sprite.java
File metadata and controls
789 lines (675 loc) · 18.6 KB
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
import java.awt.*; // Using AWT's Graphics and Color
import java.awt.image.*;
import javax.imageio.ImageIO;
import java.io.File;
import java.awt.geom.AffineTransform;
import java.util.HashMap;
class Sprite
{
enum BorderAction {NONE, BOUNCE, STOP, WARP};
// Simple Sprite Class
// Default location and acceleration
private double x = 0.0;
private double y = 0.0;
private double dx = 0.0;
private double dy = 0.0;
private double ddx = 0.0;
private double ddy = 0.0;
private double heading = 0.0;
private double dh = 0.0;
private String type = "";
// Default sizes
private int width = 24;
private int height = 24;
private int size = 24;
// Initial state
private boolean boundingBox = false;
private boolean isVisible = true;
private BorderAction borderAction = BorderAction.NONE;
private boolean hasPhysics = true;
private Color color = Color.BLUE;
private boolean active = true;
// Image
private String filename;
// Currently Displayed Image
private BufferedImage image = null;
// Original Image (used for resizing / rotating)
private BufferedImage originalImage = null;
// Images For Sprites that are reused
private static HashMap<String, BufferedImage> imageMap = new HashMap<>();
/**
* Class constructor specifying x and y location.
* @param x the x location
* @param y the y location
*/
Sprite(double x, double y)
{
this.x = x;
this.y = y;
// this.initialize();
}
/**
* Class constructor specifying x and y location and filename of sprite image.
* @param filename the name of the file
* @param x the x location
* @param y the y location
*/
Sprite(double x, double y, String filename)
{
this.x = x;
this.y = y;
this.filename = filename;
this.setImage(filename);
}
/**
* Set the image of a sprite.
* @param filename the name of the file
*/
public void setImage(String filename)
{
try
{
// Check if image in imageMap
if(imageMap.containsKey(filename))
{
if(this.originalImage != imageMap.get(filename))
{
this.image = imageMap.get(filename);
this.originalImage = imageMap.get(filename);
this.width = this.image.getWidth();
this.height = this.image.getHeight();
this.size = (this.width + this.height) / 2;
// System.out.print(imageMap.size() + " ");
}
}
else
{
BufferedImage image = ImageIO.read(new File(filename));
this.image = image;
this.originalImage = image;
this.width = image.getWidth();
this.height = image.getHeight();
this.size = (this.width + this.height) / 2;
// System.out.println(this.width + " x " + this.height);
imageMap.put(filename, image);
}
}
catch(Exception e)
{
}
}
/**
* Move a sprite to x and y coordinates.
* @param x the x coordinate
* @param y the y coordinate
*/
public void goTo(int x, int y)
{
this.x = x;
this.y = y;
}
/**
* Move a sprite to x and y coordinates.
* @param x the x coordinate
* @param y the y coordinate
*/
public void goTo(double x, double y)
{
this.x = x;
this.y = y;
}
/**
* Move a sprite to x and y coordinates (center of sprite).
* @param x the x coordinate
* @param y the y coordinate
*/
public void goToCentered(int x, int y)
{
this.x = x - this.width/2;
this.y = y - this.height/2;
}
/**
* Move a sprite to x and y coordinates (center of sprite).
* @param x the x coordinate
* @param y the y coordinate
*/
public void goToCentered(double x, double y)
{
this.x = x - this.width/2.0;
this.y = y - this.height/2.0;
}
/**
* Set the x coordinate.
* @param x the x coordinate
*/
public void setX(int x)
{
this.x = x;
}
/**
* Set the x coordinate.
* @param x the x coordinate
*/
public void setX(double x)
{
this.x = x;
}
/**
* Get the x coordinate.
* @return the x coordinate
*/
public double getX()
{
return this.x;
}
/**
* Set the y coordinate.
* @param y the y coordinate
*/
public void setY(int y)
{
this.y = y;
}
/**
* Set the y coordinate.
* @param y the y coordinate
*/
public void setY(double y)
{
this.y = y;
}
/**
* Get the y coordinate.
* @return the y coordinate
*/
public double getY()
{
return this.y;
}
/**
* Get the current heading.
* @return the heading in degrees
*/
public double getHeading()
{
return this.heading;
}
/**
* Get the type.
* @return the type
*/
public String getType()
{
return this.type;
}
/**
* Set the type.
* @param type the type
*/
public void setType(String type)
{
this.type = type;
}
/**
* Set delta x.
* @param dx delta x
*/
public void setDX(int dx)
{
this.dx = dx;
}
/**
* Set delta x.
* @param dx delta x
*/
public void setDX(double dx)
{
this.dx = dx;
}
/**
* Set delta y.
* @param dy delta y
*/
public void setDY(int dy)
{
this.dy = dy;
}
/**
* Set delta y.
* @param dy delta y
*/
public void setDY(double dy)
{
this.dy = dy;
}
/**
* Set delta delta x.
* @param ddx delta delta x
*/
public void setDDX(double ddx)
{
this.ddx = ddx;
}
/**
* Set delta delta y.
* @param ddy delta delta y
*/
public void setDDY(double ddy)
{
this.ddy = ddy;
}
/**
* Set heading in degrees.
* @param heading heading
*/
public void setHeading(int heading)
{
this.heading = heading;
rotate(heading);
}
/**
* Set heading in degrees.
* @param heading heading
*/
public void setHeading(double heading)
{
this.heading = heading;
rotate(heading);
}
/**
* Set delta heading in degrees.
* @param dh delta heading
*/
public void setDH(int dh)
{
this.dh = dh;
}
/**
* Set delta heading in degrees.
* @param dh delta heading
*/
public void setDH(double dh)
{
this.dh = dh;
}
/**
* Set whether or not the sprite has physics (dx, dy, gravity, collisions, etc).
* @param hasPhysics has physics
*/
public void setHasPhysics(boolean hasPhysics)
{
this.hasPhysics = hasPhysics;
}
/**
* Get whether or not the sprite has physics.
* @return hasPhysics
*/
public boolean getHasPhysics()
{
return this.hasPhysics;
}
/**
* Get delta x
* @return dx
*/
public double getDX()
{
return this.dx;
}
/**
* Get delta y
* @return dy
*/
public double getDY()
{
return this.dy;
}
/**
* Get delta delta x (acceleration x)
* @return ddx
*/
public double getDDX()
{
return this.ddx;
}
/**
* Get delta delta y (acceleration y)
* @return ddy
*/
public double getDDY()
{
return this.ddy;
}
/**
* Get sprite width
* @return width
*/
public int getWidth()
{
return this.width;
}
/**
* Get sprite height
* @return height
*/
public int getHeight()
{
return this.height;
}
/**
* Set height in pixels.
* @param height height in pixels
*/
public void setHeight(int height)
{
this.height = height;
}
/**
* Set width in pixels.
* @param width width in pixels
*/
public void setWidth(int width)
{
this.width = width;
}
/**
* Set border action: NONE, BOUNCE, STOP, WARP.
* @param borderAction Sprite.BorderAction.____
*/
public void setBorderAction(BorderAction borderAction)
{
this.borderAction = borderAction;
}
/**
* Set color.
* @param color Color object
* @see java.awt.Color
*/
public void setColor(Color color)
{
this.color = color;
}
/**
* Set whether or not to show a red bounding box around the sprite. Useful for debugging.
* @param boundingBox true / false
*/
public void setBoundingBox(boolean boundingBox)
{
this.boundingBox = boundingBox;
}
/**
* Renders the sprite on a Graphics object, offset with a Camera object, canvas width, and canvas height.
* @param g Graphics object
* @param camera Camera object
* @param canvasWidth width of the canvas (used for not rendering off canvas objects and border action)
* @param canvasHeight height of the canvas (used for not rendering off canvas objects and border action)
* @see Graphics
* @see Camera
*/
public void render(Graphics g, Camera camera, int canvasWidth, int canvasHeight)
{
if(!active)
{
return;
}
// Check if visible
if(this.isVisible)
{
int renderX = (int)this.x;
int renderY = (int)this.y;
// If there is a camera, add camera offset
if(camera != null)
{
renderX = renderX - camera.getX() + (canvasWidth / 2);
renderY = renderY - camera.getY() + (canvasHeight / 2);
}
// Check if coordinates are on the visible screen
// If not, do not render
if(renderX < -this.width || renderX > canvasWidth)
{
return;
}
if(renderY < -this.height || renderY > canvasHeight)
{
return;
}
if(!(image==null))
{
// Render image
g.drawImage(image, renderX , renderY, null);
}
else
{
// Render rectangle (default)
g.setColor(color);
g.fillRect(renderX, renderY, width, height);
}
// Render bounding box
if(this.boundingBox)
{
g.setColor(Color.RED);
g.drawLine(renderX, renderY, (renderX + this.width), renderY);
g.drawLine(renderX, renderY, renderX, (renderY + this.height));
g.drawLine((renderX + this.width), renderY, (renderX + this.width), (renderY + this.height));
g.drawLine(renderX, (renderY + this.height), (renderX + this.width), (renderY + this.height));
}
}
}
/**
* Updates sprite attributes based on physics (dx, dy)
* @param canvasWidth canvas width used for border action
* @param canvasHeight canvas height used for border action
* @param dt delta time
*/
public void update(int canvasWidth, int canvasHeight, double dt)
{
if(!active)
{
return;
}
if(hasPhysics)
{
dx += ddx;
dy -= ddy;
x += dx * dt;
y += dy * dt;
heading += dh * dt;
// System.out.println(dh + " " + heading);
if(dh!=0)
{
rotate(heading);
}
// bounce
if(this.borderAction == BorderAction.BOUNCE)
{
if (x > canvasWidth - this.width)
{
x = canvasWidth - this.width;
dx = -dx;
}
else if (x < 0)
{
x = 0;
dx = -dx;
}
if (y > canvasHeight - this.height)
{
y = canvasHeight - this.height;
dy = -dy;
}
else if (y < 0)
{
y = 0;
dy = -dy;
}
}
else if(this.borderAction == BorderAction.WARP)
{
if (this.x > canvasWidth)
{
this.x = -this.width;
}
else if (this.y > canvasHeight)
{
this.y = -this.height;
}
else if (this.x < -this.width)
{
this.x = canvasWidth;
}
else if (this.y < -this.height)
{
this.y = canvasHeight;
}
}
else if(this.borderAction == BorderAction.STOP)
{
if (x > canvasWidth - this.width)
{
x = canvasWidth - this.width;
dx = 0;
}
else if (x < 0)
{
x = 0;
dx = 0;
}
if (y > canvasHeight - this.height)
{
y = canvasHeight - this.height;
dy = 0;
}
else if (y < 0)
{
y = 0;
dy = 0;
}
}
}
}
/**
* Checks for a collision between two sprites using AABB collision detection
* @return true if a collision occurs
* @return false if no collision occurs
*/
public boolean isCollision(Sprite other)
{
// Do not register collision with self
if(this==other)
{
return false;
}
// Return false if it does not havePhysics
if(!hasPhysics)
{
return false;
}
boolean xOverlap = (this.x <= (other.x + other.width)) && ((this.x + this.width) >= other.x);
boolean yOverlap = (this.y <= (other.y + other.height)) && ((this.y + this.height) >= other.y);
return xOverlap && yOverlap;
}
/**
* Checks for a collision between two sprites using distance checking (based on sprite size)
* @return true if a collision occurs
* @return false if no collision occurs
*/
public boolean isCircleCollision(Sprite other)
{
double a = this.x - other.x;
double b = this.y - other.y;
double distance = Math.sqrt(a*a + b*b);
if(distance < ((this.size+other.size)/2.0))
{
return true;
}
return false;
}
/**
* Set whether or not a sprite is active. Non-active sprites are not rendered.
* @param active true / false
*/
public void setActive(boolean active)
{
this.active = active;
}
/**
* Resize a sprite.
* @param width width in pixels
* @param height in pixels
*/
public void resize(int width, int height)
{
Image tempImage = this.originalImage.getScaledInstance(width, height, BufferedImage.TYPE_INT_ARGB);
BufferedImage buffered = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
buffered.getGraphics().drawImage(tempImage, 0, 0 , null);
this.image = buffered;
this.width = width;
this.height = height;
}
/**
* Scale a sprite.
* @param ratio percent to scale (0.5 is half current size, 2.0 is twice current size)
*/
public void scale(double ratio)
{
this.width = (int)(this.width * ratio);
this.height = (int)(this.height * ratio);
if(this.image != null)
{
resize(this.width, this.height);
}
}
/**
* Rotate a sprite.
* @param angle angle in degrees to rotate the sprite from current heading.
*/
public void rotate(double angle) {
// REF: https://stackoverflow.com/questions/37758061/rotate-a-buffered-image-in-java
// This ensures positive is counterclockwise rotation
double centerX = this.x + (this.width/2);
double centerY = this.y + (this.height/2);
angle = -angle;
double rads = Math.toRadians(angle);
double sin = Math.abs(Math.sin(rads));
double cos = Math.abs(Math.cos(rads));
int w = this.originalImage.getWidth();
int h = this.originalImage.getHeight();
int newWidth = (int)(Math.floor(w * cos + h * sin));
int newHeight = (int)(Math.floor(h * cos + w * sin));
BufferedImage rotated = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = rotated.createGraphics();
AffineTransform at = new AffineTransform();
at.translate((newWidth - w) / 2, (newHeight - h) / 2);
int x = w / 2;
int y = h / 2;
at.rotate(rads, x, y);
g2d.setTransform(at);
g2d.drawImage(this.originalImage, 0, 0, null);
Color transparent = new Color(255, 255, 255, 0);
g2d.setColor(transparent);
g2d.drawRect(0, 0, newWidth - 1, newHeight - 1);
g2d.dispose();
// Update width and height and heading
this.width = rotated.getWidth();
this.height = rotated.getHeight();
this.image = rotated;
double xOffset = (this.x + this.width/2) - centerX;
double yOffset = (this.y + this.height/2) - centerY;
this.x -= xOffset;
this.y -= yOffset;
}
/**
* Show a sprite. Sets isVisible to true.
*/
public void show()
{
this.isVisible = true;
}
/**
* Hide a sprite. Sets isVisible to false.
*/
public void hide()
{
this.isVisible = false;
}
}