forked from SauravGShephertz/libgdxMultiplayerSuperJumper
/
World.java
238 lines (199 loc) · 6.4 KB
/
World.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
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
/*******************************************************************************
* Copyright 2011 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package app.badlogicgames.superjumper;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import org.json.JSONArray;
import org.json.JSONObject;
import appwarp.WarpController;
import com.badlogic.gdx.math.Vector2;
public class World {
public interface WorldListener {
public void jump ();
public void highJump ();
public void hit ();
public void coin ();
}
public static final float WORLD_WIDTH = 10;
public static final float WORLD_HEIGHT = 15 * 20;
public static final int WORLD_STATE_RUNNING = 0;
public static final int WORLD_STATE_NEXT_LEVEL = 1;
public static final int WORLD_STATE_GAME_OVER = 2;
public static final Vector2 gravity = new Vector2(0, -12);
public final Bob bob;
public final List<Platform> platforms;
public final List<Spring> springs;
public final List<Squirrel> squirrels;
public final List<Coin> coins;
public Castle castle;
public final WorldListener listener;
public final Random rand;
public float heightSoFar;
public int score;
public int state;
protected World (WorldListener listener) {
this.bob = new Bob(5, 1);
this.platforms = new ArrayList<Platform>();
this.springs = new ArrayList<Spring>();
this.squirrels = new ArrayList<Squirrel>();
this.coins = new ArrayList<Coin>();
this.listener = listener;
rand = new Random();
generateLevel();
this.heightSoFar = 0;
this.score = 0;
this.state = WORLD_STATE_RUNNING;
}
private void generateLevel () {
try{
float y = Platform.PLATFORM_HEIGHT / 2;
float maxJumpHeight = Bob.BOB_JUMP_VELOCITY * Bob.BOB_JUMP_VELOCITY / (2 * -gravity.y);
while (y < WORLD_HEIGHT - WORLD_WIDTH / 2) {
int type = rand.nextFloat() > 0.8f ? Platform.PLATFORM_TYPE_MOVING : Platform.PLATFORM_TYPE_STATIC;
float x = rand.nextFloat() * (WORLD_WIDTH - Platform.PLATFORM_WIDTH) + Platform.PLATFORM_WIDTH / 2;
Platform platform = new Platform(type, x, y);
platforms.add(platform);
if (rand.nextFloat() > 0.9f && type != Platform.PLATFORM_TYPE_MOVING) {
Spring spring = new Spring(platform.position.x, platform.position.y + Platform.PLATFORM_HEIGHT / 2
+ Spring.SPRING_HEIGHT / 2);
springs.add(spring);
}
if (y > WORLD_HEIGHT / 3 && rand.nextFloat() > 0.8f) {
Squirrel squirrel = new Squirrel(platform.position.x + rand.nextFloat(), platform.position.y
+ Squirrel.SQUIRREL_HEIGHT + rand.nextFloat() * 2);
squirrels.add(squirrel);
}
if (rand.nextFloat() > 0.6f) {
Coin coin = new Coin(platform.position.x + rand.nextFloat(), platform.position.y + Coin.COIN_HEIGHT
+ rand.nextFloat() * 3);
coins.add(coin);
}
y += (maxJumpHeight - 0.5f);
y -= rand.nextFloat() * (maxJumpHeight / 3);
}
castle = new Castle(WORLD_WIDTH / 2, y);
}catch(Exception e){
e.printStackTrace();
}
}
public void update (float deltaTime, float accelX) {
updateBob(deltaTime, accelX);
updatePlatforms(deltaTime);
updateSquirrels(deltaTime);
updateCoins(deltaTime);
if (bob.state != Bob.BOB_STATE_HIT) checkCollisions();
checkGameOver();
}
private void updateBob (float deltaTime, float accelX) {
if (bob.state != Bob.BOB_STATE_HIT && bob.position.y <= 0.5f) bob.hitPlatform();
if (bob.state != Bob.BOB_STATE_HIT) bob.velocity.x = -accelX / 10 * Bob.BOB_MOVE_VELOCITY;
bob.update(deltaTime);
heightSoFar = Math.max(bob.position.y, heightSoFar);
}
private void updatePlatforms (float deltaTime) {
int len = platforms.size();
for (int i = 0; i < len; i++) {
Platform platform = platforms.get(i);
platform.update(deltaTime);
if (platform.state == Platform.PLATFORM_STATE_PULVERIZING && platform.stateTime > Platform.PLATFORM_PULVERIZE_TIME) {
platforms.remove(platform);
len = platforms.size();
}
}
}
private void updateSquirrels (float deltaTime) {
int len = squirrels.size();
for (int i = 0; i < len; i++) {
Squirrel squirrel = squirrels.get(i);
squirrel.update(deltaTime);
}
}
private void updateCoins (float deltaTime) {
int len = coins.size();
for (int i = 0; i < len; i++) {
Coin coin = coins.get(i);
coin.update(deltaTime);
}
}
private void checkCollisions () {
checkPlatformCollisions();
checkSquirrelCollisions();
checkItemCollisions();
checkCastleCollisions();
}
private void checkPlatformCollisions () {
if (bob.velocity.y > 0) return;
int len = platforms.size();
for (int i = 0; i < len; i++) {
Platform platform = platforms.get(i);
if (bob.position.y > platform.position.y) {
if (bob.bounds.overlaps(platform.bounds)) {
bob.hitPlatform();
listener.jump();
if (rand.nextFloat() > 0.5f) {
platform.pulverize();
}
break;
}
}
}
}
private void checkSquirrelCollisions () {
int len = squirrels.size();
for (int i = 0; i < len; i++) {
Squirrel squirrel = squirrels.get(i);
if (squirrel.bounds.overlaps(bob.bounds)) {
bob.hitSquirrel();
listener.hit();
}
}
}
private void checkItemCollisions () {
int len = coins.size();
for (int i = 0; i < len; i++) {
Coin coin = coins.get(i);
if (bob.bounds.overlaps(coin.bounds)) {
coins.remove(coin);
len = coins.size();
listener.coin();
score += Coin.COIN_SCORE;
}
}
if (bob.velocity.y > 0) return;
len = springs.size();
for (int i = 0; i < len; i++) {
Spring spring = springs.get(i);
if (bob.position.y > spring.position.y) {
if (bob.bounds.overlaps(spring.bounds)) {
bob.hitSpring();
listener.highJump();
}
}
}
}
private void checkCastleCollisions () {
if (castle.bounds.overlaps(bob.bounds)) {
state = WORLD_STATE_NEXT_LEVEL;
}
}
private void checkGameOver () {
if (heightSoFar - 7.5f > bob.position.y) {
state = WORLD_STATE_GAME_OVER;
}
}
}