Skip to content

Instantly share code, notes, and snippets.

@bencbartlett
Last active Dec 20, 2018
Embed
What would you like to do?
Hatchery: build() vs refresh()
import {$} from '../caching/GlobalCache';
export class Hatchery extends HiveCluster {
// constructor gets run only on ticks with build() phase
constructor(colony: Colony, headSpawn: StructureSpawn) {
super(colony, headSpawn, 'hatchery');
this.memory = Mem.wrap(this.colony.memory, 'hatchery', HatcheryMemoryDefaults, true);
// Register physical structure components
this.spawns = colony.spawns;
this.availableSpawns = _.filter(this.spawns, spawn => !spawn.spawning);
this.extensions = colony.extensions;
this.towers = colony.commandCenter ? _.difference(colony.towers, colony.commandCenter.towers) : colony.towers;
this.battery = _.first(_.filter(this.room.containers, cont => insideBunkerBounds(cont.pos, this.colony)));
$.set(this, 'energyStructures', () => this.computeEnergyStructures());
}
// refresh() gets run on all other ticks
refresh() {
this.memory = Mem.wrap(this.colony.memory, 'hatchery', HatcheryMemoryDefaults, true); // refresh memory object
// refresh room and structure properties for next tick
$.refreshRoom(this);
$.refresh(this, 'spawns', 'extensions', 'energyStructures', 'link', 'towers', 'battery');
this.availableSpawns = _.filter(this.spawns, spawn => !spawn.spawning); // recalculate available spawns
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment