-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSprite.java
More file actions
212 lines (191 loc) · 4.38 KB
/
Sprite.java
File metadata and controls
212 lines (191 loc) · 4.38 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
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
/**
* This class represents a graphic or animation that can be drawn on the screen.
*
* @author John Kurlak (kurlak)
* Weston Thayer (weston5)
* Panhavorn Hok (onehok1)
* @version 12.07.09
*/
public abstract class Sprite
{
private Point position;
private Image[] images;
private int firstIndex;
private int lastIndex;
private int currentIndex;
private int width;
private int height;
private boolean isAlive = true;
/**
* This constructor creates a new sprite that can be drawn on the screen.
*
* @param x The starting x position of the sprite
* @param y The starting y position of the sprite
* @param spriteImages The images representing the sprite
* @param firstFrame The index of the first frame to be drawn during
* animation
* @param lastFrame The index of the last frame to be drawn during
* animation
*/
public Sprite(int x, int y, Image[] spriteImages, int firstFrame, int
lastFrame)
{
position = new Point(x, y);
images = spriteImages;
setRange(firstFrame, lastFrame);
width = images[0].getWidth(null);
height = images[0].getHeight(null);
}
/**
* This method iterates the sprite through its animation, if any.
*
* @return Returns true if there are following animations and false if it
* has completed its animation
*/
public boolean iterate()
{
if (currentIndex < lastIndex)
{
currentIndex++;
return true;
}
return false;
}
/**
* This method sets the range of animation for the sprite.
*
* @param firstFrame The start of the animation's range
* @param lastFrame The end of the animation's range
*/
public void setRange(int firstFrame, int lastFrame)
{
firstIndex = firstFrame;
lastIndex = lastFrame;
currentIndex = firstIndex;
}
/**
* This method draws the sprite on the screen.
*
* @param g The graphics object to which we can draw
*/
public void draw(Graphics g)
{
if (GameState.getCurrentState() != GameState.GAME_PAUSED)
{
updatePosition();
}
g.drawImage(images[currentIndex], position.x, position.y, null);
}
/**
* This method sets the current index of the animation, if any.
*
* @param index The current index of the animation
*/
public void setCurrentIndex(int index)
{
currentIndex = index;
}
/**
* This method removes a sprite.
*/
public void remove()
{
isAlive = false;
}
/**
* This method determines whether or not a sprite still exists.
*
* @return Returns true if the sprite exists and false if not
*/
public boolean isAlive()
{
return isAlive;
}
/**
* This method resurrects a sprite. ZOMBIEZZZ!!!1!
*/
public void makeAlive()
{
isAlive = true;
}
/**
* This method offsets the x position of the sprite.
*
* @param amount The number of pixels to offset the sprite by
*/
public void offsetX(int amount)
{
position.x += amount;
}
/**
* This method offsets the y position of the sprite.
*
* @param amount The number of pixels to offset the sprite by
*/
public void offsetY(int amount)
{
position.y += amount;
}
/**
* This method sets the x position of the sprite.
*
* @param xPos The x position to set the sprite to
*/
public void setX(int xPos)
{
position.x = xPos;
}
/**
* This method sets the y position of the sprite.
*
* @param yPos The y position to set the sprite to
*/
public void setY(int yPos)
{
position.y = yPos;
}
/**
* This method gets the x position of the sprite.
*
* @return Returns the x position
*/
public int getX()
{
return position.x;
}
/**
* This method gets the y position of the sprite.
*
* @return Returns the y position
*/
public int getY()
{
return position.y;
}
/**
* This method gets the width of the sprite.
*
* @return Returns the width in pixels
*/
public int getWidth()
{
return width;
}
/**
* This method gets the height of the sprite.
*
* @return Returns the height in pixels
*/
public int getHeight()
{
return height;
}
/**
* This method requires that children of Sprite implement an update position
* method.
*/
public abstract void updatePosition();
}