Changing the "Point" notion to add a notion of "moving" in addition to the notion of direction.
Also, refactoring JOIN_ROOM event to add complete position.
This commit is contained in:
parent
f44a44c109
commit
ab798b1c09
9 changed files with 101 additions and 88 deletions
|
@ -26,7 +26,7 @@ export const PLAYER_RESOURCES: Array<any> = [
|
|||
|
||||
export class PlayableCaracter extends Phaser.Physics.Arcade.Sprite {
|
||||
private bubble: SpeechBubble;
|
||||
private playerName: BitmapText;
|
||||
private readonly playerName: BitmapText;
|
||||
public PlayerValue: string;
|
||||
public PlayerTexture: string;
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@ export enum StatusGameManagerEnum {
|
|||
|
||||
export interface HasMovedEvent {
|
||||
direction: string;
|
||||
moving: boolean;
|
||||
x: number;
|
||||
y: number;
|
||||
}
|
||||
|
@ -71,8 +72,8 @@ export class GameManager {
|
|||
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 {
|
||||
|
@ -80,7 +81,7 @@ export class GameManager {
|
|||
userId: message.userId,
|
||||
character: message.character,
|
||||
name: message.name,
|
||||
position: new Point(-1000, -1000)
|
||||
position: message.position
|
||||
}
|
||||
this.currentGameScene.addPlayer(userMessage);
|
||||
}
|
||||
|
@ -159,7 +160,7 @@ export class GameManager {
|
|||
}
|
||||
|
||||
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 {
|
||||
|
|
|
@ -13,6 +13,7 @@ import Texture = Phaser.Textures.Texture;
|
|||
import Sprite = Phaser.GameObjects.Sprite;
|
||||
import CanvasTexture = Phaser.Textures.CanvasTexture;
|
||||
import {AddPlayerInterface} from "./AddPlayerInterface";
|
||||
import {PlayerAnimationNames} from "../Player/Animation";
|
||||
|
||||
export enum Textures {
|
||||
Player = "male1"
|
||||
|
@ -273,16 +274,17 @@ export class GameScene extends Phaser.Scene {
|
|||
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))
|
||||
|
@ -415,9 +417,10 @@ export class GameScene extends Phaser.Scene {
|
|||
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);
|
||||
|
@ -429,6 +432,7 @@ export class GameScene extends Phaser.Scene {
|
|||
}
|
||||
|
||||
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);
|
||||
|
|
|
@ -14,7 +14,6 @@ export enum PlayerAnimationNames {
|
|||
WalkLeft = 'left',
|
||||
WalkUp = 'up',
|
||||
WalkRight = 'right',
|
||||
None = 'none',
|
||||
}
|
||||
|
||||
export const getPlayerAnimations = (name: string = Textures.Player): AnimationData[] => {
|
||||
|
@ -48,11 +47,3 @@ export const getPlayerAnimations = (name: string = Textures.Player): AnimationDa
|
|||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
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";
|
||||
|
@ -7,14 +7,12 @@ import {PlayableCaracter} from "../Entity/PlayableCaracter";
|
|||
|
||||
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;
|
||||
}
|
||||
|
@ -22,7 +20,8 @@ export interface GamerInterface extends PlayableCaracter{
|
|||
export class Player extends PlayableCaracter implements CurrentGamerInterface, GamerInterface {
|
||||
userId: string;
|
||||
userInputManager: UserInputManager;
|
||||
previousMove: string;
|
||||
previousDirection: string;
|
||||
wasMoving: boolean;
|
||||
|
||||
constructor(
|
||||
userId: string,
|
||||
|
@ -30,7 +29,9 @@ export class Player extends PlayableCaracter implements CurrentGamerInterface, G
|
|||
x: number,
|
||||
y: number,
|
||||
name: string,
|
||||
PlayerTexture: string = Textures.Player
|
||||
PlayerTexture: string,
|
||||
direction: string,
|
||||
moving: boolean
|
||||
) {
|
||||
super(Scene, x, y, PlayerTexture, name, 1);
|
||||
|
||||
|
@ -42,9 +43,18 @@ export class Player extends PlayableCaracter implements CurrentGamerInterface, G
|
|||
|
||||
//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,
|
||||
|
@ -58,6 +68,7 @@ export class Player extends PlayableCaracter implements CurrentGamerInterface, G
|
|||
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;
|
||||
|
@ -67,39 +78,56 @@ export class Player extends PlayableCaracter implements CurrentGamerInterface, G
|
|||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue