refactor: packet listeners
This commit is contained in:
parent
767cd4d564
commit
216e528c13
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
|
@ -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) {
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
import Packet from '@/net/packet.js';
|
||||
|
||||
export default class Tick extends Packet {};
|
||||
export default class Tick extends Packet {}
|
||||
|
|
Loading…
Reference in New Issue
Block a user