Last active
October 4, 2021 23:42
-
-
Save ChrisRimmer/bf80418a2b07bdacc14437ee2421170b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Just fuckin' imagine this is a class idgaf any more kill me | |
let networks = [] | |
class Network { | |
constructor (masterNode) { | |
this.id = id | |
this.momentOfInertia = 0 | |
this.frequency = 0 | |
this.nodes = [masterNode] | |
} | |
const changeFrequencyByEnergy = energy => { | |
const storedEnergy = (this.frequency ** 2) * this.momentOfInertia | |
this.frequency = ((storedEnergy + energy) ** 0.5) / this.momentOfInertia | |
} | |
const addNode = (newNode) => { | |
this.momentOfInertia += newNode.momentOfInertia | |
this.nodes.push(newNode) | |
} | |
const removeNode = (deadNode) => { | |
this.momentOfInertia -= deadNode.momentOfInertia | |
this.nodes = this.nodes.filter(node => node.id !== deadNode.id) | |
if (this.nodes.length === 0) networks.removeNetwork(this.id) // kill self | |
} | |
const stabilisationfactor = () => (this.frequency / this.targetFrequency) ** 2 | |
generate = energy => this.frequency = this.changeFrequencyByEnergy(energy / this.stabilisationFactor()) | |
consume = energy => this.frequency = this.changeFrequencyByEnergy(energy * this.stabilisationFactor() * -1) | |
} | |
class ElectricalNode { | |
constructor (powerDraw, momentOfInertia) { | |
if (networkID === null) {} | |
else {this.network = networks[networkID]} | |
this.powerDraw = powerDraw | |
this.momentOfInertia = momentOfInertia | |
} | |
onConnect = (targetNetworkID) => { | |
this.network = networks[targetNetworkID] | |
this.network.addNode(this) | |
} | |
onDisconnect = () => { | |
this.network.removeNode(this) | |
this.network = networks.newNetwork() | |
} | |
onDelete = () => { | |
this.network.removeNode(this) | |
} | |
} | |
class LightBulb extends ElectricalNode { | |
constructor (power) { | |
super(power || 60, 0) | |
} | |
onTick = () => {this.network.consume(this.powerDraw)} | |
} | |
class Inverter extends ElectricalNode { | |
constructor (capacity) { | |
super(-capacity, 0) | |
this.capacity = capacity | |
} | |
onTick = () => {this.network.generate(this.capacity)} | |
} | |
class Generator extends ElectricalNode { | |
constructor (capacity, momentOfInertia) { | |
super(-capacity, momentOfInertia) | |
this.capacity = capacity | |
} | |
onTick = () => this.network.generate(this.capacity)} | |
} | |
class Flywheel extends ElectricalNode { | |
constructor (_, momentOfInertia) { | |
super(0, momentOfInertia) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment