refactor: packet listeners
This commit is contained in:
parent
767cd4d564
commit
216e528c13
|
@ -37,7 +37,7 @@ export default class Engine {
|
||||||
|
|
||||||
static Ecs = Ecs;
|
static Ecs = Ecs;
|
||||||
|
|
||||||
incoming = [];
|
incomingActions = [];
|
||||||
|
|
||||||
constructor(Server) {
|
constructor(Server) {
|
||||||
const ecs = new this.constructor.Ecs();
|
const ecs = new this.constructor.Ecs();
|
||||||
|
@ -81,15 +81,11 @@ export default class Engine {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.server = new SilphiusServer();
|
this.server = new SilphiusServer();
|
||||||
this.server.addPacketListener((connection, packet) => {
|
this.server.addPacketListener('Action', (connection, payload) => {
|
||||||
this.accept(connection, packet);
|
this.incomingActions.push([this.connectedPlayers.get(connection).entity, payload]);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
accept(connection, packet) {
|
|
||||||
this.incoming.push([this.connectedPlayers.get(connection).entity, packet]);
|
|
||||||
}
|
|
||||||
|
|
||||||
async connectPlayer(connection) {
|
async connectPlayer(connection) {
|
||||||
this.connections.push(connection);
|
this.connections.push(connection);
|
||||||
const entityJson = await this.loadPlayer(connection);
|
const entityJson = await this.loadPlayer(connection);
|
||||||
|
@ -134,18 +130,12 @@ export default class Engine {
|
||||||
for (const i in this.ecses) {
|
for (const i in this.ecses) {
|
||||||
this.ecses[i].setClean();
|
this.ecses[i].setClean();
|
||||||
}
|
}
|
||||||
for (const [{Controlled}, {payload, type}] of this.incoming) {
|
for (const [{Controlled}, payload] of this.incomingActions) {
|
||||||
switch (type) {
|
if (payload.type in MOVE_MAP) {
|
||||||
case 'Action': {
|
Controlled[MOVE_MAP[payload.type]] = payload.value;
|
||||||
if (payload.type in MOVE_MAP) {
|
|
||||||
Controlled[MOVE_MAP[payload.type]] = payload.value;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.incoming = [];
|
this.incomingActions = [];
|
||||||
for (const i in this.ecses) {
|
for (const i in this.ecses) {
|
||||||
this.ecses[i].tick(elapsed);
|
this.ecses[i].tick(elapsed);
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,14 +5,9 @@ import ClientContext from '@/context/client.js';
|
||||||
export default function usePacket(type, fn, dependencies) {
|
export default function usePacket(type, fn, dependencies) {
|
||||||
const client = useContext(ClientContext);
|
const client = useContext(ClientContext);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
function onPacket(packet) {
|
client.addPacketListener(type, fn);
|
||||||
if (packet.type === type) {
|
|
||||||
fn(packet.payload);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
client.addPacketListener(onPacket);
|
|
||||||
return () => {
|
return () => {
|
||||||
client.removePacketListener(onPacket);
|
client.removePacketListener(type, fn);
|
||||||
};
|
};
|
||||||
}, dependencies);
|
}, dependencies);
|
||||||
}
|
}
|
|
@ -2,20 +2,31 @@ import {CLIENT_LATENCY} from '@/constants.js';
|
||||||
|
|
||||||
export default class Client {
|
export default class Client {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.listeners = [];
|
this.listeners = {};
|
||||||
}
|
}
|
||||||
accept(packet) {
|
accept(packet) {
|
||||||
for (const i in this.listeners) {
|
const listeners = this.listeners[packet.type];
|
||||||
this.listeners[i](packet);
|
if (!listeners) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (const i in listeners) {
|
||||||
|
listeners[i](packet.payload);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
addPacketListener(listener) {
|
addPacketListener(type, listener) {
|
||||||
this.listeners.push(listener);
|
if (!this.listeners[type]) {
|
||||||
|
this.listeners[type] = [];
|
||||||
|
}
|
||||||
|
this.listeners[type].push(listener);
|
||||||
}
|
}
|
||||||
removePacketListener(listener) {
|
removePacketListener(type, listener) {
|
||||||
const index = this.listeners.indexOf(listener);
|
const listeners = this.listeners[type];
|
||||||
|
if (!listeners) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const index = listeners.indexOf(listener);
|
||||||
if (-1 !== index) {
|
if (-1 !== index) {
|
||||||
this.listeners.splice(index, 1);
|
listeners.splice(index, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
send(packet) {
|
send(packet) {
|
||||||
|
|
|
@ -2,20 +2,31 @@ import {SERVER_LATENCY} from '@/constants.js';
|
||||||
|
|
||||||
export default class Server {
|
export default class Server {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.listeners = [];
|
this.listeners = {};
|
||||||
}
|
}
|
||||||
accept(connection, packet) {
|
accept(connection, packet) {
|
||||||
for (const i in this.listeners) {
|
const listeners = this.listeners[packet.type];
|
||||||
this.listeners[i](connection, packet);
|
if (!listeners) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for (const i in listeners) {
|
||||||
|
listeners[i](connection, packet.payload);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
addPacketListener(listener) {
|
addPacketListener(type, listener) {
|
||||||
this.listeners.push(listener);
|
if (!this.listeners[type]) {
|
||||||
|
this.listeners[type] = [];
|
||||||
|
}
|
||||||
|
this.listeners[type].push(listener);
|
||||||
}
|
}
|
||||||
removePacketListener(listener) {
|
removePacketListener(type, listener) {
|
||||||
const index = this.listeners.indexOf(listener);
|
const listeners = this.listeners[type];
|
||||||
|
if (!listeners) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const index = listeners.indexOf(listener);
|
||||||
if (-1 !== index) {
|
if (-1 !== index) {
|
||||||
this.listeners.splice(index, 1);
|
listeners.splice(index, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
send(connection, packet) {
|
send(connection, packet) {
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
import Packet from '@/net/packet.js';
|
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