Buildings produce from their own stock, and queue orders until supplied
Forgejo #16, part 3 of #12. BuildController deducted the cost from the bank at the moment of purchase and refused outright with "Not enough rocks", so nothing ever had to be delivered anywhere. Each building now carries its own stock, and an order it cannot pay for yet is QUEUED and completes by itself once ore arrives. Records get an `id` because a building has to be referable across the space <-> planet-surface scene swap, when every Building NODE has been destroyed and rebuilt. Position would nearly work and then fail on two buildings placed on the same spot. Production advances in the GameState autoload, the same way extractor haulers already do, so an order can complete while the player is down a gravity well. A finished vessel becomes `ships_owed` rather than being handed straight to a Fleet that may not exist, and the fleet claims what it is owed when it next has room. The craft menu remembers which crafter it was opened for: _process() clears current_building while any menu is open, so by the next frame the menu would no longer know. It shows the ore delivered HERE rather than the bank, since showing the bank would promise something the crafter cannot actually build. Ship crafting cannot be supplied until #17 lands - nothing yet moves ore into a building. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
d1b82f4f5a
commit
3573ccf44f
6 changed files with 371 additions and 11 deletions
98
GameState.gd
98
GameState.gd
|
|
@ -211,8 +211,96 @@ func reset_progression() -> void:
|
|||
unlocked.clear()
|
||||
|
||||
|
||||
func add_building(type: String, at_position: Vector2) -> void:
|
||||
buildings.append({"type": type, "position": at_position})
|
||||
# --- building stock and production queue (#16) -----------------------------
|
||||
|
||||
# Since #12, a building cannot just deduct from the bank at the moment of
|
||||
# purchase: what it produces has to be paid for out of resources physically
|
||||
# delivered to it. So each building carries its own `stock`, and an order it
|
||||
# cannot afford yet is QUEUED rather than refused, and completes by itself
|
||||
# once the stock turns up.
|
||||
#
|
||||
# `id` exists because a building has to be referable across the space <->
|
||||
# planet-surface scene swap, when every Building NODE has been destroyed and
|
||||
# rebuilt. Position would nearly work and then fail on two buildings placed
|
||||
# on the same spot.
|
||||
var next_building_id: int = 1
|
||||
|
||||
# Ships that have been paid for and built but not yet handed to the fleet.
|
||||
# Production is simulated here, in the autoload, the same way extractor
|
||||
# haulers are - so an order can complete while the player is down on a
|
||||
# planet's surface, where there is no Fleet node to hand a ship to. The fleet
|
||||
# claims them when it next can.
|
||||
var ships_owed: int = 0
|
||||
|
||||
signal order_completed(building_id: int, type: String)
|
||||
|
||||
|
||||
func add_building(type: String, at_position: Vector2) -> int:
|
||||
var id: int = next_building_id
|
||||
next_building_id += 1
|
||||
buildings.append({
|
||||
"id": id, "type": type, "position": at_position, "stock": 0, "queued": 0,
|
||||
})
|
||||
return id
|
||||
|
||||
|
||||
func find_building(id: int) -> Dictionary:
|
||||
for b in buildings:
|
||||
if b.get("id", 0) == id:
|
||||
return b
|
||||
return {}
|
||||
|
||||
|
||||
func production_cost(type: String) -> int:
|
||||
# What one order out of this building costs it. 0 means it produces
|
||||
# nothing and cannot be ordered from.
|
||||
if type == BUILDING_SHIP_CRAFTER:
|
||||
return SHIP_COST
|
||||
return 0
|
||||
|
||||
|
||||
func building_stock(id: int) -> int:
|
||||
var b: Dictionary = find_building(id)
|
||||
return 0 if b.is_empty() else int(b.get("stock", 0))
|
||||
|
||||
|
||||
func pending_orders(id: int) -> int:
|
||||
var b: Dictionary = find_building(id)
|
||||
return 0 if b.is_empty() else int(b.get("queued", 0))
|
||||
|
||||
|
||||
func deposit_to_building(id: int, amount: int) -> int:
|
||||
if amount <= 0:
|
||||
return 0
|
||||
var b: Dictionary = find_building(id)
|
||||
if b.is_empty():
|
||||
return 0
|
||||
b.stock = int(b.get("stock", 0)) + amount
|
||||
return amount
|
||||
|
||||
|
||||
func queue_order(id: int) -> bool:
|
||||
var b: Dictionary = find_building(id)
|
||||
if b.is_empty() or production_cost(b.type) <= 0:
|
||||
return false
|
||||
b.queued = int(b.get("queued", 0)) + 1
|
||||
return true
|
||||
|
||||
|
||||
func _advance_production() -> void:
|
||||
for b in buildings:
|
||||
var cost: int = production_cost(b.type)
|
||||
if cost <= 0:
|
||||
continue
|
||||
# `while`, not `if`: a delivery big enough to cover several queued
|
||||
# orders should clear them all rather than dribbling one out per
|
||||
# frame.
|
||||
while int(b.get("queued", 0)) > 0 and int(b.get("stock", 0)) >= cost:
|
||||
b.stock = int(b.stock) - cost
|
||||
b.queued = int(b.queued) - 1
|
||||
if b.type == BUILDING_SHIP_CRAFTER:
|
||||
ships_owed += 1
|
||||
order_completed.emit(int(b.id), String(b.type))
|
||||
|
||||
|
||||
func has_mothership() -> bool:
|
||||
|
|
@ -340,8 +428,10 @@ func get_hauler_position(extractor: Dictionary) -> Variant:
|
|||
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
# Runs in the autoload, so extraction keeps earning while the player is
|
||||
# on a planet's surface or nowhere near the route.
|
||||
# Runs in the autoload, so extraction keeps earning - and queued orders
|
||||
# keep completing - while the player is on a planet's surface or nowhere
|
||||
# near either.
|
||||
_advance_production()
|
||||
if extractors.is_empty():
|
||||
return
|
||||
for e in extractors:
|
||||
|
|
|
|||
Loading…
Reference in a new issue