diff options
Diffstat (limited to 'client/src/JumpStorm.ts')
-rw-r--r-- | client/src/JumpStorm.ts | 69 |
1 files changed, 45 insertions, 24 deletions
diff --git a/client/src/JumpStorm.ts b/client/src/JumpStorm.ts index 01cc8d8..92bddcf 100644 --- a/client/src/JumpStorm.ts +++ b/client/src/JumpStorm.ts @@ -1,6 +1,6 @@ -import { Game } from "@engine/Game"; -import { Entity } from "@engine/entities"; -import { Grid } from "@engine/structures"; +import { Game } from '@engine/Game'; +import { Entity, Floor } from '@engine/entities'; +import { Grid } from '@engine/structures'; import { WallBounds, FacingDirection, @@ -8,17 +8,19 @@ import { Physics, Input, Collision, - NetworkUpdate, -} from "@engine/systems"; + NetworkUpdate +} from '@engine/systems'; import { type MessageQueueProvider, type MessagePublisher, type MessageProcessor, type Message, type EntityAddBody, - MessageType, -} from "@engine/network"; -import { stringify, parse } from "@engine/utils"; + MessageType +} from '@engine/network'; +import { stringify, parse } from '@engine/utils'; +import { BoundingBox, Sprite } from '@engine/components'; +import { Miscellaneous } from '@engine/config'; class ClientMessageProcessor implements MessageProcessor { private game: Game; @@ -29,14 +31,19 @@ class ClientMessageProcessor implements MessageProcessor { public process(message: Message) { switch (message.type) { - case MessageType.NEW_ENTITY: - const entityAddBody = message.body as unknown as EntityAddBody; - this.game.addEntity( - Entity.from(entityAddBody.entityName, entityAddBody.args), + case MessageType.NEW_ENTITIES: + const entityAdditions = message.body as unknown as EntityAddBody[]; + entityAdditions.forEach((addBody) => + this.game.addEntity(Entity.from(addBody.entityName, addBody.args)) ); break; + case MessageType.REMOVE_ENTITIES: + const ids = message.body as unknown as string[]; + ids.forEach((id) => this.game.removeEntity(id)); + break; + default: + break; } - console.log(message); } } @@ -49,9 +56,9 @@ class ClientSocketMessageQueueProvider implements MessageQueueProvider { this.socket = socket; this.messages = []; - this.socket.addEventListener("message", (e) => { - const message = parse<Message>(e.data); - this.messages.push(message); + this.socket.addEventListener('message', (e) => { + const messages = parse<Message[]>(e.data); + this.messages = this.messages.concat(messages); }); } @@ -79,7 +86,7 @@ class ClientSocketMessagePublisher implements MessagePublisher { public publish() { this.messages.forEach((message: Message) => - this.socket.send(stringify(message)), + this.socket.send(stringify(message)) ); } } @@ -96,7 +103,7 @@ export class JumpStorm { ctx: CanvasRenderingContext2D, httpMethod: string, wsMethod: string, - host: string, + host: string ) { await fetch(`${httpMethod}://${host}/assign`) .then((resp) => { @@ -115,7 +122,7 @@ export class JumpStorm { const clientSocketMessageQueueProvider = new ClientSocketMessageQueueProvider(socket); const clientSocketMessagePublisher = new ClientSocketMessagePublisher( - socket, + socket ); const clientMessageProcessor = new ClientMessageProcessor(this.game); [ @@ -123,14 +130,28 @@ export class JumpStorm { new FacingDirection(), new Physics(), new Collision(grid), - new WallBounds(ctx.canvas.width), + new WallBounds(), new NetworkUpdate( clientSocketMessageQueueProvider, clientSocketMessagePublisher, - clientMessageProcessor, + clientMessageProcessor ), - new Render(ctx), + new Render(ctx) ].forEach((system) => this.game.addSystem(system)); + + const floor = new Floor(160); + const floorHeight = 40; + + floor.addComponent( + new BoundingBox( + { + x: Miscellaneous.WIDTH / 2, + y: Miscellaneous.HEIGHT - floorHeight / 2 + }, + { width: Miscellaneous.WIDTH, height: floorHeight } + ) + ); + this.game.addEntity(floor); } public play() { @@ -146,13 +167,13 @@ export class JumpStorm { private createInputSystem(): Input { const inputSystem = new Input(this.clientId); - window.addEventListener("keydown", (e) => { + window.addEventListener('keydown', (e) => { if (!e.repeat) { inputSystem.keyPressed(e.key); } }); - window.addEventListener("keyup", (e) => inputSystem.keyReleased(e.key)); + window.addEventListener('keyup', (e) => inputSystem.keyReleased(e.key)); return inputSystem; } |