-
Notifications
You must be signed in to change notification settings - Fork 0
/
World.pde
320 lines (270 loc) · 9.13 KB
/
World.pde
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
/**
* Contians the world and its functions, like menus, constant gravity force,
* camera, and much more.
*
*/
class World {
// the storeage for the current frame's onscreen patforms
ArrayList<Platform> platforms = new ArrayList<Platform>();
// the whole level, returned by the level reader
Actor[][] level;
// the player
Player player;
// the camera
Cam2D camera;
// constant gravity
float gravity;
// values for the start delay at the end of pause menu, death menu, and checkpoint reset.
int lastStartDelay;
int startDelay;
// get all of the menus working
MenuLoading menuLoading;
MenuDeath menuDeath;
MenuPause menuPause;
Gui gui;
MenuWin menuWin;
// the level reader
LevelReader lr;
// the zhold state in the last frame
boolean oldZHold;
// the xhold state in the last frame
boolean oldXHold;
// if the world should reload from file
boolean shouldReload;
// the score, as in coins
int score;
/**
* start a world in the center
*/
World() {
// make a new level reader
lr = new LevelReader();
// set the delay to 250
startDelay = 250;
// set global gravity force
gravity = 1;
// make a new player
player = new Player();
// make a new camera that centers at player's location
camera = new Cam2D(player.loc.x,player.loc.y, 0);
// make all the new menus
menuLoading = new MenuLoading();
menuDeath = new MenuDeath();
menuPause = new MenuPause();
gui = new Gui();
menuWin = new MenuWin();
// set should load level to true;
shouldReload = true;
//set current millis to the last start delay so it knows when to count down
lastStartDelay = millis();
}
/**
* updates the world in every frame
*/
void update() {
// set the background to the 8th the brightness of the dynamicColor for a dark background
background(red(dynamicColor)/8, green(dynamicColor)/8, blue(dynamicColor)/8);
// let the camera do its thing.
pushMatrix();
// update the camera
camera.des = new PVector(-player.loc.x,-player.loc.y);
camera.angleDes = -player.gravityDirection;
camera.update();
if (lr.percentDone == -1) {
// if percetn done is -1, that means the level reader has not been ran before
lr.startLevelReader("Sprits/MainMap.png");
lr.percentDone = 0;
}
if (!menuLoading.on) {
// if the the menu loading is not on, it means loading is over and the world can function
if (millis() - lastStartDelay > startDelay && !menuPause.on && !menuLoading.on && !menuDeath.on && !menuWin.on) {
// if no menus are on
// a little delay before the player starts moving.
player.update();
// is holding the reset key. RESET.
if (xHold && !oldXHold) {
lastStartDelay = millis();
if (checkpointState == -1) {
// default starting position
world.resetWorld(new CheckPoint(new PVector(0,0), 0));
} else {
world.resetWorld(checkpoints[checkpointState]);
}
}
}
// if the gui should show up or not
if (!menuLoading.on && !menuDeath.on)
gui.on = true;
else
gui.on = false;
// if should reaload, reload the level array from the level reader
if (shouldReload) {
copyLevelOver(lr.level);
shouldReload = false;
}
if (player.dead && !menuDeath.on) {
// dead, so get on it and turn on the death menu.
menuDeath.on = true;
}
// draw the minimap backgournd
drawBackgroundMinmap();
// clear the temp arraylist for platforms
platforms.clear();
// whole drawing the screen, it will fill in the platforms arraylist
onScreenObjectsManager();
}
// render the player with a tint
playerRenderTint();
// close camera influence
popMatrix();
// check dismiss menu or pause
if (zHold && !oldZHold && !menuLoading.on) {
if (menuDeath.on)
menuDeath.on = false;
else if (menuWin.on)
menuWin.on = false;
else
menuPause.on = menuPause.on ? false : true;
lastStartDelay = millis();
}
// update all menus
gui.update();
gui.render();
menuLoading.update();
menuLoading.render();
menuDeath.update();
menuDeath.render();
menuPause.update();
menuPause.render();
menuWin.update();
menuWin.render();
// update old to new.
oldZHold = zHold;
oldXHold = xHold;
}
/**
* Copies an external level into the world
* @param level_ a 2D array that contains level info
*/
void copyLevelOver(Actor[][] level_) {
level = new Actor[level_.length][level_[0].length];
for (int x = 0; x < level_.length; x++) {
for (int y = 0; y < level_[0].length; y++ ) {
level[x][y] = level_[x][y];
}
}
}
/**
* go through and loads only the ones that the player may see on screen
* uses dynamic loading system that maps the player's location to an 2d Level array
*/
void onScreenObjectsManager() {
// map the player location to the 2D array location
PVector topLeft = locToArrayIndex(new PVector(player.loc.x-max(width,height)+width/4,
player.loc.y-max(width,height)+width/4));
PVector bottomRight = locToArrayIndex(new PVector(player.loc.x+max(width,height)+width/4,
player.loc.y+max(width,height)+width/4));
// find the start and end of this box
int xStart = (int)(topLeft.x > 0 ? topLeft.x : 0);
int xEnd = (int)(bottomRight.x < level.length ? bottomRight.x : level.length);
int yStart = (int)(topLeft.y > 0 ? topLeft.y : 0);
int yEnd = (int)(bottomRight.y < level[0].length ? bottomRight.y : level[0].length);
// loop though the whole thing
for (int y = yStart; y < yEnd; y++) {
for (int x = xStart; x < xEnd; x++) {
Actor a = level[x][y];
if (a != null) {
if (a instanceof Platform) {
Platform p = (Platform)a;
// this is a no collision block, for decoration, so just render it
if (p.collisionMode == p.NOC) {
p.render();
} else {
platforms.add(p);
p.update();
p.render();
}
} else if (a instanceof Coin) {
if (player.collision(a)) {
// if player is touching the coin, remove it and score
landSound.rewind();
landSound.play();
score++;
level[x][y] = null;
} else {
a.update();
noTint();
a.render();
}
}
}
}
}
}
/**
* draws the player with a tint
*/
void playerRenderTint() {
// bitshift, faster.
tint((dynamicColor >> 16) & 0xFF,
(dynamicColor >> 8) & 0xFF,
dynamicColor & 0xFF);
player.render();
noTint();
}
/**
* draws the backgorund minimap. It is o.5x version of the real platform
* this draws the background using rect() I did not use an image for this
* beuase it makes no sense ot store a blank image and then draw it.
*/
void drawBackgroundMinmap() {
// map the player location to the 2D array location
PVector topLeft = locToArrayIndex(new PVector(player.loc.x-max(width*2,height*2)+width/4,
player.loc.y-max(width*2,height*2)+width/4));
PVector bottomRight = locToArrayIndex(new PVector(player.loc.x+max(width*2,height*2)+width/4,
player.loc.y+max(width*2,height*2)+width/4));
// find the start and end of this box
int xStart = (int)(topLeft.x > 0 ? topLeft.x : 0);
int xEnd = (int)(bottomRight.x < level.length ? bottomRight.x : level.length);
int yStart = (int)(topLeft.y > 0 ? topLeft.y : 0);
int yEnd = (int)(bottomRight.y < level[0].length ? bottomRight.y : level[0].length);
fill(red(dynamicColor)/4, green(dynamicColor)/4, blue(dynamicColor)/4);
// grab all the real platofrm and draw it
for (int y = yStart; y < yEnd; y++) {
for (int x = xStart; x < xEnd; x++) {
Actor a = level[x][y];
if (a != null) {
if (a instanceof Platform) {
Platform p = (Platform)a;
pushMatrix();
translate(p.loc.x/2+player.loc.x/2, p.loc.y/2+player.loc.y/2);
rotate(p.angle);
rect(0,0,(p.cLeft+p.cRight)/2,(p.cTop+p.cBottom)/2);
popMatrix();
}
}
}
}
}
/**
* Maps the location of the player to the level 2D array
* @param loc_ players location
*/
PVector locToArrayIndex(PVector loc_) {
float pX = loc_.x/(lr.platformSize*2)+level.length/2;
float pY = loc_.y/(lr.platformSize*2)+level[0].length/2;
return new PVector(pX,pY);
}
/**
* Copies an external level into the world
* @param cp check point object that that contains the repswn point
*/
void resetWorld(CheckPoint cp) {
player = new Player(cp.loc.x,cp.loc.y,cp.angle);
score = cp.score;
bgMusic.rewind();
bgMusic.skip(cp.musicLoc);
bgMusic.loop();
shouldReload = true;
}
}