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