-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScrollWorld.java
More file actions
379 lines (342 loc) · 12.2 KB
/
Copy pathScrollWorld.java
File metadata and controls
379 lines (342 loc) · 12.2 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import greenfoot.*;
import java.util.ArrayList;
import java.awt.Color;
/**
* A world which has the possibility to scroll
* over a really big area. That big area is called
* the big space, It scrolls over it using a camera.
* The camera's location starts as x=half width, y=half height
* of the world. It can't get outside of the big space.<p>
*
* The background is always scrolling, you don't have
* to do anything for it. Just make a background like you
* always do in your scenarios, and this scenario will
* scroll over it. The only note is that the method
* {@link setBackground} is replaced by {@link
* setNewBackground}.<p>
*
* The world supports adding ScrollActors in 2 ways:
* <Li>
* Adding it in the regular way, which means that it can
* go off screen and come back on screen.
* </Li><Li>
* Adding a camera follower, which means that it will
* move just as much as the camera does. So if you add it
* to the center of the screen, it will always be there
* (except for when you change it yourself).
* </Li>
* <b>IMPORTANT NOTE:</b> if your Actor isn't a subclass of ScrollActor, it
* will looklike it is a camera follower.
*
* @author Sven van Nigtevecht
* @version 2.1.2
*/
public abstract class ScrollWorld extends World
{
private final int width, height, cellSize;
private final ArrayList<ScrollActor> objects;
private final ArrayList<ScrollActor> camFollowers;
private final int fullWidth, fullHeight;
private int camX, camY, camDir;
private final GreenfootImage bigBackground, back, agua;
private int scrollPosX, scrollPosY;
/**
* Create a new ScrollWorld.
* @param width The width of the scroll world in cells.
* @param height The height of the scroll world in cells.
* @param cellSize The size of the cells (in pixels).
* @param fullWidth The total width of the world.
* That means, objects can't move further than this limit.
* @param fullHeight The total height of the world.
* That means, objects can't move further than this limit.
* @throws IllegalArgumentException If one of the arguments
* is smaller or equal to 0.
*/
public ScrollWorld(int width, int height, int cellSize, int fullWidth, int fullHeight)
{
super(width, height, cellSize, false);
this.back = getBackground();
this.width = back.getWidth();
this.height = back.getHeight();
this.cellSize = cellSize;
this.fullWidth = fullWidth;
this.fullHeight = fullHeight;
if (fullWidth <= 0)
throw new IllegalArgumentException("The width of the big space ("+fullWidth
+") can't be smaller then the width of the world ("+width+")");
if (fullHeight <= 0)
throw new IllegalArgumentException("The height of the big space ("+fullHeight
+") can't be smaller then the height of the world ("+height+")");
objects = new ArrayList<ScrollActor>();
camFollowers = new ArrayList<ScrollActor>();
camX = getWidth() /2;
camY = getHeight() /2;
camDir = 0;
scrollPosX = 0;
scrollPosY = 0;
agua = new GreenfootImage(800,600);
this.cambiaColorAgua(1);
agua.fillRect(0,0,800,600);
agua.setTransparency(255);
bigBackground = new GreenfootImage(width+width, height+height);
setNewBackground(back);
}
public void cambiaColorAgua(int tipo)
{
if(tipo == 1)
agua.setColor(new Color(0,148,255));
else
agua.setColor(new Color(0,82,85));
}
public void cambiaFondo(int profundidad)
{
//Borrar
//agua.setColor(new Color(0,148,255));
//agua.setTransparency(255);
//agua.fillRect(0,0,800,600);
if(profundidad == 0)
setNewBackground(new GreenfootImage("Oceano.png"));
else
{
agua.setColor(new Color(0,148-profundidad,255));
agua.fillRect(0,0,800,600);
setNewBackground(agua);
}
}
/** EXTRA METHODS: */
/**
* Sets the background of the world. This will also initialize
* everything to make the background scroll, something the
* normal {@link setBackground} method doesn't.
*/
public void setNewBackground(GreenfootImage background)
{
bigBackground.clear();
/**Opcional*/
if (background.getWidth() == bigBackground.getWidth() &&
background.getHeight() == bigBackground.getHeight()) {
bigBackground.drawImage(background, 0,0);
back.clear();
back.drawImage(bigBackground, scrollPosX,scrollPosY);
return;
}
bigBackground.drawImage(background, 0,0);
bigBackground.drawImage(background, background.getWidth(),0);
bigBackground.drawImage(background, 0,background.getHeight());
bigBackground.drawImage(background, background.getWidth(),background.getHeight());
back.clear();
back.drawImage(bigBackground, scrollPosX,scrollPosY);
}
/** ADDING + REMOVING OBJECTS: */
/**
* Adds an object which will follow the camera.
* The location is seen from the camera, not from the
* big space.
* @param cameraFollower The object that will be added to the world
* as a camera follower.
* @param x The x coördinate seen from the camera where the object
* will be added.
* @param y The y coördinate seen from the camera where the object
* will be added.
* @see #addObject(ScrollActor, int, int)
*/
public void addCameraFollower(ScrollActor cameraFollower, int x, int y)
{
super.addObject(cameraFollower, getWidth() /2 +x, getHeight() /2 +y);
camFollowers.add(cameraFollower);
cameraFollower.setIsCameraFollower(true);
}
/**
* Adds an object to the the world. If the given object
* is a ScrollActor or a subclass of it, the x and y
* coördinates are in the big space.
*
* @param object The object that will be added to the world.
* @param x The x coördinate in the world where the object
* will be added.
* @param y The y coördinate in the world where the object
* will be added.
* @see #addCameraFollower(ScrollActor, int, int)
*/
public void addObject(Actor object, int x, int y)
{
if (object instanceof ScrollActor) {
if (x >= fullWidth)
x = fullWidth -1;
else if (x < 0)
x = 0;
if (y >= fullHeight)
y = fullHeight -1;
else if (y < 0)
y = 0;
ScrollActor sa = (ScrollActor) object;
super.addObject(sa, x -(camX -getWidth() /2), y -(camY -getHeight() /2));
objects.add(sa);
sa.setIsCameraFollower(false);
} else
super.addObject(object,x,y);
}
/**
* Removes an object from the world.
* @param object The object that will be removed
* from the world. This can either be a camera follower,
* or just a regular object.
*/
public void removeObject(Actor object)
{
super.removeObject(object);
if (object instanceof ScrollActor) {
ScrollActor a = (ScrollActor) object;
objects.remove(a);
camFollowers.remove(a);
a.setIsCameraFollower(false);
}
}
/** RETURN VALUES: */
/**
* Returns the camera's x coördinate in big space.
* @see #getCameraY
*/
public int getCameraX()
{
return camX;
}
/**
* Returns the camera's y coördinate in big space.
* @see #getCameraX
*/
public int getCameraY()
{
return camY;
}
/**
* Returns the width of the big space.
* @see #getFullHeight
*/
public int getFullWidth()
{
return fullWidth;
}
/**
* Returns the height of the big space.
* @see #getFullWidth
*/
public int getFullHeight()
{
return fullHeight;
}
/** CAMERA MOVEMENT + ROTATION: */
/**
* Moves the camera to a particular location.
* Note that this is a location in the big space.
* @param x The new x coördinate of the camera.
* @param y The new y coördinate of the camera.
*/
public void setCameraLocation(int x, int y)
{
if (camX == x && camY == y) return;
if (x > fullWidth -getWidth() /2)
x = fullWidth -getWidth() /2;
else if (x < getWidth() /2)
x = getWidth() /2;
if (y > fullHeight -getHeight() /2)
y = fullHeight -getHeight() /2;
else if (y < getHeight() /2)
y = getHeight() /2;
int dx = x -camX;
int dy = y -camY;
camX = x;
camY = y;
for (ScrollActor a : objects)
a.setLocation(a.getX() -dx, a.getY() -dy);
for (ScrollActor a : camFollowers)
a.setLocation(a.getX(), a.getY());
moveBackgroundRight(dx *cellSize);
moveBackgroundUp(dy *cellSize);
}
/**
* Sets the direction the camera is facing.
* It doesn't change anything you see, but it makes
* it possible to use the {@link moveCamera} method.
* @param degrees The new rotation in degrees.
* @see #turnCamera(int)
* @see #moveCamera(int)
*/
public void setCameraDirection(int degrees)
{
if (degrees >= 360) {
if (degrees < 720)
degrees -= 360;
else
degrees %= 360;
} else if (degrees < 0) {
if (degrees >= -360)
degrees += 360;
else
degrees = 360 +(degrees %360);
}
if (camDir == degrees) return;
camDir = degrees;
}
/**
* Turns the camera.
* It doesn't change anything you see, but it makes
* it possible to use the {@link moveCamera} method.
* @param amount The number of degrees the camera will
* turn clockwise. If this is negative, it will turn
* counter-clockwise.
* @see #setCameraDirection(int)
* @see #moveCamera(int)
*/
public void turnCamera(int amount)
{
setCameraDirection(camDir +amount);
}
/**
* Moves the camera forward to the direction
* it's facing (to go backwards, enter a negative number).
* @param amount The number of cells the camera will move.
* When this is negative, the camera will move forward.
*/
public void moveCamera(int amount)
{
if (amount == 0) return;
double radians = Math.toRadians(camDir);
double dx = Math.cos(radians) *amount;
double dy = Math.sin(radians) *amount;
setCameraLocation((int)(camX +dx +0.5), (int)(camY +dy +0.5));
}
/** MOVING BACKGROUND: */
/**
* All the honor for this goes to Busch2207 from
* greenfoot.org
*/
private void moveBackgroundUp(int amount)
{
if (amount == 0) return;
int height = getHeight();
//System.out.println("1.-"+scrollPosY);
scrollPosY -= amount;
//System.out.println(scrollPosY);
while (scrollPosY < 0){
scrollPosY += height;
}
if(scrollPosY!=600)
scrollPosY %= height;
getBackground().drawImage(bigBackground, scrollPosX -getWidth(),scrollPosY -height);
}
/**
* All the honor for this goes to Busch2207 from
* greenfoot.org
*/
private void moveBackgroundRight(int amount)
{
if (amount == 0) return;
int width = getWidth();
scrollPosX -= amount;
while (scrollPosX < 0)
scrollPosX += width;
scrollPosX %= width;
getBackground().drawImage(bigBackground, scrollPosX -width,scrollPosY -getHeight());
}
}