Also, refactoring JOIN_ROOM event to add complete position.
x: user x position on map
y: user y position on map
*/
- socket.on(SockerIoEvent.JOIN_ROOM, (roomId: any, answerFn): void => {
+ socket.on(SockerIoEvent.JOIN_ROOM, (message: any, answerFn): void => {
try {
+ let roomId = message.roomId;
+
if (typeof(roomId) !== 'string') {
socket.emit(SockerIoEvent.MESSAGE_ERROR, {message: 'Expected roomId as a string.'});
return;
}
+ let position = this.hydratePositionReceive(message.position);
+ if (position instanceof Error) {
+ socket.emit(SockerIoEvent.MESSAGE_ERROR, {message: position.message});
+ return;
+ }
let Client = (socket as ExSocketInterface);
this.leaveRoom(Client);
//join new previous room
- let world = this.joinRoom(Client, roomId);
+ let world = this.joinRoom(Client, roomId, position);
//add function to refresh position user in real time.
//this.refreshUserPosition(Client);
- let messageUserJoined = new MessageUserJoined(Client.id, Client.name, Client.character);
+ let messageUserJoined = new MessageUserJoined(Client.id, Client.name, Client.character, Client.position);
socket.to(roomId).emit(SockerIoEvent.JOIN_ROOM, messageUserJoined);
// sending to all clients in room except sender
Client.position = position;
- //refresh position of all user in all rooms in real time
- //this.refreshUserPosition(Client);
-
// update position in the world
let world = this.Worlds.get(Client.roomId);
if (!world) {
console.error("Could not find world with id '", Client.roomId, "'");
return;
}
- world.updatePosition(Client, Client.position);
+ world.updatePosition(Client, position);
socket.to(Client.roomId).emit(SockerIoEvent.USER_MOVED, new MessageUserMoved(Client.id, Client.position));
} catch (e) {
try {
let Client = (socket as ExSocketInterface);
- if (Client.roomId) {
- socket.to(Client.roomId).emit(SockerIoEvent.USER_LEFT, socket.id);
- }
-
//leave room
this.leaveRoom(Client);
}
}
- private joinRoom(Client : ExSocketInterface, roomId: string): World {
+ private joinRoom(Client : ExSocketInterface, roomId: string, position: Point): World {
//join user in room
Client.join(roomId);
Client.roomId = roomId;
- Client.position = new Point(-1000, -1000);
+ Client.position = position;
//check and create new world for a room
let world = this.Worlds.get(roomId)
//Hydrate and manage error
hydratePositionReceive(message: any): Point | Error {
try {
- if (!message.x || !message.y || !message.direction) {
+ if (!message.x || !message.y || !message.direction || message.moving === undefined) {
return new Error("invalid point message sent");
}
- return new Point(message.x, message.y, message.direction);
+ return new Point(message.x, message.y, message.direction, message.moving);
} catch (err) {
//TODO log error
return new Error(err);
+import {Point} from "./MessageUserPosition";
+import {PointInterface} from "_Model/Websocket/PointInterface";
export class MessageUserJoined {
- constructor(public userId: string, public name: string, public character: string) {
+ constructor(public userId: string, public name: string, public character: string, public position: PointInterface) {
}
}
import {PointInterface} from "./PointInterface";
export class Point implements PointInterface{
- constructor(public x : number, public y : number, public direction : string = "none") {
+ constructor(public x : number, public y : number, public direction : string = "none", public moving : boolean = false) {
}
}
const SocketIo = require('socket.io-client');
import Socket = SocketIOClient.Socket;
+import {PlayerAnimationNames} from "./Phaser/Player/Animation";
enum EventMessage{
x: number;
y: number;
direction : string;
+ moving: boolean;
}
export class Point implements PointInterface{
- x: number;
- y: number;
- direction : string;
-
- constructor(x : number, y : number, direction : string = "none") {
+ constructor(public x : number, public y : number, public direction : string = PlayerAnimationNames.WalkDown, public moving : boolean = false) {
if(x === null || y === null){
throw Error("position x and y cannot be null");
}
- this.x = x;
- this.y = y;
- this.direction = direction;
}
}
userId: string;
name: string;
character: string;
+ position: PointInterface
}
export interface ListMessageUserPositionInterface {
new Point(
userPosition.position.x,
userPosition.position.y,
- userPosition.position.direction
+ userPosition.position.direction,
+ userPosition.position.moving
),
userPosition.name,
userPosition.character
loadMaps(): Promise<any>;
- joinARoom(roomId: string, character: string): void;
+ joinARoom(roomId: string, startX: number, startY: number, direction: string, moving: boolean): void;
- sharePosition(x: number, y: number, direction: string, roomId: string, character: string): void;
+ sharePosition(x: number, y: number, direction: string, moving: boolean): void;
positionOfAllUser(): void;
//if try to reconnect with last position
if(this.lastRoom) {
//join the room
- this.joinARoom(
- this.lastRoom
- );
+ this.joinARoom(this.lastRoom,
+ this.lastPositionShared ? this.lastPositionShared.x : 0,
+ this.lastPositionShared ? this.lastPositionShared.y : 0,
+ this.lastPositionShared ? this.lastPositionShared.direction : PlayerAnimationNames.WalkDown,
+ this.lastPositionShared ? this.lastPositionShared.moving : false);
}
- if(this.lastPositionShared) {
+ /*if(this.lastPositionShared) {
//share your first position
this.sharePosition(
this.lastPositionShared ? this.lastPositionShared.x : 0,
this.lastPositionShared ? this.lastPositionShared.y : 0,
- this.lastPositionShared.direction
+ this.lastPositionShared.direction,
+ this.lastPositionShared.moving
);
- }
+ }*/
resolve(this);
});
});
}
- /**
- *
- * @param roomId
- * @param character
- */
- joinARoom(roomId: string): void {
- this.socket.emit(EventMessage.JOIN_ROOM, roomId, (userPositions: MessageUserPositionInterface[]) => {
+ joinARoom(roomId: string, startX: number, startY: number, direction: string, moving: boolean): void {
+ this.socket.emit(EventMessage.JOIN_ROOM, { roomId, position: {x: startX, y: startY, direction, moving }}, (userPositions: MessageUserPositionInterface[]) => {
this.GameManager.initUsersPosition(userPositions);
});
this.lastRoom = roomId;
}
- /**
- *
- * @param x
- * @param y
- * @param character
- * @param roomId
- * @param direction
- */
- sharePosition(x : number, y : number, direction : string = "none") : void{
+ sharePosition(x : number, y : number, direction : string, moving: boolean) : void{
if(!this.socket){
return;
}
- let point = new Point(x, y, direction);
+ let point = new Point(x, y, direction, moving);
this.lastPositionShared = point;
this.socket.emit(EventMessage.USER_POSITION, point);
}
* [
* {
* userId: <string>,
- * roomId: <string>,
* position: {
* x : <number>,
* y : <number>,
- * direction: <string>
+ * direction: <string>,
+ * moving: <bool>
* }
* },
* ...
export class PlayableCaracter extends Phaser.Physics.Arcade.Sprite {
private bubble: SpeechBubble;
- private playerName: BitmapText;
+ private readonly playerName: BitmapText;
public PlayerValue: string;
public PlayerTexture: string;
export interface HasMovedEvent {
direction: string;
+ moving: boolean;
x: number;
y: number;
}
this.status = StatusGameManagerEnum.CURRENT_USER_CREATED;
}
- joinRoom(sceneKey : string){
- this.ConnexionInstance.joinARoom(sceneKey);
+ joinRoom(sceneKey: string, startX: number, startY: number, direction: string, moving: boolean){
+ this.ConnexionInstance.joinARoom(sceneKey, startX, startY, direction, moving);
}
onUserJoins(message: MessageUserJoined): void {
userId: message.userId,
character: message.character,
name: message.name,
- position: new Point(-1000, -1000)
+ position: message.position
}
this.currentGameScene.addPlayer(userMessage);
}
}
pushPlayerPosition(event: HasMovedEvent) {
- this.ConnexionInstance.sharePosition(event.x, event.y, event.direction);
+ this.ConnexionInstance.sharePosition(event.x, event.y, event.direction, event.moving);
}
loadMap(mapUrl: string, scene: ScenePlugin): string {
import Sprite = Phaser.GameObjects.Sprite;
import CanvasTexture = Phaser.Textures.CanvasTexture;
import {AddPlayerInterface} from "./AddPlayerInterface";
+import {PlayerAnimationNames} from "../Player/Animation";
export enum Textures {
Player = "male1"
this.startX,
this.startY,
this.GameManager.getPlayerName(),
- this.GameManager.getCharacterSelected()
+ this.GameManager.getCharacterSelected(),
+ PlayerAnimationNames.WalkDown,
+ false
);
- this.CurrentPlayer.initAnimation();
//create collision
this.createCollisionWithPlayer();
this.createCollisionObject();
//join room
- this.GameManager.joinRoom(this.scene.key);
+ this.GameManager.joinRoom(this.scene.key, this.startX, this.startY, PlayerAnimationNames.WalkDown, false);
//listen event to share position of user
this.CurrentPlayer.on(hasMovedEventName, this.pushPlayerPosition.bind(this))
addPlayerData.position.x,
addPlayerData.position.y,
addPlayerData.name,
- addPlayerData.character
+ addPlayerData.character,
+ addPlayerData.position.direction,
+ addPlayerData.position.moving
);
- player.initAnimation();
this.MapPlayers.add(player);
this.MapPlayersByKey.set(player.userId, player);
player.updatePosition(addPlayerData.position);
}
public removePlayer(userId: string) {
+ console.log('Removing player ', userId)
let player = this.MapPlayersByKey.get(userId);
if (player === undefined) {
console.error('Cannot find user with id ', userId);
WalkLeft = 'left',
WalkUp = 'up',
WalkRight = 'right',
- None = 'none',
}
export const getPlayerAnimations = (name: string = Textures.Player): AnimationData[] => {
repeat: -1
}];
};
-
-export const playAnimation = (Player : Phaser.GameObjects.Sprite, direction : string) => {
- if (direction !== PlayerAnimationNames.None && (!Player.anims.currentAnim || Player.anims.currentAnim.key !== direction)) {
- Player.anims.play(direction);
- } else if (direction === PlayerAnimationNames.None && Player.anims.currentAnim) {
- Player.anims.stop();
- }
-}
-import {getPlayerAnimations, playAnimation, PlayerAnimationNames} from "./Animation";
+import {getPlayerAnimations, PlayerAnimationNames} from "./Animation";
import {GameScene, Textures} from "../Game/GameScene";
import {MessageUserPositionInterface, PointInterface} from "../../Connexion";
import {ActiveEventList, UserInputEvent, UserInputManager} from "../UserInput/UserInputManager";
export const hasMovedEventName = "hasMoved";
export interface CurrentGamerInterface extends PlayableCaracter{
- initAnimation() : void;
moveUser(delta: number) : void;
say(text : string) : void;
}
export interface GamerInterface extends PlayableCaracter{
userId : string;
- initAnimation() : void;
updatePosition(position: PointInterface): void;
say(text : string) : void;
}
export class Player extends PlayableCaracter implements CurrentGamerInterface, GamerInterface {
userId: string;
userInputManager: UserInputManager;
- previousMove: string;
+ previousDirection: string;
+ wasMoving: boolean;
constructor(
userId: string,
x: number,
y: number,
name: string,
- PlayerTexture: string = Textures.Player
+ PlayerTexture: string,
+ direction: string,
+ moving: boolean
) {
super(Scene, x, y, PlayerTexture, name, 1);
//the current player model should be push away by other players to prevent conflict
this.setImmovable(false);
+ this.initAnimation();
+
+ console.warn("Start direction for added player ", direction)
+ console.warn("Position ", x, y)
+ /*this.play(`${PlayerTexture}-${direction}`, true);
+ if (!moving) {
+ this.stop();
+ }*/
+ this.playAnimation(direction, moving);
}
- initAnimation(): void {
+ private initAnimation(): void {
getPlayerAnimations(this.PlayerTexture).forEach(d => {
this.scene.anims.create({
key: d.key,
moveUser(delta: number): void {
//if user client on shift, camera and player speed
let direction = null;
+ let moving = false;
let activeEvents = this.userInputManager.getEventListForGameTick();
let speedMultiplier = activeEvents.get(UserInputEvent.SpeedUp) ? 25 : 9;
let y = 0;
if (activeEvents.get(UserInputEvent.MoveUp)) {
y = - moveAmount;
- direction = `${this.PlayerTexture}-${PlayerAnimationNames.WalkUp}`;
+ direction = PlayerAnimationNames.WalkUp;
+ moving = true;
} else if (activeEvents.get(UserInputEvent.MoveDown)) {
y = moveAmount;
- direction = `${this.PlayerTexture}-${PlayerAnimationNames.WalkDown}`;
+ direction = PlayerAnimationNames.WalkDown;
+ moving = true;
}
if (activeEvents.get(UserInputEvent.MoveLeft)) {
x = -moveAmount;
- direction = `${this.PlayerTexture}-${PlayerAnimationNames.WalkLeft}`;
+ direction = PlayerAnimationNames.WalkLeft;
+ moving = true;
} else if (activeEvents.get(UserInputEvent.MoveRight)) {
x = moveAmount;
- direction = `${this.PlayerTexture}-${PlayerAnimationNames.WalkRight}`;
+ direction = PlayerAnimationNames.WalkRight;
+ moving = true;
}
if (x !== 0 || y !== 0) {
this.move(x, y);
- this.emit(hasMovedEventName, {direction, x: this.x, y: this.y});
+ this.emit(hasMovedEventName, {moving, direction, x: this.x, y: this.y});
} else {
- if (this.previousMove !== PlayerAnimationNames.None) {
- direction = PlayerAnimationNames.None;
+ if (this.wasMoving) {
+ //direction = PlayerAnimationNames.None;
this.stop();
- this.emit(hasMovedEventName, {direction, x: this.x, y: this.y});
+ this.emit(hasMovedEventName, {moving, direction: this.previousDirection, x: this.x, y: this.y});
}
}
if (direction !== null) {
- this.previousMove = direction;
+ this.previousDirection = direction;
}
+ this.wasMoving = moving;
}
//todo: put this method into the NonPlayer class instead
updatePosition(position: PointInterface): void {
- playAnimation(this, position.direction);
+ this.playAnimation(position.direction, position.moving);
this.setX(position.x);
this.setY(position.y);
this.setDepth(position.y);
}
+
+ private playAnimation(direction : string, moving: boolean): void {
+ if (moving && (!this.anims.currentAnim || this.anims.currentAnim.key !== direction)) {
+ this.anims.play(this.PlayerTexture+'-'+direction);
+ } else if (!moving) {
+ /*if (this.anims.currentAnim) {
+ this.anims.stop();
+ }*/
+ this.anims.play(this.PlayerTexture+'-'+direction);
+ this.anims.stop();
+ }
+ }
}