Sending position only every 200ms while moving

This commit is contained in:
David Négrier 2020-06-01 22:42:18 +02:00
parent 6f646745c1
commit 077b29bfbd
2 changed files with 41 additions and 3 deletions

View File

@ -1,13 +1,13 @@
const DEBUG_MODE: boolean = process.env.DEBUG_MODE as any === true; const DEBUG_MODE: boolean = process.env.DEBUG_MODE as any === true;
const API_URL = process.env.API_URL || "http://api.workadventure.localhost"; const API_URL = process.env.API_URL || "http://api.workadventure.localhost";
const ROOM = [process.env.ROOM || "THECODINGMACHINE"];
const RESOLUTION = 3; const RESOLUTION = 3;
const ZOOM_LEVEL = 1/*3/4*/; const ZOOM_LEVEL = 1/*3/4*/;
const POSITION_DELAY = 200; // Wait 200ms between sending position events
export { export {
DEBUG_MODE, DEBUG_MODE,
API_URL, API_URL,
RESOLUTION, RESOLUTION,
ZOOM_LEVEL, ZOOM_LEVEL,
ROOM POSITION_DELAY
} }

View File

@ -5,7 +5,7 @@ import {
MessageUserPositionInterface, PointInterface, PositionInterface MessageUserPositionInterface, PointInterface, PositionInterface
} from "../../Connection"; } from "../../Connection";
import {CurrentGamerInterface, GamerInterface, hasMovedEventName, Player} from "../Player/Player"; import {CurrentGamerInterface, GamerInterface, hasMovedEventName, Player} from "../Player/Player";
import { DEBUG_MODE, ZOOM_LEVEL} from "../../Enum/EnvironmentVariable"; import { DEBUG_MODE, ZOOM_LEVEL, POSITION_DELAY } from "../../Enum/EnvironmentVariable";
import {ITiledMap, ITiledMapLayer, ITiledTileSet} from "../Map/ITiledMap"; import {ITiledMap, ITiledMapLayer, ITiledTileSet} from "../Map/ITiledMap";
import {PLAYER_RESOURCES} from "../Entity/PlayableCaracter"; import {PLAYER_RESOURCES} from "../Entity/PlayableCaracter";
import Texture = Phaser.Textures.Texture; import Texture = Phaser.Textures.Texture;
@ -43,6 +43,15 @@ export class GameScene extends Phaser.Scene {
RoomId: string; RoomId: string;
instance: string; instance: string;
currentTick: number;
lastSentTick: number; // The last tick at which a position was sent.
lastMoveEventSent: HasMovedEvent = {
direction: '',
moving: false,
x: -1000,
y: -1000
}
PositionNextScene: Array<any> = new Array<any>(); PositionNextScene: Array<any> = new Array<any>();
static createFromUrl(mapUrlFile: string, instance: string): GameScene { static createFromUrl(mapUrlFile: string, instance: string): GameScene {
@ -323,6 +332,34 @@ export class GameScene extends Phaser.Scene {
} }
pushPlayerPosition(event: HasMovedEvent) { pushPlayerPosition(event: HasMovedEvent) {
if (this.lastMoveEventSent === event) {
return;
}
// If the player is not moving, let's send the info right now.
if (event.moving === false) {
this.doPushPlayerPosition(event);
return;
}
// If the player is moving, and if it changed direction, let's send an event
if (event.direction !== this.lastMoveEventSent.direction) {
this.doPushPlayerPosition(event);
return;
}
// If more than 200ms happened since last event sent
if (this.currentTick - this.lastSentTick >= POSITION_DELAY) {
this.doPushPlayerPosition(event);
return;
}
// Otherwise, do nothing.
}
private doPushPlayerPosition(event: HasMovedEvent): void {
this.lastMoveEventSent = event;
this.lastSentTick = this.currentTick;
this.GameManager.pushPlayerPosition(event); this.GameManager.pushPlayerPosition(event);
} }
@ -342,6 +379,7 @@ export class GameScene extends Phaser.Scene {
* @param delta The delta time in ms since the last frame. This is a smoothed and capped value based on the FPS rate. * @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 { update(time: number, delta: number) : void {
this.currentTick = time;
this.CurrentPlayer.moveUser(delta); this.CurrentPlayer.moveUser(delta);
let nextSceneKey = this.checkToExit(); let nextSceneKey = this.checkToExit();
if(nextSceneKey){ if(nextSceneKey){