seedpackets and handlers
This commit is contained in:
parent
2a7c402cd0
commit
73a2fe42ad
16 changed files with 157 additions and 58 deletions
|
|
@ -23,7 +23,7 @@ config/windows_native_icon="res://icon.ico"
|
|||
|
||||
[autoload]
|
||||
|
||||
LevelEventBus="*res://scripts/level_event_bus.gd"
|
||||
LevelEventBus="*res://scripts/autoloads/level_event_bus.gd"
|
||||
|
||||
[display]
|
||||
|
||||
|
|
|
|||
14
scenes/gui/control.tscn
Normal file
14
scenes/gui/control.tscn
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
[gd_scene format=3 uid="uid://b1mbgkjvq5j76"]
|
||||
|
||||
[node name="Control" type="Control"]
|
||||
layout_mode = 3
|
||||
anchors_preset = 15
|
||||
anchor_right = 1.0
|
||||
anchor_bottom = 1.0
|
||||
grow_horizontal = 2
|
||||
grow_vertical = 2
|
||||
|
||||
[node name="ColorRect" type="ColorRect" parent="."]
|
||||
layout_mode = 0
|
||||
offset_right = 189.0
|
||||
offset_bottom = 128.0
|
||||
23
scripts/autoloads/game_registry.gd
Normal file
23
scripts/autoloads/game_registry.gd
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
extends Node
|
||||
|
||||
class_name GameRegistry
|
||||
|
||||
static func load_resources(directory : String, recursion : bool) -> Array[Resource]:
|
||||
var result : Array[Resource] = []
|
||||
var dir = DirAccess.open(directory)
|
||||
if dir == null:
|
||||
return result
|
||||
# Used to ignore last slash if it was provided in _path
|
||||
var path = dir.get_current_dir()
|
||||
if recursion:
|
||||
for subdir in dir.get_directories():
|
||||
var subdir_path = "%s/%s" % [ path, subdir ]
|
||||
result.append_array(load_resources(subdir_path,true))
|
||||
|
||||
for filename in dir.get_files():
|
||||
if !filename.ends_with('.tres'):
|
||||
continue
|
||||
var filepath = "%s/%s" % [ path, filename ]
|
||||
var res = ResourceLoader.load(filepath)
|
||||
result.append(res)
|
||||
return result
|
||||
1
scripts/autoloads/game_registry.gd.uid
Normal file
1
scripts/autoloads/game_registry.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://cu3cfhesth2np
|
||||
|
|
@ -7,7 +7,7 @@ class_name LevelSignals
|
|||
#region Field
|
||||
|
||||
## Emitted when entity is placed by player via seedpacket
|
||||
signal entity_placed(entity : SeedpacketResource)
|
||||
signal packet_placed(resource : SeedpacketResource)
|
||||
|
||||
#endregion
|
||||
|
||||
|
|
@ -25,16 +25,7 @@ signal entity_hp_changed(context : Entity.HPChangedContext)
|
|||
#region Seedpacket manipulation
|
||||
|
||||
## Called when player selects SeedpacketResource
|
||||
signal packet_selected(packet : SeedpacketResource)
|
||||
|
||||
## Called when player selects SeedpacketResource during game phase
|
||||
signal packet_selected_during_game(packet : SeedpacketResource)
|
||||
|
||||
## Called when something requests SeedpacketResource to be added to hotbar collection
|
||||
signal requested_packet_add(packet : SeedpacketResource)
|
||||
|
||||
## Called when something requests SeedpacketResourcesource to be deleted from hotbar collection
|
||||
signal requested_packet_remove(packet : SeedpacketResource)
|
||||
signal packet_selected(resource : SeedpacketResource)
|
||||
|
||||
## Called when selected packets are updated
|
||||
signal hotbar_packets_update(selected : Array[SeedpacketResource])
|
||||
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
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
extends Resource
|
||||
extends GameIdentifiableResource
|
||||
|
||||
## Base class for every possible entity in game. Makes possible for entities to be spawned using seedpackets
|
||||
class_name SeedpacketResource
|
||||
|
|
@ -20,11 +20,3 @@ class_name SeedpacketResource
|
|||
|
||||
## Order to sort in almanach
|
||||
@export_range(0,4056,1,"or_greater","hide_slider") var order : int
|
||||
|
||||
var gid : StringName = ""
|
||||
|
||||
func get_gid() -> StringName:
|
||||
if gid == "":
|
||||
var split_path = resource_path.split("/")
|
||||
gid = split_path[split_path.size()].trim_suffix(".tres").to_lower()
|
||||
return gid
|
||||
|
|
|
|||
15
scripts/resources/game_identifiable_resource.gd
Normal file
15
scripts/resources/game_identifiable_resource.gd
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
extends Resource
|
||||
|
||||
## Class that has unique GID specified by its filename.
|
||||
|
||||
class_name GameIdentifiableResource
|
||||
|
||||
## Game identifactor. Generates from resource filename. Should be unique.
|
||||
var gid : StringName = ""
|
||||
|
||||
## Gets GID. If resource has none, generates one.
|
||||
func get_gid() -> StringName:
|
||||
if gid == "":
|
||||
var split_path = resource_path.split("/")
|
||||
gid = split_path[split_path.size()].trim_suffix(".tres").to_lower()
|
||||
return gid
|
||||
1
scripts/resources/game_identifiable_resource.gd.uid
Normal file
1
scripts/resources/game_identifiable_resource.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://v044g4j1khxd
|
||||
Loading…
Add table
Add a link
Reference in a new issue