diff options
| author | Mike Vink <mike1994vink@gmail.com> | 2021-07-20 12:58:19 +0200 |
|---|---|---|
| committer | Mike Vink <mike1994vink@gmail.com> | 2021-07-20 12:58:19 +0200 |
| commit | 36730af06964e75735ae5d099ee9c5bf2c6b7fc9 (patch) | |
| tree | 97fc4f9edaba6c5e405f62b76603dd6d8fa5cd0a | |
| parent | 07b199ba66762370bf79b4df747002304bfc03df (diff) | |
tmp(client): preparation for actor messaging
| -rw-r--r-- | client/src/GridPhysics.ts | 15 | ||||
| -rw-r--r-- | client/src/app.ts | 41 | ||||
| -rw-r--r-- | client/src/client.ts | 32 | ||||
| -rw-r--r-- | client/src/events.ts | 21 | ||||
| -rw-r--r-- | client/src/socket.ts | 28 |
5 files changed, 59 insertions, 78 deletions
diff --git a/client/src/GridPhysics.ts b/client/src/GridPhysics.ts index 5fca10a..cb867cd 100644 --- a/client/src/GridPhysics.ts +++ b/client/src/GridPhysics.ts @@ -2,10 +2,16 @@ import Phaser from 'phaser'; import type PlayerSprite from './sprite'; import { Direction } from './Direction'; import AkkamonStartScene from './game'; +import { + akkamonClient +} from './app'; +import { + GridMoveStartEvent +} from './events'; export class GridPhysics { - private movementDirectionVectors: { + static movementDirectionVectors: { [key in Direction]?: Phaser.Math.Vector2; } = { [Direction.UP]: Phaser.Math.Vector2.UP, @@ -43,6 +49,7 @@ export class GridPhysics { } private startMoving(direction: Direction): void { + // Client.getInstance().out(); this.playerSprite.startAnimation(direction); this.movementDirection = direction; this.updatePlayerSpriteTilePosition(); @@ -73,7 +80,7 @@ export class GridPhysics { this.playerSprite.setTilePos( this.playerSprite .getTilePos() - .add(this.movementDirectionVectors[this.movementDirection]!) + .add(GridPhysics.movementDirectionVectors[this.movementDirection]!) ); } @@ -91,7 +98,7 @@ export class GridPhysics { this.tileSizePixelsWalked %= AkkamonStartScene.TILE_SIZE; - const directionVec = this.movementDirectionVectors[ + const directionVec = GridPhysics.movementDirectionVectors[ this.movementDirection ]!.clone(); @@ -128,7 +135,7 @@ export class GridPhysics { private tilePosInDirection(direction: Direction): Phaser.Math.Vector2 { return this.playerSprite .getTilePos() - .add(this.movementDirectionVectors[direction]!); + .add(GridPhysics.movementDirectionVectors[direction]!); } private hasBlockingTile(pos: Phaser.Math.Vector2): boolean { diff --git a/client/src/app.ts b/client/src/app.ts index 773699a..21b210e 100644 --- a/client/src/app.ts +++ b/client/src/app.ts @@ -1,16 +1,10 @@ // import Phaser from 'phaser'; -import GameState from './GameState'; -import Socket from './socket'; -import Client from './client'; -import Player from './player'; -import { Event } from './events'; +import AkkamonStartScene from './game'; +import { Client } from './client'; -const url = 'ws://localhost:8080'; -const session = Socket.getInstance('ws://localhost:8080', {name: "", password: ""}); -const client = Client.getInstance(); -client.setSession(session); +const serviceUrl = 'ws://localhost:8080'; -import AkkamonStartScene from './game'; +export const akkamonClient = new Client(serviceUrl); const config: Phaser.Types.Core.GameConfig & Phaser.Types.Core.RenderConfig = { type: Phaser.AUTO, @@ -27,29 +21,4 @@ const config: Phaser.Types.Core.GameConfig & Phaser.Types.Core.RenderConfig = { } }; -function delay(ms: number) { - return new Promise( resolve => setTimeout(resolve, ms) ); -} - -function gameStateSender() { - Client.getInstance().out({ - type: "clientSidePosUpdate", - gameState: GameState.getInstance().withoutSprite() - }); -} - -async function startGame() { - while (true) { - console.log(GameState.getInstance().currentPlayer); - if (GameState.getInstance().currentPlayer) { - const game: Phaser.Game = new Phaser.Game(config); - setInterval( - () => {gameStateSender()}, 200 - ); - break; - } - await delay(1000); - } -} - -startGame(); +const game: Phaser.Game = new Phaser.Game(config); diff --git a/client/src/client.ts b/client/src/client.ts index 6911872..b40f250 100644 --- a/client/src/client.ts +++ b/client/src/client.ts @@ -1,21 +1,19 @@ +import type AkkamonSession from './session'; +import { Socket } from './socket'; import type { - Event, + AkkamonEvent } from './events'; -import type AkkamonSession from './session'; -import GameState from './GameState'; -export default class Client +export class Client { - static instance: Client - session: AkkamonSession | undefined; - - static getInstance() { - if (Client.instance) return Client.instance; - else { - Client.instance = new Client(); - return Client.instance; - } + + private session: AkkamonSession; + + constructor( + url: string + ) { + this.session = new Socket(url, this); } setSession(akkamonSession: AkkamonSession) { @@ -23,17 +21,15 @@ export default class Client } in(eventString: string) { - let event: Event = JSON.parse(eventString); + let event: AkkamonEvent = JSON.parse(eventString); // console.log("-> client is handling incoming event:"); // console.log(event); switch (event.type) { - case 'serverSidePosUpdate': - GameState.getInstance().posUpdate(event.gameState!); - break; + } } - out(event: Event) { + out(event: AkkamonEvent) { // console.log("-> client is now sending out message:"); // console.log(event) if (this.session) { diff --git a/client/src/events.ts b/client/src/events.ts index 89cadaf..d8ccf02 100644 --- a/client/src/events.ts +++ b/client/src/events.ts @@ -1,9 +1,26 @@ import Phaser from 'phaser'; import type Player from './player'; import type GameState from './GameState'; +import type { Direction } from './Direction'; -export interface Event { +export interface AkkamonEvent { type: string - gameState?: GameState +} + +export class PlayerRegistrationEvent implements AkkamonEvent { + + public type: string = "PlayerRegistrationEvent"; + + constructor( + ) { } +} + +export class GridMoveStartEvent implements AkkamonEvent { + + public type: string = "GridMoveStartEvent"; + + constructor( + public direction: Direction + ) { } } diff --git a/client/src/socket.ts b/client/src/socket.ts index 7cb7ace..cb584a1 100644 --- a/client/src/socket.ts +++ b/client/src/socket.ts @@ -1,32 +1,24 @@ import Phaser from 'phaser'; -import Client from './client' +import type { Client } from './client' import type AkkamonSession from './session' +import { + PlayerRegistrationEvent +} from './events'; -export default class Socket extends WebSocket implements AkkamonSession +export class Socket extends WebSocket implements AkkamonSession { - static instance: AkkamonSession; - - static getInstance(url: string, user: {name: string, password: string}) { - if (Socket.instance) return Socket.instance; - else { - Socket.instance = new Socket(url, user); - return Socket.instance; - } - } - - constructor(url: string, user: {name: string, password: string}) { + constructor( + url: string, + client: Client + ) { super(url); - let client = Client.getInstance(); - - let session = this; - this.onopen = function echo(this: WebSocket, ev: Event) { console.log("opening socket"); console.log("this is the websocket"); console.log(this); console.log("logging in the session to the server"); - client.login(user); + client.out(new PlayerRegistrationEvent()); } this.onmessage = function incomingMessage(this: WebSocket, ev: MessageEvent) { |
