hadean-godot/Scripts/ResourceController.gd

114 lines
2.8 KiB
GDScript
Raw Normal View History

2025-10-10 03:01:08 -04:00
extends Node
enum ResourceType {
Logs,
Labor,
Berries,
Food
}
const res_names = {
#ResourceType.Logs: "Logs"
}
signal produce
signal consume
signal upgrade
signal resource_type_added
signal resource_stats_updated
class ResourceStats:
var max: int
var value: int
var delta: int
var stock: Dictionary[ResourceType, int] = {}
var storage: Dictionary[ResourceType, int] = {}
func add_storage(data: ResourceAmount):
ensure_resource(data.resource)
storage[data.resource] += data.amount
func remove_storage(data: ResourceAmount):
ensure_resource(data.resource)
storage[data.resource] -= data.amount
if stock[data.resource] > storage[data.resource]:
stock[data.resource] = storage[data.resource]
func _ready():
Clock.day_passed.connect(_tick_resources)
func _tick_resources():
var stats: Dictionary[ResourceType, ResourceStats] = {}
for resource in stock.keys():
var stat = ResourceStats.new()
stat.delta = -stock[resource]
stat.max = storage[resource]
stats[resource] = stat
produce.emit()
consume.emit()
for resource in stock.keys():
if !stats.has(resource):
var stat = ResourceStats.new()
stat.delta = stock[resource]
stat.max = storage[resource]
stats[resource] = stat
continue
stats[resource].delta += stock[resource]
# \/ this really should change here but like shrug
stats[resource].max = storage[resource]
for resource in stock.keys():
var max = storage[resource]
stock[resource] = clamp(stock[resource], 0, storage[resource])
stats[resource].value = stock[resource]
upgrade.emit()
for resource in stock:
if !stats.has(resource):
var stat = ResourceStats.new()
stat.value = stock[resource]
stat.delta = stock[resource]
stat.max = storage[resource]
stats[resource] = stat
continue
stats[resource].value = stock[resource]
stats[resource].max = storage[resource]
for resource in stats.keys():
var stat = stats[resource]
resource_stats_updated.emit(resource, stat)
func ensure_resource(resource: ResourceType):
if resource in stock: return
stock[resource] = 0
storage[resource] = 0
resource_type_added.emit(resource)
func consume_resource(resource: ResourceType, amount: int) -> bool:
if !stock.has(resource): return false
if stock[resource] == 0:
return false
stock[resource] -= amount
var failed = stock[resource] < 0
if failed:
stock[resource] = 0
return !failed
func add_resource(resource_type: ResourceType, amount: int):
ensure_resource(resource_type)
stock[resource_type] += amount
func get_resource_value(resource_type: ResourceType) -> int:
if !stock.has(resource_type): return 0
return stock[resource_type]
func get_resource_name(resource: ResourceType) -> String:
if resource in res_names: return res_names[resource]
return str(ResourceType.keys()[resource])
func get_resource_storage(resource: ResourceType):
if !storage.has(resource): return 0
return storage[resource]