-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameElement.java
129 lines (119 loc) · 3.68 KB
/
GameElement.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
/**
* The GameElement class represents the basic elements in the Asteroid Adventure Game.
* It is an abstract class that provides foundational attributes and behaviors for
* various game elements like asteroids, bullets, and the ship.
*
* @version 1.0
* @since 2023-04-04
*/
public abstract class GameElement implements Updatable, Drawable {
protected Pose pose;
protected Vector2D velocity;
protected double radius;
protected boolean destroyed;
/**
* Constructs a GameElement object with specified pose, velocity, and radius.
*
* @param pose The position and heading of the game element.
* @param velocity The velocity of the game element.
* @param radius The collision radius of the game element.
*/
public GameElement(Pose pose, Vector2D velocity, double radius) {
this.pose = pose;
this.velocity = velocity;
this.radius = radius;
}
/**
* Gets the current pose of the game element.
*
* @return The current pose.
*/
public Pose getPose() {
return this.pose;
}
/**
* Gets the current velocity of the game element.
*
* @return The current velocity.
*/
public Vector2D getVelocity() {
return this.velocity;
}
/**
* Gets the collision radius of the game element.
*
* @return The collision radius.
*/
public double getRadius() {
return this.radius;
}
/**
* Checks if the game element is destroyed.
*
* @return True if the game element is destroyed, otherwise false.
*/
public boolean isDestroyed() {
return this.destroyed;
}
/**
* Sets the destroyed state of the game element.
*
* @param destroyed The new destroyed state.
*/
public void setDestroyed(boolean destroyed) {
this.destroyed = destroyed;
}
/**
* Checks for collision with another game element.
*
* @param other The other game element to check for collision.
* @return True if a collision occurs, otherwise false.
*/
public boolean checkCollision(GameElement other) {
double x1 = this.getPose().getX();
double x2 = other.getPose().getX();
double y1 = this.getPose().getY();
double y2 = other.getPose().getY();
double distance = Math.sqrt(Math.pow(x2 - x1, 2)
+ Math.pow(y2 - y1, 2));
double inBetween = distance - (this.getRadius()
+ other.getRadius());
if (inBetween < 0) {
return true;
}
return false;
}
/**
* Updates the position of the game element based on its velocity.
*/
public void update() {
this.pose = this.pose.move(velocity);
double x = this.pose.getX();
double y = this.pose.getY();
int width = GameDriver.SCREEN_WIDTH;
int hight = GameDriver.SCREEN_HEIGHT;
if (x > width) {
if (y < 0) {
this.pose = this.pose.newX(0);
this.pose = this.pose.newY(hight);
}
if (y > hight) {
this.pose = this.pose.newX(0);
this.pose = this.pose.newY(0);
} else {
this.pose = this.pose.newX(0);
}
} else if (x < 0) {
if (y > hight) {
this.pose = this.pose.newX(width);
this.pose = this.pose.newY(0);
}
if (y < 0) {
this.pose = this.pose.newX(width);
this.pose = this.pose.newY(hight);
} else {
this.pose = this.pose.newX(width);
}
}
}
}