-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSprite.java
71 lines (59 loc) · 1.67 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
import java.awt.Graphics;
import java.awt.Point;
import javax.swing.ImageIcon;
/**
* The word "sprite" is an old word in computer graphics.
* http://en.wikipedia.org/wiki/Sprite_(computer_graphics)
* In simple terms, a sprite is simply a 2D picture that
* can move around the screen.
*
*/
public abstract class Sprite {
protected Point absolutePosition;
protected Point relativePosition;
protected ImageIcon image;
public final int SIZE = 50;
public Sprite() {
relativePosition = new Point(-1,-1);
absolutePosition = new Point();
}
public void setLocation(Point p) {
if (p == null) {
relativePosition = null;
absolutePosition = null;
} else {
relativePosition.setLocation(p);
if (image != null) {
int iconWidth = image.getIconWidth();
int iconHeight = image.getIconHeight();
int x = 10+SIZE*relativePosition.x + (SIZE - iconWidth)/2;
int y = 10+SIZE*relativePosition.y + (SIZE - iconHeight)/2;
absolutePosition.setLocation(x, y);
}
}
}
public void setLocation(int x, int y) {
setLocation(new Point(x,y));
}
public Point getLocation() {
return relativePosition;
}
public void draw(Graphics g) {
if (absolutePosition != null) {
image.paintIcon(null, g, absolutePosition.x, absolutePosition.y);
}
}
public boolean isNear(Sprite other) {
boolean result = false;
if (other.relativePosition != null) {
if ((Math.abs(this.relativePosition.x - other.relativePosition.x) <= 1) &&
(Math.abs(this.relativePosition.y - other.relativePosition.y) <= 1)) {
result = true;
}
}
return result;
}
public boolean isTouching(Sprite other) {
return this.relativePosition.equals(other.relativePosition);
}
}