/
Sprite.java
122 lines (108 loc) · 2.77 KB
/
Sprite.java
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
import blayzeTechUtils.env.*;
import blayzeTechUtils.math.*;
import java.util.*;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.util.concurrent.ThreadLocalRandom;
public class Sprite extends TPPolygonEntity {
private BufferedImage img;
private int xOffset = 0, yOffset = 0;
private int columns = 1, rows = 1;
private int frameWidth, frameHeight;
private int renderWidth, renderHeight;
private float fps = 24;
private long lastTime = System.currentTimeMillis();
private int interval = 42;
private int totalFrames = 1; // Storing this for speed reasons.
private boolean centered = true;
public int frame = 0;
public boolean play = true;
public boolean playOnce = false;
public boolean markedForDeath = false;
public Sprite(double x, double y, BufferedImage img, int columns, int rows, float fps)
{
super(x,y);
this.img = img;
this.columns = columns;
this.rows = rows;
this.fps = fps;
this.frameWidth = img.getWidth()/columns;
this.frameHeight = img.getHeight()/rows;
this.interval = (int)(1000/fps);
this.totalFrames = columns*rows;
this.renderWidth = frameWidth;
this.renderHeight = frameHeight;
center(true);
}
public Sprite(double x, double y, String path, int columns, int rows, float fps)
{
this(x, y, loadImage(path), columns, rows, fps);
}
public Sprite(double x, double y, String path)
{
this(x, y, loadImage(path), 1, 1, (float)24);
}
public Sprite(double x, double y, BufferedImage img)
{
this(x, y, img, 1, 1, (float)24);
}
public void center()
{
center(true);
}
public void setRenderDims(int w, int h)
{
this.renderWidth = w;
this.renderHeight = h;
center(centered);
}
public void center(boolean c)
{
this.clearPoints();
PolygonEntities.addRectangleTo(this, renderWidth, renderHeight, c);
centered = c;
}
@Override
public void draw(Graphics2D g)
{
int x = frame%columns;
int y = frame/columns;
Point renderStart = this.getGlobalPoint(0);
Point renderEnd = this.getGlobalPoint(2);
g.drawImage(img, (int)renderStart.getX(), (int)renderStart.getY(),
(int)renderEnd.getX(), (int)renderEnd.getY(),
x*frameWidth, y*frameHeight, (x+1)*frameWidth, (y+1)*frameHeight, null);
}
public void update(long time)
{
if(play)
{
if(time - lastTime > interval)
{
frame = (frame+1)%totalFrames;
if(playOnce && frame == totalFrames-1)
play = false;
lastTime = time;
}
}else{
lastTime = time;
}
}
public static BufferedImage loadImage(String path)
{
BufferedImage img = null;
try
{
img = ImageIO.read(new File(path));
}
catch (IOException e)
{
System.out.println("Error Loading file \""+path+"\":");
System.out.println(e);
}
return img;
}
}