{name: "Female8", img: "resources/characters/pipoya/Female 16-4.png"/*, x: 128, y: 128*/}
];
-export class PlayableCharacter extends Phaser.Physics.Arcade.Sprite {
+interface AnimationData {
+ key: string;
+ frameRate: number;
+ repeat: number;
+ frameModel: string; //todo use an enum
+ frameStart: number;
+ frameEnd: number;
+}
+
+export abstract class Character extends Phaser.Physics.Arcade.Sprite {
private bubble: SpeechBubble|null = null;
private readonly playerName: BitmapText;
public PlayerValue: string;
public PlayerTexture: string;
- constructor(scene: Phaser.Scene, x: number, y: number, texture: string, name: string, frame?: string | number) {
+ constructor(scene: Phaser.Scene,
+ x: number,
+ y: number,
+ texture: string,
+ name: string,
+ direction: string,
+ moving: boolean,
+ frame?: string | number
+ ) {
super(scene, x, y, texture, frame);
this.PlayerValue = name;
this.setDepth(-1);
this.scene.events.on('postupdate', this.postupdate.bind(this));
+
+ this.initAnimation();
+ this.playAnimation(direction, moving);
+ }
+
+ private initAnimation(): void {
+ this.getPlayerAnimations(this.PlayerTexture).forEach(d => {
+ this.scene.anims.create({
+ key: d.key,
+ frames: this.scene.anims.generateFrameNumbers(d.frameModel, {start: d.frameStart, end: d.frameEnd}),
+ frameRate: d.frameRate,
+ repeat: d.repeat
+ });
+ })
+ }
+
+ 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
+ }];
+ }
+
+ protected playAnimation(direction : string, moving: boolean): void {
+ if (moving && (!this.anims.currentAnim || this.anims.currentAnim.key !== direction)) {
+ this.play(this.PlayerTexture+'-'+direction, true);
+ } else if (!moving) {
+ /*if (this.anims.currentAnim) {
+ this.anims.stop();
+ }*/
+ this.play(this.PlayerTexture+'-'+direction, true);
+ this.stop();
+ }
}
move(x: number, y: number) {
--- /dev/null
+import {GameScene} from "../Game/GameScene";
+import {PointInterface} from "../../Connection";
+import {Character} from "../Entity/Character";
+
+/**
+ * Class representing the sprite of a remote player (a player that plays on another computer)
+ */
+export class RemotePlayer extends Character {
+ userId: string;
+ previousDirection: string;
+ wasMoving: boolean;
+
+ constructor(
+ userId: string,
+ Scene: GameScene,
+ x: number,
+ y: number,
+ name: string,
+ PlayerTexture: string,
+ direction: string,
+ moving: boolean
+ ) {
+ super(Scene, x, y, PlayerTexture, name, direction, moving, 1);
+
+ //set data
+ this.userId = userId;
+
+ //the current player model should be push away by other players to prevent conflict
+ //this.setImmovable(false);
+ }
+
+ updatePosition(position: PointInterface): void {
+ this.playAnimation(position.direction, position.moving);
+ this.setX(position.x);
+ this.setY(position.y);
+ this.setDepth(position.y);
+ }
+}
import Scene = Phaser.Scene;
-import {PlayableCharacter} from "./PlayableCharacter";
+import {Character} from "./Character";
export class SpeechBubble {
private bubble: Phaser.GameObjects.Graphics;
* @param player
* @param text
*/
- constructor(scene: Scene, player: PlayableCharacter, text: string = "") {
+ constructor(scene: Scene, player: Character, text: string = "") {
let bubbleHeight = 50;
let bubblePadding = 10;
return this.playerName;
}
- getPlayerId(): string {
+ getPlayerId(): string|null {
return this.ConnectionInstance.userId;
}
MessageUserMovedInterface,
MessageUserPositionInterface, PointInterface, PositionInterface
} from "../../Connection";
-import {CurrentGamerInterface, GamerInterface, hasMovedEventName, Player} from "../Player/Player";
+import {CurrentGamerInterface, hasMovedEventName, Player} from "../Player/Player";
import { DEBUG_MODE, ZOOM_LEVEL, POSITION_DELAY } from "../../Enum/EnvironmentVariable";
import {ITiledMap, ITiledMapLayer, ITiledTileSet} from "../Map/ITiledMap";
-import {PLAYER_RESOURCES} from "../Entity/PlayableCharacter";
+import {PLAYER_RESOURCES} from "../Entity/Character";
import Texture = Phaser.Textures.Texture;
import Sprite = Phaser.GameObjects.Sprite;
import CanvasTexture = Phaser.Textures.CanvasTexture;
import {PlayerAnimationNames} from "../Player/Animation";
import {PlayerMovement} from "./PlayerMovement";
import {PlayersPositionInterpolator} from "./PlayersPositionInterpolator";
+import {RemotePlayer} from "../Entity/RemotePlayer";
export enum Textures {
Player = "male1"
Terrains : Array<Phaser.Tilemaps.Tileset>;
CurrentPlayer: CurrentGamerInterface;
MapPlayers : Phaser.Physics.Arcade.Group;
- MapPlayersByKey : Map<string, GamerInterface> = new Map<string, GamerInterface>();
+ MapPlayersByKey : Map<string, RemotePlayer> = new Map<string, RemotePlayer>();
Map: Phaser.Tilemaps.Tilemap;
Layers : Array<Phaser.Tilemaps.StaticTilemapLayer>;
Objects : Array<Phaser.Physics.Arcade.Sprite>;
- mapFile: ITiledMap|null;
+ mapFile: ITiledMap;
groups: Map<string, Sprite>;
startX = 704;// 22 case
startY = 32; // 1 case
// FIXME: entry should be dictated by a property passed to init()
path += '#'+url.hash;
}
- window.history.pushState({}, null, path);
+ window.history.pushState({}, 'WorkAdventure', path);
}
private getExitSceneUrl(layer: ITiledMapLayer): string|undefined {
*/
private loadNextGame(layer: ITiledMapLayer, mapWidth: number, tileWidth: number, tileHeight: number){
let exitSceneUrl = this.getExitSceneUrl(layer);
+ if (exitSceneUrl === undefined) {
+ throw new Error('Layer is not an exit scene layer.');
+ }
let instance = this.getExitSceneInstance(layer);
if (instance === undefined) {
instance = this.instance;
//initialise player
//TODO create animation moving between exit and start
this.CurrentPlayer = new Player(
- null, // The current player has no id (because the id can change if connection is lost and we should check that id using the GameManager.)
this,
this.startX,
this.startY,
// Let's move all users
let updatedPlayersPositions = this.playersPositionInterpolator.getUpdatedPositions(time);
updatedPlayersPositions.forEach((moveEvent: HasMovedEvent, userId: string) => {
- let player : GamerInterface | undefined = this.MapPlayersByKey.get(userId);
+ let player : RemotePlayer | undefined = this.MapPlayersByKey.get(userId);
if (player === undefined) {
throw new Error('Cannot find player with ID "' + userId +'"');
}
let currentPlayerId = this.GameManager.getPlayerId();
// clean map
- this.MapPlayersByKey.forEach((player: GamerInterface) => {
+ this.MapPlayersByKey.forEach((player: RemotePlayer) => {
player.destroy();
this.MapPlayers.remove(player);
});
- this.MapPlayersByKey = new Map<string, GamerInterface>();
+ this.MapPlayersByKey = new Map<string, RemotePlayer>();
// load map
usersPosition.forEach((userPosition : MessageUserPositionInterface) => {
return;
}
//initialise player
- let player = new Player(
+ let player = new RemotePlayer(
addPlayerData.userId,
this,
addPlayerData.position.x,
}
updatePlayerPosition(message: MessageUserMovedInterface): void {
- let player : GamerInterface | undefined = this.MapPlayersByKey.get(message.userId);
+ let player : RemotePlayer | undefined = this.MapPlayersByKey.get(message.userId);
if (player === undefined) {
throw new Error('Cannot find player with ID "' + message.userId +'"');
}
import {ClickButton} from "../Components/ClickButton";
import Image = Phaser.GameObjects.Image;
import Rectangle = Phaser.GameObjects.Rectangle;
-import {PLAYER_RESOURCES} from "../Entity/PlayableCharacter";
+import {PLAYER_RESOURCES} from "../Entity/Character";
import {cypressAsserter} from "../../Cypress/CypressAsserter";
import {SelectCharacterSceneInitDataInterface, SelectCharacterSceneName} from "./SelectCharacterScene";
import {ClickButton} from "../Components/ClickButton";
import Image = Phaser.GameObjects.Image;
import Rectangle = Phaser.GameObjects.Rectangle;
-import {PLAYER_RESOURCES} from "../Entity/PlayableCharacter";
+import {PLAYER_RESOURCES} from "../Entity/Character";
//todo: put this constants in a dedicated file
export const SelectCharacterSceneName = "SelectCharacterScene";
import {GameScene, Textures} from "../Game/GameScene";
import {MessageUserPositionInterface, PointInterface} from "../../Connection";
import {ActiveEventList, UserInputEvent, UserInputManager} from "../UserInput/UserInputManager";
-import {PlayableCharacter} from "../Entity/PlayableCharacter";
+import {Character} from "../Entity/Character";
export const hasMovedEventName = "hasMoved";
-export interface CurrentGamerInterface extends PlayableCharacter{
+export interface CurrentGamerInterface extends Character{
moveUser(delta: number) : void;
say(text : string) : void;
}
-export interface GamerInterface extends PlayableCharacter{
- userId : string;
- updatePosition(position: PointInterface): void;
- say(text : string) : void;
-}
-
-interface AnimationData {
- key: string;
- frameRate: number;
- repeat: number;
- frameModel: string; //todo use an enum
- frameStart: number;
- frameEnd: number;
-}
-
-
-export class Player extends PlayableCharacter implements CurrentGamerInterface, GamerInterface {
- userId: string;
+export class Player extends Character implements CurrentGamerInterface {
userInputManager: UserInputManager;
previousDirection: string;
wasMoving: boolean;
constructor(
- userId: string,
Scene: GameScene,
x: number,
y: number,
direction: string,
moving: boolean
) {
- super(Scene, x, y, PlayerTexture, name, 1);
+ super(Scene, x, y, PlayerTexture, name, direction, moving, 1);
//create input to move
this.userInputManager = new UserInputManager(Scene);
- //set data
- this.userId = userId;
-
//the current player model should be push away by other players to prevent conflict
this.setImmovable(false);
- this.initAnimation();
-
- this.playAnimation(direction, moving);
- }
-
- private initAnimation(): void {
- this.getPlayerAnimations(this.PlayerTexture).forEach(d => {
- this.scene.anims.create({
- key: d.key,
- frames: this.scene.anims.generateFrameNumbers(d.frameModel, {start: d.frameStart, end: d.frameEnd}),
- frameRate: d.frameRate,
- repeat: d.repeat
- });
- })
- }
-
- 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
- }];
}
moveUser(delta: number): void {
}
this.wasMoving = moving;
}
-
- //todo: put this method into the NonPlayer class instead
- updatePosition(position: PointInterface): void {
- 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.play(this.PlayerTexture+'-'+direction, true);
- } else if (!moving) {
- /*if (this.anims.currentAnim) {
- this.anims.stop();
- }*/
- this.play(this.PlayerTexture+'-'+direction, true);
- this.stop();
- }
- }
}
import {ClickButton} from "../Components/ClickButton";
import Image = Phaser.GameObjects.Image;
import Rectangle = Phaser.GameObjects.Rectangle;
-import {PLAYER_RESOURCES} from "../Entity/PlayableCharacter";
+import {PLAYER_RESOURCES} from "../Entity/Character";
import {cypressAsserter} from "../../Cypress/CypressAsserter";
import Sprite = Phaser.GameObjects.Sprite;