-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSprite.java
More file actions
executable file
·132 lines (113 loc) · 4.05 KB
/
Copy pathSprite.java
File metadata and controls
executable file
·132 lines (113 loc) · 4.05 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
/**
* @author Todd Pickell CISS 238
* Chapter
* Programming Challenge pg
*/
import java.awt.*;
import java.applet.*;
public class Sprite extends Object {
private ImageEntity entity;
protected Point2D pos;
protected Point2D vel;
protected double rotRate;
protected int currentState;
//constructor
Sprite(Applet a, Graphics2D g2d) {
entity = new ImageEntity(a);
entity.setGraphics(g2d);
entity.setAlive(false);
pos = new Point2D(0, 0);
vel = new Point2D(0, 0);
rotRate = 0.0;
currentState = 0;
}
//load bitmap file
public void load(String filename) {
entity.load(filename);
}
//perform affine transformations
public void transform() {
entity.setX(pos.X());
entity.setY(pos.Y());
entity.transform();
}
//draw the image
public void draw() {
entity.g2d.drawImage(entity.getImage(),entity.at,entity.applet);
}
//draw bounding rectangle around sprite
public void drawBounds(Color c) {
entity.g2d.setColor(c);
entity.g2d.draw(getBounds());
}
//update the position based on velocity
public void updatePosition() {
pos.setX(pos.X() + vel.X());
pos.setY(pos.Y() + vel.Y());
}
//methods related to automatic rotation factor
public double rotationRate() { return rotRate; }
public void setRotationRate(double rate) { rotRate = rate; }
public void updateRotation() {
setFaceAngle(faceAngle() + rotRate);
if (faceAngle() < 0)
setFaceAngle(360 - rotRate);
else if (faceAngle() > 360)
setFaceAngle(rotRate);
}
//generic sprite state variable (alive, dead, collided, etc)
public int state() { return currentState; }
public void setState(int state) { currentState = state; }
//returns a bounding rectangle
public Rectangle getBounds() { return entity.getBounds(); }
//sprite position
public Point2D position() { return pos; }
public void setPosition(Point2D pos) { this.pos = pos; }
//sprite movement velocity
public Point2D velocity() { return vel; }
public void setVelocity(Point2D vel) { this.vel = vel; }
//returns the center of the sprite as a Point2D
public Point2D center() {
return(new Point2D(entity.getCenterX(),entity.getCenterY()));
}
//generic variable for selectively using sprites
public boolean alive() { return entity.isAlive(); }
public void setAlive(boolean alive) { entity.setAlive(alive); }
//face angle indicates which direction sprite is facing
public double faceAngle() { return entity.getFaceAngle(); }
public void setFaceAngle(double angle) {
entity.setFaceAngle(angle);
}
public void setFaceAngle(float angle) {
entity.setFaceAngle((double) angle);
}
public void setFaceAngle(int angle) {
entity.setFaceAngle((double) angle);
}
//move angle indicates direction sprite is moving
public double moveAngle() { return entity.getMoveAngle(); }
public void setMoveAngle(double angle) {
entity.setMoveAngle(angle);
}
public void setMoveAngle(float angle) {
entity.setMoveAngle((double) angle);
}
public void setMoveAngle(int angle) {
entity.setMoveAngle((double) angle);
}
//returns the source image width/height
public int imageWidth() { return entity.width(); }
public int imageHeight() { return entity.height(); }
//check for collision with a rectangular shape
public boolean collidesWith(Rectangle rect) {
return (rect.intersects(getBounds()));
}
//check for collision with another sprite
public boolean collidesWith(Sprite sprite) {
return (getBounds().intersects(sprite.getBounds()));
}
//check for collision with a point
public boolean collidesWith(Point2D point) {
return (getBounds().contains(point.X(), point.Y()));
}
}