diff options
author | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2023-08-29 12:05:02 -0600 |
---|---|---|
committer | Elizabeth Hunt <elizabeth.hunt@simponic.xyz> | 2023-08-29 12:05:02 -0600 |
commit | fd1bb1cca9521348ae2849ef30be09264503681e (patch) | |
tree | 6859c24c53fdd2a83ed0a92ee10272aa70b6c55d /client | |
parent | 8a4ab8d79b5ce1dabb431168398b5d5111fe326c (diff) | |
download | jumpstorm-fd1bb1cca9521348ae2849ef30be09264503681e.tar.gz jumpstorm-fd1bb1cca9521348ae2849ef30be09264503681e.zip |
don't update controllable entities on the client
Diffstat (limited to 'client')
-rw-r--r-- | client/src/JumpStorm.ts | 51 | ||||
-rw-r--r-- | client/vite.config.ts | 3 |
2 files changed, 42 insertions, 12 deletions
diff --git a/client/src/JumpStorm.ts b/client/src/JumpStorm.ts index 6f9e24f..1beeb0d 100644 --- a/client/src/JumpStorm.ts +++ b/client/src/JumpStorm.ts @@ -8,7 +8,8 @@ import { Physics, Input, Collision, - NetworkUpdate + NetworkUpdate, + SystemNames } from '@engine/systems'; import { type MessageQueueProvider, @@ -20,6 +21,7 @@ import { type EntityUpdateBody } from '@engine/network'; import { stringify, parse } from '@engine/utils'; +import { ComponentNames, Control, NetworkUpdateable } from '@engine/components'; class ClientMessageProcessor implements MessageProcessor { private game: Game; @@ -32,11 +34,27 @@ class ClientMessageProcessor implements MessageProcessor { switch (message.type) { case MessageType.NEW_ENTITIES: const entityAdditions = message.body as unknown as EntityAddBody[]; - entityAdditions.forEach((addBody) => - this.game.addEntity( - Entity.from(addBody.entityName, addBody.id, addBody.args) - ) - ); + entityAdditions.forEach((addBody) => { + const entity = Entity.from( + addBody.entityName, + addBody.id, + addBody.args + ); + if (entity.hasComponent(ComponentNames.Control)) { + const clientId = this.game.getSystem<Input>( + SystemNames.Input + ).clientId; + const control = entity.getComponent<Control>( + ComponentNames.Control + ); + + if (control.controllableBy === clientId) { + entity.addComponent(new NetworkUpdateable()); + } + } + + this.game.addEntity(entity); + }); break; case MessageType.REMOVE_ENTITIES: const ids = message.body as unknown as string[]; @@ -44,9 +62,22 @@ class ClientMessageProcessor implements MessageProcessor { break; case MessageType.UPDATE_ENTITIES: const entityUpdates = message.body as unknown as EntityUpdateBody[]; - entityUpdates.forEach( - ({ id, args }) => this.game.getEntity(id)?.setFrom(args) - ); + entityUpdates.forEach(({ id, args }) => { + const entity = this.game.getEntity(id); + if (!entity) return; + if (entity && entity.hasComponent(ComponentNames.Control)) { + const clientId = this.game.getSystem<Input>( + SystemNames.Input + ).clientId; + const control = entity.getComponent<Control>( + ComponentNames.Control + ); + + // don't listen to entities which we control + if (control.controllableBy == clientId) return; + } + entity.setFrom(args); + }); break; default: break; @@ -131,6 +162,7 @@ export class JumpStorm { const grid = new Grid(); [ + new Physics(), new NetworkUpdate( clientSocketMessageQueueProvider, clientSocketMessagePublisher, @@ -138,7 +170,6 @@ export class JumpStorm { ), inputSystem, new FacingDirection(), - new Physics(), new Collision(grid), new WallBounds(), new Render(ctx) diff --git a/client/vite.config.ts b/client/vite.config.ts index 6f0e1d0..d8b999c 100644 --- a/client/vite.config.ts +++ b/client/vite.config.ts @@ -5,10 +5,9 @@ import { fileURLToPath, URL } from 'node:url'; // https://vitejs.dev/config/ export default defineConfig({ server: { - host: '0.0.0.0', proxy: { '/api': { - target: 'http://10.0.0.237:8080', + target: 'http://localhost:8080', ws: true, rewrite: (path) => path.replace(/^\/api/, '') } |