68 lines
2.3 KiB
GDScript
68 lines
2.3 KiB
GDScript
## Economy.gd
|
|
## Manages minerals, gas, workers, and income for all players.
|
|
extends Node
|
|
|
|
const BASE_MINERAL_INCOME: int = 50
|
|
const GAS_PER_WORKER_PER_ROUND: int = 10
|
|
|
|
# Per-player economy state
|
|
var _minerals: Dictionary = {} # player_id -> int
|
|
var _gas: Dictionary = {} # player_id -> int
|
|
var _workers: Dictionary = {} # player_id -> int
|
|
var _income: Dictionary = {} # player_id -> int (cumulative bonus income)
|
|
|
|
func init_player(player_id: int, starting_minerals: int = 200, starting_workers: int = 3) -> void:
|
|
_minerals[player_id] = starting_minerals
|
|
_gas[player_id] = 0
|
|
_workers[player_id] = starting_workers
|
|
_income[player_id] = 0
|
|
|
|
## Called at end of each round to pay out income and gas.
|
|
func process_income_phase() -> void:
|
|
for player_id in _minerals.keys():
|
|
var mineral_gain = BASE_MINERAL_INCOME + _income[player_id]
|
|
_minerals[player_id] += mineral_gain
|
|
EventBus.minerals_changed.emit(player_id, _minerals[player_id])
|
|
EventBus.income_earned.emit(player_id, mineral_gain)
|
|
|
|
var gas_gain = _workers[player_id] * GAS_PER_WORKER_PER_ROUND
|
|
_gas[player_id] += gas_gain
|
|
EventBus.gas_changed.emit(player_id, _gas[player_id])
|
|
|
|
## Spend minerals. Returns false if insufficient funds.
|
|
func spend_minerals(player_id: int, amount: int) -> bool:
|
|
if _minerals.get(player_id, 0) < amount:
|
|
return false
|
|
_minerals[player_id] -= amount
|
|
EventBus.minerals_changed.emit(player_id, _minerals[player_id])
|
|
return true
|
|
|
|
## Spend gas. Returns false if insufficient funds.
|
|
func spend_gas(player_id: int, amount: int) -> bool:
|
|
if _gas.get(player_id, 0) < amount:
|
|
return false
|
|
_gas[player_id] -= amount
|
|
EventBus.gas_changed.emit(player_id, _gas[player_id])
|
|
return true
|
|
|
|
## Add a permanent income bonus (from sends or upgrades).
|
|
func add_income(player_id: int, bonus: int) -> void:
|
|
_income[player_id] = _income.get(player_id, 0) + bonus
|
|
|
|
func add_worker(player_id: int, cost_minerals: int) -> bool:
|
|
if not spend_minerals(player_id, cost_minerals):
|
|
return false
|
|
_workers[player_id] = _workers.get(player_id, 0) + 1
|
|
return true
|
|
|
|
func get_minerals(player_id: int) -> int:
|
|
return _minerals.get(player_id, 0)
|
|
|
|
func get_gas(player_id: int) -> int:
|
|
return _gas.get(player_id, 0)
|
|
|
|
func get_workers(player_id: int) -> int:
|
|
return _workers.get(player_id, 0)
|
|
|
|
func get_income(player_id: int) -> int:
|
|
return BASE_MINERAL_INCOME + _income.get(player_id, 0)
|