implementing platforms 50% done

This commit is contained in:
Mottributo 2023-03-26 19:36:47 +03:00
parent 1543e60d68
commit 3f733eea06

View File

@ -40,7 +40,9 @@ function GameCharacter() {
this.isFalling = false; this.isFalling = false;
this.isJumping = false; this.isJumping = false;
this.isPlummeting = false; this.isPlummeting = false;
this.isOnPlatform = false;
this.curGroundIndex = 0; this.curGroundIndex = 0;
this.memorizedPlatform;
this._updateSprite = function () { this._updateSprite = function () {
if (this.isPlummeting) { if (this.isPlummeting) {
@ -79,7 +81,7 @@ function GameCharacter() {
} }
} }
this._drawHead = function () { this._drawHead = function () {
fill(palette.head_color); fill(palette.ground_colors[this.curGroundIndex]);
ellipse( ellipse(
this.x, this.x,
this.y - this.y_step * 5, this.y - this.y_step * 5,
@ -267,10 +269,11 @@ function GameCharacter() {
} else if (this.isRight && !this.isLeft) { } else if (this.isRight && !this.isLeft) {
this.x += this.speed; this.x += this.speed;
} }
if (this.isJumping) { if (this.isJumping) {
this.memorizedPlatform = undefined;
this.y -= this.curJumpingStrength; this.y -= this.curJumpingStrength;
this.curJumpingStrength -= gravity; this.curJumpingStrength -= gravity;
// If the jump peak is reached, the character is considered to be falling instead.
if (this.curJumpingStrength <= 0) { if (this.curJumpingStrength <= 0) {
this.isFalling = true; this.isFalling = true;
this.isJumping = false; this.isJumping = false;
@ -278,11 +281,39 @@ function GameCharacter() {
} else if (this.isFalling) { } else if (this.isFalling) {
this.y -= this.curJumpingStrength; this.y -= this.curJumpingStrength;
this.curJumpingStrength -= gravity; this.curJumpingStrength -= gravity;
for (i = 0; i < platforms[this.curGroundIndex].length; i++) {
// If a character is falling above some platform, memorize it
if (platforms[this.curGroundIndex][i].isBelow(this)) {
this.memorizedPlatform = platforms[this.curGroundIndex][i];
}
// If a character is below a memorized platform, make them stand on that platform
else if (typeof memorizedPlatform !== 'undefined' &&
this.memorizedPlatform.isAbove(this)) {
this.y = this.memorizedPlatform.y;
this.isFalling = false;
this.curJumpingStrength = 0;
this.isOnPlatform = true;
}
}
// if a character is below the ground, make them stand on the ground
if (this.y >= this.getCurGroundY()) { if (this.y >= this.getCurGroundY()) {
this.y = this.getCurGroundY(); this.y = this.getCurGroundY();
this.isFalling = false; this.isFalling = false;
this.curJumpingStrength = 0;
this.isOnPlatform = false;
} }
} else { } else if (this.isOnPlatform) {
// Not plummeting, standing or falling means being on a platform.
// Perform checks whether the player is still on the same platform,
// and fall them in case they aren't.
if (this.x < this.memorizedPlatform.x - this.memorizedPlatform.width
||
this.x > this.memorizedPlatform.x + this.memorizedPlatform.width
||
this.curGroundIndex != this.memorizedPlatform.curGroundIndex) {
this.isFalling = true;
this.isOnPlatform = false;
}
} }
} }
this._checkPlayerDie(); this._checkPlayerDie();
@ -294,6 +325,7 @@ function GameCharacter() {
this.y += 3; this.y += 3;
} }
}; };
this.getCurGroundY = function () { this.getCurGroundY = function () {
return groundPositions[this.curGroundIndex]; return groundPositions[this.curGroundIndex];
}; };
@ -336,7 +368,7 @@ function Platform(curGroundIndex, x, width, y) {
fill(palette.ground_colors[curGroundIndex]); fill(palette.ground_colors[curGroundIndex]);
rect( rect(
this.x - this.width, this.x - this.width,
this.y + height / 100, this.y,
width * 2, width * 2,
height / 100, height / 100,
); );
@ -584,6 +616,7 @@ function startGame(level_start) {
gameChar.isJumping = false; gameChar.isJumping = false;
gameChar.isLeft = false; gameChar.isLeft = false;
gameChar.isRight = false; gameChar.isRight = false;
gameChar.isOnPlatform = false;
} }
} }
function draw() { function draw() {
@ -690,7 +723,8 @@ function drawCharTrace() {
function checkRiver(t_river) { function checkRiver(t_river) {
if ( if (
gameChar.x > river.x - river.width / 2 && gameChar.x > river.x - river.width / 2 &&
gameChar.x < river.x + river.width / 2 gameChar.x < river.x + river.width / 2 &&
gameChar.y > floorPos_y
) { ) {
gameChar.isPlummeting = true; gameChar.isPlummeting = true;
} }