everything_has_a_price_jam/economy.gd
2026-09-26 12:37:45 -06:00

49 lines
1.2 KiB
GDScript

extends Node
signal productions_update(p: Array[Production])
signal production_log(producer: Currency, p: Production)
@export var currencies: Array[Production]
@export var members: Array
func get_currency(c: Currency) -> Production:
sync_currency.call_deferred()
for p in currencies:
if c == p.currency:
return p
var p := Production.new()
p.currency = c
p.amount = 0
currencies.append(p)
return p
func sync_currency():
var did_remove := true
while did_remove:
did_remove = false
for i in range(len(currencies)):
var p: Production = currencies[i]
if p.amount == 0:
currencies.remove_at(i)
did_remove = true
break
productions_update.emit(currencies)
func produce(producer: Currency, production: Production):
sync_currency.call_deferred()
var c := get_currency(production.currency)
c.amount += production.amount
func simulate():
productions_update.emit.call_deferred(currencies)
for c in currencies:
for p in c.currency.production:
var pp := Production.new()
pp.currency = p.currency
pp.amount = c.amount * p.amount
produce(c.currency, pp)
# TODO remove this
production_log.emit.call_deferred(c.currency, pp)
func _ready() -> void:
simulate()