feat: basic latency simulation

This commit is contained in:
cha0s 2024-05-31 08:33:11 -05:00
parent d6b3ffa308
commit 7b286806d0
3 changed files with 30 additions and 4 deletions

View File

@ -1,8 +1,14 @@
export const CLIENT_LATENCY = 100;
export const RESOLUTION = [ export const RESOLUTION = [
800, 800,
450, 450,
]; ];
export const SERVER_LATENCY = 100;
export const TPS = 60;
export const ACTION_MAP = { export const ACTION_MAP = {
w: 'moveUp', w: 'moveUp',
d: 'moveRight', d: 'moveRight',

View File

@ -1,3 +1,5 @@
import {CLIENT_LATENCY} from '@/constants.js';
import Packet from '../packet/packet.js'; import Packet from '../packet/packet.js';
export default class Client { export default class Client {
@ -20,6 +22,14 @@ export default class Client {
} }
} }
send(packet) { send(packet) {
this.transmit(Packet.transmit(packet)); if (CLIENT_LATENCY > 0) {
setTimeout(() => {
this.transmit(Packet.transmit(packet));
}, CLIENT_LATENCY);
}
else {
this.transmit(Packet.transmit(packet));
}
} }
} }

View File

@ -1,10 +1,13 @@
import {MOVE_MAP} from '@/constants.js'; import {
MOVE_MAP,
SERVER_LATENCY,
TPS,
} from '@/constants.js';
import Cell from '@/cell.js'; import Cell from '@/cell.js';
import {System} from '@/ecs/index.js'; import {System} from '@/ecs/index.js';
import Packet from "../packet/packet.js"; import Packet from "../packet/packet.js";
const SPEED = 100; const SPEED = 100;
const TPS = 60;
await import ('@/isomorphinit.js'); await import ('@/isomorphinit.js');
@ -82,7 +85,14 @@ export default class Server {
} }
send(connection, packet) { send(connection, packet) {
this.transmit(connection, Packet.transmit(packet)); if (SERVER_LATENCY > 0) {
setTimeout(() => {
this.transmit(connection, Packet.transmit(packet));
}, SERVER_LATENCY);
}
else {
this.transmit(connection, Packet.transmit(packet));
}
} }
start() { start() {