From 927e3a2c84ed3e94c5bc614eb9d295926d6fe091 Mon Sep 17 00:00:00 2001 From: Mottributo <87079566+Mottributo@users.noreply.github.com> Date: Sun, 26 Mar 2023 20:40:53 +0300 Subject: [PATCH] Finished platforms, refactoring --- sketch.js | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/sketch.js b/sketch.js index 9ff608e..047aedf 100644 --- a/sketch.js +++ b/sketch.js @@ -270,7 +270,6 @@ function GameCharacter() { 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. @@ -283,12 +282,12 @@ function GameCharacter() { 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)) { + if (platforms[this.curGroundIndex][i].isAbove(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)) { + if (typeof this.memorizedPlatform !== 'undefined' && + this.memorizedPlatform.isBelow(this)) { this.y = this.memorizedPlatform.y; this.isFalling = false; this.curJumpingStrength = 0; @@ -306,13 +305,12 @@ function GameCharacter() { // 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 + if (!this.memorizedPlatform._isWithinX(this) || this.curGroundIndex != this.memorizedPlatform.curGroundIndex) { this.isFalling = true; this.isOnPlatform = false; + this.memorizedPlatform = undefined; } } } @@ -349,6 +347,10 @@ function GameCharacter() { } }; } +// NB: All the platforms mechanics were done +// before the task was seen on Week 20. +// Therefore, the implementation may +// differ from one in the lecture. function Platform(curGroundIndex, x, width, y) { this.curGroundIndex = curGroundIndex; this.x = x; @@ -390,21 +392,24 @@ function Platform(curGroundIndex, x, width, y) { }; this._isWithinX = function (who) { - if (who.x > this.x - this.width && who.x < this.x + this.width) { + if (who.x >= this.x - this.width && who.x <= this.x + this.width) { return true; } + return false; }; this.isBelow = function (who) { - if (this._isWithinX(who) && who.y > this.y) { + if (this._isWithinX(who) && who.y >= this.y) { return true; } + return false; }; this.isAbove = function (who) { if (this._isWithinX(who) && who.y < this.y) { return true; } + return false; }; } @@ -989,7 +994,6 @@ function drawTrees() { } } } - function drawPlatforms(rowIndex) { push(); for (k = 0; k < platforms[rowIndex].length; k++) { @@ -1004,7 +1008,6 @@ function drawPlatformsShadows(rowIndex) { } pop(); } - function renderFlagpole() { // NB - This function is implemented a bit differently than how it was requested in Part 6. // It has no states to switch between - instead, the flag gradually goes into the opposite direction