project-dung/scripts/power_station.gd

52 lines
1.2 KiB
GDScript

extends Spatial
export var max_charge_capacity : int = 1
export var time_to_generate : float = 120
export var current_holding : int = 0 setget set_current_holding
var current_timer : float = 0
var rtg_lights = []
func set_current_holding(new_value):
current_holding = new_value
if new_value > 0:
for light in rtg_lights:
light.activate()
$CointCount.text = "Charge Count: %d" % current_holding
func _ready():
rtg_lights = get_tree().get_nodes_in_group("rtg_lights")
assert(rtg_lights != null)
set_current_holding(current_holding)
func _process(delta):
current_timer += delta
if current_timer >= time_to_generate:
generate_charge()
current_timer = 0
func generate_charge():
if current_holding >= max_charge_capacity:
return
self.current_holding += 1
print("power station: charge generated")
func collect_charges_from_station():
var charges_to_return : int = current_holding
self.current_holding = 0
return charges_to_return
func on_player_interact(player) -> bool:
assert(player.has_method("modify_inventory"))
var charges = collect_charges_from_station()
if charges <= 0:
$sounds/error.play()
return false
player.modify_inventory("coins", charges)
$sounds/charge_get.play()
for light in rtg_lights:
light.warn()
return true