From 6f69a62d4d18601a95decec3d7e93fc381099c9e Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20N=C3=A9grier?= Date: Thu, 4 Jun 2020 18:11:07 +0200 Subject: [PATCH] More strict fixes --- ...ayableCaracter.ts => PlayableCharacter.ts} | 10 +++--- front/src/Phaser/Entity/SpeechBubble.ts | 11 +++--- front/src/Phaser/Game/GameManager.ts | 3 +- front/src/Phaser/Game/GameScene.ts | 27 +++++++++------ front/src/Phaser/Login/LoginScene.ts | 2 +- .../src/Phaser/Login/SelectCharacterScene.ts | 2 +- front/src/Phaser/Player/Player.ts | 8 ++--- .../Phaser/Reconnecting/ReconnectingScene.ts | 2 +- .../src/Phaser/UserInput/UserInputManager.ts | 34 +++++++++---------- 9 files changed, 50 insertions(+), 49 deletions(-) rename front/src/Phaser/Entity/{PlayableCaracter.ts => PlayableCharacter.ts} (94%) diff --git a/front/src/Phaser/Entity/PlayableCaracter.ts b/front/src/Phaser/Entity/PlayableCharacter.ts similarity index 94% rename from front/src/Phaser/Entity/PlayableCaracter.ts rename to front/src/Phaser/Entity/PlayableCharacter.ts index e1b774e..99b4d63 100644 --- a/front/src/Phaser/Entity/PlayableCaracter.ts +++ b/front/src/Phaser/Entity/PlayableCharacter.ts @@ -24,8 +24,8 @@ export const PLAYER_RESOURCES: Array = [ {name: "Female8", img: "resources/characters/pipoya/Female 16-4.png"/*, x: 128, y: 128*/} ]; -export class PlayableCaracter extends Phaser.Physics.Arcade.Sprite { - private bubble: SpeechBubble; +export class PlayableCharacter extends Phaser.Physics.Arcade.Sprite { + private bubble: SpeechBubble|null = null; private readonly playerName: BitmapText; public PlayerValue: string; public PlayerTexture: string; @@ -91,8 +91,10 @@ export class PlayableCaracter extends Phaser.Physics.Arcade.Sprite { this.bubble = new SpeechBubble(this.scene, this, text) //todo make the bubble destroy on player movement? setTimeout(() => { - this.bubble.destroy(); - this.bubble = null; + if (this.bubble !== null) { + this.bubble.destroy(); + this.bubble = null; + } }, 3000) } diff --git a/front/src/Phaser/Entity/SpeechBubble.ts b/front/src/Phaser/Entity/SpeechBubble.ts index 51aaa16..6a696b6 100644 --- a/front/src/Phaser/Entity/SpeechBubble.ts +++ b/front/src/Phaser/Entity/SpeechBubble.ts @@ -1,5 +1,5 @@ import Scene = Phaser.Scene; -import {PlayableCaracter} from "./PlayableCaracter"; +import {PlayableCharacter} from "./PlayableCharacter"; export class SpeechBubble { private bubble: Phaser.GameObjects.Graphics; @@ -11,7 +11,7 @@ export class SpeechBubble { * @param player * @param text */ - constructor(scene: Scene, player: PlayableCaracter, text: string = "") { + constructor(scene: Scene, player: PlayableCharacter, text: string = "") { let bubbleHeight = 50; let bubblePadding = 10; @@ -76,13 +76,10 @@ export class SpeechBubble { this.content.setPosition(this.bubble.x + (bubbleWidth / 2) - (bounds.width / 2), this.bubble.y + (bubbleHeight / 2) - (bounds.height / 2)); } } - + destroy(): void { this.bubble.setVisible(false) //todo find a better way this.bubble.destroy(); - this.bubble = null; this.content.destroy(); - this.content = null; } - -} \ No newline at end of file +} diff --git a/front/src/Phaser/Game/GameManager.ts b/front/src/Phaser/Game/GameManager.ts index 39ed4bd..c4c4c98 100644 --- a/front/src/Phaser/Game/GameManager.ts +++ b/front/src/Phaser/Game/GameManager.ts @@ -155,9 +155,8 @@ export class GameManager { let sceneKey = GameScene.getMapKeyByUrl(mapUrl); let gameIndex = scene.getIndex(sceneKey); - let game : Phaser.Scene = null; if(gameIndex === -1){ - game = GameScene.createFromUrl(mapUrl, instance); + let game : Phaser.Scene = GameScene.createFromUrl(mapUrl, instance); scene.add(sceneKey, game, false); } return sceneKey; diff --git a/front/src/Phaser/Game/GameScene.ts b/front/src/Phaser/Game/GameScene.ts index 126e236..27052e7 100644 --- a/front/src/Phaser/Game/GameScene.ts +++ b/front/src/Phaser/Game/GameScene.ts @@ -7,7 +7,7 @@ import { import {CurrentGamerInterface, GamerInterface, 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/PlayableCaracter"; +import {PLAYER_RESOURCES} from "../Entity/PlayableCharacter"; import Texture = Phaser.Textures.Texture; import Sprite = Phaser.GameObjects.Sprite; import CanvasTexture = Phaser.Textures.CanvasTexture; @@ -30,7 +30,7 @@ export class GameScene extends Phaser.Scene { CurrentPlayer: CurrentGamerInterface; MapPlayers : Phaser.Physics.Arcade.Group; MapPlayersByKey : Map = new Map(); - Map: Phaser.Tilemaps.Tilemap|null = null; + Map: Phaser.Tilemaps.Tilemap; Layers : Array; Objects : Array; mapFile: ITiledMap|null; @@ -38,7 +38,7 @@ export class GameScene extends Phaser.Scene { startX = 704;// 22 case startY = 32; // 1 case circleTexture: CanvasTexture; - initPosition: PositionInterface|null = null; + private initPosition: PositionInterface|null = null; private playersPositionInterpolator = new PlayersPositionInterpolator(); MapKey: string; @@ -121,7 +121,9 @@ export class GameScene extends Phaser.Scene { //hook initialisation init(initData : GameSceneInitInterface) { - this.initPosition = initData.initPosition; + if (initData.initPosition !== undefined) { + this.initPosition = initData.initPosition; + } } //hook create scene @@ -338,7 +340,7 @@ export class GameScene extends Phaser.Scene { //initialise player //TODO create animation moving between exit and start this.CurrentPlayer = new Player( - null, // The current player is not has no id (because the id can change if connection is lost and we should check that id using the GameManager. + 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, @@ -503,9 +505,10 @@ export class GameScene extends Phaser.Scene { let player = this.MapPlayersByKey.get(userId); if (player === undefined) { console.error('Cannot find user with id ', userId); + } else { + player.destroy(); + this.MapPlayers.remove(player); } - player.destroy(); - this.MapPlayers.remove(player); this.MapPlayersByKey.delete(userId); this.playersPositionInterpolator.removePlayer(userId); } @@ -525,8 +528,9 @@ export class GameScene extends Phaser.Scene { shareGroupPosition(groupPositionMessage: GroupCreatedUpdatedMessageInterface) { let groupId = groupPositionMessage.groupId; - if (this.groups.has(groupId)) { - this.groups.get(groupId).setPosition(Math.round(groupPositionMessage.position.x), Math.round(groupPositionMessage.position.y)); + let group = this.groups.get(groupId); + if (group !== undefined) { + group.setPosition(Math.round(groupPositionMessage.position.x), Math.round(groupPositionMessage.position.y)); } else { // TODO: circle radius should not be hard stored let sprite = new Sprite( @@ -541,10 +545,11 @@ export class GameScene extends Phaser.Scene { } deleteGroup(groupId: string): void { - if(!this.groups.get(groupId)){ + let group = this.groups.get(groupId); + if(!group){ return; } - this.groups.get(groupId).destroy(); + group.destroy(); this.groups.delete(groupId); } diff --git a/front/src/Phaser/Login/LoginScene.ts b/front/src/Phaser/Login/LoginScene.ts index f00b387..606b407 100644 --- a/front/src/Phaser/Login/LoginScene.ts +++ b/front/src/Phaser/Login/LoginScene.ts @@ -4,7 +4,7 @@ import {TextInput} from "../Components/TextInput"; import {ClickButton} from "../Components/ClickButton"; import Image = Phaser.GameObjects.Image; import Rectangle = Phaser.GameObjects.Rectangle; -import {PLAYER_RESOURCES} from "../Entity/PlayableCaracter"; +import {PLAYER_RESOURCES} from "../Entity/PlayableCharacter"; import {cypressAsserter} from "../../Cypress/CypressAsserter"; import {SelectCharacterSceneInitDataInterface, SelectCharacterSceneName} from "./SelectCharacterScene"; diff --git a/front/src/Phaser/Login/SelectCharacterScene.ts b/front/src/Phaser/Login/SelectCharacterScene.ts index 8a52afc..f91a810 100644 --- a/front/src/Phaser/Login/SelectCharacterScene.ts +++ b/front/src/Phaser/Login/SelectCharacterScene.ts @@ -3,7 +3,7 @@ import {TextField} from "../Components/TextField"; import {ClickButton} from "../Components/ClickButton"; import Image = Phaser.GameObjects.Image; import Rectangle = Phaser.GameObjects.Rectangle; -import {PLAYER_RESOURCES} from "../Entity/PlayableCaracter"; +import {PLAYER_RESOURCES} from "../Entity/PlayableCharacter"; //todo: put this constants in a dedicated file export const SelectCharacterSceneName = "SelectCharacterScene"; diff --git a/front/src/Phaser/Player/Player.ts b/front/src/Phaser/Player/Player.ts index ae40efa..2e88bce 100644 --- a/front/src/Phaser/Player/Player.ts +++ b/front/src/Phaser/Player/Player.ts @@ -2,16 +2,16 @@ import {PlayerAnimationNames} from "./Animation"; import {GameScene, Textures} from "../Game/GameScene"; import {MessageUserPositionInterface, PointInterface} from "../../Connection"; import {ActiveEventList, UserInputEvent, UserInputManager} from "../UserInput/UserInputManager"; -import {PlayableCaracter} from "../Entity/PlayableCaracter"; +import {PlayableCharacter} from "../Entity/PlayableCharacter"; export const hasMovedEventName = "hasMoved"; -export interface CurrentGamerInterface extends PlayableCaracter{ +export interface CurrentGamerInterface extends PlayableCharacter{ moveUser(delta: number) : void; say(text : string) : void; } -export interface GamerInterface extends PlayableCaracter{ +export interface GamerInterface extends PlayableCharacter{ userId : string; updatePosition(position: PointInterface): void; say(text : string) : void; @@ -27,7 +27,7 @@ interface AnimationData { } -export class Player extends PlayableCaracter implements CurrentGamerInterface, GamerInterface { +export class Player extends PlayableCharacter implements CurrentGamerInterface, GamerInterface { userId: string; userInputManager: UserInputManager; previousDirection: string; diff --git a/front/src/Phaser/Reconnecting/ReconnectingScene.ts b/front/src/Phaser/Reconnecting/ReconnectingScene.ts index 273820b..45651ed 100644 --- a/front/src/Phaser/Reconnecting/ReconnectingScene.ts +++ b/front/src/Phaser/Reconnecting/ReconnectingScene.ts @@ -4,7 +4,7 @@ import {TextInput} from "../Components/TextInput"; import {ClickButton} from "../Components/ClickButton"; import Image = Phaser.GameObjects.Image; import Rectangle = Phaser.GameObjects.Rectangle; -import {PLAYER_RESOURCES} from "../Entity/PlayableCaracter"; +import {PLAYER_RESOURCES} from "../Entity/PlayableCharacter"; import {cypressAsserter} from "../../Cypress/CypressAsserter"; import Sprite = Phaser.GameObjects.Sprite; diff --git a/front/src/Phaser/UserInput/UserInputManager.ts b/front/src/Phaser/UserInput/UserInputManager.ts index 9c4ca66..fec4f58 100644 --- a/front/src/Phaser/UserInput/UserInputManager.ts +++ b/front/src/Phaser/UserInput/UserInputManager.ts @@ -2,7 +2,6 @@ import Map = Phaser.Structs.Map; import {GameScene} from "../Game/GameScene"; interface UserInputManagerDatum { - keyCode: number; keyInstance: Phaser.Input.Keyboard.Key; event: UserInputEvent } @@ -33,27 +32,26 @@ export class ActiveEventList { //this class is responsible for catching user inputs and listing all active user actions at every game tick events. export class UserInputManager { - private KeysCode: UserInputManagerDatum[] = [ - {keyCode: Phaser.Input.Keyboard.KeyCodes.Z, event: UserInputEvent.MoveUp, keyInstance: null}, - {keyCode: Phaser.Input.Keyboard.KeyCodes.Q, event: UserInputEvent.MoveLeft, keyInstance: null}, - {keyCode: Phaser.Input.Keyboard.KeyCodes.S, event: UserInputEvent.MoveDown, keyInstance: null}, - {keyCode: Phaser.Input.Keyboard.KeyCodes.D, event: UserInputEvent.MoveRight, keyInstance: null}, + private KeysCode: UserInputManagerDatum[]; - {keyCode: Phaser.Input.Keyboard.KeyCodes.UP, event: UserInputEvent.MoveUp, keyInstance: null}, - {keyCode: Phaser.Input.Keyboard.KeyCodes.LEFT, event: UserInputEvent.MoveLeft, keyInstance: null}, - {keyCode: Phaser.Input.Keyboard.KeyCodes.DOWN, event: UserInputEvent.MoveDown, keyInstance: null}, - {keyCode: Phaser.Input.Keyboard.KeyCodes.RIGHT, event: UserInputEvent.MoveRight, keyInstance: null}, + constructor(Scene : GameScene) { - {keyCode: Phaser.Input.Keyboard.KeyCodes.SHIFT, event: UserInputEvent.SpeedUp, keyInstance: null}, + this.KeysCode = [ + {event: UserInputEvent.MoveUp, keyInstance: Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.Z) }, + {event: UserInputEvent.MoveLeft, keyInstance: Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.Q) }, + {event: UserInputEvent.MoveDown, keyInstance: Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.S) }, + {event: UserInputEvent.MoveRight, keyInstance: Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.D) }, - {keyCode: Phaser.Input.Keyboard.KeyCodes.E, event: UserInputEvent.Interact, keyInstance: null}, - {keyCode: Phaser.Input.Keyboard.KeyCodes.F, event: UserInputEvent.Shout, keyInstance: null}, - ]; + {event: UserInputEvent.MoveUp, keyInstance: Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.UP) }, + {event: UserInputEvent.MoveLeft, keyInstance: Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.LEFT) }, + {event: UserInputEvent.MoveDown, keyInstance: Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.DOWN) }, + {event: UserInputEvent.MoveRight, keyInstance: Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.RIGHT) }, - constructor(Scene : GameScene) { - this.KeysCode.forEach(d => { - d.keyInstance = Scene.input.keyboard.addKey(d.keyCode); - }); + {event: UserInputEvent.SpeedUp, keyInstance: Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SHIFT) }, + + {event: UserInputEvent.Interact, keyInstance: Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.E) }, + {event: UserInputEvent.Shout, keyInstance: Scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.F) }, + ]; } getEventListForGameTick(): ActiveEventList { -- 2.25.1