2020-05-26 17:37:26 +02:00
|
|
|
import {PlayerAnimationNames} from "./Animation";
|
2020-05-19 19:11:12 +02:00
|
|
|
import {GameScene, Textures} from "../Game/GameScene";
|
2020-05-24 23:14:12 +02:00
|
|
|
import {MessageUserPositionInterface, PointInterface} from "../../Connection";
|
2020-04-13 13:42:21 +02:00
|
|
|
import {ActiveEventList, UserInputEvent, UserInputManager} from "../UserInput/UserInputManager";
|
2020-06-04 18:11:07 +02:00
|
|
|
import {PlayableCharacter} from "../Entity/PlayableCharacter";
|
2020-04-07 19:23:21 +02:00
|
|
|
|
2020-05-02 16:54:52 +02:00
|
|
|
|
|
|
|
export const hasMovedEventName = "hasMoved";
|
2020-06-04 18:11:07 +02:00
|
|
|
export interface CurrentGamerInterface extends PlayableCharacter{
|
2020-04-18 17:16:39 +02:00
|
|
|
moveUser(delta: number) : void;
|
2020-04-13 13:42:21 +02:00
|
|
|
say(text : string) : void;
|
2020-04-12 13:57:00 +02:00
|
|
|
}
|
|
|
|
|
2020-06-04 18:11:07 +02:00
|
|
|
export interface GamerInterface extends PlayableCharacter{
|
2020-04-12 13:57:00 +02:00
|
|
|
userId : string;
|
2020-05-19 19:11:12 +02:00
|
|
|
updatePosition(position: PointInterface): void;
|
2020-04-13 13:42:21 +02:00
|
|
|
say(text : string) : void;
|
2020-04-12 13:57:00 +02:00
|
|
|
}
|
|
|
|
|
2020-05-26 17:25:29 +02:00
|
|
|
interface AnimationData {
|
|
|
|
key: string;
|
|
|
|
frameRate: number;
|
|
|
|
repeat: number;
|
|
|
|
frameModel: string; //todo use an enum
|
|
|
|
frameStart: number;
|
|
|
|
frameEnd: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-04 18:11:07 +02:00
|
|
|
export class Player extends PlayableCharacter implements CurrentGamerInterface, GamerInterface {
|
2020-04-13 15:34:09 +02:00
|
|
|
userId: string;
|
2020-04-13 13:42:21 +02:00
|
|
|
userInputManager: UserInputManager;
|
2020-05-22 22:59:43 +02:00
|
|
|
previousDirection: string;
|
|
|
|
wasMoving: boolean;
|
2020-04-07 19:23:21 +02:00
|
|
|
|
|
|
|
constructor(
|
2020-04-10 12:54:05 +02:00
|
|
|
userId: string,
|
2020-05-19 19:11:12 +02:00
|
|
|
Scene: GameScene,
|
2020-04-13 15:34:09 +02:00
|
|
|
x: number,
|
|
|
|
y: number,
|
2020-05-01 23:38:09 +02:00
|
|
|
name: string,
|
2020-05-22 22:59:43 +02:00
|
|
|
PlayerTexture: string,
|
|
|
|
direction: string,
|
|
|
|
moving: boolean
|
2020-04-07 19:23:21 +02:00
|
|
|
) {
|
2020-05-06 01:50:01 +02:00
|
|
|
super(Scene, x, y, PlayerTexture, name, 1);
|
2020-04-13 13:42:21 +02:00
|
|
|
|
|
|
|
//create input to move
|
|
|
|
this.userInputManager = new UserInputManager(Scene);
|
|
|
|
|
|
|
|
//set data
|
2020-04-10 12:54:05 +02:00
|
|
|
this.userId = userId;
|
2020-05-06 01:50:01 +02:00
|
|
|
|
2020-04-13 13:42:21 +02:00
|
|
|
//the current player model should be push away by other players to prevent conflict
|
|
|
|
this.setImmovable(false);
|
2020-05-22 22:59:43 +02:00
|
|
|
this.initAnimation();
|
|
|
|
|
|
|
|
this.playAnimation(direction, moving);
|
2020-04-07 19:23:21 +02:00
|
|
|
}
|
|
|
|
|
2020-05-22 22:59:43 +02:00
|
|
|
private initAnimation(): void {
|
2020-05-26 17:25:29 +02:00
|
|
|
this.getPlayerAnimations(this.PlayerTexture).forEach(d => {
|
2020-04-07 19:23:21 +02:00
|
|
|
this.scene.anims.create({
|
|
|
|
key: d.key,
|
2020-04-13 13:42:21 +02:00
|
|
|
frames: this.scene.anims.generateFrameNumbers(d.frameModel, {start: d.frameStart, end: d.frameEnd}),
|
2020-04-07 19:23:21 +02:00
|
|
|
frameRate: d.frameRate,
|
|
|
|
repeat: d.repeat
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-26 17:25:29 +02:00
|
|
|
private getPlayerAnimations(name: string): AnimationData[] {
|
|
|
|
return [{
|
|
|
|
key: `${name}-${PlayerAnimationNames.WalkDown}`,
|
|
|
|
frameModel: name,
|
|
|
|
frameStart: 0,
|
|
|
|
frameEnd: 2,
|
|
|
|
frameRate: 10,
|
|
|
|
repeat: -1
|
|
|
|
}, {
|
|
|
|
key: `${name}-${PlayerAnimationNames.WalkLeft}`,
|
|
|
|
frameModel: name,
|
|
|
|
frameStart: 3,
|
|
|
|
frameEnd: 5,
|
|
|
|
frameRate: 10,
|
|
|
|
repeat: -1
|
|
|
|
}, {
|
|
|
|
key: `${name}-${PlayerAnimationNames.WalkRight}`,
|
|
|
|
frameModel: name,
|
|
|
|
frameStart: 6,
|
|
|
|
frameEnd: 8,
|
|
|
|
frameRate: 10,
|
|
|
|
repeat: -1
|
|
|
|
}, {
|
|
|
|
key: `${name}-${PlayerAnimationNames.WalkUp}`,
|
|
|
|
frameModel: name,
|
|
|
|
frameStart: 9,
|
|
|
|
frameEnd: 11,
|
|
|
|
frameRate: 10,
|
|
|
|
repeat: -1
|
|
|
|
}];
|
2020-05-26 17:43:25 +02:00
|
|
|
}
|
2020-05-26 17:25:29 +02:00
|
|
|
|
2020-04-18 17:16:39 +02:00
|
|
|
moveUser(delta: number): void {
|
2020-04-07 19:23:21 +02:00
|
|
|
//if user client on shift, camera and player speed
|
2020-04-07 21:02:23 +02:00
|
|
|
let direction = null;
|
2020-05-22 22:59:43 +02:00
|
|
|
let moving = false;
|
2020-04-07 19:23:21 +02:00
|
|
|
|
2020-04-13 13:42:21 +02:00
|
|
|
let activeEvents = this.userInputManager.getEventListForGameTick();
|
2020-04-18 17:16:39 +02:00
|
|
|
let speedMultiplier = activeEvents.get(UserInputEvent.SpeedUp) ? 25 : 9;
|
2020-05-12 11:49:55 +02:00
|
|
|
let moveAmount = speedMultiplier * 20;
|
2020-04-13 13:42:21 +02:00
|
|
|
|
2020-04-30 19:36:28 +02:00
|
|
|
let x = 0;
|
|
|
|
let y = 0;
|
2020-04-13 13:42:21 +02:00
|
|
|
if (activeEvents.get(UserInputEvent.MoveUp)) {
|
2020-04-30 19:36:28 +02:00
|
|
|
y = - moveAmount;
|
2020-05-22 22:59:43 +02:00
|
|
|
direction = PlayerAnimationNames.WalkUp;
|
|
|
|
moving = true;
|
2020-04-30 19:36:28 +02:00
|
|
|
} else if (activeEvents.get(UserInputEvent.MoveDown)) {
|
|
|
|
y = moveAmount;
|
2020-05-22 22:59:43 +02:00
|
|
|
direction = PlayerAnimationNames.WalkDown;
|
|
|
|
moving = true;
|
2020-04-07 19:23:21 +02:00
|
|
|
}
|
2020-04-13 13:42:21 +02:00
|
|
|
if (activeEvents.get(UserInputEvent.MoveLeft)) {
|
2020-04-30 19:36:28 +02:00
|
|
|
x = -moveAmount;
|
2020-05-22 22:59:43 +02:00
|
|
|
direction = PlayerAnimationNames.WalkLeft;
|
|
|
|
moving = true;
|
2020-04-30 19:36:28 +02:00
|
|
|
} else if (activeEvents.get(UserInputEvent.MoveRight)) {
|
|
|
|
x = moveAmount;
|
2020-05-22 22:59:43 +02:00
|
|
|
direction = PlayerAnimationNames.WalkRight;
|
|
|
|
moving = true;
|
2020-04-07 19:23:21 +02:00
|
|
|
}
|
2020-04-30 19:36:28 +02:00
|
|
|
if (x !== 0 || y !== 0) {
|
|
|
|
this.move(x, y);
|
2020-05-22 22:59:43 +02:00
|
|
|
this.emit(hasMovedEventName, {moving, direction, x: this.x, y: this.y});
|
2020-04-30 19:36:28 +02:00
|
|
|
} else {
|
2020-05-22 22:59:43 +02:00
|
|
|
if (this.wasMoving) {
|
|
|
|
//direction = PlayerAnimationNames.None;
|
2020-05-04 23:11:59 +02:00
|
|
|
this.stop();
|
2020-05-22 22:59:43 +02:00
|
|
|
this.emit(hasMovedEventName, {moving, direction: this.previousDirection, x: this.x, y: this.y});
|
2020-05-04 23:11:59 +02:00
|
|
|
}
|
2020-04-07 20:41:35 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 23:11:59 +02:00
|
|
|
if (direction !== null) {
|
2020-05-22 22:59:43 +02:00
|
|
|
this.previousDirection = direction;
|
2020-04-07 20:41:35 +02:00
|
|
|
}
|
2020-05-22 22:59:43 +02:00
|
|
|
this.wasMoving = moving;
|
2020-04-07 19:23:21 +02:00
|
|
|
}
|
|
|
|
|
2020-05-02 16:54:52 +02:00
|
|
|
//todo: put this method into the NonPlayer class instead
|
2020-05-19 19:11:12 +02:00
|
|
|
updatePosition(position: PointInterface): void {
|
2020-05-22 22:59:43 +02:00
|
|
|
this.playAnimation(position.direction, position.moving);
|
2020-05-19 19:11:12 +02:00
|
|
|
this.setX(position.x);
|
|
|
|
this.setY(position.y);
|
|
|
|
this.setDepth(position.y);
|
2020-04-10 12:54:05 +02:00
|
|
|
}
|
2020-05-22 22:59:43 +02:00
|
|
|
|
|
|
|
private playAnimation(direction : string, moving: boolean): void {
|
|
|
|
if (moving && (!this.anims.currentAnim || this.anims.currentAnim.key !== direction)) {
|
2020-06-01 13:20:45 +02:00
|
|
|
this.play(this.PlayerTexture+'-'+direction, true);
|
2020-05-22 22:59:43 +02:00
|
|
|
} else if (!moving) {
|
|
|
|
/*if (this.anims.currentAnim) {
|
|
|
|
this.anims.stop();
|
|
|
|
}*/
|
2020-06-01 13:20:45 +02:00
|
|
|
this.play(this.PlayerTexture+'-'+direction, true);
|
|
|
|
this.stop();
|
2020-05-22 22:59:43 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-18 17:16:39 +02:00
|
|
|
}
|