-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScrolling.java
More file actions
154 lines (126 loc) · 4.1 KB
/
Scrolling.java
File metadata and controls
154 lines (126 loc) · 4.1 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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
* Scrolling system.
* Creates a scrolling texture of the given backround image.
* Constantly checks movement and moves that texture accordingly.
*/
public abstract class Scrolling extends World {
public static int WORLD_WIDTH;
public static int WORLD_HEIGHT;
protected int totalXMovement = 0;
protected int totalYMovement = 0;
private GreenfootSound sound = new GreenfootSound("ready.mp3");
private GreenfootSound sound2 = new GreenfootSound("normal.mp3");
protected GreenfootImage texture;
public Scrolling(int screenWidth, int screenHeight, int cellSize, int scrollingWidth, int scrollingHeight) {
super(screenWidth, screenHeight, cellSize, false);
WORLD_WIDTH = scrollingWidth;
WORLD_HEIGHT = scrollingHeight;
}
/**
* Reset the position of the ScrollingActor
* and set the position of all other objects in the world.
*/
public final void resetPlayersPosition(ScrollingActor scrollingActor) {
int xMovement = (int) ((double) getWidth()/2 - scrollingActor.getExactX());
int yMovement = (int) ((double) getHeight()/2 - scrollingActor.getExactY());
totalXMovement += xMovement;
totalYMovement += yMovement;
List<Actor> actors = getObjects(Actor.class);
for (Actor actor : actors) {
if (actor instanceof ScrollingActor) {
((ScrollingActor) actor).setLocation(actor.getX() + xMovement, actor.getY() + yMovement, false);
}
else if (actor instanceof OnScreen) {
;
}
else {
actor.setLocation(actor.getX() + xMovement, actor.getY() + yMovement);
}
}
createTexture();
}
/**
* Creates a texture on a background image that is moving accordingly.
*/
protected final void createTexture() {
int x;
int y;
if (totalXMovement > 0) {
for (x = totalXMovement; x > 0; x -= texture.getWidth()) {
;
}
}
else {
for (x = totalXMovement; x < 0; x += texture.getWidth()) {
;
}
x -= texture.getWidth();
}
if (totalYMovement > 0) {
for (y = totalYMovement; y > 0; y -= texture.getHeight()) {
;
}
}
else {
for (y = totalYMovement; y < 0; y += texture.getHeight()) {
;
}
y -= texture.getHeight();
}
getBackground().clear();
for (int i = x; i < getWidth(); i += texture.getWidth()) {
for (int j = y; j < getHeight(); j += texture.getHeight()) {
getBackground().drawImage(texture, i, j);
}
}
}
/**
* Change the background image of the scrolling world to the given image.
*/
public void setScrollingBackground(GreenfootImage bgImage) {
texture = bgImage;
}
/**
* Get the total movement in x direction.
*/
public int getTotalXMovement() {
return totalXMovement;
}
/**
* Get the total movement in y direction.
*/
public int getTotalYMovement() {
return totalYMovement;
}
/**
* Checks which sounds to play according to game state
*/
public void checkSounds() {
if (Timer.timer == 0) {
sound.stop();
sound2.play();
} else if (Timer.timer > 0){
sound2.stop();
sound.play();
}
}
/**
* Stops sounds
*/
public void stopSounds() {
sound.stop();
sound2.stop();
}
/**
* Checks lives. If equal 0 stops sounds and shows EndScreenLost.
*/
public void checkLives() {
if (PackLives.lives == 0) {
Timer.timer = -1;
stopSounds();
Greenfoot.setWorld(new EndScreenLost());
}
}
}