Feat: ability activation UI with targeting flow

- Ability panel now starts a per-action targeting flow instead of
  immediately firing the ability
- Board abilities: blue tile highlights for unit/tile targets; click
  to collect AbilityTarget and advance to the next action
- Player/zone abilities: left-side player panel with player name
  buttons; zone (Hand/Deck/Discard) sub-buttons appear for zone-scoped
  abilities
- Re-clicking a selected spirit toggles the ability panel open/closed
- Auto-skips actions whose ownership is Self/ExactSelf/Random or whose
  count.all = true (server resolves those without player input)
- Game.send_activate_ability now accepts an optional targets array

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
dingo
2026-03-09 22:22:02 -04:00
co-authored by Claude Happy
parent 696452f4c9
commit 9e400c87b4
2 changed files with 253 additions and 28 deletions
+7 -3
View File
@@ -204,11 +204,15 @@ func send_transition(incoming_card_instance_id: String) -> void:
{ "incomingCardInstanceId": incoming_card_instance_id })
func send_activate_ability(source_id: String, ability_index: int) -> void:
_game_request("game.activate_ability", {
func send_activate_ability(source_id: String, ability_index: int,
targets: Array = []) -> void:
var payload := {
"sourceInstanceId": source_id,
"abilityIndex": ability_index
})
}
if not targets.is_empty():
payload["targets"] = targets
_game_request("game.activate_ability", payload)
# ── Private ───────────────────────────────────────────────────────────────────
+246 -25
View File
@@ -132,9 +132,13 @@ var _terra_count : int = 0 # total tiles needed (1/2/3)
var _sac_arch_coord : Vector2i = Vector2i(-1, -1)
# Ability
var _ability_unit_id : String = ""
var _ability_idx : int = -1
var _ability_panel : Control = null
var _ability_unit_id : String = ""
var _ability_idx : int = -1
var _ability_panel : Control = null
var _ability_def : Array = [] # Actions array from parsed ability JSON
var _ability_targets : Array = [] # collected AbilityTarget dicts to send
var _ability_cur_action_idx : int = 0 # which Action we are currently collecting for
var _player_panel : Control = null
# ── Unit ownership lookup ─────────────────────────────────────────────────────
# Rebuilt on each state update for O(1) ownership checks.
@@ -908,6 +912,14 @@ func _handle_board_click(mouse_pos: Vector2) -> void:
if _is_enemy_unit(unit_id):
Game.send_attack(_sel_unit_id, unit_id)
_set_mode(Mode.NONE)
elif unit_id == _sel_unit_id:
# Re-click same unit → toggle ability panel.
if is_instance_valid(_ability_panel):
_hide_ability_panel()
else:
var resel_unit := Game.get_unit_by_id(unit_id)
if not resel_unit.is_empty():
_show_ability_panel(unit_id, resel_unit)
elif _is_friendly_unit(unit_id):
_select_unit(unit_id) # switch to new unit
elif coord.x >= 0:
@@ -929,12 +941,23 @@ func _handle_board_click(mouse_pos: Vector2) -> void:
_confirm_sacrifice(unit_id, unit.get("name", "this spirit"))
Mode.ABILITY_TARGET:
# Blue-highlighted target clicked.
# Collect the clicked board target then advance the targeting flow.
if not unit_id.is_empty():
Game.send_activate_ability(_ability_unit_id, _ability_idx)
_ability_targets.append({
"actionIndex": _ability_cur_action_idx,
"instanceId": unit_id
})
_ability_cur_action_idx += 1
_advance_ability_targeting()
elif coord.x >= 0:
Game.send_activate_ability(_ability_unit_id, _ability_idx)
_set_mode(Mode.NONE)
_ability_targets.append({
"actionIndex": _ability_cur_action_idx,
"coord": { "x": coord.x, "y": coord.y }
})
_ability_cur_action_idx += 1
_advance_ability_targeting()
else:
_set_mode(Mode.NONE)
func _select_unit(unit_id: String) -> void:
@@ -982,12 +1005,16 @@ func _set_mode(new_mode: Mode) -> void:
_terra_targets = []
_terra_count = 0
if new_mode == Mode.NONE:
_sel_card_id = ""
_sel_unit_id = ""
_ability_unit_id = ""
_ability_idx = -1
_sel_card_id = ""
_sel_unit_id = ""
_ability_unit_id = ""
_ability_idx = -1
_ability_def = []
_ability_targets = []
_ability_cur_action_idx = 0
_clear_highlights()
_hide_ability_panel()
_hide_player_panel()
_refresh_hand_selection()
# ── ArchSpirit popup menu (Sacrifice / Ascend / Transition) ───────────────────
@@ -1151,16 +1178,19 @@ func _show_ability_panel(unit_id: String, unit: Dictionary) -> void:
for i in parsed.size():
var ab : Dictionary = parsed[i] as Dictionary
if ab.get("type", "") == "Active":
actives.append({ "index": i, "name": ab.get("name", "Ability %d" % i),
"desc": ab.get("description", "") })
actives.append({ "index": i,
"name": ab.get("name", "Ability %d" % i),
"desc": ab.get("description", ""),
"def": ab })
if actives.is_empty(): return
# Build a floating panel anchored above the hand.
# Build a floating panel anchored near the bottom-left above the hand area.
var panel := PanelContainer.new()
panel.position = Vector2(20, 480) # fixed position; adjust if layout changes
panel.position = Vector2(12, 420)
var vbox := VBoxContainer.new()
vbox.add_theme_constant_override("separation", 4)
var title := Label.new()
title.text = "Abilities"
title.add_theme_font_size_override("font_size", 12)
@@ -1168,21 +1198,32 @@ func _show_ability_panel(unit_id: String, unit: Dictionary) -> void:
vbox.add_child(title)
vbox.add_child(HSeparator.new())
# Wrap buttons in a ScrollContainer in case there are many.
var scroll := ScrollContainer.new()
scroll.custom_minimum_size = Vector2(180, 0)
scroll.horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED
var btn_vbox := VBoxContainer.new()
btn_vbox.add_theme_constant_override("separation", 4)
btn_vbox.size_flags_horizontal = Control.SIZE_EXPAND_FILL
for ab: Dictionary in actives:
var btn := Button.new()
btn.text = ab.get("name", "")
btn.add_theme_font_size_override("font_size", 12)
btn.text = ab.get("name", "")
btn.tooltip_text = ab.get("desc", "")
var idx : int = ab.get("index", 0)
btn.add_theme_font_size_override("font_size", 12)
var idx : int = ab.get("index", 0)
var ab_def : Dictionary = ab.get("def", {})
btn.pressed.connect(func() -> void:
_ability_unit_id = unit_id
_ability_idx = idx
# Activate without specific target (server will validate).
Game.send_activate_ability(unit_id, idx)
_hide_ability_panel()
_set_mode(Mode.NONE))
vbox.add_child(btn)
_ability_unit_id = unit_id
_ability_idx = idx
_ability_def = ab_def.get("actions", [])
_ability_targets = []
_ability_cur_action_idx = 0
_start_ability_targeting())
btn_vbox.add_child(btn)
scroll.add_child(btn_vbox)
vbox.add_child(scroll)
panel.add_child(vbox)
_hud.add_child(panel)
_ability_panel = panel
@@ -1194,6 +1235,186 @@ func _hide_ability_panel() -> void:
_ability_panel = null
## Determine what input is needed for the current action and either enter
## ABILITY_TARGET mode (board highlights) or show the player panel.
func _start_ability_targeting() -> void:
if _ability_cur_action_idx >= _ability_def.size():
_send_ability()
return
var action : Dictionary = {}
if _ability_def[_ability_cur_action_idx] is Dictionary:
action = _ability_def[_ability_cur_action_idx] as Dictionary
var target_sel : Dictionary = action.get("target", {})
var scope : String = target_sel.get("scope", "Battlefield")
var ownership : String = target_sel.get("ownership", "Any")
var count_dict : Dictionary = target_sel.get("count", {})
var count_all : bool = count_dict.get("all", false)
# Actions auto-resolved by the server need no player input.
var auto_ownerships := ["Self", "ExactSelf", "Random"]
if count_all or ownership in auto_ownerships:
_ability_cur_action_idx += 1
_start_ability_targeting()
return
# Player / zone selection — show the left-side player panel.
var zone_scopes := ["Player", "Hand", "Deck", "Discard", "AnyZone"]
if scope in zone_scopes:
_set_mode(Mode.ABILITY_TARGET)
_clear_highlights()
_show_player_panel(scope, ownership)
return
# Board selection — enter ABILITY_TARGET mode with blue tile highlights.
_set_mode(Mode.ABILITY_TARGET)
_clear_highlights()
_highlight_ability_targets(action)
## Called after each target is collected; advances to the next action or sends.
func _advance_ability_targeting() -> void:
_hide_player_panel()
_clear_highlights()
_start_ability_targeting()
## Highlight valid ability targets in blue.
func _highlight_ability_targets(action: Dictionary) -> void:
var target_sel : Dictionary = action.get("target", {})
var type_ : String = target_sel.get("type", "Unit")
var ownership : String = target_sel.get("ownership", "Any")
# Tile target — paint the whole board blue.
if type_ == "Tile":
var bw := Game.board_width()
var bh := Game.board_height()
for tx in range(bw):
for ty in range(bh):
_add_highlight(tx, ty, HI_BLUE)
return
# Unit-type target — highlight the board position of each matching unit.
var include_enemy : bool = ownership in ["Enemy", "Any"]
var include_friendly : bool = ownership in ["Owner", "Ally", "Any"]
# Fallback: if nothing matched, treat as enemy.
if not include_enemy and not include_friendly:
include_enemy = true
var targets : Array = []
if include_enemy:
targets.append_array(Game.get_opponent().get("field", []))
var opp_arch : Dictionary = Game.get_opponent().get("archSpirit", {})
if not opp_arch.is_empty():
targets.append(opp_arch)
if include_friendly:
targets.append_array(Game.get_field())
var my_arch : Dictionary = Game.get_my_player().get("archSpirit", {})
if not my_arch.is_empty():
targets.append(my_arch)
for u: Dictionary in targets:
var pos : Dictionary = u.get("position", {})
if pos.is_empty(): continue
_add_highlight(pos.get("x", 0), pos.get("y", 0), HI_BLUE)
## Fire the ability request with all collected targets.
func _send_ability() -> void:
Game.send_activate_ability(_ability_unit_id, _ability_idx, _ability_targets)
_set_mode(Mode.NONE)
## Show a left-side panel to let the player select a player (and optionally a
## zone) as the ability target.
func _show_player_panel(scope: String, ownership: String) -> void:
_hide_player_panel()
var panel := PanelContainer.new()
panel.position = Vector2(8, 180)
var vbox := VBoxContainer.new()
vbox.add_theme_constant_override("separation", 8)
vbox.custom_minimum_size = Vector2(160, 0)
var title := Label.new()
title.text = "Choose Player"
title.add_theme_font_size_override("font_size", 13)
title.add_theme_color_override("font_color", Color(0.7, 0.8, 1.0))
vbox.add_child(title)
vbox.add_child(HSeparator.new())
var my_id : String = Auth.user_id
var my_name : String = Auth.display_name if not Auth.display_name.is_empty() else "You"
var opp_id : String = str(Game.get_opponent().get("accountId", ""))
var opp_name : String = Game.opponent_name if not Game.opponent_name.is_empty() else "Opponent"
var show_self : bool = ownership in ["Owner", "Ally", "Any"]
var show_enemy : bool = ownership in ["Enemy", "Any"]
if not show_self and not show_enemy:
show_self = true
show_enemy = true
if show_self:
_add_player_option(vbox, my_name, my_id, scope)
if show_enemy:
_add_player_option(vbox, opp_name, opp_id, scope)
panel.add_child(vbox)
_hud.add_child(panel)
_player_panel = panel
## Add a player row (name + optional zone buttons) to the player panel vbox.
func _add_player_option(vbox: VBoxContainer, name_: String,
account_id: String, scope: String) -> void:
var zone_scopes := ["Hand", "Deck", "Discard", "AnyZone"]
if scope in zone_scopes:
# Player name label + zone buttons on the same row.
var lbl := Label.new()
lbl.text = name_
lbl.add_theme_font_size_override("font_size", 12)
vbox.add_child(lbl)
var zone_hbox := HBoxContainer.new()
zone_hbox.add_theme_constant_override("separation", 4)
var zones : Array = ["Hand", "Deck", "Discard"] if scope == "AnyZone" else [scope]
for zone_name : String in zones:
var zbtn := Button.new()
zbtn.text = zone_name
zbtn.add_theme_font_size_override("font_size", 11)
var cap_id : String = account_id
zbtn.pressed.connect(func() -> void:
_ability_targets.append({
"actionIndex": _ability_cur_action_idx,
"instanceId": cap_id
})
_ability_cur_action_idx += 1
_advance_ability_targeting())
zone_hbox.add_child(zbtn)
vbox.add_child(zone_hbox)
else:
# Pure player target — single button with the player's name.
var btn := Button.new()
btn.text = name_
btn.add_theme_font_size_override("font_size", 12)
var cap_id : String = account_id
btn.pressed.connect(func() -> void:
_ability_targets.append({
"actionIndex": _ability_cur_action_idx,
"instanceId": cap_id
})
_ability_cur_action_idx += 1
_advance_ability_targeting())
vbox.add_child(btn)
func _hide_player_panel() -> void:
if is_instance_valid(_player_panel):
_player_panel.queue_free()
_player_panel = null
func _show_unit_detail_popup(unit_id: String) -> void:
var unit := Game.get_unit_by_id(unit_id)
if unit.is_empty(): return