everything_has_a_price_jam/economy.gd
2026-09-26 15:39:49 -06:00

69 lines
1.7 KiB
GDScript

extends Node
signal productions_update(p: Array[Production])
@warning_ignore("unused_signal")
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 get_currency_amount(currency: Currency) -> int:
var c := get_currency(currency)
return c.amount
func can_consume(production: Production) -> bool:
return get_currency_amount(production.currency) >= production.amount
func produce(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(pp)
func get_all_currencies_in(dir: StringName):
var reses := ResourceLoader.list_directory(dir)
var resources := []
for res in reses:
if res.ends_with(".tres"):
resources.append(load(res))
return resources
func get_all_sentient_currencies() -> Array:
return get_all_currencies_in("res://currency/sentient")
func get_all_inanimate_currencies() -> Array:
return get_all_currencies_in("res://currency/inanimate")
func _ready() -> void:
simulate()