everything_has_a_price_jam/economy.gd
2026-09-26 23:54:33 -06:00

113 lines
2.8 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
@onready var events: Array[Event] = load_events()
func load_events() -> Array[Event]:
var dir := "res://random events"
var es: Array[Event] = []
for event_name in ResourceLoader.list_directory(dir):
var event: Event = load(dir + "/" + event_name)
es.push_back(event)
return es
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 do_bribing(p: Production) -> bool:
if get_currency_amount(p.currency) < p.amount:
return false
produce(negate_production(p))
return true
func negate_production(p: Production) -> Production:
var pp := p.duplicate()
pp.amount = -pp.amount
return pp
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 update_currencies():
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 simulate():
update_currencies()
for e in events:
if e.requirements_satisfied() and e.will_happen():
var outcome := e.outcome()
for o in outcome:
produce(o)
production_log.emit(null, o)
if e.timeline != null:
GameState.add_letter.emit(e.into_letter())
if randf() < 0.01:
LetterGeneration.try_generate_offer()
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(dir + "/" + 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:
%SimulateTickTimer.start()
update_currencies()
func _on_timer_timeout() -> void:
if Dialogic.current_timeline != null:
%SimulateTickTimer.start()
return
simulate()