workadventure/front/src/Phaser/Player/Player.ts

88 lines
3 KiB
TypeScript
Raw Normal View History

2020-05-26 17:37:26 +02:00
import {PlayerAnimationNames} from "./Animation";
import {GameScene, Textures} from "../Game/GameScene";
import {MessageUserPositionInterface, PointInterface} from "../../Connection";
import {ActiveEventList, UserInputEvent, UserInputManager} from "../UserInput/UserInputManager";
2020-06-04 18:54:34 +02:00
import {Character} from "../Entity/Character";
import {OutlinePipeline} from "../Shaders/OutlinePipeline";
export const hasMovedEventName = "hasMoved";
2020-06-04 18:54:34 +02:00
export interface CurrentGamerInterface extends Character{
moveUser(delta: number) : void;
say(text : string) : void;
2020-04-12 13:57:00 +02:00
}
2020-06-04 18:54:34 +02:00
export class Player extends Character implements CurrentGamerInterface {
userInputManager: UserInputManager;
previousDirection: string;
wasMoving: boolean;
constructor(
Scene: GameScene,
2020-04-13 15:34:09 +02:00
x: number,
y: number,
name: string,
PlayerTexture: string,
direction: string,
moving: boolean
) {
2020-06-04 18:54:34 +02:00
super(Scene, x, y, PlayerTexture, name, direction, moving, 1);
//create input to move
this.userInputManager = new UserInputManager(Scene);
//the current player model should be push away by other players to prevent conflict
this.setImmovable(false);
this.setPipeline(OutlinePipeline.KEY);
this.pipeline.setFloat2('uTextureSize',
this.texture.getSourceImage().width, this.texture.getSourceImage().height);
2020-05-26 17:43:25 +02:00
}
moveUser(delta: number): void {
//if user client on shift, camera and player speed
2020-04-07 21:02:23 +02:00
let direction = null;
let moving = false;
2020-06-09 23:13:26 +02:00
const activeEvents = this.userInputManager.getEventListForGameTick();
const speedMultiplier = activeEvents.get(UserInputEvent.SpeedUp) ? 25 : 9;
const moveAmount = speedMultiplier * 20;
2020-04-30 19:36:28 +02:00
let x = 0;
let y = 0;
if (activeEvents.get(UserInputEvent.MoveUp)) {
2020-04-30 19:36:28 +02:00
y = - moveAmount;
direction = PlayerAnimationNames.WalkUp;
moving = true;
2020-04-30 19:36:28 +02:00
} else if (activeEvents.get(UserInputEvent.MoveDown)) {
y = moveAmount;
direction = PlayerAnimationNames.WalkDown;
moving = true;
}
if (activeEvents.get(UserInputEvent.MoveLeft)) {
2020-04-30 19:36:28 +02:00
x = -moveAmount;
direction = PlayerAnimationNames.WalkLeft;
moving = true;
2020-04-30 19:36:28 +02:00
} else if (activeEvents.get(UserInputEvent.MoveRight)) {
x = moveAmount;
direction = PlayerAnimationNames.WalkRight;
moving = true;
}
2020-04-30 19:36:28 +02:00
if (x !== 0 || y !== 0) {
this.move(x, y);
this.emit(hasMovedEventName, {moving, direction, x: this.x, y: this.y});
2020-04-30 19:36:28 +02:00
} else {
if (this.wasMoving) {
//direction = PlayerAnimationNames.None;
this.stop();
this.emit(hasMovedEventName, {moving, direction: this.previousDirection, x: this.x, y: this.y});
}
2020-04-07 20:41:35 +02:00
}
if (direction !== null) {
this.previousDirection = direction;
2020-04-07 20:41:35 +02:00
}
this.wasMoving = moving;
}
}