1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2024-10-27 20:34:29 +00:00
tobspr_shapez.io/src/js/game/game_system_manager.js

138 lines
3.8 KiB
JavaScript
Raw Normal View History

2020-05-09 14:45:23 +00:00
/* typehints:start */
import { GameRoot } from "./root";
/* typehints:end */
import { createLogger } from "../core/logging";
import { BeltSystem } from "./systems/belt";
import { ItemEjectorSystem } from "./systems/item_ejector";
import { MapResourcesSystem } from "./systems/map_resources";
import { MinerSystem } from "./systems/miner";
import { ItemProcessorSystem } from "./systems/item_processor";
import { UndergroundBeltSystem } from "./systems/underground_belt";
import { HubSystem } from "./systems/hub";
import { StaticMapEntitySystem } from "./systems/static_map_entity";
2020-05-17 08:07:20 +00:00
import { ItemAcceptorSystem } from "./systems/item_acceptor";
2020-05-20 13:51:06 +00:00
import { StorageSystem } from "./systems/storage";
2020-06-24 20:23:10 +00:00
import { EnergyGeneratorSystem } from "./systems/energy_generator";
import { WiredPinsSystem } from "./systems/wired_pins";
import { EnergyConsumerSystem } from "./systems/energy_consumer";
2020-05-09 14:45:23 +00:00
const logger = createLogger("game_system_manager");
export class GameSystemManager {
/**
*
* @param {GameRoot} root
*/
constructor(root) {
this.root = root;
this.systems = {
/* typehints:start */
/** @type {BeltSystem} */
belt: null,
/** @type {ItemEjectorSystem} */
itemEjector: null,
/** @type {MapResourcesSystem} */
mapResources: null,
/** @type {MinerSystem} */
miner: null,
/** @type {ItemProcessorSystem} */
itemProcessor: null,
/** @type {UndergroundBeltSystem} */
undergroundBelt: null,
/** @type {HubSystem} */
hub: null,
/** @type {StaticMapEntitySystem} */
staticMapEntities: null,
2020-05-17 08:07:20 +00:00
/** @type {ItemAcceptorSystem} */
itemAcceptor: null,
2020-05-20 13:51:06 +00:00
/** @type {StorageSystem} */
storage: null,
2020-06-24 20:23:10 +00:00
/** @type {EnergyGeneratorSystem} */
energyGenerator: null,
/** @type {WiredPinsSystem} */
wiredPins: null,
/** @type {EnergyConsumerSystem} */
energyConsumer: null,
2020-05-09 14:45:23 +00:00
/* typehints:end */
};
this.systemUpdateOrder = [];
this.internalInitSystems();
}
/**
* Initializes all systems
*/
internalInitSystems() {
const add = (id, systemClass) => {
this.systems[id] = new systemClass(this.root);
this.systemUpdateOrder.push(id);
};
// Order is important!
add("belt", BeltSystem);
2020-05-20 13:51:06 +00:00
add("undergroundBelt", UndergroundBeltSystem);
2020-05-09 14:45:23 +00:00
add("miner", MinerSystem);
2020-05-20 13:51:06 +00:00
add("storage", StorageSystem);
2020-05-09 14:45:23 +00:00
add("itemProcessor", ItemProcessorSystem);
2020-05-20 13:51:06 +00:00
add("itemEjector", ItemEjectorSystem);
add("mapResources", MapResourcesSystem);
2020-05-09 14:45:23 +00:00
add("hub", HubSystem);
2020-06-24 20:23:10 +00:00
add("energyGenerator", EnergyGeneratorSystem);
2020-05-09 14:45:23 +00:00
add("staticMapEntities", StaticMapEntitySystem);
2020-06-24 20:23:10 +00:00
add("wiredPins", WiredPinsSystem);
add("energyConsumer", EnergyConsumerSystem);
// IMPORTANT: Must be after belt system since belt system can change the
// orientation of an entity after it is placed -> the item acceptor cache
// then would be invalid
2020-05-17 08:07:20 +00:00
add("itemAcceptor", ItemAcceptorSystem);
2020-05-09 14:45:23 +00:00
logger.log("📦 There are", this.systemUpdateOrder.length, "game systems");
}
/**
* Updates all systems
*/
update() {
for (let i = 0; i < this.systemUpdateOrder.length; ++i) {
const system = this.systems[this.systemUpdateOrder[i]];
system.update();
}
}
refreshCaches() {
for (let i = 0; i < this.systemUpdateOrder.length; ++i) {
const system = this.systems[this.systemUpdateOrder[i]];
system.refreshCaches();
}
}
}