diff --git a/app/engine/engine.js b/app/engine/engine.js index 712c29b..a0bea16 100644 --- a/app/engine/engine.js +++ b/app/engine/engine.js @@ -37,7 +37,7 @@ export default class Engine { static Ecs = Ecs; - incoming = []; + incomingActions = []; constructor(Server) { const ecs = new this.constructor.Ecs(); @@ -81,15 +81,11 @@ export default class Engine { } } this.server = new SilphiusServer(); - this.server.addPacketListener((connection, packet) => { - this.accept(connection, packet); + this.server.addPacketListener('Action', (connection, payload) => { + this.incomingActions.push([this.connectedPlayers.get(connection).entity, payload]); }); } - accept(connection, packet) { - this.incoming.push([this.connectedPlayers.get(connection).entity, packet]); - } - async connectPlayer(connection) { this.connections.push(connection); const entityJson = await this.loadPlayer(connection); @@ -134,18 +130,12 @@ export default class Engine { for (const i in this.ecses) { this.ecses[i].setClean(); } - for (const [{Controlled}, {payload, type}] of this.incoming) { - switch (type) { - case 'Action': { - if (payload.type in MOVE_MAP) { - Controlled[MOVE_MAP[payload.type]] = payload.value; - } - break; - } - default: + for (const [{Controlled}, payload] of this.incomingActions) { + if (payload.type in MOVE_MAP) { + Controlled[MOVE_MAP[payload.type]] = payload.value; } } - this.incoming = []; + this.incomingActions = []; for (const i in this.ecses) { this.ecses[i].tick(elapsed); } diff --git a/app/hooks/use-packet.js b/app/hooks/use-packet.js index 6f3e8cd..e717fb4 100644 --- a/app/hooks/use-packet.js +++ b/app/hooks/use-packet.js @@ -5,14 +5,9 @@ import ClientContext from '@/context/client.js'; export default function usePacket(type, fn, dependencies) { const client = useContext(ClientContext); useEffect(() => { - function onPacket(packet) { - if (packet.type === type) { - fn(packet.payload); - } - } - client.addPacketListener(onPacket); + client.addPacketListener(type, fn); return () => { - client.removePacketListener(onPacket); + client.removePacketListener(type, fn); }; }, dependencies); } \ No newline at end of file diff --git a/app/net/client/client.js b/app/net/client/client.js index 23415d2..cf36c3e 100644 --- a/app/net/client/client.js +++ b/app/net/client/client.js @@ -2,20 +2,31 @@ import {CLIENT_LATENCY} from '@/constants.js'; export default class Client { constructor() { - this.listeners = []; + this.listeners = {}; } accept(packet) { - for (const i in this.listeners) { - this.listeners[i](packet); + const listeners = this.listeners[packet.type]; + if (!listeners) { + return; + } + for (const i in listeners) { + listeners[i](packet.payload); } } - addPacketListener(listener) { - this.listeners.push(listener); + addPacketListener(type, listener) { + if (!this.listeners[type]) { + this.listeners[type] = []; + } + this.listeners[type].push(listener); } - removePacketListener(listener) { - const index = this.listeners.indexOf(listener); + removePacketListener(type, listener) { + const listeners = this.listeners[type]; + if (!listeners) { + return; + } + const index = listeners.indexOf(listener); if (-1 !== index) { - this.listeners.splice(index, 1); + listeners.splice(index, 1); } } send(packet) { diff --git a/app/net/server/server.js b/app/net/server/server.js index b79e8ec..3b2c7dd 100644 --- a/app/net/server/server.js +++ b/app/net/server/server.js @@ -2,20 +2,31 @@ import {SERVER_LATENCY} from '@/constants.js'; export default class Server { constructor() { - this.listeners = []; + this.listeners = {}; } accept(connection, packet) { - for (const i in this.listeners) { - this.listeners[i](connection, packet); + const listeners = this.listeners[packet.type]; + if (!listeners) { + return; + } + for (const i in listeners) { + listeners[i](connection, packet.payload); } } - addPacketListener(listener) { - this.listeners.push(listener); + addPacketListener(type, listener) { + if (!this.listeners[type]) { + this.listeners[type] = []; + } + this.listeners[type].push(listener); } - removePacketListener(listener) { - const index = this.listeners.indexOf(listener); + removePacketListener(type, listener) { + const listeners = this.listeners[type]; + if (!listeners) { + return; + } + const index = listeners.indexOf(listener); if (-1 !== index) { - this.listeners.splice(index, 1); + listeners.splice(index, 1); } } send(connection, packet) { diff --git a/app/packets/tick.js b/app/packets/tick.js index 24ef48a..f30aff1 100644 --- a/app/packets/tick.js +++ b/app/packets/tick.js @@ -1,3 +1,3 @@ import Packet from '@/net/packet.js'; -export default class Tick extends Packet {}; +export default class Tick extends Packet {}