refactor: packet listeners

This commit is contained in:
cha0s 2024-06-13 01:26:01 -05:00
parent 767cd4d564
commit 216e528c13
5 changed files with 48 additions and 41 deletions

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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) {

View File

@ -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) {

View File

@ -1,3 +1,3 @@
import Packet from '@/net/packet.js';
export default class Tick extends Packet {};
export default class Tick extends Packet {}