Fix sector drag and drop
This commit is contained in:
@@ -1,49 +1,69 @@
|
||||
extends Control
|
||||
|
||||
## Offers a set of random modifiers each round as draggable pieces.
|
||||
## Dropping one on the board applies it and consumes the whole set.
|
||||
|
||||
const SectorSwatch := preload("res://core/ui/sector_swatch.gd")
|
||||
## Spawns draggable modifier pieces along the bottom of the screen.
|
||||
##
|
||||
## For now it spawns ALL modifiers in the registry, one of each, and they never
|
||||
## run out. That's a debug affordance — the real economy (random draws, rounds,
|
||||
## consumption) is a later phase. Right now we just want to prove drag-and-drop
|
||||
## works.
|
||||
|
||||
@export var drop_catcher: Control
|
||||
@export var pieces_per_round := 3
|
||||
|
||||
@export_group("Layout")
|
||||
@export var piece_size := Vector2(90, 110)
|
||||
@export var spacing := 24.0
|
||||
@export var bottom_margin := 30.0
|
||||
|
||||
@export_group("Behaviour")
|
||||
## If false, pieces stay in the tray after a successful drop. Useful while
|
||||
## testing; set true when the real economy lands.
|
||||
@export var consume_on_use := false
|
||||
|
||||
const SectorSwatch := preload("res://core/ui/sector_swatch.gd")
|
||||
|
||||
var _pieces: Array[Control] = []
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
# The tray itself must not eat mouse events — only its pieces should.
|
||||
mouse_filter = Control.MOUSE_FILTER_IGNORE
|
||||
spawn_round()
|
||||
|
||||
|
||||
func spawn_round() -> void:
|
||||
_clear_pieces()
|
||||
|
||||
# ModRegistry is the autoload. (Was "ModifierCatalog" — renamed.)
|
||||
var choices: Array = ModRegistry.pick_random(pieces_per_round)
|
||||
if choices.is_empty():
|
||||
push_warning("ModifierTray: registry returned no modifiers.")
|
||||
if drop_catcher == null:
|
||||
push_error("ModifierTray: `drop_catcher` export is not set.")
|
||||
return
|
||||
|
||||
for mod in choices:
|
||||
var piece := Control.new()
|
||||
piece.set_script(SectorSwatch)
|
||||
piece.custom_minimum_size = piece_size
|
||||
piece.size = piece_size
|
||||
piece.modifier = mod
|
||||
piece.paint_texture = mod.texture
|
||||
piece.drop_catcher = drop_catcher
|
||||
piece.tray = self
|
||||
add_child(piece)
|
||||
_pieces.append(piece)
|
||||
|
||||
_layout_pieces()
|
||||
spawn_all()
|
||||
|
||||
|
||||
func _layout_pieces() -> void:
|
||||
## Spawn one piece per registered modifier.
|
||||
func spawn_all() -> void:
|
||||
_clear_pieces()
|
||||
|
||||
var mods: Array[Modifier] = ModRegistry.modifiers
|
||||
if mods.is_empty():
|
||||
push_warning("ModifierTray: registry has no modifiers. Nothing to spawn.")
|
||||
return
|
||||
|
||||
for mod in mods:
|
||||
_spawn_piece(mod)
|
||||
|
||||
_layout()
|
||||
|
||||
|
||||
func _spawn_piece(mod: Modifier) -> void:
|
||||
var piece := Control.new()
|
||||
piece.set_script(SectorSwatch)
|
||||
piece.custom_minimum_size = piece_size
|
||||
piece.size = piece_size
|
||||
piece.modifier = mod
|
||||
piece.paint_texture = mod.texture
|
||||
piece.drop_catcher = drop_catcher
|
||||
piece.tray = self
|
||||
add_child(piece)
|
||||
_pieces.append(piece)
|
||||
|
||||
|
||||
func _layout() -> void:
|
||||
var count := _pieces.size()
|
||||
if count == 0:
|
||||
return
|
||||
@@ -57,9 +77,14 @@ func _layout_pieces() -> void:
|
||||
_pieces[i].position = Vector2(start_x + i * (piece_size.x + spacing), y)
|
||||
|
||||
|
||||
## Called by a piece when it lands successfully — the whole set is spent.
|
||||
func consume() -> void:
|
||||
_clear_pieces()
|
||||
## Called by a piece after a successful drop.
|
||||
func on_piece_used(piece: Control) -> void:
|
||||
if not consume_on_use:
|
||||
return # piece snaps home; it's reusable
|
||||
|
||||
_pieces.erase(piece)
|
||||
piece.queue_free()
|
||||
_layout()
|
||||
|
||||
|
||||
func _clear_pieces() -> void:
|
||||
@@ -69,7 +94,6 @@ func _clear_pieces() -> void:
|
||||
_pieces.clear()
|
||||
|
||||
|
||||
# TESTING ONLY — SPACE rolls a new set.
|
||||
func _unhandled_input(event: InputEvent) -> void:
|
||||
if event is InputEventKey and event.pressed and event.keycode == KEY_SPACE:
|
||||
spawn_round()
|
||||
func _notification(what: int) -> void:
|
||||
if what == NOTIFICATION_RESIZED:
|
||||
_layout()
|
||||
|
||||
Reference in New Issue
Block a user