-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameObject.java
executable file
·99 lines (77 loc) · 2.36 KB
/
GameObject.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
import java.awt.Graphics;
public abstract class GameObject {
int x; // x and y coordinates upper left
int y;
int width;
int height;
int velocityX; // Pixels to move each time move() is called.
double velocityY;
int rightBound; // Maximum permissible x, y values.
int bottomBound;
public GameObject(int x, int y, int velocityX, int velocityY, int width,
int height) {
this.x = x;
this.y = y;
this.velocityX = velocityX;
this.velocityY = velocityY;
this.width = width;
this.height = height;
}
public void setBounds(int width, int height) {
rightBound = width - this.width;
bottomBound = height - this.height;
}
public void setVelocity(int velocityX, double velocityY) {
this.velocityX = velocityX;
this.velocityY = velocityY;
}
// Move the object at the given velocity.
public void move() {
x += velocityX;
y += velocityY;
accelerate();
clip();
}
// Keep the object in the bounds of the court
public void clip() {
if (x < 0)
x = 0;
else if (x > rightBound)
x = rightBound;
//else if (y > bottomBound)
//y = bottomBound;
}
/**
* Compute whether two GameObjects intersect.
*
* @param other
* The other game object to test for intersection with.
* @return NONE if the objects do not intersect. Otherwise, a direction
* (relative to <code>this</code>) which points towards the other
* object.
*/
public Intersection intersects(GameObject other) {
if ( other.x > x + width
|| other.y > y + height
|| other.x + other.width < x
|| other.y + other.height < y)
return Intersection.NONE;
// compute the vector from the center of this object to the center of
// the other
double dx = other.x + other.width /2 - (x + width /2);
double dy = other.y + other.height/2 - (y + height/2);
double theta = Math.atan2(dy, dx);
double diagTheta = Math.atan2(height, width);
if ( -diagTheta <= theta && theta <= diagTheta )
return Intersection.RIGHT;
if ( Math.PI - diagTheta <= theta || theta <= diagTheta - Math.PI )
return Intersection.LEFT;
if ( diagTheta <= theta && theta <= Math.PI - diagTheta )
return Intersection.DOWN;
if ( diagTheta - Math.PI <= theta && theta <= diagTheta && Ball.postMove>Ball.holder)
return Intersection.UP;
return Intersection.NONE;
}
public abstract void accelerate();
public abstract void draw(Graphics g);
}