From d64e782e1f216484d32825347b81a7191f6f7bed Mon Sep 17 00:00:00 2001 From: jm1021 Date: Sun, 24 Mar 2024 06:12:15 -0700 Subject: [PATCH] center falling on tube and tree --- assets/js/platformer2x/GameObject.js | 8 +++--- assets/js/platformer2x/Player.js | 39 +++------------------------- 2 files changed, 9 insertions(+), 38 deletions(-) diff --git a/assets/js/platformer2x/GameObject.js b/assets/js/platformer2x/GameObject.js index 39b24900..7255c616 100644 --- a/assets/js/platformer2x/GameObject.js +++ b/assets/js/platformer2x/GameObject.js @@ -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; @@ -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 && @@ -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, }, }, }; diff --git a/assets/js/platformer2x/Player.js b/assets/js/platformer2x/Player.js index d464e6e3..f44da30a 100644 --- a/assets/js/platformer2x/Player.js +++ b/assets/js/platformer2x/Player.js @@ -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; @@ -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 @@ -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") { @@ -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;