rewrote ground position mechanism

This commit is contained in:
Mottributo 2023-03-25 20:33:19 +03:00
parent 845f7cbb6f
commit da9e0b188c

View File

@ -54,7 +54,11 @@ function setup() {
function startGame(full_start, update_objects) { function startGame(full_start, update_objects) {
floorPos_y = 432; floorPos_y = 432;
cameraPosX = 0; cameraPosX = 0;
groundPositions = [
floorPos_y + (height - floorPos_y) / 6,
floorPos_y + (3 * (height - floorPos_y)) / 6,
floorPos_y + (5 * (height - floorPos_y)) / 6,
];
if (full_start) { if (full_start) {
gameChar = { gameChar = {
x: width / 2, x: width / 2,
@ -71,12 +75,7 @@ function startGame(full_start, update_objects) {
isFalling: false, isFalling: false,
isJumping: false, isJumping: false,
isPlummeting: false, isPlummeting: false,
possibleGroundPosY: [ curGroundIndex: 0,
floorPos_y + (height - floorPos_y) / 6,
floorPos_y + (3 * (height - floorPos_y)) / 6,
floorPos_y + (5 * (height - floorPos_y)) / 6,
],
curGroundPosYIndex: 0,
draw: function () { draw: function () {
push(); push();
@ -248,24 +247,24 @@ function startGame(full_start, update_objects) {
this.y_step /= this.scale; this.y_step /= this.scale;
pop(); pop();
}, },
getCurGroundPosY: function () { getCurGroundY: function () {
return this.possibleGroundPosY[this.curGroundPosYIndex]; return groundPositions[this.curGroundIndex];
}, },
goUp: function () { goUp: function () {
memorizedIndex = this.curGroundPosYIndex; memorizedIndex = this.curGroundIndex;
this.curGroundPosYIndex = max(0, this.curGroundPosYIndex - 1); this.curGroundIndex = max(0, this.curGroundIndex - 1);
if (memorizedIndex != this.curGroundPosYIndex) { if (memorizedIndex != this.curGroundIndex) {
gameChar.y -= (height - floorPos_y) / 3; gameChar.y -= (height - floorPos_y) / 3;
gameChar.scale -= 0.1; gameChar.scale -= 0.1;
} }
}, },
goDown: function () { goDown: function () {
memorizedIndex = this.curGroundPosYIndex; memorizedIndex = this.curGroundIndex;
this.curGroundPosYIndex = min( this.curGroundIndex = min(
this.possibleGroundPosY.length - 1, groundPositions.length - 1,
this.curGroundPosYIndex + 1 this.curGroundIndex + 1
); );
if (memorizedIndex != this.curGroundPosYIndex) { if (memorizedIndex != this.curGroundIndex) {
gameChar.y += (height - floorPos_y) / 3; gameChar.y += (height - floorPos_y) / 3;
gameChar.scale += 0.1; gameChar.scale += 0.1;
} }
@ -275,7 +274,7 @@ function startGame(full_start, update_objects) {
gameChar.x = width / 2; gameChar.x = width / 2;
gameChar.y = floorPos_y + (height - floorPos_y) / 6; gameChar.y = floorPos_y + (height - floorPos_y) / 6;
gameChar.scale = 1; gameChar.scale = 1;
gameChar.curGroundPosYIndex = 0; gameChar.curGroundIndex = 0;
gameChar.isPlummeting = false; gameChar.isPlummeting = false;
gameChar.isFalling = false; gameChar.isFalling = false;
gameChar.isJumping = false; gameChar.isJumping = false;
@ -288,7 +287,7 @@ function startGame(full_start, update_objects) {
finish_position_x = 2000; finish_position_x = 2000;
if (update_objects || full_start) { if (update_objects || full_start) {
// Assigning values of trees, clouds, mountains, canyons, collectables. // 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);
} }
@ -339,7 +338,7 @@ function startGame(full_start, update_objects) {
if (i == 0) { if (i == 0) {
collectables[0] = { collectables[0] = {
x: 600, x: 600,
y: gameChar.possibleGroundPosY[0], y: groundPositions[0],
size: 75, size: 75,
isFound: false, isFound: false,
}; };
@ -350,8 +349,8 @@ function startGame(full_start, update_objects) {
collectables[i] = { collectables[i] = {
x: collectables[i - 1].x + 50 + 100 * random(0.5, 1), x: collectables[i - 1].x + 50 + 100 * random(0.5, 1),
y: gameChar.possibleGroundPosY[ y: groundPositions[
floor(random(0, gameChar.possibleGroundPosY.length)) floor(random(0, groundPositions.length))
], ],
size: 75, size: 75,
isFound: false, isFound: false,
@ -437,8 +436,8 @@ function draw() {
} else if (gameChar.isFalling) { } else if (gameChar.isFalling) {
gameChar.y -= gameChar.jumpingStrength; gameChar.y -= gameChar.jumpingStrength;
gameChar.jumpingStrength -= gravity; gameChar.jumpingStrength -= gravity;
if (gameChar.y >= gameChar.getCurGroundPosY()) { if (gameChar.y >= gameChar.getCurGroundY()) {
gameChar.y = gameChar.getCurGroundPosY(); gameChar.y = gameChar.getCurGroundY();
gameChar.isFalling = false; gameChar.isFalling = false;
} }
} else { } else {
@ -479,7 +478,7 @@ function draw() {
else if (flagpole.isReached) else if (flagpole.isReached)
text("Level complete. Press space to continue...", 0, height / 2); text("Level complete. Press space to continue...", 0, height / 2);
if (showDebugData) { if (showDebugData) {
text(gameChar.curGroundPosYIndex, 99, 99); text(gameChar.curGroundIndex, 99, 99);
text(gameChar.getCurGroundPosY(), 188, 99); text(gameChar.getCurGroundPosY(), 188, 99);
text(gameChar.sprite, 277, 99); text(gameChar.sprite, 277, 99);
drawFps(); drawFps();
@ -497,7 +496,7 @@ function checkCanyon(t_canyon) {
function checkCollectable(t_collectable) { function checkCollectable(t_collectable) {
if ( if (
!t_collectable.isFound && !t_collectable.isFound &&
collectable.y == gameChar.getCurGroundPosY() && collectable.y == gameChar.getCurGroundY() &&
dist( dist(
t_collectable.x, t_collectable.x,
t_collectable.y, t_collectable.y,
@ -846,4 +845,4 @@ function keyReleased() {
console.log(frameCount + " released " + key + " " + keyCode); console.log(frameCount + " released " + key + " " + keyCode);
if (keyCode == 65 /*A*/) gameChar.isLeft = false; if (keyCode == 65 /*A*/) gameChar.isLeft = false;
if (keyCode == 68 /*D*/) gameChar.isRight = false; if (keyCode == 68 /*D*/) gameChar.isRight = false;
} }