diff --git a/assets/js/platformer3x/Character.js b/assets/js/platformer3x/Character.js index 7bc6b7c8..c38def1c 100644 --- a/assets/js/platformer3x/Character.js +++ b/assets/js/platformer3x/Character.js @@ -21,6 +21,9 @@ class Character extends GameObject { // gravity for character enabled by default this.gravityEnabled = true; this.onTop = false; + + // Velocity + this.yv = 0; } setSpriteAnimation(animation) { @@ -133,11 +136,24 @@ class Character extends GameObject { /** * Update the y posiion and update y related states */ + colY() { + while (!this.bottom > this.y) { + if (this.yv < 0) { + this.yv += 1; + } else { + this.yv -= 1; + } + } + } + updateY() { if (this.bottom > this.y && this.gravityEnabled) { - this.y += GameEnv.gravity; + this.yv += GameEnv.gravity; + this.yv *= 0.9; this.onTop = false; } else { + this.colY(); + this.yv = 0; this.onTop = true; } } diff --git a/assets/js/platformer3x/PlayerBase.js b/assets/js/platformer3x/PlayerBase.js index e244244c..df2b643b 100644 --- a/assets/js/platformer3x/PlayerBase.js +++ b/assets/js/platformer3x/PlayerBase.js @@ -69,6 +69,10 @@ export class PlayerBase extends Character { // Add event listeners document.addEventListener('keydown', this.keydownListener); document.addEventListener('keyup', this.keyupListener); + + // Velocity + this.xv = 0; + this.yv = 0; } /** @@ -103,11 +107,6 @@ export class PlayerBase extends Character { /** * gameLoop helper: Udate Player jump height */ - updateJump() { - // Jump height is 35% of the screen bottom, same as screen height - this.setY(this.y - (this.bottom * 0.35)); - } - /** * gameLoop: updates the player's movement based on the player's animation (idle, walk, run, jump, etc.) */ @@ -120,7 +119,7 @@ export class PlayerBase extends Character { if (this.state.movement.up && !this.state.movement.falling) { // jump GameEnv.playSound("PlayerJump"); - this.updateJump(); + this.yv = GameEnv.bottom * -0.06; // start falling this.state.movement.falling = true; } @@ -129,16 +128,33 @@ export class PlayerBase extends Character { // Player is moving left if (this.state.direction === 'left' && this.state.movement.left && 'a' in this.pressedKeys) { // Decrease the player's x position according to run or walk animation and related speed - this.setX(this.x - (this.state.animation === 'run' ? this.runSpeed : this.speed)); + if (this.state.animation === 'run') { + this.xv -= this.runSpeed; + } else { + this.xv -= this.speed; + } // Player is moving right } else if (this.state.direction === 'right' && this.state.movement.right && 'd' in this.pressedKeys){ // Increase the player's x position according to run or walk animation and related speed - this.setX(this.x + (this.state.animation === 'run' ? this.runSpeed : this.speed)); + if (this.state.animation === 'run') { + this.xv += this.runSpeed; + } else { + this.xv += this.speed; + } } } - GameEnv.PlayerPosition.playerX = this.x - GameEnv.PlayerPosition.playerY = this.y + + // Update X + this.xv *= 0.8; + this.x += this.xv; + this.setX(this.x); + // Update Y + this.y += this.yv; + this.setY(this.y); + + GameEnv.PlayerPosition.playerX = this.x; + GameEnv.PlayerPosition.playerY = this.y; } /** @@ -335,16 +351,20 @@ export class PlayerBase extends Character { if (this.collisionData.touchPoints.this.onTopofOther) { this.state.movement = { up: false, down: false, left: true, right: true, falling: false}; this.gravityEnabled = false; + this.yv = 0; + this.y -= 1; // Player is touching the wall with right side } else if (this.collisionData.touchPoints.this.right) { this.state.movement = { up: false, down: false, left: true, right: false, falling: false}; - this.y -= 4; + this.x -= this.xv; + this.xv *= -1; // Player is touching the wall with left side } else if (this.collisionData.touchPoints.this.left) { this.state.movement = { up: false, down: false, left: false, right: true, falling: false}; - this.y -= 4; + this.x -= this.xv; + this.xv *= -1.5; } break; @@ -354,12 +374,18 @@ export class PlayerBase extends Character { if (this.collisionData.touchPoints.this.top && this.collisionData.touchPoints.other.bottom) { this.state.movement = { up: false, down: false, left: true, right: true, falling: false}; this.gravityEnabled = false; + this.y -= this.yv; + this.yv = 0; // Player is touching the wall with right side } else if (this.collisionData.touchPoints.this.right) { this.state.movement = { up: false, down: false, left: true, right: false, falling: false}; + this.x -= this.xv; + this.xv *= -1; // Player is touching the wall with left side } else if (this.collisionData.touchPoints.this.left) { this.state.movement = { up: false, down: false, left: false, right: true, falling: false}; + this.x -= this.xv; + this.xv *= -1; } break; @@ -369,6 +395,8 @@ export class PlayerBase extends Character { // Player is on the floor if (this.onTop) { this.state.movement = { up: false, down: false, left: true, right: true, falling: false}; + this.y -= this.yv; + this.yv = 0; // Player is falling, there are no collisions, but is in default state } else { this.state.movement = { up: false, down: false, left: true, right: true, falling: true}; diff --git a/assets/js/platformer3x/PlayerBoss.js b/assets/js/platformer3x/PlayerBoss.js index 454123ae..c88e08f0 100644 --- a/assets/js/platformer3x/PlayerBoss.js +++ b/assets/js/platformer3x/PlayerBoss.js @@ -31,14 +31,16 @@ export class PlayerBoss extends PlayerBase { updateJump() { let jumpHeightFactor; if (GameEnv.difficulty === "easy") { - jumpHeightFactor = 0.50; + jumpHeightFactor = 0.05; } else if (GameEnv.difficulty === "normal") { - jumpHeightFactor = 0.40; + jumpHeightFactor = 0.04; } if (GameEnv.currentLevel.tag == "boss") { - jumpHeightFactor = 0.50; + jumpHeightFactor = 0.05; } - this.setY(this.y - (this.bottom * jumpHeightFactor)); + this.yv = -this.bottom * jumpHeightFactor; + this.y += this.yv; + this.setY(this.y); } /** * @override @@ -123,11 +125,11 @@ export class PlayerBoss extends PlayerBase { // GoombaBounce deals with player.js and goomba.js if (GameEnv.goombaBounce === true) { GameEnv.goombaBounce = false; - this.y = this.y - 100; + this.y = -10; } if (GameEnv.goombaBounce1 === true) { GameEnv.goombaBounce1 = false; - this.y = this.y - 250 + this.yv = -25; } // 2. Player touches goomba sides of goomba } else if (this.collisionData.touchPoints.this.right || this.collisionData.touchPoints.this.left) { diff --git a/assets/js/platformer3x/PlayerGreece.js b/assets/js/platformer3x/PlayerGreece.js index 3eb89c39..f84a5986 100644 --- a/assets/js/platformer3x/PlayerGreece.js +++ b/assets/js/platformer3x/PlayerGreece.js @@ -33,16 +33,19 @@ export class PlayerGreece extends PlayerBase { * @override * gameLoop helper: Update Player jump height, replaces PlayerBase updateJump using settings from GameEnv */ - updateJump() { + updateJump() { let jumpHeightFactor; if (GameEnv.difficulty === "easy") { - jumpHeightFactor = 0.50; + jumpHeightFactor = 0.05; } else if (GameEnv.difficulty === "normal") { - jumpHeightFactor = 0.40; - } else { - jumpHeightFactor = 0.30; + jumpHeightFactor = 0.04; } - this.setY(this.y - (this.bottom * jumpHeightFactor)); + if(GameEnv.currentLevel.tag == "boss"){ + jumpHeightFactor = 0; + } + this.yv = -this.bottom * jumpHeightFactor + this.y += this.yv; + this.setY(this.y); } /** diff --git a/assets/js/platformer3x/PlayerHills.js b/assets/js/platformer3x/PlayerHills.js index 534bfa03..48efbfdc 100644 --- a/assets/js/platformer3x/PlayerHills.js +++ b/assets/js/platformer3x/PlayerHills.js @@ -35,14 +35,16 @@ export class PlayerHills extends PlayerBase { updateJump() { let jumpHeightFactor; if (GameEnv.difficulty === "easy") { - jumpHeightFactor = 0.50; + jumpHeightFactor = 0.05; } else if (GameEnv.difficulty === "normal") { - jumpHeightFactor = 0.40; + jumpHeightFactor = 0.04; } if(GameEnv.currentLevel.tag == "boss"){ jumpHeightFactor = 0; } - this.setY(this.y - (this.bottom * jumpHeightFactor)); + this.yv = -this.bottom * jumpHeightFactor + this.y += this.yv; + this.setY(this.y); } /** @@ -95,11 +97,11 @@ export class PlayerHills extends PlayerBase { // GoombaBounce deals with player.js and goomba.js if (GameEnv.goombaBounce === true) { GameEnv.goombaBounce = false; - this.y = this.y - 100; + this.yv = -10; } if (GameEnv.goombaBounce1 === true) { GameEnv.goombaBounce1 = false; - this.y = this.y - 250 + this.yv = -25; } // 2. Player touches goomba sides of goomba } else if (this.collisionData.touchPoints.this.right || this.collisionData.touchPoints.this.left) { @@ -127,11 +129,11 @@ export class PlayerHills extends PlayerBase { // GoombaBounce deals with player.js and goomba.js if (GameEnv.goombaBounce === true) { GameEnv.goombaBounce = false; - this.y = this.y - 100; + this.y = -10; } if (GameEnv.goombaBounce1 === true) { GameEnv.goombaBounce1 = false; - this.y = this.y - 250 + this.y = -25; } // 2. Player touches goomba sides of goomba } else if (this.collisionData.touchPoints.this.right || this.collisionData.touchPoints.this.left) { diff --git a/assets/js/platformer3x/PlayerIce.js b/assets/js/platformer3x/PlayerIce.js index 3e6bc144..9f57b926 100644 --- a/assets/js/platformer3x/PlayerIce.js +++ b/assets/js/platformer3x/PlayerIce.js @@ -40,13 +40,15 @@ export class PlayerIce extends PlayerBase { updateJump() { let jumpHeightFactor; if (GameEnv.difficulty === "easy") { - jumpHeightFactor = 0.50; + jumpHeightFactor = 0.05; } else if (GameEnv.difficulty === "normal") { - jumpHeightFactor = 0.40; + jumpHeightFactor = 0.04; } else { - jumpHeightFactor = 0.30; + jumpHeightFactor = 0.03; } - this.setY(this.y - (this.bottom * jumpHeightFactor)); + this.yv = -this.bottom * jumpHeightFactor; + this.y += this.yv; + this.setY(this.y); } update(){ super.update(); @@ -103,11 +105,11 @@ export class PlayerIce extends PlayerBase { // GoombaBounce deals with player.js and goomba.js if (GameEnv.goombaBounce === true) { GameEnv.goombaBounce = false; - this.y = this.y - 100; + this.y = -10; } if (GameEnv.goombaBounce1 === true) { GameEnv.goombaBounce1 = false; - this.y = this.y - 250 + this.y = -25; } // 2. Player touches goomba sides of goomba } else if (this.collisionData.touchPoints.this.right || this.collisionData.touchPoints.this.left) { diff --git a/assets/js/platformer3x/PlayerMini.js b/assets/js/platformer3x/PlayerMini.js index 7b3b3d5a..12ecc73d 100644 --- a/assets/js/platformer3x/PlayerMini.js +++ b/assets/js/platformer3x/PlayerMini.js @@ -39,13 +39,15 @@ export class PlayerMini extends PlayerBase { updateJump() { let jumpHeightFactor; if (GameEnv.difficulty === "easy") { - jumpHeightFactor = 0.50; + jumpHeightFactor = 0.05; } else if (GameEnv.difficulty === "normal") { - jumpHeightFactor = 0.40; + jumpHeightFactor = 0.04; } else { - jumpHeightFactor = 0.30; + jumpHeightFactor = 0.03; } - this.setY(this.y - (this.bottom * jumpHeightFactor)); + this.yv = -this.bottom * jumpHeightFactor; + this.y += this.yv; + this.setY(this.y); } update(){ super.update(); @@ -106,11 +108,11 @@ export class PlayerMini extends PlayerBase { // GoombaBounce deals with player.js and goomba.js if (GameEnv.goombaBounce === true) { GameEnv.goombaBounce = false; - this.y = this.y - 100; + this.y = -10; } if (GameEnv.goombaBounce1 === true) { GameEnv.goombaBounce1 = false; - this.y = this.y - 250 + this.y = -25; } // 2. Player touches goomba sides of goomba } else if (this.collisionData.touchPoints.this.right || this.collisionData.touchPoints.this.left) { diff --git a/assets/js/platformer3x/PlayerMiniHogwarts.js b/assets/js/platformer3x/PlayerMiniHogwarts.js index 061b279c..ed5bf789 100644 --- a/assets/js/platformer3x/PlayerMiniHogwarts.js +++ b/assets/js/platformer3x/PlayerMiniHogwarts.js @@ -39,13 +39,15 @@ export class PlayerMiniHogwarts extends PlayerBase { updateJump() { let jumpHeightFactor; if (GameEnv.difficulty === "easy") { - jumpHeightFactor = 0.50; + jumpHeightFactor = 0.05; } else if (GameEnv.difficulty === "normal") { - jumpHeightFactor = 0.40; + jumpHeightFactor = 0.04; } else { - jumpHeightFactor = 0.30; + jumpHeightFactor = 0.03; } - this.setY(this.y - (this.bottom * jumpHeightFactor)); + this.yv = -this.bottom * jumpHeightFactor; + this.y += this.yv; + this.setY(this.y); } update(){ super.update(); @@ -107,11 +109,11 @@ export class PlayerMiniHogwarts extends PlayerBase { // GoombaBounce deals with player.js and goomba.js if (GameEnv.goombaBounce === true) { GameEnv.goombaBounce = false; - this.y = this.y - 100; + this.y = -10; } if (GameEnv.goombaBounce1 === true) { GameEnv.goombaBounce1 = false; - this.y = this.y - 250 + this.y = -25; } // 2. Player touches goomba sides of goomba } else if (this.collisionData.touchPoints.this.right || this.collisionData.touchPoints.this.left) { diff --git a/assets/js/platformer3x/PlayerQuidditch.js b/assets/js/platformer3x/PlayerQuidditch.js index 33a3fda0..d47b5fb1 100644 --- a/assets/js/platformer3x/PlayerQuidditch.js +++ b/assets/js/platformer3x/PlayerQuidditch.js @@ -35,13 +35,15 @@ export class PlayerQuidditch extends PlayerBase { updateJump() { let jumpHeightFactor; if (GameEnv.difficulty === "easy") { - jumpHeightFactor = 0.50; + jumpHeightFactor = 0.05; } else if (GameEnv.difficulty === "normal") { - jumpHeightFactor = 0.40; + jumpHeightFactor = 0.04; } else { - jumpHeightFactor = 0.30; + jumpHeightFactor = 0.03; } - this.setY(this.y - (this.bottom * jumpHeightFactor)); + this.yv = -this.bottom * jumpHeightFactor; + this.y += this.yv; + this.setY(this.y); } /** @@ -119,11 +121,11 @@ export class PlayerQuidditch extends PlayerBase { // GoombaBounce deals with player.js and goomba.js if (GameEnv.goombaBounce === true) { GameEnv.goombaBounce = false; - this.y = this.y - 100; + this.y = -10; } if (GameEnv.goombaBounce1 === true) { GameEnv.goombaBounce1 = false; - this.y = this.y - 250; + this.y = -25; } // 2. Player touches goomba sides of goomba } else if (this.collisionData.touchPoints.this.right || this.collisionData.touchPoints.this.left) { diff --git a/assets/js/platformer3x/PlayerSkibidi.js b/assets/js/platformer3x/PlayerSkibidi.js index f10c2060..8ed87aaa 100644 --- a/assets/js/platformer3x/PlayerSkibidi.js +++ b/assets/js/platformer3x/PlayerSkibidi.js @@ -38,13 +38,15 @@ export class PlayerSkibidi extends PlayerBaseOneD { /// Using PlayerBaseOneD add updateJump() { let jumpHeightFactor; if (GameEnv.difficulty === "easy") { - jumpHeightFactor = 0.50; + jumpHeightFactor = 0.05; } else if (GameEnv.difficulty === "normal") { - jumpHeightFactor = 0.40; + jumpHeightFactor = 0.04; } else { - jumpHeightFactor = 0.30; + jumpHeightFactor = 0.03; } - this.setY(this.y - (this.bottom * jumpHeightFactor)); + this.yv = -this.bottom * jumpHeightFactor; + this.y += this.yv; + this.setY(this.y); } updateFrameX(){ @@ -112,11 +114,11 @@ export class PlayerSkibidi extends PlayerBaseOneD { /// Using PlayerBaseOneD add // GoombaBounce deals with player.js and goomba.js if (GameEnv.goombaBounce === true) { GameEnv.goombaBounce = false; - this.y = this.y - 100; + this.y = -10; } if (GameEnv.goombaBounce1 === true) { GameEnv.goombaBounce1 = false; - this.y = this.y - 250 + this.y = -25; } // 2. Player touches goomba sides of goomba } else if (this.collisionData.touchPoints.this.right || this.collisionData.touchPoints.this.left) { diff --git a/assets/js/platformer3x/PlayerWinter.js b/assets/js/platformer3x/PlayerWinter.js index 7e10109c..2163f9f4 100644 --- a/assets/js/platformer3x/PlayerWinter.js +++ b/assets/js/platformer3x/PlayerWinter.js @@ -32,17 +32,18 @@ export class PlayerWinter extends PlayerBase { * @override * gameLoop helper: Update Player jump height, replaces PlayerBase updateJump using settings from GameEnv */ - updateJump() { + updateJump() { let jumpHeightFactor; if (GameEnv.difficulty === "easy") { - jumpHeightFactor = 0.50; + jumpHeightFactor = 0.05; } else if (GameEnv.difficulty === "normal") { - jumpHeightFactor = 0.40; + jumpHeightFactor = 0.04; + } else { + jumpHeightFactor = 0.03; } - if (GameEnv.currentLevel.tag == "narwhalboss") { - jumpHeightFactor = 0.50; - } - this.setY(this.y - (this.bottom * jumpHeightFactor)); + this.yv = -this.bottom * jumpHeightFactor; + this.y += this.yv; + this.setY(this.y); } /** @@ -122,11 +123,11 @@ export class PlayerWinter extends PlayerBase { // GoombaBounce deals with player.js and goomba.js if (GameEnv.goombaBounce === true) { GameEnv.goombaBounce = false; - this.y = this.y - 100; + this.y = -10; } if (GameEnv.goombaBounce1 === true) { GameEnv.goombaBounce1 = false; - this.y = this.y - 250 + this.y = -25; } // 2. Player touches goomba sides of goomba } else if (this.collisionData.touchPoints.this.right || this.collisionData.touchPoints.this.left) { @@ -154,11 +155,11 @@ export class PlayerWinter extends PlayerBase { // GoombaBounce deals with player.js and goomba.js if (GameEnv.goombaBounce === true) { GameEnv.goombaBounce = false; - this.y = this.y - 100; + this.y = -10; } if (GameEnv.goombaBounce1 === true) { GameEnv.goombaBounce1 = false; - this.y = this.y - 250 + this.y = -25; } // 2. Player touches goomba sides of goomba } else if (this.collisionData.touchPoints.this.right || this.collisionData.touchPoints.this.left) { diff --git a/assets/js/platformer3x/PlayerZombie.js b/assets/js/platformer3x/PlayerZombie.js index 4fe81301..5c9a061f 100644 --- a/assets/js/platformer3x/PlayerZombie.js +++ b/assets/js/platformer3x/PlayerZombie.js @@ -29,17 +29,18 @@ export class PlayerZombie extends PlayerBaseOneD { * @override * gameLoop helper: Update Player jump height, replaces PlayerBase updateJump using settings from GameEnv */ - updateJump() { + updateJump() { let jumpHeightFactor; if (GameEnv.difficulty === "easy") { - jumpHeightFactor = 0.50; + jumpHeightFactor = 0.05; } else if (GameEnv.difficulty === "normal") { - jumpHeightFactor = 0.40; + jumpHeightFactor = 0.04; + } else { + jumpHeightFactor = 0.05; } - if (GameEnv.currentLevel.tag == "boss") { - jumpHeightFactor = 0.50; - } - this.setY(this.y - (this.bottom * jumpHeightFactor)); + this.yv = -this.bottom * jumpHeightFactor; + this.y += this.yv; + this.setY(this.y); } /** * @override @@ -252,11 +253,11 @@ export class PlayerZombie extends PlayerBaseOneD { // GoombaBounce deals with player.js and goomba.js if (GameEnv.goombaBounce === true) { GameEnv.goombaBounce = false; - this.y = this.y - 100; + this.y = -10; } if (GameEnv.goombaBounce1 === true) { GameEnv.goombaBounce1 = false; - this.y = this.y - 250 + this.y = -25; } // 2. Player touches goomba sides of goomba } else if (this.collisionData.touchPoints.this.right || this.collisionData.touchPoints.this.left) {