From: David NĂ©grier Date: Sat, 18 Apr 2020 15:16:39 +0000 (+0200) Subject: Computing movement amount from framerate X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=46fcb86b28ecaf58cb06e97a2737c579ae9c2889;p=libreadventure.git Computing movement amount from framerate Depending on the amount of power a computer has, the framerate will not be the same. Hence, the amount of movement of a user should be constant on each frame. If a frame was slow to print, the movement should be higher to keep a constant speed. This PR takes the framerate into account when moving the players. --- diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index a08c8fd..337d765 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -182,8 +182,12 @@ export class GameScene extends Phaser.Scene implements GameSceneInterface{ }); } - update() : void { - this.CurrentPlayer.moveUser(); + /** + * @param time + * @param delta The delta time in ms since the last frame. This is a smoothed and capped value based on the FPS rate. + */ + update(time: number, delta: number) : void { + this.CurrentPlayer.moveUser(delta); } /** diff --git a/front/src/Phaser/Player/Player.ts b/front/src/Phaser/Player/Player.ts index 4cd1a6a..e99fa42 100644 --- a/front/src/Phaser/Player/Player.ts +++ b/front/src/Phaser/Player/Player.ts @@ -9,7 +9,7 @@ export interface CurrentGamerInterface extends PlayableCaracter{ userId : string; PlayerValue : string; initAnimation() : void; - moveUser() : void; + moveUser(delta: number) : void; say(text : string) : void; } @@ -57,31 +57,32 @@ export class Player extends PlayableCaracter implements CurrentGamerInterface, G }) } - moveUser(): void { + moveUser(delta: number): void { //if user client on shift, camera and player speed let haveMove = false; let direction = null; let activeEvents = this.userInputManager.getEventListForGameTick(); - let speedMultiplier = activeEvents.get(UserInputEvent.SpeedUp) ? 500 : 100; + let speedMultiplier = activeEvents.get(UserInputEvent.SpeedUp) ? 25 : 9; + let moveAmount = speedMultiplier * delta; if (activeEvents.get(UserInputEvent.MoveUp)) { - this.move(0, -speedMultiplier); + this.move(0, -moveAmount); haveMove = true; direction = PlayerAnimationNames.WalkUp; } if (activeEvents.get(UserInputEvent.MoveLeft)) { - this.move(-speedMultiplier, 0); + this.move(-moveAmount, 0); haveMove = true; direction = PlayerAnimationNames.WalkLeft; } if (activeEvents.get(UserInputEvent.MoveDown)) { - this.move(0, speedMultiplier); + this.move(0, moveAmount); haveMove = true; direction = PlayerAnimationNames.WalkDown; } if (activeEvents.get(UserInputEvent.MoveRight)) { - this.move(speedMultiplier, 0); + this.move(moveAmount, 0); haveMove = true; direction = PlayerAnimationNames.WalkRight; } @@ -103,4 +104,4 @@ export class Player extends PlayableCaracter implements CurrentGamerInterface, G this.setX(MessageUserPosition.position.x); this.setY(MessageUserPosition.position.y); } -} \ No newline at end of file +}