Transporter vessels, an explicit store button, and bottom-anchored menus
Forgejo #19, part 6 of #12. Transporters are built at the ship crafter, fly themselves to the nearest hub and wait there. When a building is short of what its queue needs, the nearest idle transporter runs hub -> mothership -> building -> hub. Ore leaves the bank when it is loaded and lands in the building's stock on arrival, so what is drawn and what happens are the same event - the extractor hauler's rule. Production orders are now TYPED (interceptor / transporter) rather than a bare count, since the crafter builds two different things. One at a time is dispatched per building: without that, every idle transporter chases the same shortfall and they all arrive carrying ore nobody needs. Three fixes from play-testing feedback: - Storing ore into a building is now a deliberate button press, not a side effect of opening the menu. - The mothership's load-ore controls were positioned from the TOP of the screen, so on any viewport shorter than ~1300px the label fell off the bottom. They are anchored to the bottom edge now, and the label had no explicit font size so it inherited the theme's 78px and wrapped off screen. Checked at 720x960 and 720x1280. - The craft button looked live and did nothing. It was not the transport work: BASE_FLEET_SIZE is 5 and a new game starts with exactly 5 ships, so the fleet is at its cap from the first frame and every interceptor order was refused. The button is now visibly disabled and says why. The underlying balance (crafter useless until Wing Doctrine) is left alone - that is a design call, not a bug to quietly change. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
823828f3b3
commit
d41128bc43
22 changed files with 753 additions and 54 deletions
230
GameState.gd
230
GameState.gd
|
|
@ -277,7 +277,7 @@ 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,
|
||||
"id": id, "type": type, "position": at_position, "stock": 0, "orders": [],
|
||||
})
|
||||
return id
|
||||
|
||||
|
|
@ -289,12 +289,22 @@ func find_building(id: int) -> Dictionary:
|
|||
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
|
||||
const PRODUCT_SHIP: String = "interceptor"
|
||||
const PRODUCT_TRANSPORTER: String = "transporter"
|
||||
|
||||
const TRANSPORTER_COST: int = 10
|
||||
|
||||
|
||||
func product_cost(product: String) -> int:
|
||||
if product == PRODUCT_TRANSPORTER:
|
||||
return TRANSPORTER_COST
|
||||
return SHIP_COST
|
||||
|
||||
|
||||
func can_produce(type: String) -> bool:
|
||||
# Only the ship crafter builds anything. The hub STATIONS transporters,
|
||||
# it does not make them (#12).
|
||||
return type == BUILDING_SHIP_CRAFTER
|
||||
|
||||
|
||||
func building_stock(id: int) -> int:
|
||||
|
|
@ -304,7 +314,29 @@ func building_stock(id: int) -> int:
|
|||
|
||||
func pending_orders(id: int) -> int:
|
||||
var b: Dictionary = find_building(id)
|
||||
return 0 if b.is_empty() else int(b.get("queued", 0))
|
||||
return 0 if b.is_empty() else (b.get("orders", []) as Array).size()
|
||||
|
||||
|
||||
func pending_orders_of(id: int, product: String) -> int:
|
||||
var b: Dictionary = find_building(id)
|
||||
if b.is_empty():
|
||||
return 0
|
||||
var n: int = 0
|
||||
for o in b.get("orders", []):
|
||||
if o == product:
|
||||
n += 1
|
||||
return n
|
||||
|
||||
|
||||
func outstanding_cost(id: int) -> int:
|
||||
# What this building still needs delivered to clear everything queued.
|
||||
var b: Dictionary = find_building(id)
|
||||
if b.is_empty():
|
||||
return 0
|
||||
var need: int = 0
|
||||
for o in b.get("orders", []):
|
||||
need += product_cost(o)
|
||||
return maxi(need - int(b.get("stock", 0)), 0)
|
||||
|
||||
|
||||
func deposit_to_building(id: int, amount: int) -> int:
|
||||
|
|
@ -317,28 +349,43 @@ func deposit_to_building(id: int, amount: int) -> int:
|
|||
return amount
|
||||
|
||||
|
||||
func queue_order(id: int) -> bool:
|
||||
func queue_order(id: int, product: String = PRODUCT_SHIP) -> bool:
|
||||
var b: Dictionary = find_building(id)
|
||||
if b.is_empty() or production_cost(b.type) <= 0:
|
||||
if b.is_empty() or not can_produce(b.type):
|
||||
return false
|
||||
b.queued = int(b.get("queued", 0)) + 1
|
||||
if product == PRODUCT_TRANSPORTER and not has_transport_hub():
|
||||
# Nowhere to station it. Refused rather than queued, so the player is
|
||||
# told now instead of watching an order sit forever.
|
||||
return false
|
||||
(b.orders as Array).append(product)
|
||||
return true
|
||||
|
||||
|
||||
func _advance_production() -> void:
|
||||
for b in buildings:
|
||||
var cost: int = production_cost(b.type)
|
||||
if cost <= 0:
|
||||
if not can_produce(b.type):
|
||||
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:
|
||||
var orders: Array = b.get("orders", [])
|
||||
while not orders.is_empty():
|
||||
var product: String = String(orders[0])
|
||||
var cost: int = product_cost(product)
|
||||
if int(b.get("stock", 0)) < cost:
|
||||
break
|
||||
if product == PRODUCT_TRANSPORTER and nearest_hub_to(b.position).is_empty():
|
||||
# Every hub was demolished while this sat in the queue. Leave
|
||||
# it queued and the stock untouched rather than producing a
|
||||
# vessel with nowhere to live.
|
||||
break
|
||||
b.stock = int(b.stock) - cost
|
||||
b.queued = int(b.queued) - 1
|
||||
if b.type == BUILDING_SHIP_CRAFTER:
|
||||
orders.pop_front()
|
||||
if product == PRODUCT_TRANSPORTER:
|
||||
_station_transporter(b.position)
|
||||
else:
|
||||
ships_owed += 1
|
||||
order_completed.emit(int(b.id), String(b.type))
|
||||
order_completed.emit(int(b.id), product)
|
||||
|
||||
|
||||
func has_mothership() -> bool:
|
||||
|
|
@ -470,6 +517,7 @@ func _process(delta: float) -> void:
|
|||
# keep completing - while the player is on a planet's surface or nowhere
|
||||
# near either.
|
||||
_advance_production()
|
||||
_advance_transporters(delta)
|
||||
if extractors.is_empty():
|
||||
return
|
||||
for e in extractors:
|
||||
|
|
@ -486,6 +534,154 @@ func _process(delta: float) -> void:
|
|||
e.progress -= 1.0
|
||||
|
||||
|
||||
# --- transporters (#19) ---------------------------------------------------
|
||||
|
||||
# Vessels stationed at a transport hub that fetch ore from the mothership and
|
||||
# deliver it to whichever building is waiting on it.
|
||||
#
|
||||
# Simulated HERE rather than by any node, exactly like the extractor haulers
|
||||
# above and for the same reason: a route the player is nowhere near still has
|
||||
# to run. `progress` drives both the drawn position and the moment the ore
|
||||
# actually moves, so what you see and what happens cannot drift apart.
|
||||
#
|
||||
# Entries are {"hub_id", "state", "target_id", "carrying", "progress",
|
||||
# "from", "to"}.
|
||||
const TRANSPORTER_SPEED: float = 340.0
|
||||
|
||||
const TRANSPORTER_IDLE: String = "idle"
|
||||
const TRANSPORTER_TO_MOTHERSHIP: String = "to_mothership"
|
||||
const TRANSPORTER_TO_BUILDING: String = "to_building"
|
||||
const TRANSPORTER_RETURNING: String = "returning"
|
||||
|
||||
var transporters: Array = []
|
||||
|
||||
|
||||
func has_transport_hub() -> bool:
|
||||
return not nearest_hub_to(Vector2.ZERO).is_empty()
|
||||
|
||||
|
||||
func nearest_hub_to(at: Vector2) -> Dictionary:
|
||||
var best: Dictionary = {}
|
||||
var best_d: float = INF
|
||||
for b in buildings:
|
||||
if b.type != BUILDING_TRANSPORT_HUB:
|
||||
continue
|
||||
var d: float = at.distance_squared_to(b.position)
|
||||
if d < best_d:
|
||||
best_d = d
|
||||
best = b
|
||||
return best
|
||||
|
||||
|
||||
func _station_transporter(built_at: Vector2) -> void:
|
||||
# Built at the crafter, then it flies itself to the nearest hub and waits
|
||||
# there for orders (#12).
|
||||
var hub: Dictionary = nearest_hub_to(built_at)
|
||||
if hub.is_empty():
|
||||
return
|
||||
transporters.append({
|
||||
"hub_id": int(hub.id), "state": TRANSPORTER_RETURNING, "target_id": 0,
|
||||
"carrying": 0, "progress": 0.0, "from": built_at, "to": hub.position,
|
||||
})
|
||||
|
||||
|
||||
func transporter_position(t: Dictionary) -> Vector2:
|
||||
return (t.from as Vector2).lerp(t.to as Vector2, clampf(t.progress, 0.0, 1.0))
|
||||
|
||||
|
||||
func _hub_position(hub_id: int) -> Variant:
|
||||
var hub: Dictionary = find_building(hub_id)
|
||||
return null if hub.is_empty() else hub.position
|
||||
|
||||
|
||||
func _begin_leg(t: Dictionary, from: Vector2, to: Vector2, state: String) -> void:
|
||||
t.from = from
|
||||
t.to = to
|
||||
t.state = state
|
||||
t.progress = 0.0
|
||||
|
||||
|
||||
func _transporter_inbound_to(building_id: int) -> bool:
|
||||
for t in transporters:
|
||||
if t.state != TRANSPORTER_IDLE and int(t.target_id) == building_id:
|
||||
return true
|
||||
return false
|
||||
|
||||
|
||||
func _dispatch_transporters() -> void:
|
||||
var mothership: Variant = get_mothership_position()
|
||||
if mothership == null:
|
||||
return # nowhere to load from
|
||||
for b in buildings:
|
||||
if outstanding_cost(int(b.id)) <= 0:
|
||||
continue
|
||||
# One at a time per building: without this every idle transporter
|
||||
# would be sent after the same shortfall and they would all arrive
|
||||
# carrying ore nobody needs any more.
|
||||
if _transporter_inbound_to(int(b.id)):
|
||||
continue
|
||||
var chosen: Dictionary = {}
|
||||
var best_d: float = INF
|
||||
for t in transporters:
|
||||
if t.state != TRANSPORTER_IDLE:
|
||||
continue
|
||||
var here: Vector2 = transporter_position(t)
|
||||
var d: float = here.distance_squared_to(b.position)
|
||||
if d < best_d:
|
||||
best_d = d
|
||||
chosen = t
|
||||
if chosen.is_empty():
|
||||
return # every transporter is busy
|
||||
chosen.target_id = int(b.id)
|
||||
_begin_leg(chosen, transporter_position(chosen), mothership, TRANSPORTER_TO_MOTHERSHIP)
|
||||
|
||||
|
||||
func _advance_transporters(delta: float) -> void:
|
||||
if transporters.is_empty():
|
||||
return
|
||||
_dispatch_transporters()
|
||||
for t in transporters:
|
||||
if t.state == TRANSPORTER_IDLE:
|
||||
continue
|
||||
var distance: float = (t.from as Vector2).distance_to(t.to as Vector2)
|
||||
var leg: float = maxf(distance / TRANSPORTER_SPEED, 0.0001)
|
||||
t.progress += delta / leg
|
||||
if t.progress < 1.0:
|
||||
continue
|
||||
t.progress = 1.0
|
||||
_arrive(t)
|
||||
|
||||
|
||||
func _arrive(t: Dictionary) -> void:
|
||||
var here: Vector2 = t.to
|
||||
var hub_pos: Variant = _hub_position(int(t.hub_id))
|
||||
match t.state:
|
||||
TRANSPORTER_TO_MOTHERSHIP:
|
||||
var target: Dictionary = find_building(int(t.target_id))
|
||||
var want: int = outstanding_cost(int(t.target_id))
|
||||
var take: int = mini(want, rocks)
|
||||
if take <= 0 or target.is_empty():
|
||||
# Nothing to carry, or the building is gone. Go home empty.
|
||||
t.target_id = 0
|
||||
_begin_leg(t, here, hub_pos if hub_pos != null else here, TRANSPORTER_RETURNING)
|
||||
return
|
||||
rocks -= take
|
||||
t.carrying = take
|
||||
_begin_leg(t, here, target.position, TRANSPORTER_TO_BUILDING)
|
||||
TRANSPORTER_TO_BUILDING:
|
||||
# The ore moves at exactly the moment the vessel arrives.
|
||||
if deposit_to_building(int(t.target_id), int(t.carrying)) <= 0:
|
||||
# Building demolished in transit - put the ore back rather
|
||||
# than quietly destroying it.
|
||||
rocks += int(t.carrying)
|
||||
t.carrying = 0
|
||||
t.target_id = 0
|
||||
_begin_leg(t, here, hub_pos if hub_pos != null else here, TRANSPORTER_RETURNING)
|
||||
TRANSPORTER_RETURNING:
|
||||
t.state = TRANSPORTER_IDLE
|
||||
t.progress = 1.0
|
||||
|
||||
|
||||
func can_afford(cost: int) -> bool:
|
||||
return rocks >= cost
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue