startGame minor refactoring

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

208
sketch.js
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,6 +285,90 @@ function startGame(full_start, update_objects) {
} }
}, },
}; };
// Creating trees, clouds, mountains, canyons, collectables.
{
// Creating trees, clouds, mountains, canyons, collectables, platforms, enemies.
for (i = 0; i < 150; i++) {
trees_x[i] = random(-100, 10000);
}
for (i = 0; i < 100; i++) {
clouds[i] = {
x: random(-100, 10000),
y: random(0, floorPos_y / 2),
size: random(0.4, 2),
};
}
// Latter mountains.
for (i = 0; i < finish_position_x / 90; i++) {
mountains[i] = {
x: random(-200, finish_position_x + 200),
width: random(60, 150),
height: random(floorPos_y - 50, floorPos_y - 150),
skew: random(-20, 20),
};
// To prevent spiky mountains which are quite ugly
mountains[i].height = max(mountains[i].width, mountains[i].height);
}
// Start mountains.
for (i = 0; i < 6; i++) {
mountains[i] = {
x: width / (i + 1.5),
width: width / (i + random(2, 4)),
height: height / (2 + 0.8 * i),
skew: (random(-10, 20) * i) / 2,
};
}
for (i = 0; i < 100; i++) {
if (i == 0) {
canyons[0] = {
x: 700,
width: 50,
points: [],
};
} else {
if (canyons[i - 1].x + 600 > finish_position_x) break;
canyons[i] = {
x: canyons[i - 1].x + 300 + 200 * random(0.5, 1),
width: 50 + 30 * random(),
points: [],
};
}
}
for (i = 0; i < 100; i++) {
if (i == 0) {
collectables[0] = {
x: 600,
y: groundPositions[0],
size: 75,
isFound: false,
};
} else {
if (collectables[i - 1].x + 200 > finish_position_x) {
break;
}
collectables[i] = {
x: collectables[i - 1].x + 50 + 100 * random(0.5, 1),
y: groundPositions[
floor(random(0, groundPositions.length))
],
size: 75,
isFound: false,
};
// Checking whether the coin is over a canyon;
// marking it as isFound (making it disabled) in case if.
for (k = 0; k < canyons.length; k++) {
if (
canyons[k].x - canyons[k].width / 2 <
collectables[i].x &&
canyons[k].x + canyons[k].width / 2 > collectables[i].x
) {
collectables[i].isFound = true;
}
}
}
}
}
} else { } else {
gameChar.x = width / 2; gameChar.x = width / 2;
gameChar.y = floorPos_y + (height - floorPos_y) / 6; gameChar.y = floorPos_y + (height - floorPos_y) / 6;
@ -280,106 +379,7 @@ function startGame(full_start, update_objects) {
gameChar.isJumping = false; gameChar.isJumping = false;
gameChar.isLeft = false; gameChar.isLeft = false;
gameChar.isRight = 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.
for (i = 0; i < 150; i++) {
trees_x[i] = random(-100, 10000);
}
for (i = 0; i < 100; i++) {
clouds[i] = {
x: random(-100, 10000),
y: random(0, floorPos_y / 2),
size: random(0.4, 2),
};
}
// Latter mountains.
for (i = 0; i < finish_position_x / 90; i++) {
mountains[i] = {
x: random(-200, finish_position_x + 200),
width: random(60, 150),
height: random(floorPos_y - 50, floorPos_y - 150),
skew: random(-20, 20),
};
// To prevent spiky mountains which are quite ugly
mountains[i].height = max(mountains[i].width, mountains[i].height);
}
// Start mountains.
for (i = 0; i < 6; i++) {
mountains[i] = {
x: width / (i + 1.5),
width: width / (i + random(2, 4)),
height: height / (2 + 0.8 * i),
skew: (random(-10, 20) * i) / 2,
};
}
for (i = 0; i < 100; i++) {
if (i == 0) {
canyons[0] = {
x: 700,
width: 50,
points: [],
};
} else {
if (canyons[i - 1].x + 600 > finish_position_x) break;
canyons[i] = {
x: canyons[i - 1].x + 300 + 200 * random(0.5, 1),
width: 50 + 30 * random(),
points: [],
};
}
}
for (i = 0; i < 100; i++) {
if (i == 0) {
collectables[0] = {
x: 600,
y: groundPositions[0],
size: 75,
isFound: false,
};
} else {
if (collectables[i - 1].x + 200 > finish_position_x) {
break;
}
collectables[i] = {
x: collectables[i - 1].x + 50 + 100 * random(0.5, 1),
y: groundPositions[
floor(random(0, groundPositions.length))
],
size: 75,
isFound: false,
};
// Checking whether the coin is over a canyon;
// marking it as isFound (making it disabled) in case if.
for (k = 0; k < canyons.length; k++) {
if (
canyons[k].x - canyons[k].width / 2 <
collectables[i].x &&
canyons[k].x + canyons[k].width / 2 > collectables[i].x
) {
collectables[i].isFound = true;
}
}
}
}
}
flagpole = {
x: finish_position_x,
isReached: false,
size_vert: 4,
size_hor: 7,
cell_size: 20,
cell_size_v: 20,
cell_size_h: 20,
};
text_size = width / 20;
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;