UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

143 lines (100 loc) 3.39 kB
import { EngineHarness } from "../EngineHarness.js"; import { RemoteController } from "./RemoteController.js"; import { ShadedGeometrySystem } from "../graphics/ecs/mesh-v2/ShadedGeometrySystem.js"; import { initializeGameBinarySerializationRegistry } from "../../../../model/game/GameBinarySerializationRegistry.js"; import { BinaryObjectSerializationAdapter } from "../ecs/storage/binary/object/BinaryObjectSerializationAdapter.js"; import BinaryBufferDeSerializer from "../ecs/storage/BinaryBufferDeSerializer.js"; const harness = new EngineHarness(); class LocalAPI { /** * * @type {Engine|null} */ engine = null; _object_serde = new BinaryObjectSerializationAdapter(); get _registry() { return this.engine.binarySerializationRegistry; } /** * * @return {EntityComponentDataset} * @private */ get _current_dataset() { return this.engine.entityManager.dataset; } /** * Server sent us a new world state * @param {BinaryBuffer} buffer */ async writeCurrentSceneDataset(buffer) { const deSerializer = new BinaryBufferDeSerializer(); const engine = this.engine; deSerializer.registry = this._registry; const ecd = this._current_dataset; ecd.clear(); const task = deSerializer.process(buffer, engine, ecd); const p = task.promise(); engine.executor.run(task); await p; } /** * * @param {BinaryBuffer} buffer * @return {Promise<void>} */ async createEntity(buffer) { // create entity on the current scene with a given ID const entity_id = buffer.readUintVar(); this._current_dataset.createEntitySpecific(entity_id); } async addComponentToEntity(buffer) { const entity_id = buffer.readUintVar(); this._object_serde.registry = this._registry; const component_instance = this._object_serde.deserialize(buffer); this._current_dataset.addComponentToEntity(entity_id, component_instance); } } /** * * @param {RemoteController} controller * @param {Object} api */ function attachLocalAPI(controller, api) { for (const apiKey in api) { controller.addHandler(apiKey, api[apiKey], api); } } /** * * @param {Engine} engine * @return {Promise<void>} */ async function main(engine) { await EngineHarness.buildBasics({ engine }); initializeGameBinarySerializationRegistry(engine.binarySerializationRegistry); const remote = new RemoteController(); const api = new LocalAPI(); api.engine = engine; Object.getOwnPropertyNames(Object.getPrototypeOf(api)) .forEach(name => { if (name.startsWith("_")) { return; } if (name === "constructor") { return; } if (typeof api[name] !== "function") { return; } remote.addHandler(name, api[name], api); }); remote.socket = new WebSocket('ws://localhost:9020'); } harness.initialize({ configuration(config, engine) { config.addSystem(new ShadedGeometrySystem(engine)); } }).then(main);