Skip to content

Commit

Permalink
center falling on tube and tree
Browse files Browse the repository at this point in the history
  • Loading branch information
jm1021 committed Mar 24, 2024
1 parent 7ff9ae2 commit d64e782
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 38 deletions.
8 changes: 5 additions & 3 deletions assets/js/platformer2x/GameObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,11 @@ class GameObject {

// Calculate center points of rectangles
const thisCenterX = (thisRect.left + thisRect.right) / 2;
//const thisCenterY = (thisRect.top + thisRect.bottom) / 2;
const otherCenterX = (otherRect.left + otherRect.right) / 2;
//const otherCenterY = (otherRect.top + otherRect.bottom) / 2;

// Calculate new center points of rectangles
const thisRectWidth = thisRect.right - thisRect.left;
const thisRectLeftNew = otherCenterX - thisRectWidth / 2;

// Calculate hitbox constants
var widthPercentage = this.widthPercentage;
Expand Down Expand Up @@ -187,6 +189,7 @@ class GameObject {

// Determine hit and touch points of hit
this.collisionData = {
newX: thisRectLeftNew, // proportionally adjust left to center over other object
hit: (
thisLeft < otherRect.right &&
thisRight > otherRect.left &&
Expand All @@ -208,7 +211,6 @@ class GameObject {
bottom: (thisRect.bottom >= otherRect.top) && !(Math.abs(thisRect.bottom - otherRect.bottom) <= GameEnv.gravity),
left: thisCenterX < otherCenterX,
right: thisCenterX > otherCenterX,
x: otherRect.left,
},
},
};
Expand Down
39 changes: 4 additions & 35 deletions assets/js/platformer2x/Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,9 @@ export class Player extends Character {
*/
collisionAction() {
// Tube collision check
if (this.collisionData.touchPoints.other.id === "tube") {
if (this.collisionData.touchPoints.other.id === "tube"
|| this.collisionData.touchPoints.other.id === "tree") {

// Collision with the left side of the Tube
if (this.collisionData.touchPoints.other.left) {
this.movement.right = false;
Expand All @@ -240,7 +242,7 @@ export class Player extends Character {
}
// Collision with the top of the player
if (this.collisionData.touchPoints.other.bottom) {
this.x = this.collisionData.touchPoints.other.x;
this.x = this.collisionData.newX;
this.gravityEnabled = false; // stop gravity
// Pause for two seconds
setTimeout(() => { // animation in tube for 1 seconds
Expand All @@ -256,34 +258,6 @@ export class Player extends Character {
this.movement.right = true;
}

// Tree collision check
if (this.collisionData.touchPoints.other.id === "tree") {
// Collision with the left side of the tree
if (this.collisionData.touchPoints.other.left) {
this.movement.right = false;
}
// Collision with the right side of the tree
if (this.collisionData.touchPoints.other.right) {
this.movement.left = false;
}
// Collision with the top of the player
if (this.collisionData.touchPoints.other.bottom) {
this.x = this.collisionData.touchPoints.other.x;
this.gravityEnabled = false; // stop gravity
// Pause for two seconds
setTimeout(() => {
this.gravityEnabled = true;
setTimeout(() => { // move to end of screen for end of game detection
this.x = GameEnv.innerWidth + 1;
}, 500);
}, 500);
}
} else {
// Reset movement flags if not colliding with a tree
this.movement.left = true;
this.movement.right = true;
}

// Goomba collision check
// Checks if collision touchpoint id is either "goomba" or "flyingGoomba"
if (this.collisionData.touchPoints.other.id === "goomba" || this.collisionData.touchPoints.other.id === "flyingGoomba") {
Expand All @@ -309,11 +283,6 @@ export class Player extends Character {
}, 2000); // 2000 milliseconds = 2 seconds
}

//if (GameEnv.destroyedMushroom === true) {
//GameEnv.playMessage = true;
//}


if (this.collisionData.touchPoints.other.id === "jumpPlatform") {
if (this.collisionData.touchPoints.other.left) {
this.movement.right = false;
Expand Down

0 comments on commit d64e782

Please sign in to comment.