Compare commits
No commits in common. "a872d9ae5f540cf1835b14f5cb988980cc00fd51" and "69573657230df4b261f6165b62747b22af2b8b3c" have entirely different histories.
a872d9ae5f
...
6957365723
|
@ -62,6 +62,6 @@ export default async function createPlayer(id) {
|
|||
activeSlot: 0,
|
||||
},
|
||||
};
|
||||
return player;
|
||||
return (new TextEncoder()).encode(JSON.stringify(player));
|
||||
}
|
||||
|
|
@ -153,7 +153,7 @@ export default class Component {
|
|||
return Component.constructor.filterDefaults(this);
|
||||
}
|
||||
toJSON() {
|
||||
return Component.constructor.filterDefaults(this);
|
||||
return this.toNet();
|
||||
}
|
||||
};
|
||||
const properties = {};
|
||||
|
|
|
@ -1,19 +1,10 @@
|
|||
import {Encoder, Decoder} from '@msgpack/msgpack';
|
||||
import {LRUCache} from 'lru-cache';
|
||||
|
||||
import Script from '@/util/script.js';
|
||||
|
||||
import EntityFactory from './entity-factory.js';
|
||||
|
||||
const cache = new LRUCache({
|
||||
max: 128,
|
||||
});
|
||||
import {Encoder, Decoder} from '@msgpack/msgpack';
|
||||
|
||||
const decoder = new Decoder();
|
||||
const encoder = new Encoder();
|
||||
|
||||
const textDecoder = new TextDecoder();
|
||||
|
||||
export default class Ecs {
|
||||
|
||||
$$caret = 1;
|
||||
|
@ -22,8 +13,6 @@ export default class Ecs {
|
|||
|
||||
deferredChanges = {}
|
||||
|
||||
destroying = new Set();
|
||||
|
||||
diff = {};
|
||||
|
||||
Systems = {};
|
||||
|
@ -208,10 +197,6 @@ export default class Ecs {
|
|||
}
|
||||
|
||||
destroy(entityId) {
|
||||
this.destroying.add(entityId);
|
||||
}
|
||||
|
||||
destroyImmediately(entityId) {
|
||||
this.destroyMany([entityId]);
|
||||
}
|
||||
|
||||
|
@ -323,48 +308,6 @@ export default class Ecs {
|
|||
}
|
||||
}
|
||||
|
||||
async readJson(uri) {
|
||||
const key = ['$$json', uri].join(':');
|
||||
if (!cache.has(key)) {
|
||||
let promise, resolve, reject;
|
||||
promise = new Promise((res, rej) => {
|
||||
resolve = res;
|
||||
reject = rej;
|
||||
});
|
||||
cache.set(key, promise);
|
||||
this.readAsset(uri)
|
||||
.then((chars) => {
|
||||
resolve(
|
||||
chars.byteLength > 0
|
||||
? JSON.parse(textDecoder.decode(chars))
|
||||
: {},
|
||||
);
|
||||
})
|
||||
.catch(reject);
|
||||
}
|
||||
return cache.get(key);
|
||||
}
|
||||
|
||||
async readScript(uriOrCode, context = {}) {
|
||||
if (!uriOrCode) {
|
||||
return undefined;
|
||||
}
|
||||
let code = '';
|
||||
if (!uriOrCode.startsWith('/')) {
|
||||
code = uriOrCode;
|
||||
}
|
||||
else {
|
||||
const buffer = await this.readAsset(uriOrCode);
|
||||
if (buffer.byteLength > 0) {
|
||||
code = textDecoder.decode(buffer);
|
||||
}
|
||||
}
|
||||
if (!code) {
|
||||
return undefined;
|
||||
}
|
||||
return Script.fromCode(code, context);
|
||||
}
|
||||
|
||||
rebuild(entityId, componentNames) {
|
||||
let Class;
|
||||
if (componentNames) {
|
||||
|
@ -433,24 +376,31 @@ export default class Ecs {
|
|||
}
|
||||
|
||||
tick(elapsed) {
|
||||
const destroying = new Set();
|
||||
for (const systemName in this.Systems) {
|
||||
const System = this.Systems[systemName];
|
||||
if (!System.active) {
|
||||
continue;
|
||||
}
|
||||
if (!System.frequency) {
|
||||
System.tick(elapsed);
|
||||
continue;
|
||||
}
|
||||
System.elapsed += elapsed;
|
||||
while (System.elapsed >= System.frequency) {
|
||||
System.tick(System.frequency);
|
||||
System.elapsed -= System.frequency;
|
||||
if (System.active) {
|
||||
if (System.frequency) {
|
||||
System.elapsed += elapsed;
|
||||
if (System.elapsed < System.frequency) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
while (!System.frequency || System.elapsed >= System.frequency) {
|
||||
System.tick(System.frequency ? System.elapsed : elapsed);
|
||||
for (let j = 0; j < System.destroying.length; j++) {
|
||||
destroying.add(System.destroying[j]);
|
||||
}
|
||||
System.tickDestruction();
|
||||
if (!System.frequency) {
|
||||
break;
|
||||
}
|
||||
System.elapsed -= System.frequency;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (this.destroying.size > 0) {
|
||||
this.destroyMany(this.destroying);
|
||||
this.destroying.clear();
|
||||
if (destroying.size > 0) {
|
||||
this.destroyMany(destroying.values());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -114,7 +114,7 @@ test('destroys entities', async () => {
|
|||
expect(ecs.get(entity))
|
||||
.to.be.undefined;
|
||||
expect(() => {
|
||||
ecs.destroyImmediately(entity);
|
||||
ecs.destroy(entity);
|
||||
})
|
||||
.to.throw();
|
||||
});
|
||||
|
@ -170,14 +170,50 @@ test('ticks systems', async () => {
|
|||
.to.deep.equal(JSON.stringify({y: 128 + 30}));
|
||||
});
|
||||
|
||||
test('creates many entities when ticking systems', () => {
|
||||
const ecs = new Ecs({
|
||||
Systems: {
|
||||
Spawn: class extends System {
|
||||
tick() {
|
||||
this.createManyEntities(Array.from({length: 5}).map(() => []));
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
ecs.system('Spawn').active = true;
|
||||
ecs.create();
|
||||
expect(ecs.get(5))
|
||||
.to.be.undefined;
|
||||
ecs.tick(1);
|
||||
expect(ecs.get(5))
|
||||
.to.not.be.undefined;
|
||||
});
|
||||
|
||||
test('creates entities when ticking systems', () => {
|
||||
const ecs = new Ecs({
|
||||
Systems: {
|
||||
Spawn: class extends System {
|
||||
tick() {
|
||||
this.createEntity();
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
ecs.system('Spawn').active = true;
|
||||
ecs.create();
|
||||
expect(ecs.get(2))
|
||||
.to.be.undefined;
|
||||
ecs.tick(1);
|
||||
expect(ecs.get(2))
|
||||
.to.not.be.undefined;
|
||||
});
|
||||
|
||||
test('schedules entities to be deleted when ticking systems', () => {
|
||||
const ecs = new Ecs({
|
||||
Systems: {
|
||||
Despawn: class extends System {
|
||||
tick() {
|
||||
this.ecs.destroy(1);
|
||||
expect(ecs.get(1))
|
||||
.to.not.be.undefined;
|
||||
this.destroyEntity(1);
|
||||
}
|
||||
},
|
||||
},
|
||||
|
@ -189,6 +225,50 @@ test('schedules entities to be deleted when ticking systems', () => {
|
|||
.to.be.undefined;
|
||||
});
|
||||
|
||||
test('adds components to and remove components from entities when ticking systems', async () => {
|
||||
let promise;
|
||||
const ecs = new Ecs({
|
||||
Components: {Foo: wrapProperties('Foo', {bar: {type: 'uint8'}})},
|
||||
Systems: {
|
||||
AddComponent: class extends System {
|
||||
static queries() {
|
||||
return {
|
||||
default: ['Foo'],
|
||||
};
|
||||
}
|
||||
tick() {
|
||||
promise = this.insertComponents(1, {Foo: {}});
|
||||
}
|
||||
},
|
||||
RemoveComponent: class extends System {
|
||||
static queries() {
|
||||
return {
|
||||
default: ['Foo'],
|
||||
};
|
||||
}
|
||||
tick() {
|
||||
this.removeComponents(1, ['Foo']);
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
ecs.system('AddComponent').active = true;
|
||||
ecs.create();
|
||||
ecs.tick(1);
|
||||
await promise;
|
||||
expect(Array.from(ecs.system('AddComponent').select('default')).length)
|
||||
.to.equal(1);
|
||||
expect(ecs.get(1).Foo)
|
||||
.to.not.be.undefined;
|
||||
ecs.system('AddComponent').active = false;
|
||||
ecs.system('RemoveComponent').active = true;
|
||||
ecs.tick(1);
|
||||
expect(Array.from(ecs.system('RemoveComponent').select('default')).length)
|
||||
.to.equal(0);
|
||||
expect(ecs.get(1).Foo)
|
||||
.to.be.undefined;
|
||||
});
|
||||
|
||||
test('generates diffs for entity creation', async () => {
|
||||
const ecs = new Ecs();
|
||||
let entity;
|
||||
|
@ -267,7 +347,7 @@ test('generates diffs for deletions', async () => {
|
|||
let entity;
|
||||
entity = await ecs.create();
|
||||
ecs.setClean();
|
||||
ecs.destroyImmediately(entity);
|
||||
ecs.destroy(entity);
|
||||
expect(ecs.diff)
|
||||
.to.deep.equal({[entity]: false});
|
||||
});
|
||||
|
|
|
@ -6,6 +6,8 @@ export default class System {
|
|||
|
||||
active = false;
|
||||
|
||||
destroying = [];
|
||||
|
||||
ecs;
|
||||
|
||||
elapsed = 0;
|
||||
|
@ -23,12 +25,38 @@ export default class System {
|
|||
this.reindex(ecs.entities);
|
||||
}
|
||||
|
||||
createEntity(components) {
|
||||
return this.ecs.create(components);
|
||||
}
|
||||
|
||||
createManyEntities(componentsList) {
|
||||
return this.ecs.createMany(componentsList);
|
||||
}
|
||||
|
||||
deindex(entityIds) {
|
||||
for (const i in this.queries) {
|
||||
this.queries[i].deindex(entityIds);
|
||||
}
|
||||
}
|
||||
|
||||
destroyEntity(entityId) {
|
||||
this.destroyManyEntities([entityId]);
|
||||
}
|
||||
|
||||
destroyManyEntities(entityIds) {
|
||||
for (let i = 0; i < entityIds.length; i++) {
|
||||
this.destroying.push(entityIds[i]);
|
||||
}
|
||||
}
|
||||
|
||||
async insertComponents(entityId, components) {
|
||||
return this.ecs.insert(entityId, components);
|
||||
}
|
||||
|
||||
async insertManyComponents(components) {
|
||||
return this.ecs.insertMany(components);
|
||||
}
|
||||
|
||||
static get priority() {
|
||||
return {
|
||||
phase: 'normal',
|
||||
|
@ -45,6 +73,14 @@ export default class System {
|
|||
}
|
||||
}
|
||||
|
||||
removeComponents(entityId, components) {
|
||||
this.ecs.remove(entityId, components);
|
||||
}
|
||||
|
||||
removeManyComponents(entityIds) {
|
||||
this.ecs.removeMany(entityIds);
|
||||
}
|
||||
|
||||
select(query) {
|
||||
return this.queries[query].select();
|
||||
}
|
||||
|
@ -81,6 +117,13 @@ export default class System {
|
|||
);
|
||||
}
|
||||
|
||||
tickDestruction() {
|
||||
if (this.destroying.length > 0) {
|
||||
this.deindex(this.destroying);
|
||||
}
|
||||
this.destroying = [];
|
||||
}
|
||||
|
||||
tick() {}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
import {LRUCache} from 'lru-cache';
|
||||
|
||||
import Ecs from '@/ecs/ecs.js';
|
||||
import {decode, encode} from '@/net/packets/index.js';
|
||||
import {decode, encode} from '@/packets/index.js';
|
||||
import {
|
||||
CHUNK_SIZE,
|
||||
RESOLUTION,
|
||||
TPS,
|
||||
} from '@/util/constants.js';
|
||||
import Script from '@/util/script.js';
|
||||
|
||||
import createEcs from './create/ecs.js';
|
||||
import createForest from './create/forest.js';
|
||||
|
@ -14,12 +13,12 @@ import createHomestead from './create/homestead.js';
|
|||
import createHouse from './create/house.js';
|
||||
import createPlayer from './create/player.js';
|
||||
|
||||
import {LRUCache} from 'lru-cache';
|
||||
|
||||
const cache = new LRUCache({
|
||||
max: 128,
|
||||
});
|
||||
|
||||
const textEncoder = new TextEncoder();
|
||||
|
||||
export default class Engine {
|
||||
|
||||
connectedPlayers = new Map();
|
||||
|
@ -60,6 +59,41 @@ export default class Engine {
|
|||
}
|
||||
return cache.get(uri);
|
||||
}
|
||||
async readJson(uri) {
|
||||
const key = ['$$json', uri].join(':');
|
||||
if (!cache.has(key)) {
|
||||
let promise, resolve, reject;
|
||||
promise = new Promise((res, rej) => {
|
||||
resolve = res;
|
||||
reject = rej;
|
||||
});
|
||||
cache.set(key, promise);
|
||||
this.readAsset(uri)
|
||||
.then((chars) => {
|
||||
resolve(chars.byteLength > 0 ? JSON.parse((new TextDecoder()).decode(chars)) : {});
|
||||
})
|
||||
.catch(reject);
|
||||
}
|
||||
return cache.get(key);
|
||||
}
|
||||
async readScript(uriOrCode, context) {
|
||||
if (!uriOrCode) {
|
||||
return undefined;
|
||||
}
|
||||
let code = '';
|
||||
if (!uriOrCode.startsWith('/')) {
|
||||
code = uriOrCode;
|
||||
}
|
||||
else {
|
||||
const buffer = await this.readAsset(uriOrCode);
|
||||
if (buffer.byteLength > 0) {
|
||||
code = (new TextDecoder()).decode(buffer);
|
||||
}
|
||||
}
|
||||
if (code) {
|
||||
return Script.fromCode(code, context);
|
||||
}
|
||||
}
|
||||
async switchEcs(entity, path, updates) {
|
||||
for (const [connection, connectedPlayer] of engine.connectedPlayers) {
|
||||
if (entity !== connectedPlayer.entity) {
|
||||
|
@ -246,9 +280,9 @@ export default class Engine {
|
|||
}
|
||||
|
||||
async loadPlayer(id) {
|
||||
let player;
|
||||
let buffer;
|
||||
try {
|
||||
player = await this.server.readJson(['players', `${id}`].join('/'))
|
||||
buffer = await this.server.readData(['players', `${id}`].join('/'))
|
||||
}
|
||||
catch (error) {
|
||||
if ('ENOENT' !== error.code) {
|
||||
|
@ -275,10 +309,13 @@ export default class Engine {
|
|||
['forests', `${id}`].join('/'),
|
||||
forest,
|
||||
);
|
||||
player = await createPlayer(id);
|
||||
await this.savePlayer(id, player);
|
||||
buffer = await createPlayer(id);
|
||||
await this.server.writeData(
|
||||
['players', `${id}`].join('/'),
|
||||
buffer,
|
||||
);
|
||||
}
|
||||
return player;
|
||||
return JSON.parse((new TextDecoder()).decode(buffer));
|
||||
}
|
||||
|
||||
async saveEcs(path, ecs) {
|
||||
|
@ -295,7 +332,8 @@ export default class Engine {
|
|||
}
|
||||
|
||||
async savePlayer(id, entity) {
|
||||
const buffer = textEncoder.encode(JSON.stringify(entity));
|
||||
const encoder = new TextEncoder();
|
||||
const buffer = encoder.encode(JSON.stringify(entity.toJSON()));
|
||||
await this.server.writeData(['players', `${id}`].join('/'), buffer);
|
||||
}
|
||||
|
||||
|
@ -322,7 +360,6 @@ export default class Engine {
|
|||
stop() {
|
||||
clearTimeout(this.handle);
|
||||
this.handle = undefined;
|
||||
this.tick(0);
|
||||
}
|
||||
|
||||
tick(elapsed) {
|
|
@ -17,10 +17,10 @@ export async function websocket(server, viteDevServer) {
|
|||
if (viteDevServer) {
|
||||
const {createViteRuntime} = await import('vite');
|
||||
const runtime = await createViteRuntime(viteDevServer);
|
||||
(await runtime.executeEntrypoint('/app/server/websocket.js')).default(server);
|
||||
(await runtime.executeEntrypoint('/app/websocket.js')).default(server);
|
||||
}
|
||||
else {
|
||||
(await import('./server/websocket.js')).default(server);
|
||||
(await import('./websocket.js')).default(server);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,27 +0,0 @@
|
|||
import {CLIENT_LATENCY} from '@/util/constants.js';
|
||||
import EventEmitter from '@/util/event-emitter.js';
|
||||
|
||||
export default class Client {
|
||||
constructor() {
|
||||
this.emitter = new EventEmitter();
|
||||
}
|
||||
accept(packet) {
|
||||
this.emitter.invoke(packet.type, packet.payload);
|
||||
}
|
||||
addPacketListener(type, listener) {
|
||||
this.emitter.addListener(type, listener);
|
||||
}
|
||||
removePacketListener(type, listener) {
|
||||
this.emitter.removeListener(type, listener);
|
||||
}
|
||||
send(packet) {
|
||||
if (CLIENT_LATENCY > 0) {
|
||||
setTimeout(() => {
|
||||
this.transmit(packet);
|
||||
}, CLIENT_LATENCY);
|
||||
}
|
||||
else {
|
||||
this.transmit(packet);
|
||||
}
|
||||
}
|
||||
}
|
42
app/net/client/client.js
Normal file
42
app/net/client/client.js
Normal file
|
@ -0,0 +1,42 @@
|
|||
import {CLIENT_LATENCY} from '@/util/constants.js';
|
||||
|
||||
export default class Client {
|
||||
constructor() {
|
||||
this.listeners = {};
|
||||
}
|
||||
accept(packet) {
|
||||
const listeners = this.listeners[packet.type];
|
||||
if (!listeners) {
|
||||
return;
|
||||
}
|
||||
for (const i in listeners) {
|
||||
listeners[i](packet.payload);
|
||||
}
|
||||
}
|
||||
addPacketListener(type, listener) {
|
||||
if (!this.listeners[type]) {
|
||||
this.listeners[type] = [];
|
||||
}
|
||||
this.listeners[type].push(listener);
|
||||
}
|
||||
removePacketListener(type, listener) {
|
||||
const listeners = this.listeners[type];
|
||||
if (!listeners) {
|
||||
return;
|
||||
}
|
||||
const index = listeners.indexOf(listener);
|
||||
if (-1 !== index) {
|
||||
listeners.splice(index, 1);
|
||||
}
|
||||
}
|
||||
send(packet) {
|
||||
if (CLIENT_LATENCY > 0) {
|
||||
setTimeout(() => {
|
||||
this.transmit(packet);
|
||||
}, CLIENT_LATENCY);
|
||||
}
|
||||
else {
|
||||
this.transmit(packet);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
import Client from '@/net/client.js';
|
||||
import Client from './client.js';
|
||||
|
||||
export default class LocalClient extends Client {
|
||||
async connect() {
|
|
@ -1,4 +1,4 @@
|
|||
import {encode} from '@/net/packets/index.js';
|
||||
import {encode} from '@/packets/index.js';
|
||||
|
||||
let connected = false;
|
||||
let socket;
|
|
@ -1,7 +1,8 @@
|
|||
import Client from '@/net/client.js';
|
||||
import {encode} from '@/net/packets/index.js';
|
||||
import {encode} from '@/packets/index.js';
|
||||
import {CLIENT_PREDICTION} from '@/util/constants.js';
|
||||
|
||||
import Client from './client.js';
|
||||
|
||||
export default class RemoteClient extends Client {
|
||||
constructor() {
|
||||
super();
|
||||
|
@ -15,7 +16,7 @@ export default class RemoteClient extends Client {
|
|||
async connect(host) {
|
||||
if (CLIENT_PREDICTION) {
|
||||
this.worker = new Worker(
|
||||
new URL('./prediction.js', import.meta.url),
|
||||
new URL('../client/prediction.js', import.meta.url),
|
||||
{type: 'module'},
|
||||
);
|
||||
this.worker.postMessage({host});
|
|
@ -1,32 +0,0 @@
|
|||
import {SERVER_LATENCY} from '@/util/constants.js';
|
||||
import EventEmitter from '@/util/event-emitter.js';
|
||||
|
||||
const textDecoder = new TextDecoder();
|
||||
|
||||
export default class Server {
|
||||
constructor() {
|
||||
this.emitter = new EventEmitter();
|
||||
}
|
||||
accept(connection, packet) {
|
||||
this.emitter.invoke(packet.type, connection, packet.payload);
|
||||
}
|
||||
addPacketListener(type, listener) {
|
||||
this.emitter.addListener(type, listener);
|
||||
}
|
||||
async readJson(path) {
|
||||
return JSON.parse(textDecoder.decode(await this.readData(path)));
|
||||
}
|
||||
removePacketListener(type, listener) {
|
||||
this.emitter.removeListener(type, listener);
|
||||
}
|
||||
send(connection, packet) {
|
||||
if (SERVER_LATENCY > 0) {
|
||||
setTimeout(() => {
|
||||
this.transmit(connection, packet);
|
||||
}, SERVER_LATENCY);
|
||||
}
|
||||
else {
|
||||
this.transmit(connection, packet);
|
||||
}
|
||||
}
|
||||
}
|
42
app/net/server/server.js
Normal file
42
app/net/server/server.js
Normal file
|
@ -0,0 +1,42 @@
|
|||
import {SERVER_LATENCY} from '@/util/constants.js';
|
||||
|
||||
export default class Server {
|
||||
constructor() {
|
||||
this.listeners = {};
|
||||
}
|
||||
accept(connection, packet) {
|
||||
const listeners = this.listeners[packet.type];
|
||||
if (!listeners) {
|
||||
return;
|
||||
}
|
||||
for (const i in listeners) {
|
||||
listeners[i](connection, packet.payload);
|
||||
}
|
||||
}
|
||||
addPacketListener(type, listener) {
|
||||
if (!this.listeners[type]) {
|
||||
this.listeners[type] = [];
|
||||
}
|
||||
this.listeners[type].push(listener);
|
||||
}
|
||||
removePacketListener(type, listener) {
|
||||
const listeners = this.listeners[type];
|
||||
if (!listeners) {
|
||||
return;
|
||||
}
|
||||
const index = listeners.indexOf(listener);
|
||||
if (-1 !== index) {
|
||||
listeners.splice(index, 1);
|
||||
}
|
||||
}
|
||||
send(connection, packet) {
|
||||
if (SERVER_LATENCY > 0) {
|
||||
setTimeout(() => {
|
||||
this.transmit(connection, packet);
|
||||
}, SERVER_LATENCY);
|
||||
}
|
||||
else {
|
||||
this.transmit(connection, packet);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,14 +1,14 @@
|
|||
import {del, get, set} from 'idb-keyval';
|
||||
|
||||
import {encode} from '@/net/packets/index.js';
|
||||
import Server from '@/net/server.js';
|
||||
import {encode} from '@/packets/index.js';
|
||||
|
||||
import createEcs from './create/ecs.js';
|
||||
import './create/forest.js';
|
||||
import './create/homestead.js';
|
||||
import './create/player.js';
|
||||
import createEcs from '../../create/ecs.js';
|
||||
import '../../create/forest.js';
|
||||
import '../../create/homestead.js';
|
||||
import '../../create/player.js';
|
||||
|
||||
import Engine from './engine.js';
|
||||
import Engine from '../../engine.js';
|
||||
import Server from './server.js';
|
||||
|
||||
class WorkerServer extends Server {
|
||||
constructor() {
|
||||
|
@ -45,8 +45,8 @@ const engine = new Engine(WorkerServer);
|
|||
|
||||
onmessage = async (event) => {
|
||||
if (0 === event.data) {
|
||||
await engine.disconnectPlayer(0);
|
||||
engine.stop();
|
||||
await engine.disconnectPlayer(0);
|
||||
await engine.saveEcses();
|
||||
postMessage(0);
|
||||
return;
|
||||
|
@ -78,12 +78,13 @@ if (import.meta.hot) {
|
|||
await engine.saveEcses();
|
||||
beforeResolver.resolve();
|
||||
});
|
||||
import.meta.hot.accept('./engine.js');
|
||||
import.meta.hot.accept('./create/player.js', async ({default: createPlayer}) => {
|
||||
import.meta.hot.accept('../../engine.js');
|
||||
import.meta.hot.accept('../../create-player.js', async ({default: createPlayer}) => {
|
||||
const resolver = createResolver();
|
||||
resolvers.push(resolver);
|
||||
await beforeResolver;
|
||||
const oldPlayer = await engine.server.readJson('players/0');
|
||||
const oldBuffer = await engine.server.readData('players/0');
|
||||
const oldPlayer = JSON.parse((new TextDecoder()).decode(oldBuffer));
|
||||
const buffer = await createPlayer(0);
|
||||
const player = JSON.parse((new TextDecoder()).decode(buffer));
|
||||
// Less jarring
|
||||
|
@ -93,7 +94,7 @@ if (import.meta.hot) {
|
|||
await engine.server.writeData('players/0', (new TextEncoder()).encode(JSON.stringify(player)));
|
||||
resolver.resolve();
|
||||
});
|
||||
import.meta.hot.accept('./create/forest.js', async ({default: createForest}) => {
|
||||
import.meta.hot.accept('../../create-forest.js', async ({default: createForest}) => {
|
||||
const resolver = createResolver();
|
||||
resolvers.push(resolver);
|
||||
await beforeResolver;
|
||||
|
@ -106,7 +107,7 @@ if (import.meta.hot) {
|
|||
await engine.saveEcs('forests/0', forest);
|
||||
resolver.resolve();
|
||||
});
|
||||
import.meta.hot.accept('./create/homestead.js', async ({default: createHomestead}) => {
|
||||
import.meta.hot.accept('../../create-homestead.js', async ({default: createHomestead}) => {
|
||||
const resolver = createResolver();
|
||||
resolvers.push(resolver);
|
||||
await beforeResolver;
|
|
@ -1,6 +1,7 @@
|
|||
import {LRUCache} from 'lru-cache';
|
||||
|
||||
import Ecs from '@/ecs/ecs.js';
|
||||
import Script from '@/util/script.js';
|
||||
|
||||
import {LRUCache} from 'lru-cache';
|
||||
|
||||
const cache = new LRUCache({
|
||||
max: 128,
|
||||
|
@ -23,4 +24,39 @@ export default class ClientEcs extends Ecs {
|
|||
}
|
||||
return cache.get(uri);
|
||||
}
|
||||
async readJson(uri) {
|
||||
const key = ['$$json', uri].join(':');
|
||||
if (!cache.has(key)) {
|
||||
let promise, resolve, reject;
|
||||
promise = new Promise((res, rej) => {
|
||||
resolve = res;
|
||||
reject = rej;
|
||||
});
|
||||
cache.set(key, promise);
|
||||
this.readAsset(uri)
|
||||
.then((chars) => {
|
||||
resolve(chars.byteLength > 0 ? JSON.parse((new TextDecoder()).decode(chars)) : {});
|
||||
})
|
||||
.catch(reject);
|
||||
}
|
||||
return cache.get(key);
|
||||
}
|
||||
async readScript(uriOrCode, context = {}) {
|
||||
if (!uriOrCode) {
|
||||
return undefined;
|
||||
}
|
||||
let code = '';
|
||||
if (!uriOrCode.startsWith('/')) {
|
||||
code = uriOrCode;
|
||||
}
|
||||
else {
|
||||
const buffer = await this.readAsset(uriOrCode);
|
||||
if (buffer.byteLength > 0) {
|
||||
code = (new TextDecoder()).decode(buffer);
|
||||
}
|
||||
}
|
||||
if (code) {
|
||||
return Script.fromCode(code, context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,6 @@ import {useDebug} from '@/react/context/debug.js';
|
|||
import {useEcs, useEcsTick} from '@/react/context/ecs.js';
|
||||
import {useMainEntity} from '@/react/context/main-entity.js';
|
||||
import {RESOLUTION} from '@/util/constants.js';
|
||||
import EventEmitter from '@/util/event-emitter.js';
|
||||
|
||||
import addKeyListener from './add-key-listener.js';
|
||||
import ClientEcs from './client-ecs.js';
|
||||
|
@ -22,7 +21,31 @@ function emptySlots() {
|
|||
return Array(10).fill(undefined);
|
||||
}
|
||||
|
||||
const devEventsChannel = new EventEmitter();
|
||||
const devEventsChannel = {
|
||||
$$listeners: {},
|
||||
addListener(type, listener) {
|
||||
if (!this.$$listeners[type]) {
|
||||
this.$$listeners[type] = new Set();
|
||||
}
|
||||
this.$$listeners[type].add(listener);
|
||||
},
|
||||
invoke(type, payload) {
|
||||
const listeners = this.$$listeners[type];
|
||||
if (!listeners) {
|
||||
return;
|
||||
}
|
||||
for (const listener of listeners) {
|
||||
listener(payload);
|
||||
}
|
||||
},
|
||||
removeListener(type, listener) {
|
||||
const listeners = this.$$listeners[type];
|
||||
if (!listeners) {
|
||||
return;
|
||||
}
|
||||
listeners.delete(listener);
|
||||
},
|
||||
};
|
||||
|
||||
function Ui({disconnected}) {
|
||||
// Key input.
|
||||
|
@ -238,7 +261,9 @@ function Ui({disconnected}) {
|
|||
return;
|
||||
}
|
||||
await ecs.apply(payload.ecs);
|
||||
client.emitter.invoke(':Ecs', payload.ecs);
|
||||
for (const listener of client.listeners[':Ecs'] ?? []) {
|
||||
listener(payload.ecs);
|
||||
}
|
||||
}, [ecs]);
|
||||
useEcsTick((payload) => {
|
||||
let localMainEntity = mainEntity;
|
||||
|
|
|
@ -9,8 +9,8 @@ import DebugContext from '@/react/context/debug.js';
|
|||
import EcsContext from '@/react/context/ecs.js';
|
||||
import MainEntityContext from '@/react/context/main-entity.js';
|
||||
import RadiansContext from '@/react/context/radians.js';
|
||||
import {juggleSession} from '@/server/session.server.js';
|
||||
import {TAU} from '@/util/math.js';
|
||||
import {juggleSession} from '@/util/session.server';
|
||||
|
||||
export async function loader({request}) {
|
||||
await juggleSession(request);
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import {useEffect, useState} from 'react';
|
||||
import {Outlet, useParams} from 'react-router-dom';
|
||||
|
||||
import {decode, encode} from '@/net/packets/index.js';
|
||||
import {decode, encode} from '@/packets/index.js';
|
||||
|
||||
import styles from './play.module.css';
|
||||
|
||||
|
@ -14,10 +14,10 @@ export default function Play() {
|
|||
let Client;
|
||||
switch (type) {
|
||||
case 'local':
|
||||
({default: Client} = await import('@/client/local.js'));
|
||||
({default: Client} = await import('@/net/client/local.js'));
|
||||
break;
|
||||
case 'remote':
|
||||
({default: Client} = await import('@/client/remote.js'));
|
||||
({default: Client} = await import('@/net/client/remote.js'));
|
||||
break;
|
||||
}
|
||||
class SilphiusClient extends Client {
|
||||
|
|
|
@ -1,30 +0,0 @@
|
|||
import fastCall from './fast-call.js';
|
||||
|
||||
export default class EventEmitter {
|
||||
$$listeners = {};
|
||||
addListener(type, listener) {
|
||||
if (!this.$$listeners[type]) {
|
||||
this.$$listeners[type] = new Set();
|
||||
}
|
||||
this.$$listeners[type].add(listener);
|
||||
}
|
||||
invoke(type, ...args) {
|
||||
const listeners = this.$$listeners[type];
|
||||
if (!listeners) {
|
||||
return;
|
||||
}
|
||||
for (const listener of listeners) {
|
||||
fastCall(listener, undefined, args);
|
||||
}
|
||||
}
|
||||
removeListener(type, listener) {
|
||||
const listeners = this.$$listeners[type];
|
||||
if (!listeners) {
|
||||
return;
|
||||
}
|
||||
listeners.delete(listener);
|
||||
if (0 === listeners.size) {
|
||||
delete this.$$listeners[type];
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
export default function fastCall(fn, holder, args) {
|
||||
export default function(fn, holder, args) {
|
||||
if (holder) {
|
||||
const {name} = fn;
|
||||
if ('object' === typeof holder && name in holder && holder[name] === fn) {
|
||||
|
|
|
@ -3,8 +3,8 @@ import {dirname, join} from 'node:path';
|
|||
|
||||
import {WebSocketServer} from 'ws';
|
||||
|
||||
import Server from '@/net/server.js';
|
||||
import {getSession} from '@/server/session.server.js';
|
||||
import Server from '@/net/server/server.js';
|
||||
import {getSession} from '@/util/session.server.js';
|
||||
|
||||
import Engine from './engine.js';
|
||||
|
||||
|
@ -48,7 +48,7 @@ class SocketServer extends Server {
|
|||
await mkdir(path, {recursive: true});
|
||||
}
|
||||
static qualify(path) {
|
||||
return join(import.meta.dirname, '..', '..', 'data', 'remote', 'UNIVERSE', path);
|
||||
return join(import.meta.dirname, '..', 'data', 'remote', 'UNIVERSE', path);
|
||||
}
|
||||
async readAsset(path) {
|
||||
const url = new URL(path, 'https://localhost:3000')
|
Loading…
Reference in New Issue
Block a user