From 3f733eea06f871a8ad66f66fb175494cafde8ade Mon Sep 17 00:00:00 2001 From: Mottributo <87079566+Mottributo@users.noreply.github.com> Date: Sun, 26 Mar 2023 19:36:47 +0300 Subject: [PATCH] implementing platforms 50% done --- sketch.js | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/sketch.js b/sketch.js index cc3565e..9ff608e 100644 --- a/sketch.js +++ b/sketch.js @@ -40,7 +40,9 @@ function GameCharacter() { this.isFalling = false; this.isJumping = false; this.isPlummeting = false; + this.isOnPlatform = false; this.curGroundIndex = 0; + this.memorizedPlatform; this._updateSprite = function () { if (this.isPlummeting) { @@ -79,7 +81,7 @@ function GameCharacter() { } } this._drawHead = function () { - fill(palette.head_color); + fill(palette.ground_colors[this.curGroundIndex]); ellipse( this.x, this.y - this.y_step * 5, @@ -267,10 +269,11 @@ function GameCharacter() { } else if (this.isRight && !this.isLeft) { this.x += this.speed; } - if (this.isJumping) { + this.memorizedPlatform = undefined; this.y -= this.curJumpingStrength; this.curJumpingStrength -= gravity; + // If the jump peak is reached, the character is considered to be falling instead. if (this.curJumpingStrength <= 0) { this.isFalling = true; this.isJumping = false; @@ -278,11 +281,39 @@ function GameCharacter() { } else if (this.isFalling) { this.y -= this.curJumpingStrength; 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()) { this.y = this.getCurGroundY(); 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(); @@ -294,6 +325,7 @@ function GameCharacter() { this.y += 3; } }; + this.getCurGroundY = function () { return groundPositions[this.curGroundIndex]; }; @@ -336,7 +368,7 @@ function Platform(curGroundIndex, x, width, y) { fill(palette.ground_colors[curGroundIndex]); rect( this.x - this.width, - this.y + height / 100, + this.y, width * 2, height / 100, ); @@ -584,6 +616,7 @@ function startGame(level_start) { gameChar.isJumping = false; gameChar.isLeft = false; gameChar.isRight = false; + gameChar.isOnPlatform = false; } } function draw() { @@ -690,7 +723,8 @@ function drawCharTrace() { function checkRiver(t_river) { if ( 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; }