Skip to content

Commit

Permalink
fix: player jump mismatch with original game
Browse files Browse the repository at this point in the history
  • Loading branch information
Autapomorph committed Jul 28, 2020
1 parent d4eb79f commit 73db197
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/config/game/prefabs/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ const PLAYER = {
},
JUMP: {
VELOCITY: {
Y: 1675 * -1,
MAX: 1500,
START: 1500 * 0.9 * -1,
SPEED_FALL: 1500 * 0.75,
INCREASE_INCREMENT: 75 * -1,
INCREASE_THRESHOLD: 200 * -1,
},
ACCELERATION: 3200,
},
Expand Down
3 changes: 1 addition & 2 deletions src/prefabs/horizon/obstacles/bird/AnimationManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ class AnimationManager {
const bodyZoneXOffset = 12;
const bodyZoneYOffset = 12;

// set body offset & size to match sprite pos & size
// Resize body to match frame dimensions
if (name === AnimationManager.CONFIG.FRAMES.FLYING[0]) {
const frameYOffset = 12;

body.setSize(bodyZoneWidth, bodyZoneHeight);
body.setOffset(bodyZoneXOffset, frameYOffset + bodyZoneYOffset);
} else if (name === AnimationManager.CONFIG.FRAMES.FLYING[1]) {
Expand Down
13 changes: 11 additions & 2 deletions src/prefabs/player/InputManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,17 @@ class InputManager {
} else {
player.run();
}
} else {
player.idle();
return;
}

if (!player.isOnFloor) {
if (this.isDuckKeyPressed) {
player.speedFall();
} else if (this.isJumpKeyPressed) {
player.jump();
} else {
player.idle();
}
}
}

Expand Down
25 changes: 23 additions & 2 deletions src/prefabs/player/PhysicsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ class PhysicsManager {
// Init physics
this.scene.physics.world.enable(player);
player.setCollideWorldBounds(true);
player.setGravityY(PhysicsManager.CONFIG.GRAVITY.Y);
player.setGravityY(PhysicsManager.CONFIG.GRAVITY.Y * 2);
player.setAccelerationY(PhysicsManager.CONFIG.JUMP.ACCELERATION);
// player.setAccelerationY(5000);
player.setMaxVelocity(0, PhysicsManager.CONFIG.JUMP.VELOCITY.MAX);
}

/**
Expand All @@ -35,7 +37,26 @@ class PhysicsManager {
* Handle player jump
*/
jump() {
this.player.setVelocityY(PhysicsManager.CONFIG.JUMP.VELOCITY.Y);
// Handle jumping while on floor
if (this.isOnFloor) {
// this.player.setVelocityY(PhysicsManager.CONFIG.JUMP.VELOCITY.MAX * -1);
this.player.setVelocityY(PhysicsManager.CONFIG.JUMP.VELOCITY.START);
return;
}

// Handle jumping while mid-air
const { INCREASE_THRESHOLD, INCREASE_INCREMENT } = PhysicsManager.CONFIG.JUMP.VELOCITY;
const velocityY = this.player.body.velocity.y;
if (velocityY < INCREASE_THRESHOLD) {
this.player.setVelocityY(velocityY + INCREASE_INCREMENT);
}
}

/**
* Handle speed fall
*/
speedFall() {
this.player.setVelocityY(PhysicsManager.CONFIG.JUMP.VELOCITY.SPEED_FALL);
}

/**
Expand Down
13 changes: 10 additions & 3 deletions src/prefabs/player/Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,27 @@ class Player extends Phaser.Physics.Arcade.Sprite {
* Set player jumping
*/
jump() {
// Allow intro to start after 1st landing
if (!this.isInitialJump && this.scene.intro.isWaiting) {
// Allow intro to start after 1st landing even if holding jump key
if (this.isOnFloor && !this.isInitialJump && this.scene.intro.isWaiting) {
this.run();
return;
}
this.isInitialJump = false;

this.setState(Player.CONFIG.STATES.JUMPING);
this.physicsManager.jump();
if (!this.scene.intro.isWaiting) {
if (!this.scene.intro.isWaiting && this.isOnFloor) {
this.scene.events.emit(CONFIG.EVENTS.PLAYER_ACTION);
}
}

/**
* Set player speed fall
*/
speedFall() {
this.physicsManager.speedFall();
}

/**
* Set player dead | Handle gameover
*/
Expand Down

0 comments on commit 73db197

Please sign in to comment.