-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameObjects.java
More file actions
187 lines (166 loc) · 6.3 KB
/
GameObjects.java
File metadata and controls
187 lines (166 loc) · 6.3 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Point;
import java.util.List;
/**
* Subclass for all game objects in the world.
*/
public abstract class GameObjects extends Actor
{
protected double exactX;
protected double exactY;
protected double velX;
protected double velY;
/**
* Sets the players location. Takes integer value.
*/
public void setLocation(int x, int y) {
if (getDistanceToScrollingActor('x') - getWorld().getTotalXMovement() > Scrolling.WORLD_WIDTH/2) {
x = (int) (getStartingPoint().getX() + Scrolling.WORLD_WIDTH/2);
}
else if (getDistanceToScrollingActor('x') - getWorld().getTotalXMovement() < -Scrolling.WORLD_WIDTH/2) {
x = (int) (getStartingPoint().getX() - Scrolling.WORLD_WIDTH/2);
}
if(getDistanceToScrollingActor('y') - getWorld().getTotalYMovement() > Scrolling.WORLD_HEIGHT/2) {
y = (int) (getStartingPoint().getY() + Scrolling.WORLD_HEIGHT/2);
}
else if(getDistanceToScrollingActor('y') - getWorld().getTotalYMovement() < -Scrolling.WORLD_HEIGHT/2) {
y = (int) (getStartingPoint().getY() - Scrolling.WORLD_HEIGHT/2);
}
exactX = x;
exactY = y;
super.setLocation(x, y);
}
/**
* Sets the players location. Takes double value.
*/
public void setLocation(double x, double y) {
if (getDistanceToScrollingActor('x') - getWorld().getTotalXMovement() > Scrolling.WORLD_WIDTH/2) {
x = getStartingPoint().getX() + Scrolling.WORLD_WIDTH/2;
}
else if (getDistanceToScrollingActor('x') - getWorld().getTotalXMovement() < -Scrolling.WORLD_WIDTH/2) {
x = getStartingPoint().getX() - Scrolling.WORLD_WIDTH/2;
}
if(getDistanceToScrollingActor('y') - getWorld().getTotalYMovement() > Scrolling.WORLD_HEIGHT/2) {
y = getStartingPoint().getY() + Scrolling.WORLD_HEIGHT/2;
}
else if(getDistanceToScrollingActor('y') - getWorld().getTotalYMovement() < -Scrolling.WORLD_HEIGHT/2) {
y = getStartingPoint().getY() - Scrolling.WORLD_HEIGHT/2;
}
exactX = x;
exactY = y;
super.setLocation((int) x, (int) y);
}
/**
* Gets the distance to the ScrollingActor.
*/
public double getDistanceToScrollingActor() {
List<ScrollingActor> actors = getWorld().getObjects(ScrollingActor.class);
if (actors != null && !actors.isEmpty()) {
ScrollingActor actor = null;
for (ScrollingActor scrollingActor : actors) {
if (scrollingActor.isScrollingCenter()) {
actor = scrollingActor;
}
}
if (actor == null) {
System.err.println("No scrollingActor in the world.");
return 0;
}
return Math.hypot(getExactX() - actor.getExactX(), getExactY() - actor.getExactY());
}
return 0;
}
/**
* Gets one component of the distance to the ScrollingActor.
*/
public double getDistanceToScrollingActor(char component) throws IllegalArgumentException {
if (component == 'x') {
List<ScrollingActor> actors = getWorld().getObjects(ScrollingActor.class);
if (actors != null && !actors.isEmpty()) {
ScrollingActor actor = null;
for (ScrollingActor scrollingActor : actors) {
if (scrollingActor.isScrollingCenter()) {
actor = scrollingActor;
}
}
if (actor == null) {
System.err.println("No scrollingActor in the world.");
return 0;
}
return getExactX() - actor.getExactX();
}
}
else if (component == 'y') {
List<ScrollingActor> actors = getWorld().getObjects(ScrollingActor.class);
if (actors != null && !actors.isEmpty()) {
ScrollingActor actor = null;
for (ScrollingActor scrollingActor : actors) {
if (scrollingActor.isScrollingCenter()) {
actor = scrollingActor;
}
}
if (actor == null) {
System.err.println("No scrollingActor in the world.");
return 0;
}
return getExactY() - actor.getExactY();
}
}
else {
throw new IllegalArgumentException("component (" + component + ") must be either 'x' or 'y'");
}
return 0;
}
/**
* Gets the location of the starting point.
*/
public java.awt.Point getStartingPoint() {
List<ScrollingActor> actors = getWorld().getObjects(ScrollingActor.class);
if (actors != null && !actors.isEmpty()) {
ScrollingActor actor = null;
for (ScrollingActor scrollingActor : actors) {
if (scrollingActor.isScrollingCenter()) {
actor = scrollingActor;
}
}
if (actor == null) {
System.err.println("No scrollingActor in the world.");
return null;
}
int x = actor.getX() + getWorld().getTotalXMovement();
int y = actor.getY() + getWorld().getTotalYMovement();
return new Point(x, y);
}
return null;
}
/**
* Gets the players exact x location.
*/
public double getExactX(){
return exactX;
}
/**
* Gets the players exact y location.
*/
public double getExactY(){
return exactY;
}
/**
* Gets the players exact x location in the scrolling world.
*/
public double getScrollingX() {
return getDistanceToScrollingActor('x') - getWorld().getTotalXMovement();
}
/**
* Gets the players exact y location in the scrolling world.
*/
public double getScrollingY() {
return getDistanceToScrollingActor('y') - getWorld().getTotalYMovement();
}
/**
* Gets the current world casted.
*/
public Scrolling getWorld() {
return ((Scrolling) super.getWorld());
}
}