-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSprite.java
170 lines (149 loc) · 3.4 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
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
/*
* File: Sprite.java
* Author: Luke S. Snyder
* UA ID: 010691128
*/
import java.awt.Graphics;
import java.awt.Image;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
@SuppressWarnings("serial")
class Sprite implements Serializable
{
private int locationX;
private int locationY;
private int velocityX;
private int velocityY;
private int span;
private boolean hasCollided;
private static int height;
private static Image beamImage;
private static Image missleImage;
private static Image enemyImage;
private static Image bossImage;
private static Image playerImage;
// Read in all images.
public static void initialize(int height)
{
Sprite.height = height;
try
{
beamImage = ImageIO.read(new File("beam.png"));
missleImage = ImageIO.read(new File("missle.png"));
// enemyImage = ImageIO.read(new File("hamster.png"));
enemyImage = ImageIO.read(new File("enemy.png"));
// bossImage = ImageIO.read(new File("evilHamster.png"));
bossImage = ImageIO.read(new File("boss.png"));
playerImage = ImageIO.read(new File("spaceship.png"));
}
catch(IOException e)
{
System.out.println("Unable to load image file.");
}
}
// Image getters and setters.
public static Image getBeamImage() { return beamImage; }
public static Image getMissleImage() { return missleImage; }
public static Image getEnemyImage() { return enemyImage; }
public static Image getBossImage() { return bossImage; }
public static Image getPlayerImage() { return playerImage; }
public static void setBossImage(String filePath)
{
try
{
bossImage = ImageIO.read(new File(filePath));
}
catch(IOException e)
{
System.out.println("Unable to load image file.");
}
}
public static void setPlayerImage(String filePath)
{
try
{
playerImage = ImageIO.read(new File(filePath));
}
catch(IOException e)
{
System.out.println("Unable to load image file.");
}
}
public int getX() { return locationX; }
public int getY() { return locationY; }
public void setX(int x) { locationX = x; }
public void setY(int y) { locationY = y; }
public void updateImage(Graphics g) { }
public void tick() { }
// Determine if invoking sprite overlaps another sprite.
public boolean overlaps(Sprite s)
{
if (getRightSide() < s.getLeftSide())
return false;
if (getLeftSide() > s.getRightSide())
return false;
if (getTopSide() > s.getBottomSide())
return false;
if (getBottomSide() < s.getTopSide())
return false;
return true;
}
// Determine if sprite has escaped.
public boolean hasEscaped()
{
if (getY() > height)
return true;
return false;
}
// Getters and setters.
public void setHasCollided(boolean hasCollided)
{
this.hasCollided = hasCollided;
}
public boolean hasCollided()
{
return hasCollided;
}
public void setSpan(int span)
{
this.span = span;
}
public int getSpan()
{
return span;
}
public int getTopSide()
{
return getY();
}
public int getBottomSide()
{
return getY() + getSpan();
}
public int getLeftSide()
{
return getX();
}
public int getRightSide()
{
return getX() + getSpan();
}
public int getVelocityX()
{
return velocityX;
}
public void setVelocityX(int velocityX)
{
this.velocityX = velocityX;
}
public int getVelocityY()
{
return velocityY;
}
public void setVelocityY(int velocityY)
{
this.velocityY = velocityY;
}
}