seedpackets and handlers
This commit is contained in:
parent
2a7c402cd0
commit
73a2fe42ad
16 changed files with 157 additions and 58 deletions
32
scripts/gui/seedpacket/hotbar_handler.gd
Normal file
32
scripts/gui/seedpacket/hotbar_handler.gd
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
extends SeedpacketHandler
|
||||
|
||||
## Seedpacket handler for hotbar plants.
|
||||
|
||||
class_name HotbarHandler
|
||||
|
||||
## Is current state valid for use?
|
||||
|
||||
var valid_state : bool = false
|
||||
var enough_sun : bool = true
|
||||
|
||||
func _init(seedpacket : Seedpacket) -> void:
|
||||
super._init(seedpacket)
|
||||
LevelEventBus.state_changed.connect(on_level_state_changed)
|
||||
LevelEventBus.sun_count_updated.connect(on_sun_count_updated)
|
||||
|
||||
func exit() -> void:
|
||||
LevelEventBus.state_changed.disconnect(on_level_state_changed)
|
||||
LevelEventBus.sun_count_updated.disconnect(on_sun_count_updated)
|
||||
|
||||
func is_avaiable():
|
||||
return valid_state and enough_sun
|
||||
|
||||
func on_level_state_changed(state : LevelData.LevelStates):
|
||||
valid_state = state == LevelData.LevelStates.PlantPick or state == LevelData.LevelStates.Game
|
||||
if state == LevelData.LevelStates.Game:
|
||||
enough_sun = LevelData.sun_count >= seedpacket.held_resource.cost
|
||||
seedpacket.update_contents()
|
||||
|
||||
func on_sun_count_updated(to : float):
|
||||
enough_sun = to >= seedpacket.held_resource.cost
|
||||
seedpacket.update_contents()
|
||||
1
scripts/gui/seedpacket/hotbar_handler.gd.uid
Normal file
1
scripts/gui/seedpacket/hotbar_handler.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://dpgqhrfbjeo4e
|
||||
21
scripts/gui/seedpacket/pickable_handler.gd
Normal file
21
scripts/gui/seedpacket/pickable_handler.gd
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
extends SeedpacketHandler
|
||||
|
||||
class_name PickableHandler
|
||||
|
||||
var chosen : bool
|
||||
var locked : bool = false
|
||||
var forbidden : bool = false
|
||||
|
||||
func _init(seedpacket : Seedpacket) -> void:
|
||||
super._init(seedpacket)
|
||||
LevelEventBus.hotbar_packets_update.connect(on_hotbar_changed)
|
||||
|
||||
func exit() -> void:
|
||||
LevelEventBus.hotbar_packets_update.disconnect(on_hotbar_changed)
|
||||
|
||||
func is_avaiable() -> bool:
|
||||
return not (chosen or locked or forbidden)
|
||||
|
||||
func on_hotbar_changed(to : Array[SeedpacketResource]):
|
||||
chosen = to.has(seedpacket.held_resource)
|
||||
seedpacket.update_contents()
|
||||
1
scripts/gui/seedpacket/pickable_handler.gd.uid
Normal file
1
scripts/gui/seedpacket/pickable_handler.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://bwp180q5f5sr5
|
||||
|
|
@ -1,5 +1,9 @@
|
|||
extends AspectRatioContainer
|
||||
|
||||
## Class that allows player to choose SeedpacketResource
|
||||
|
||||
class_name Seedpacket
|
||||
|
||||
@onready var button := $TextureButton
|
||||
@onready var preview :=$TextureButton/PreviewContainer/Preview
|
||||
@onready var cost := $TextureButton/Cost
|
||||
|
|
@ -9,53 +13,33 @@ extends AspectRatioContainer
|
|||
@onready var locked_rect := $TextureButton/Locked
|
||||
|
||||
var held_resource : SeedpacketResource
|
||||
var forbidden : bool = false
|
||||
var locked : bool = false
|
||||
var hotbar : bool = false
|
||||
var disabled : bool = false
|
||||
|
||||
#region Godot methods
|
||||
func _ready() -> void:
|
||||
LevelEventBus.entity_placed.connect(on_entity_placed)
|
||||
LevelEventBus.sun_count_updated.connect(on_sun_count_updated)
|
||||
LevelEventBus.state_changed.connect(on_level_state_changed)
|
||||
var handler : SeedpacketHandler
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
button.disabled = recharge_timer.time_left > 0 or forbidden or locked or disabled
|
||||
update_contents()
|
||||
#endregion
|
||||
button.disabled = recharge_timer.time_left > 0 or handler.is_avaiable() == false
|
||||
|
||||
#region Signal methods
|
||||
func on_pressed():
|
||||
LevelEventBus.packet_selected.emit(held_resource)
|
||||
if LevelData.state == LevelData.LevelStates.Game:
|
||||
LevelEventBus.packet_selected_during_game.emit(held_resource)
|
||||
if LevelEventBus.packet_placed.is_connected(on_packet_placed) == false:
|
||||
LevelEventBus.packet_placed.connect(on_packet_placed)
|
||||
focus_exited.connect(disconnect_placement)
|
||||
|
||||
func on_entity_placed(entity : SeedpacketResource):
|
||||
if entity == held_resource:
|
||||
recharge_timer.start()
|
||||
#endregion
|
||||
|
||||
func set_resource(to : SeedpacketResource):
|
||||
held_resource = to
|
||||
func set_handler(to : SeedpacketHandler):
|
||||
if handler:
|
||||
handler.exit()
|
||||
handler = to
|
||||
update_contents()
|
||||
|
||||
func update_contents():
|
||||
cost.text = str(held_resource.cost)
|
||||
preview.texture = held_resource.preview
|
||||
avaibility.visible = forbidden or locked or disabled
|
||||
locked_rect.visible = locked
|
||||
forbidden_rect.visible = forbidden
|
||||
avaibility.visible = handler.is_avaiable() == false
|
||||
handler.on_updated_contents()
|
||||
|
||||
func on_sun_count_updated(to : float):
|
||||
if hotbar:
|
||||
disabled = held_resource.cost < to
|
||||
func on_packet_placed(packet : SeedpacketResource):
|
||||
if held_resource != packet: return
|
||||
disconnect_placement()
|
||||
|
||||
func on_level_state_changed(state : LevelData.LevelStates):
|
||||
match state:
|
||||
LevelData.LevelStates.Game:
|
||||
disabled = held_resource.cost < LevelData.sun_count
|
||||
LevelData.LevelStates.PlantPick:
|
||||
disabled = false
|
||||
_:
|
||||
disabled = true
|
||||
func disconnect_placement():
|
||||
LevelEventBus.packet_placed.disconnect(on_packet_placed)
|
||||
focus_exited.disconnect(disconnect_placement)
|
||||
|
|
|
|||
22
scripts/gui/seedpacket/seedpacket_handler.gd
Normal file
22
scripts/gui/seedpacket/seedpacket_handler.gd
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
extends RefCounted
|
||||
|
||||
## Class that is used as modular runtime behaviour for seedpacket
|
||||
|
||||
class_name SeedpacketHandler
|
||||
|
||||
## Seedpacket that uses this handler
|
||||
var seedpacket : Seedpacket
|
||||
|
||||
func _init(packet : Seedpacket) -> void:
|
||||
seedpacket = packet
|
||||
|
||||
func exit() -> void:
|
||||
pass
|
||||
|
||||
## Invoked to check for avaiability
|
||||
func is_avaiable() -> bool:
|
||||
return true
|
||||
|
||||
## Invoked when seedpacket is requested to update
|
||||
func on_updated_contents() -> void:
|
||||
pass
|
||||
1
scripts/gui/seedpacket/seedpacket_handler.gd.uid
Normal file
1
scripts/gui/seedpacket/seedpacket_handler.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://d4cd7rku78x21
|
||||
Loading…
Add table
Add a link
Reference in a new issue