startGame minor refactoring

This commit is contained in:
Mottributo 2023-03-25 20:41:54 +03:00
parent da9e0b188c
commit 235cb7776a

View File

@ -49,17 +49,32 @@ function setup() {
enemy_head_color: color("red"), enemy_head_color: color("red"),
enemy_body_color: color("red"), enemy_body_color: color("red"),
}; };
startGame((full_start = true), (update_objects = true)); startGame(level_start = true);
} }
function startGame(full_start, update_objects) { function startGame(level_start) {
floorPos_y = 432; floorPos_y = 432;
cameraPosX = 0; cameraPosX = 0;
// levels of depth where the character, collectable and enemies stand.
groundPositions = [ groundPositions = [
floorPos_y + (height - floorPos_y) / 6, floorPos_y + (height - floorPos_y) / 6,
floorPos_y + (3 * (height - floorPos_y)) / 6, floorPos_y + (3 * (height - floorPos_y)) / 6,
floorPos_y + (5 * (height - floorPos_y)) / 6, floorPos_y + (5 * (height - floorPos_y)) / 6,
]; ];
if (full_start) { gravity = 1;
game_score = 0;
finish_position_x = 2000;
textSize(width / 20);
flagpole = {
x: finish_position_x,
isReached: false,
size_vert: 4,
size_hor: 7,
cell_size: 20,
cell_size_v: 20,
cell_size_h: 20,
};
if (level_start) {
gameChar = { gameChar = {
x: width / 2, x: width / 2,
y: floorPos_y + (height - floorPos_y) / 6, y: floorPos_y + (height - floorPos_y) / 6,
@ -270,23 +285,8 @@ function startGame(full_start, update_objects) {
} }
}, },
}; };
} else { // Creating trees, clouds, mountains, canyons, collectables.
gameChar.x = width / 2; {
gameChar.y = floorPos_y + (height - floorPos_y) / 6;
gameChar.scale = 1;
gameChar.curGroundIndex = 0;
gameChar.isPlummeting = false;
gameChar.isFalling = false;
gameChar.isJumping = false;
gameChar.isLeft = false;
gameChar.isRight = false;
}
gravity = 1;
game_score = 0;
finish_position_x = 2000;
if (update_objects || full_start) {
// Creating trees, clouds, mountains, canyons, collectables, platforms, enemies. // Creating trees, clouds, mountains, canyons, collectables, platforms, enemies.
for (i = 0; i < 150; i++) { for (i = 0; i < 150; i++) {
trees_x[i] = random(-100, 10000); trees_x[i] = random(-100, 10000);
@ -369,17 +369,17 @@ function startGame(full_start, update_objects) {
} }
} }
} }
flagpole = { } else {
x: finish_position_x, gameChar.x = width / 2;
isReached: false, gameChar.y = floorPos_y + (height - floorPos_y) / 6;
size_vert: 4, gameChar.scale = 1;
size_hor: 7, gameChar.curGroundIndex = 0;
cell_size: 20, gameChar.isPlummeting = false;
cell_size_v: 20, gameChar.isFalling = false;
cell_size_h: 20, gameChar.isJumping = false;
}; gameChar.isLeft = false;
text_size = width / 20; gameChar.isRight = false;
textSize(text_size); }
} }
function draw() { function draw() {
// -------- SKY ---------------- // -------- SKY ----------------
@ -822,7 +822,7 @@ function keyPressed() {
if (gameChar.curLives < 1 || flagpole.isReached) { if (gameChar.curLives < 1 || flagpole.isReached) {
if (keyCode == 32 /*Space*/) { if (keyCode == 32 /*Space*/) {
/*Hard resets the game*/ /*Hard resets the game*/
startGame((full_start = true), (update_objects = true)); startGame((level_start= true), );
} }
} else if (!gameChar.isPlummeting) { } else if (!gameChar.isPlummeting) {
if (keyCode == 65 /*A*/) gameChar.isLeft = true; if (keyCode == 65 /*A*/) gameChar.isLeft = true;