2024-05-19 04:37:53 +00:00
|
|
|
console.log("loading engine");
|
|
|
|
|
|
|
|
const initEvent = () => {
|
|
|
|
let subscribers = {};
|
|
|
|
|
|
|
|
return (message, ...messageArgs) => {
|
|
|
|
switch (message) {
|
|
|
|
case "addSubscriber":
|
|
|
|
let subscriberName = messageArgs[0];
|
|
|
|
let subscriberFunc = messageArgs[1];
|
|
|
|
subscribers[subscriberName] = subscriberFunc;
|
|
|
|
break;
|
|
|
|
case "raise":
|
|
|
|
let delta = messageArgs[0];
|
|
|
|
let eventData = messageArgs[1];
|
|
|
|
Object.entries(subscribers).forEach(([id, subscriber]) => {
|
|
|
|
subscriber(delta, eventData);
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const engine = (() => {
|
|
|
|
let queue = [];
|
|
|
|
let timer;
|
|
|
|
let time = Date.now();
|
|
|
|
let entities = {};
|
2024-05-19 22:07:43 +00:00
|
|
|
let components = {};
|
2024-05-19 04:37:53 +00:00
|
|
|
let events = {};
|
|
|
|
|
|
|
|
const engineTick = 1000 / 60;
|
|
|
|
const tick = () => {
|
|
|
|
let now = Date.now();
|
|
|
|
let delta = now - time;
|
|
|
|
time = now;
|
|
|
|
while (queue.length > 0) {
|
|
|
|
let eventName = queue.shift();
|
|
|
|
events[eventName]("raise", delta);
|
|
|
|
}
|
|
|
|
timer = setTimeout(tick, engineTick);
|
|
|
|
};
|
|
|
|
const halt = () => {
|
|
|
|
console.log("halted");
|
|
|
|
clearTimeout(timer);
|
|
|
|
};
|
|
|
|
return (message, ...messageArgs) => {
|
|
|
|
switch (message) {
|
2024-05-19 22:07:43 +00:00
|
|
|
case "queueEvent": {
|
2024-05-19 04:37:53 +00:00
|
|
|
queue.push(messageArgs[0]);
|
|
|
|
break;
|
2024-05-19 22:07:43 +00:00
|
|
|
}
|
|
|
|
case "events": {
|
2024-05-19 04:37:53 +00:00
|
|
|
return events;
|
2024-05-19 22:07:43 +00:00
|
|
|
}
|
|
|
|
case "entities": {
|
|
|
|
return entities;
|
|
|
|
}
|
|
|
|
case "components": {
|
|
|
|
return components;
|
|
|
|
}
|
|
|
|
case "world": {
|
|
|
|
return {
|
|
|
|
entities,
|
|
|
|
components,
|
|
|
|
events,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
case "registerEntity": {
|
|
|
|
let node = messageArgs[0];
|
|
|
|
entities[node] = [];
|
2024-05-19 04:37:53 +00:00
|
|
|
break;
|
2024-05-19 22:07:43 +00:00
|
|
|
}
|
|
|
|
case "removeEntity": {
|
|
|
|
let node = messageArgs[0];
|
|
|
|
entities[node].forEach(([componentType, component]) => {
|
2024-05-20 19:40:17 +00:00
|
|
|
delete components[componentType].instances[component];
|
2024-05-19 22:07:43 +00:00
|
|
|
});
|
|
|
|
delete entities[node];
|
|
|
|
queue.push((_) => node.remove());
|
2024-05-19 04:37:53 +00:00
|
|
|
break;
|
2024-05-19 22:07:43 +00:00
|
|
|
}
|
|
|
|
case "registerComponentType": {
|
2024-05-20 19:40:17 +00:00
|
|
|
let [componentType, constructor] = messageArgs;
|
|
|
|
components[componentType] = {
|
|
|
|
constructor,
|
|
|
|
instances: {}
|
|
|
|
};
|
2024-05-19 04:37:53 +00:00
|
|
|
break;
|
2024-05-19 22:07:43 +00:00
|
|
|
}
|
2024-05-20 19:40:17 +00:00
|
|
|
case "getComponentContructor": {
|
|
|
|
let [componentType] = messageArgs;
|
|
|
|
return components [componentType].constructor;
|
|
|
|
}
|
2024-05-19 22:07:43 +00:00
|
|
|
case "addComponent": {
|
|
|
|
let [entity, componentType, component] = messageArgs;
|
|
|
|
entities[entity].push([componentType, component]);
|
2024-05-20 19:40:17 +00:00
|
|
|
components[componentType].instances[entity] = component;
|
2024-05-19 22:07:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "registerEvent": {
|
|
|
|
let eventName = messageArgs[0];
|
2024-05-19 04:37:53 +00:00
|
|
|
let newEvent = initEvent();
|
2024-05-19 22:07:43 +00:00
|
|
|
events[eventName] = newEvent;
|
2024-05-19 04:37:53 +00:00
|
|
|
break;
|
2024-05-19 22:07:43 +00:00
|
|
|
}
|
|
|
|
case "subscribeToEvent": {
|
|
|
|
let [eventName, subscriberName, newSubscriber] = messageArgs;
|
|
|
|
events[eventName]("addSubscriber", subscriberName, newSubscriber);
|
2024-05-19 04:37:53 +00:00
|
|
|
break;
|
2024-05-19 22:07:43 +00:00
|
|
|
}
|
|
|
|
case 'start': {
|
2024-05-19 04:37:53 +00:00
|
|
|
tick();
|
|
|
|
break;
|
2024-05-19 22:07:43 +00:00
|
|
|
}
|
|
|
|
case 'halt': {
|
2024-05-19 04:37:53 +00:00
|
|
|
halt();
|
|
|
|
break;
|
2024-05-19 22:07:43 +00:00
|
|
|
}
|
2024-05-19 04:37:53 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
2024-05-19 22:07:43 +00:00
|
|
|
initTransformSystem(engine);
|
2024-05-19 04:37:53 +00:00
|
|
|
inputSystem(engine);
|
|
|
|
playerSystem(engine);
|
|
|
|
|
|
|
|
engine("start");
|