diff options
| author | Mike Vink <mike1994vink@gmail.com> | 2021-07-20 22:05:13 +0200 |
|---|---|---|
| committer | Mike Vink <mike1994vink@gmail.com> | 2021-07-20 22:05:13 +0200 |
| commit | f4dc48cd8359d5b8da35f678ae6630c5c4427769 (patch) | |
| tree | 075acbc259735c096751d9ece5044b8114c1fddb | |
| parent | b5ec13318a8b6a5f825eb726d6465873495a49ab (diff) | |
refactor(client): typed message improvement!
| -rw-r--r-- | api/src/main/java/akkamon/api/MessagingEngine.java | 14 | ||||
| -rw-r--r-- | api/src/main/java/akkamon/api/models/Event.java | 6 | ||||
| -rw-r--r-- | api/src/main/java/akkamon/api/models/EventType.java | 5 | ||||
| -rw-r--r-- | client/src/Direction.ts | 10 | ||||
| -rw-r--r-- | client/src/GameState.ts | 29 | ||||
| -rw-r--r-- | client/src/GridPhysics.ts | 11 | ||||
| -rw-r--r-- | client/src/client.ts | 21 | ||||
| -rw-r--r-- | client/src/events.ts | 8 | ||||
| -rw-r--r-- | client/src/player.ts | 21 | ||||
| -rw-r--r-- | client/src/scene.ts | 24 | ||||
| -rw-r--r-- | client/src/sprite.ts | 2 | ||||
| -rw-r--r-- | domain/src/main/java/akkamon/domain/AkkamonNexus.java | 15 | ||||
| -rw-r--r-- | domain/src/main/java/akkamon/domain/Direction.java | 8 |
13 files changed, 91 insertions, 83 deletions
diff --git a/api/src/main/java/akkamon/api/MessagingEngine.java b/api/src/main/java/akkamon/api/MessagingEngine.java index 4919b32..e888d8b 100644 --- a/api/src/main/java/akkamon/api/MessagingEngine.java +++ b/api/src/main/java/akkamon/api/MessagingEngine.java @@ -11,6 +11,7 @@ import com.google.gson.Gson; import java.util.HashMap; import java.util.Map; +import java.util.UUID; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; @@ -59,10 +60,19 @@ public class MessagingEngine implements AkkamonMessageEngine { void incoming(AkkamonSession session, String message) { Event event = gson.fromJson(message, Event.class); + String trainerId = String.valueOf(trainerIdToAkkamonSessions.size()); + String sceneId = "akkamonStartScene"; + switch (event.type) { + case START_MOVING: + system.tell(new AkkamonNexus.RequestStartMoving( + UUID.randomUUID().getMostSignificantBits() & Long.MAX_VALUE, + "0", + event.sceneId, + event.direction + )); + break; case TRAINER_REGISTRATION: - String trainerId = String.valueOf(trainerIdToAkkamonSessions.size()); - String sceneId = "AkkamonStartScene"; system.tell(new AkkamonNexus.RequestTrainerRegistration( trainerId, diff --git a/api/src/main/java/akkamon/api/models/Event.java b/api/src/main/java/akkamon/api/models/Event.java index 8517b06..351d39f 100644 --- a/api/src/main/java/akkamon/api/models/Event.java +++ b/api/src/main/java/akkamon/api/models/Event.java @@ -1,6 +1,10 @@ package akkamon.api.models; +import akkamon.domain.Direction; + public class Event { public EventType type; - + public String trainerId; + public Direction direction; + public String sceneId; } diff --git a/api/src/main/java/akkamon/api/models/EventType.java b/api/src/main/java/akkamon/api/models/EventType.java index 1ca2ef2..ad537a5 100644 --- a/api/src/main/java/akkamon/api/models/EventType.java +++ b/api/src/main/java/akkamon/api/models/EventType.java @@ -7,5 +7,8 @@ public enum EventType { TRAINER_REGISTRATION, @SerializedName("HeartBeat") - HEART_BEAT + HEART_BEAT, + + @SerializedName("StartMoving") + START_MOVING } diff --git a/client/src/Direction.ts b/client/src/Direction.ts index 87484ad..a173585 100644 --- a/client/src/Direction.ts +++ b/client/src/Direction.ts @@ -1,7 +1,7 @@ export enum Direction { - NONE = "none", - LEFT = "left", - UP = "up", - RIGHT = "right", - DOWN = "down", + NONE = "NONE", + LEFT = "LEFT", + UP = "UP", + RIGHT = "RIGHT", + DOWN = "DOWN", } diff --git a/client/src/GameState.ts b/client/src/GameState.ts index a226864..f1d8ce9 100644 --- a/client/src/GameState.ts +++ b/client/src/GameState.ts @@ -2,31 +2,10 @@ import type { Player } from './player'; export class GameState { - static instance: GameState; - - currentPlayer: Player | undefined; - remotePlayers: { [name: string]: Player } = {}; - - static getInstance() { - if (GameState.instance) return GameState.instance; - else { - GameState.instance = new GameState(); - return GameState.instance; - } - } - - setCurrentPlayer(player: Player) { - this.currentPlayer = player; - } - - posUpdate(receivedState: GameState) { - if (this.currentPlayer === undefined) { - this.currentPlayer = receivedState.currentPlayer!; - } - - Object.keys(receivedState.remotePlayers) - .forEach(key => this.remotePlayers[key] = receivedState.remotePlayers[key]); + localPlayerState?: Player; + remoteTrainerIdToPlayerState: { [trainerid: string]: Player } = {}; + getLocalMutablePlayerState(): Player { + return this.localPlayerState!; } - } diff --git a/client/src/GridPhysics.ts b/client/src/GridPhysics.ts index cb867cd..d363a60 100644 --- a/client/src/GridPhysics.ts +++ b/client/src/GridPhysics.ts @@ -1,12 +1,12 @@ import Phaser from 'phaser'; -import type PlayerSprite from './sprite'; +import type { PlayerSprite } from './sprite'; import { Direction } from './Direction'; -import AkkamonStartScene from './game'; +import AkkamonStartScene from './scene'; import { akkamonClient } from './app'; import { - GridMoveStartEvent + StartMovingEvent } from './events'; export class GridPhysics { @@ -49,7 +49,10 @@ export class GridPhysics { } private startMoving(direction: Direction): void { - // Client.getInstance().out(); + console.log("Sending startMovingEvent"); + akkamonClient.send( + new StartMovingEvent(direction) + ); this.playerSprite.startAnimation(direction); this.movementDirection = direction; this.updatePlayerSpriteTilePosition(); diff --git a/client/src/client.ts b/client/src/client.ts index aeb9845..c83c39a 100644 --- a/client/src/client.ts +++ b/client/src/client.ts @@ -1,4 +1,5 @@ import type AkkamonSession from './session'; +import type { GameState } from './GameState'; import { Socket } from './socket'; import { EventType, @@ -11,6 +12,7 @@ export class Client { private session: AkkamonSession; + private akkamonState?: GameState; constructor( url: string @@ -18,8 +20,8 @@ export class Client this.session = new Socket(url, this); } - setSession(akkamonSession: AkkamonSession) { - this.session = akkamonSession; + getMutableState(): GameState { + return this.akkamonState!; } in(eventString: string) { @@ -40,19 +42,4 @@ export class Client this.session.send(JSON.stringify(event)); } } - - login(user: {name:string, password: string}) { - console.log("Sending the login message"); - if (this.session) { - this.session.send(JSON.stringify( - { - type: 'login', - user: { - name: user.name, - password: user.password - } - } - )); - } - } } diff --git a/client/src/events.ts b/client/src/events.ts index 7b58ddd..5a64d5b 100644 --- a/client/src/events.ts +++ b/client/src/events.ts @@ -6,7 +6,7 @@ import type { Direction } from './Direction'; export enum EventType { HEART_BEAT = "HeartBeat", PLAYER_REGISTRATION = "PlayerRegistrationEvent", - GRID_MOVE_START = "GridMoveStartEvent" + START_MOVING = "StartMoving" } export interface AkkamonEvent { @@ -21,12 +21,12 @@ export class PlayerRegistrationEvent implements AkkamonEvent { ) { } } -export class GridMoveStartEvent implements AkkamonEvent { +export class StartMovingEvent implements AkkamonEvent { - public type: EventType = EventType.GRID_MOVE_START; + public type: EventType = EventType.START_MOVING; constructor( - public direction: Direction + public direction: Direction, ) { } } diff --git a/client/src/player.ts b/client/src/player.ts index 93040a4..20a93fd 100644 --- a/client/src/player.ts +++ b/client/src/player.ts @@ -3,8 +3,8 @@ import type Phaser from 'phaser'; type Sprite = Phaser.Types.Physics.Arcade.SpriteWithDynamicBody; type PlayerConfig = { - name: string, - position: {x: number, y: number} + trainerId: string, + position: Phaser.Math.Vector2; } type Input = { @@ -13,21 +13,12 @@ type Input = { export class Player { - name: string - position: {x:number, y: number} - sprite: Sprite | undefined; - input: Input | undefined; + trainerId: string + position: Phaser.Math.Vector2 - constructor({name, position}: PlayerConfig) { - this.name = name; + constructor({trainerId, position}: PlayerConfig) { + this.trainerId = trainerId; this.position = position } - setSprite(sprite: Sprite) { - this.sprite = sprite; - } - - setInput(input: Input) { - this.input = input; - } } diff --git a/client/src/scene.ts b/client/src/scene.ts index 76ac029..29bf253 100644 --- a/client/src/scene.ts +++ b/client/src/scene.ts @@ -1,7 +1,7 @@ import Phaser from 'phaser'; -import type { Player } from './player'; -import { Client } from './client'; -import { GameState } from './GameState'; +import { akkamonClient } from './app'; +import type { GameState } from './GameState'; +import { Player } from './player'; import { PlayerSprite } from './sprite'; import { GridControls } from './GridControls'; import { GridPhysics } from './GridPhysics'; @@ -17,6 +17,7 @@ export default class AkkamonStartScene extends Phaser.Scene static readonly TILE_SIZE = 32; + private akkamonState?: GameState private gridPhysics?: GridPhysics private gridControls?: GridControls @@ -80,15 +81,22 @@ export default class AkkamonStartScene extends Phaser.Scene // Create a sprite with physics enabled via the physics system. The image used for the sprite has // a bit of whitespace, so I'm using setSize & setOffset to control the size of the player's body. - let player = new PlayerSprite({ - scene: this, - tilePos: new Phaser.Math.Vector2( + this.akkamonState = akkamonClient.getMutableState(); + + var tilePos = new Phaser.Math.Vector2( Math.floor(this.spawnPoint.x! / AkkamonStartScene.TILE_SIZE), Math.floor(this.spawnPoint.y! / AkkamonStartScene.TILE_SIZE), - ), + ); + + let player = new PlayerSprite({ + scene: this, + tilePos: tilePos, texture: this.textures.get("atlas"), frame: "misa-front", - player: GameState.getInstance().currentPlayer!, + player: new Player({ + trainerId: 'ash', + position: tilePos + })// this.akkamonState.getLocalMutablePlayerState(), }); this.add.existing(player); diff --git a/client/src/sprite.ts b/client/src/sprite.ts index e173131..77ca2a0 100644 --- a/client/src/sprite.ts +++ b/client/src/sprite.ts @@ -1,6 +1,6 @@ import Phaser from 'phaser'; import AkkamonStartScene from './scene'; -import type Player from './player'; +import type { Player } from './player'; import type { Direction } from './Direction'; type PlayerSpriteConfig = { diff --git a/domain/src/main/java/akkamon/domain/AkkamonNexus.java b/domain/src/main/java/akkamon/domain/AkkamonNexus.java index acae59d..c9e07f0 100644 --- a/domain/src/main/java/akkamon/domain/AkkamonNexus.java +++ b/domain/src/main/java/akkamon/domain/AkkamonNexus.java @@ -47,6 +47,20 @@ public class AkkamonNexus extends AbstractBehavior<AkkamonNexus.Command> { } } + public static class RequestStartMoving implements Command, SceneTrainerGroup.Command { + public long requestId; + public String trainerid; + public String sceneId; + public Direction direction; + + public RequestStartMoving(long requestId, String trainerid, String sceneId, Direction direction) { + this.requestId = requestId; + this.trainerid = trainerid; + this.sceneId = sceneId; + this.direction = direction; + } + } + private static class SceneTrainerGroupTerminated implements AkkamonNexus.Command { public SceneTrainerGroupTerminated(String sceneId) { } @@ -107,4 +121,5 @@ public class AkkamonNexus extends AbstractBehavior<AkkamonNexus.Command> { return this; } + } diff --git a/domain/src/main/java/akkamon/domain/Direction.java b/domain/src/main/java/akkamon/domain/Direction.java new file mode 100644 index 0000000..c0908d8 --- /dev/null +++ b/domain/src/main/java/akkamon/domain/Direction.java @@ -0,0 +1,8 @@ +package akkamon.domain; + +public enum Direction { + UP, + DOWN, + LEFT, + RIGHT +} |
