Finished platforms, refactoring
This commit is contained in:
parent
3f733eea06
commit
927e3a2c84
25
sketch.js
25
sketch.js
@ -270,7 +270,6 @@ function GameCharacter() {
|
|||||||
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 the jump peak is reached, the character is considered to be falling instead.
|
||||||
@ -283,12 +282,12 @@ function GameCharacter() {
|
|||||||
this.curJumpingStrength -= gravity;
|
this.curJumpingStrength -= gravity;
|
||||||
for (i = 0; i < platforms[this.curGroundIndex].length; i++) {
|
for (i = 0; i < platforms[this.curGroundIndex].length; i++) {
|
||||||
// If a character is falling above some platform, memorize it
|
// 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];
|
this.memorizedPlatform = platforms[this.curGroundIndex][i];
|
||||||
}
|
}
|
||||||
// If a character is below a memorized platform, make them stand on that platform
|
// If a character is below a memorized platform, make them stand on that platform
|
||||||
else if (typeof memorizedPlatform !== 'undefined' &&
|
if (typeof this.memorizedPlatform !== 'undefined' &&
|
||||||
this.memorizedPlatform.isAbove(this)) {
|
this.memorizedPlatform.isBelow(this)) {
|
||||||
this.y = this.memorizedPlatform.y;
|
this.y = this.memorizedPlatform.y;
|
||||||
this.isFalling = false;
|
this.isFalling = false;
|
||||||
this.curJumpingStrength = 0;
|
this.curJumpingStrength = 0;
|
||||||
@ -306,13 +305,12 @@ function GameCharacter() {
|
|||||||
// Not plummeting, standing or falling means being on a platform.
|
// Not plummeting, standing or falling means being on a platform.
|
||||||
// Perform checks whether the player is still on the same platform,
|
// Perform checks whether the player is still on the same platform,
|
||||||
// and fall them in case they aren't.
|
// and fall them in case they aren't.
|
||||||
if (this.x < this.memorizedPlatform.x - this.memorizedPlatform.width
|
if (!this.memorizedPlatform._isWithinX(this)
|
||||||
||
|
|
||||||
this.x > this.memorizedPlatform.x + this.memorizedPlatform.width
|
|
||||||
||
|
||
|
||||||
this.curGroundIndex != this.memorizedPlatform.curGroundIndex) {
|
this.curGroundIndex != this.memorizedPlatform.curGroundIndex) {
|
||||||
this.isFalling = true;
|
this.isFalling = true;
|
||||||
this.isOnPlatform = false;
|
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) {
|
function Platform(curGroundIndex, x, width, y) {
|
||||||
this.curGroundIndex = curGroundIndex;
|
this.curGroundIndex = curGroundIndex;
|
||||||
this.x = x;
|
this.x = x;
|
||||||
@ -390,21 +392,24 @@ function Platform(curGroundIndex, x, width, y) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
this._isWithinX = function (who) {
|
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 true;
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
this.isBelow = function (who) {
|
this.isBelow = function (who) {
|
||||||
if (this._isWithinX(who) && who.y > this.y) {
|
if (this._isWithinX(who) && who.y >= this.y) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
this.isAbove = function (who) {
|
this.isAbove = function (who) {
|
||||||
if (this._isWithinX(who) && who.y < this.y) {
|
if (this._isWithinX(who) && who.y < this.y) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -989,7 +994,6 @@ function drawTrees() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function drawPlatforms(rowIndex) {
|
function drawPlatforms(rowIndex) {
|
||||||
push();
|
push();
|
||||||
for (k = 0; k < platforms[rowIndex].length; k++) {
|
for (k = 0; k < platforms[rowIndex].length; k++) {
|
||||||
@ -1004,7 +1008,6 @@ function drawPlatformsShadows(rowIndex) {
|
|||||||
}
|
}
|
||||||
pop();
|
pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
function renderFlagpole() {
|
function renderFlagpole() {
|
||||||
// NB - This function is implemented a bit differently than how it was requested in Part 6.
|
// 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
|
// It has no states to switch between - instead, the flag gradually goes into the opposite direction
|
||||||
|
Loading…
Reference in New Issue
Block a user