Actually: Add ranked gameplay UI — matchmaking + 3D game screen
- Game.gd autoload: stores session/state, handles match_found and
game.state_update.push, exposes helpers and all server action methods
- MatchmakingScreen: fetches decks, joins ranked queue, shows elapsed
timer, cancels cleanly; navigates to GameScreen on match_found push
- GameScreen (3D + 2D overlay):
- 3D board: 1×1×0.5 BoxMesh tiles colored by affinity (24 affinities),
StaticBody3D per tile for raycast picking
- Unit placeholders: sphere=Spirit, cylinder=ArchSpirit, box=Ruin,
team-colored (blue/red), exhaust dimming
- Camera: orbit (left-drag / one-finger), zoom (scroll / pinch),
pan (middle-drag / two-finger); isometric default ~40° pitch
- Hand: scrollable row of GameCardWidgets; hover → scale+rise anim;
click → select; selected card highlights valid play tiles green
- Unit selection: click unit → movement range (green), attack
range (red); click target tile → move; click enemy → attack
- Placement phase: valid ArchSpirit tiles highlighted automatically
- Draw phase: auto-draws after 0.3s delay
- Context popup: hold 0.5s on tile → tile coord/affinity + unit
stats + ability names
- Discard viewer: own + opponent, scrollable card list
- Game-over overlay: result, rank delta, return to main menu
- All game actions wired: play, move, attack, sacrifice, end turn,
draw, concede, ascend, transition, activate ability
- main_screen.gd: MatchBtn → MatchmakingScreen
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>
1066 lines
39 KiB
GDScript
1066 lines
39 KiB
GDScript
extends Node3D
|
||
## GameScreen — 3D ranked game board with 2D HUD overlay.
|
||
##
|
||
## 3D world: 1×1×0.5 tile cubes, sphere/cylinder/box unit placeholders,
|
||
## thin highlight planes, orbit/zoom/pan camera.
|
||
## HUD: hand scroll, resources, opponent panel, action bar,
|
||
## context popup (hold), discard viewer, game-over overlay.
|
||
|
||
# ── Selection mode ────────────────────────────────────────────────────────────
|
||
|
||
enum Mode { NONE, CARD_FROM_HAND, UNIT_SELECTED, PLACEMENT }
|
||
|
||
# ── Node refs ─────────────────────────────────────────────────────────────────
|
||
|
||
@onready var _camera_rig : Node3D = $CameraRig
|
||
@onready var _camera : Camera3D = $CameraRig/Camera3D
|
||
@onready var _tiles_root : Node3D = $BoardRoot/TilesRoot
|
||
@onready var _units_root : Node3D = $BoardRoot/UnitsRoot
|
||
@onready var _hi_root : Node3D = $BoardRoot/HighlightsRoot
|
||
@onready var _hold_timer : Timer = $HoldTimer
|
||
|
||
# HUD
|
||
@onready var _phase_lbl : Label = %PhaseLabel
|
||
@onready var _turn_lbl : Label = %TurnLabel
|
||
@onready var _opp_panel : HBoxContainer = %OpponentPanel
|
||
@onready var _opp_name_lbl : Label = %OppNameLabel
|
||
@onready var _opp_hand_lbl : Label = %OppHandLabel
|
||
@onready var _opp_deck_lbl : Label = %OppDeckLabel
|
||
@onready var _opp_discard_btn : Button = %OppDiscardBtn
|
||
@onready var _hand_cards : HBoxContainer = %HandCards
|
||
@onready var _sp_lbl : Label = %SpLabel
|
||
@onready var _st_lbl : Label = %StLabel
|
||
@onready var _deck_lbl : Label = %DeckLabel
|
||
@onready var _discard_btn : Button = %DiscardBtn
|
||
@onready var _draw_btn : Button = %DrawBtn
|
||
@onready var _end_turn_btn : Button = %EndTurnBtn
|
||
@onready var _concede_btn : Button = %ConcedeBtn
|
||
@onready var _ctx_popup : PanelContainer = %ContextPopup
|
||
@onready var _ctx_tile_lbl : Label = %ContextTileLabel
|
||
@onready var _ctx_unit_lbl : Label = %ContextUnitLabel
|
||
@onready var _ctx_abil_lbl : Label = %ContextAbilitiesLabel
|
||
@onready var _ctx_close_btn: Button = %ContextClose
|
||
@onready var _dv_overlay : PanelContainer = %DiscardViewer
|
||
@onready var _dv_title : Label = %DVTitle
|
||
@onready var _dv_list : VBoxContainer = %DVList
|
||
@onready var _dv_close_btn : Button = %DVCloseBtn
|
||
@onready var _go_overlay : PanelContainer = %GameOverOverlay
|
||
@onready var _go_result_lbl: Label = %GOResultLabel
|
||
@onready var _go_rank_lbl : Label = %GORankLabel
|
||
@onready var _go_menu_btn : Button = %GOMainMenuBtn
|
||
|
||
# ── Affinity colors ───────────────────────────────────────────────────────────
|
||
|
||
const AFFINITY_COLORS : Dictionary = {
|
||
"Void": Color(0.18, 0.18, 0.22),
|
||
"Aether": Color(0.60, 0.45, 0.85),
|
||
"Lake": Color(0.28, 0.52, 0.88),
|
||
"Hill": Color(0.58, 0.44, 0.26),
|
||
"Forest": Color(0.22, 0.52, 0.24),
|
||
"Field": Color(0.62, 0.72, 0.28),
|
||
"Ocean": Color(0.10, 0.20, 0.72),
|
||
"Island": Color(0.38, 0.68, 0.84),
|
||
"Marsh": Color(0.38, 0.52, 0.34),
|
||
"Swamp": Color(0.24, 0.33, 0.20),
|
||
"Mountain": Color(0.50, 0.44, 0.38),
|
||
"Steppe": Color(0.72, 0.63, 0.38),
|
||
"Fort": Color(0.40, 0.34, 0.30),
|
||
"Plains": Color(0.78, 0.74, 0.50),
|
||
"Grove": Color(0.33, 0.58, 0.28),
|
||
"Jungle": Color(0.15, 0.42, 0.20),
|
||
"PerfectAether": Color(0.88, 0.72, 1.00),
|
||
"PureVoid": Color(0.05, 0.05, 0.10),
|
||
"UnstableAether":Color(0.70, 0.55, 0.90),
|
||
"GatewayLake": Color(0.22, 0.42, 0.70),
|
||
"GatewayHill": Color(0.46, 0.34, 0.18),
|
||
"GatewayForest": Color(0.18, 0.42, 0.18),
|
||
"GatewayField": Color(0.52, 0.60, 0.22),
|
||
}
|
||
|
||
# ── Camera state ──────────────────────────────────────────────────────────────
|
||
|
||
var _cam_yaw : float = 0.0
|
||
var _cam_pitch : float = 40.0 # degrees, clamped 15–75
|
||
var _cam_distance : float = 14.0 # clamped 4–22
|
||
var _cam_pivot : Vector3 = Vector3.ZERO
|
||
var _drag_active : bool = false
|
||
var _pan_active : bool = false
|
||
var _last_mouse : Vector2 = Vector2.ZERO
|
||
# Touch state
|
||
var _touch_points : Dictionary = {} # finger_id → pos
|
||
var _pinch_dist : float = 0.0
|
||
|
||
# ── Selection state ───────────────────────────────────────────────────────────
|
||
|
||
var _mode : Mode = Mode.NONE
|
||
var _sel_card_id : String = "" # hand card InstanceId
|
||
var _sel_unit_id : String = "" # field unit InstanceId
|
||
var _held_coord : Vector2i = Vector2i(-1, -1) # tile under hold timer
|
||
|
||
# ── Tile StaticBody3D map ─────────────────────────────────────────────────────
|
||
# Maps "x,y" → StaticBody3D so we can update materials.
|
||
var _tile_bodies : Dictionary = {}
|
||
# Maps unit InstanceId → MeshInstance3D
|
||
var _unit_meshes : Dictionary = {}
|
||
|
||
# ── Lifecycle ─────────────────────────────────────────────────────────────────
|
||
|
||
func _ready() -> void:
|
||
# Environment
|
||
_setup_environment()
|
||
_apply_camera()
|
||
|
||
# HUD buttons
|
||
_draw_btn.pressed.connect(_on_draw_pressed)
|
||
_end_turn_btn.pressed.connect(_on_end_turn_pressed)
|
||
_concede_btn.pressed.connect(_on_concede_pressed)
|
||
_discard_btn.pressed.connect(func() -> void: _show_own_discard())
|
||
_opp_discard_btn.pressed.connect(func() -> void: _show_opp_discard())
|
||
_ctx_close_btn.pressed.connect(func() -> void: _ctx_popup.visible = false)
|
||
_dv_close_btn.pressed.connect(func() -> void: _dv_overlay.visible = false)
|
||
_go_menu_btn.pressed.connect(func() -> void:
|
||
get_tree().change_scene_to_file("res://scenes/main/MainScreen.tscn"))
|
||
_hold_timer.timeout.connect(_on_hold_timer_fired)
|
||
|
||
# Game signals
|
||
Game.state_updated.connect(_on_state_updated)
|
||
Game.game_over.connect(_on_game_over)
|
||
|
||
# Initial state: send ready, or apply if state already received.
|
||
if not Game.state.is_empty():
|
||
_on_state_updated(Game.state)
|
||
else:
|
||
Game.send_ready()
|
||
|
||
|
||
func _exit_tree() -> void:
|
||
if Game.state_updated.is_connected(_on_state_updated):
|
||
Game.state_updated.disconnect(_on_state_updated)
|
||
if Game.game_over.is_connected(_on_game_over):
|
||
Game.game_over.disconnect(_on_game_over)
|
||
|
||
# ── Environment / skybox ──────────────────────────────────────────────────────
|
||
|
||
func _setup_environment() -> void:
|
||
var env := Environment.new()
|
||
var sky := Sky.new()
|
||
var proc := ProceduralSkyMaterial.new()
|
||
proc.sky_top_color = Color(0.08, 0.10, 0.20)
|
||
proc.sky_horizon_color = Color(0.28, 0.38, 0.55)
|
||
proc.ground_bottom_color = Color(0.05, 0.05, 0.08)
|
||
proc.ground_horizon_color = Color(0.20, 0.28, 0.38)
|
||
sky.sky_material = proc
|
||
env.background_mode = Environment.BG_SKY
|
||
env.sky = sky
|
||
env.ambient_light_source = Environment.AMBIENT_SOURCE_SKY
|
||
env.ambient_light_energy = 0.6
|
||
$WorldEnvironment.environment = env
|
||
|
||
# ── Camera ────────────────────────────────────────────────────────────────────
|
||
|
||
func _apply_camera() -> void:
|
||
_camera_rig.position = _cam_pivot
|
||
_camera_rig.rotation_degrees = Vector3(0.0, _cam_yaw, 0.0)
|
||
_camera.position = Vector3(0.0, 0.0, _cam_distance)
|
||
_camera.rotation_degrees = Vector3(-_cam_pitch, 0.0, 0.0)
|
||
|
||
|
||
func _zoom(delta: float) -> void:
|
||
_cam_distance = clampf(_cam_distance - delta, 4.0, 22.0)
|
||
_apply_camera()
|
||
|
||
# ── Input ─────────────────────────────────────────────────────────────────────
|
||
|
||
func _unhandled_input(event: InputEvent) -> void:
|
||
# ── Mouse ──────────────────────────────────────────────────────────────────
|
||
if event is InputEventMouseButton:
|
||
var mb := event as InputEventMouseButton
|
||
if mb.button_index == MOUSE_BUTTON_WHEEL_UP:
|
||
_zoom(1.5)
|
||
elif mb.button_index == MOUSE_BUTTON_WHEEL_DOWN:
|
||
_zoom(-1.5)
|
||
elif mb.button_index == MOUSE_BUTTON_MIDDLE:
|
||
_pan_active = mb.pressed
|
||
_last_mouse = mb.position
|
||
elif mb.button_index == MOUSE_BUTTON_LEFT:
|
||
if mb.pressed:
|
||
_last_mouse = mb.position
|
||
_drag_active = false
|
||
# Start hold timer for context popup.
|
||
var coord := _mouse_to_coord(mb.position)
|
||
if coord.x >= 0:
|
||
_held_coord = coord
|
||
_hold_timer.start()
|
||
else:
|
||
_hold_timer.stop()
|
||
if not _drag_active:
|
||
_handle_board_click(_mouse_to_coord(mb.position))
|
||
_drag_active = false
|
||
|
||
elif event is InputEventMouseMotion:
|
||
var mm := event as InputEventMouseMotion
|
||
var delta := mm.position - _last_mouse
|
||
_last_mouse = mm.position
|
||
if mm.button_mask & MOUSE_BUTTON_MASK_LEFT:
|
||
if delta.length() > 2.0:
|
||
_drag_active = true
|
||
_hold_timer.stop()
|
||
if _drag_active:
|
||
_cam_yaw -= delta.x * 0.4
|
||
_cam_pitch = clampf(_cam_pitch + delta.y * 0.3, 15.0, 75.0)
|
||
_apply_camera()
|
||
if _pan_active:
|
||
var pan_speed := _cam_distance * 0.0015
|
||
var right := _camera_rig.global_basis.x * -delta.x * pan_speed
|
||
var fwd := Vector3(_camera_rig.global_basis.z.x, 0,
|
||
_camera_rig.global_basis.z.z).normalized()
|
||
_cam_pivot += right + fwd * delta.y * pan_speed
|
||
_cam_pivot.x = clampf(_cam_pivot.x, -6.0, 6.0)
|
||
_cam_pivot.z = clampf(_cam_pivot.z, -6.0, 6.0)
|
||
_apply_camera()
|
||
|
||
# ── Touch ──────────────────────────────────────────────────────────────────
|
||
elif event is InputEventScreenTouch:
|
||
var st := event as InputEventScreenTouch
|
||
if st.pressed:
|
||
_touch_points[st.index] = st.position
|
||
if _touch_points.size() == 2:
|
||
_pinch_dist = _touch_pair_dist()
|
||
_hold_timer.stop()
|
||
else:
|
||
_touch_points.erase(st.index)
|
||
|
||
elif event is InputEventScreenDrag:
|
||
var sd := event as InputEventScreenDrag
|
||
_touch_points[sd.index] = sd.position
|
||
if _touch_points.size() == 1:
|
||
# Single finger → orbit
|
||
_cam_yaw -= sd.relative.x * 0.4
|
||
_cam_pitch = clampf(_cam_pitch + sd.relative.y * 0.3, 15.0, 75.0)
|
||
_apply_camera()
|
||
if sd.relative.length() > 2.0:
|
||
_hold_timer.stop()
|
||
elif _touch_points.size() == 2:
|
||
var keys := _touch_points.keys()
|
||
var pos_a : Vector2 = _touch_points[keys[0]]
|
||
var pos_b : Vector2 = _touch_points[keys[1]]
|
||
var mid := (pos_a + pos_b) * 0.5
|
||
var new_dist := pos_a.distance_to(pos_b)
|
||
# Pinch zoom
|
||
_zoom((new_dist - _pinch_dist) * 0.03)
|
||
_pinch_dist = new_dist
|
||
# Two-finger pan
|
||
var pan_speed := _cam_distance * 0.002
|
||
var center_delta := mid - _last_mouse
|
||
_last_mouse = mid
|
||
var right := _camera_rig.global_basis.x * -center_delta.x * pan_speed
|
||
var fwd := Vector3(_camera_rig.global_basis.z.x, 0,
|
||
_camera_rig.global_basis.z.z).normalized()
|
||
_cam_pivot += right + fwd * center_delta.y * pan_speed
|
||
_cam_pivot.x = clampf(_cam_pivot.x, -6.0, 6.0)
|
||
_cam_pivot.z = clampf(_cam_pivot.z, -6.0, 6.0)
|
||
_apply_camera()
|
||
|
||
|
||
func _touch_pair_dist() -> float:
|
||
var keys := _touch_points.keys()
|
||
if keys.size() < 2:
|
||
return 0.0
|
||
return (_touch_points[keys[0]] as Vector2).distance_to(_touch_points[keys[1]])
|
||
|
||
# ── Coord helpers ─────────────────────────────────────────────────────────────
|
||
|
||
func _coord_to_world(x: int, y: int) -> Vector3:
|
||
var bw := Game.board_width()
|
||
var bh := Game.board_height()
|
||
return Vector3(x - (bw - 1) * 0.5, 0.0, y - (bh - 1) * 0.5)
|
||
|
||
|
||
func _mouse_to_coord(mouse_pos: Vector2) -> Vector2i:
|
||
var space := get_world_3d().direct_space_state
|
||
var from := _camera.project_ray_origin(mouse_pos)
|
||
var to := from + _camera.project_ray_normal(mouse_pos) * 100.0
|
||
var query := PhysicsRayQueryParameters3D.create(from, to)
|
||
var result := space.intersect_ray(query)
|
||
if result.is_empty():
|
||
return Vector2i(-1, -1)
|
||
var body = result.get("collider")
|
||
if body == null:
|
||
return Vector2i(-1, -1)
|
||
# Tile bodies store coord in metadata.
|
||
if body.has_meta("tile_coord"):
|
||
return body.get_meta("tile_coord") as Vector2i
|
||
# Unit bodies store their unit ID.
|
||
if body.has_meta("unit_id"):
|
||
return Vector2i(-2, -2) # sentinel: hit a unit, not a tile
|
||
return Vector2i(-1, -1)
|
||
|
||
|
||
func _mouse_to_unit_id(mouse_pos: Vector2) -> String:
|
||
var space := get_world_3d().direct_space_state
|
||
var from := _camera.project_ray_origin(mouse_pos)
|
||
var to := from + _camera.project_ray_normal(mouse_pos) * 100.0
|
||
var query := PhysicsRayQueryParameters3D.create(from, to)
|
||
var result := space.intersect_ray(query)
|
||
if result.is_empty():
|
||
return ""
|
||
var body = result.get("collider")
|
||
if body != null and body.has_meta("unit_id"):
|
||
return str(body.get_meta("unit_id"))
|
||
return ""
|
||
|
||
# ── Board building ────────────────────────────────────────────────────────────
|
||
|
||
func _build_board(board: Dictionary) -> void:
|
||
# Clear existing tiles and units.
|
||
for c in _tiles_root.get_children():
|
||
c.queue_free()
|
||
for c in _units_root.get_children():
|
||
c.queue_free()
|
||
for c in _hi_root.get_children():
|
||
c.queue_free()
|
||
_tile_bodies.clear()
|
||
_unit_meshes.clear()
|
||
|
||
for tile: Dictionary in board.get("tiles", []):
|
||
var coord : Dictionary = tile.get("coord", {})
|
||
var x : int = coord.get("x", 0)
|
||
var y : int = coord.get("y", 0)
|
||
var aff : String = tile.get("resolvedAffinity", "Void")
|
||
_add_tile(x, y, aff)
|
||
|
||
|
||
func _add_tile(x: int, y: int, affinity: String) -> void:
|
||
var world_pos := _coord_to_world(x, y)
|
||
|
||
var body := StaticBody3D.new()
|
||
body.position = world_pos + Vector3(0, 0.25, 0)
|
||
body.set_meta("tile_coord", Vector2i(x, y))
|
||
|
||
var shape := CollisionShape3D.new()
|
||
var box := BoxShape3D.new()
|
||
box.size = Vector3(0.98, 0.5, 0.98)
|
||
shape.shape = box
|
||
body.add_child(shape)
|
||
|
||
var mesh_inst := MeshInstance3D.new()
|
||
var box_mesh := BoxMesh.new()
|
||
box_mesh.size = Vector3(0.98, 0.5, 0.98)
|
||
mesh_inst.mesh = box_mesh
|
||
var mat := StandardMaterial3D.new()
|
||
mat.albedo_color = AFFINITY_COLORS.get(affinity, Color(0.2, 0.2, 0.2))
|
||
mat.roughness = 0.85
|
||
mesh_inst.material_override = mat
|
||
body.add_child(mesh_inst)
|
||
|
||
_tiles_root.add_child(body)
|
||
_tile_bodies["%d,%d" % [x, y]] = body
|
||
|
||
|
||
func _update_tile_affinity(x: int, y: int, affinity: String) -> void:
|
||
var key := "%d,%d" % [x, y]
|
||
var body = _tile_bodies.get(key)
|
||
if body == null:
|
||
return
|
||
for child in body.get_children():
|
||
if child is MeshInstance3D:
|
||
var mat := child.material_override as StandardMaterial3D
|
||
if mat:
|
||
mat.albedo_color = AFFINITY_COLORS.get(affinity, Color(0.2, 0.2, 0.2))
|
||
|
||
# ── Unit meshes ───────────────────────────────────────────────────────────────
|
||
|
||
func _rebuild_units(state: Dictionary) -> void:
|
||
for c in _units_root.get_children():
|
||
c.queue_free()
|
||
_unit_meshes.clear()
|
||
|
||
var my_id := Auth.user_id
|
||
var players : Array = state.get("players", [])
|
||
|
||
for player: Dictionary in players:
|
||
var is_mine : bool = str(player.get("accountId", "")) == my_id
|
||
var color : Color = Color(0.3, 0.5, 1.0) if is_mine else Color(1.0, 0.35, 0.35)
|
||
|
||
# ArchSpirit
|
||
var arch : Dictionary = player.get("archSpirit", {})
|
||
if not arch.is_empty() and not (arch.get("position") == null):
|
||
_add_unit_mesh(arch, color, true)
|
||
|
||
# Field units
|
||
for unit: Dictionary in player.get("field", []):
|
||
if not (unit.get("position") == null):
|
||
_add_unit_mesh(unit, color, false)
|
||
|
||
|
||
func _add_unit_mesh(unit: Dictionary, team_color: Color, is_leader: bool) -> void:
|
||
var pos_dict : Dictionary = unit.get("position", {})
|
||
if pos_dict.is_empty():
|
||
return
|
||
var ux : int = pos_dict.get("x", 0)
|
||
var uy : int = pos_dict.get("y", 0)
|
||
var world_pos := _coord_to_world(ux, uy)
|
||
var card_type : String = unit.get("cardType", "Spirit")
|
||
var iid : String = unit.get("instanceId", "")
|
||
|
||
var body := StaticBody3D.new()
|
||
body.set_meta("unit_id", iid)
|
||
|
||
var shape := CollisionShape3D.new()
|
||
var mesh_inst := MeshInstance3D.new()
|
||
var mat := StandardMaterial3D.new()
|
||
mat.roughness = 0.65
|
||
|
||
var y_offset : float
|
||
match card_type:
|
||
"ArchSpirit":
|
||
var cyl := CylinderMesh.new()
|
||
cyl.top_radius = 0.22
|
||
cyl.bottom_radius = 0.22
|
||
cyl.height = 0.8
|
||
mesh_inst.mesh = cyl
|
||
mat.albedo_color = team_color.lightened(0.25)
|
||
mat.emission_enabled = true
|
||
mat.emission = team_color * 0.4
|
||
var cs := CylinderShape3D.new()
|
||
cs.radius = 0.22
|
||
cs.height = 0.8
|
||
shape.shape = cs
|
||
y_offset = 0.5 + 0.4
|
||
|
||
"Ruin":
|
||
var bm := BoxMesh.new()
|
||
bm.size = Vector3(0.25, 1.0, 0.25)
|
||
mesh_inst.mesh = bm
|
||
mat.albedo_color = Color(0.35, 0.32, 0.30)
|
||
var bs := BoxShape3D.new()
|
||
bs.size = Vector3(0.25, 1.0, 0.25)
|
||
shape.shape = bs
|
||
y_offset = 0.5 + 0.5
|
||
|
||
_: # Spirit, SpiritToken, default
|
||
var sm := SphereMesh.new()
|
||
sm.radius = 0.28
|
||
sm.height = 0.56
|
||
mesh_inst.mesh = sm
|
||
mat.albedo_color = team_color
|
||
var ss := SphereShape3D.new()
|
||
ss.radius = 0.28
|
||
shape.shape = ss
|
||
y_offset = 0.5 + 0.28
|
||
|
||
mesh_inst.material_override = mat
|
||
body.add_child(shape)
|
||
body.add_child(mesh_inst)
|
||
body.position = world_pos + Vector3(0, y_offset, 0)
|
||
|
||
# Exhaust indicator: dim the material.
|
||
if unit.get("isExhausted", false):
|
||
mat.albedo_color = mat.albedo_color.darkened(0.45)
|
||
|
||
_units_root.add_child(body)
|
||
_unit_meshes[iid] = mesh_inst
|
||
|
||
# ── Highlights ────────────────────────────────────────────────────────────────
|
||
|
||
func _clear_highlights() -> void:
|
||
for c in _hi_root.get_children():
|
||
c.queue_free()
|
||
|
||
|
||
func _add_highlight(x: int, y: int, color: Color) -> void:
|
||
var world_pos := _coord_to_world(x, y)
|
||
var mi := MeshInstance3D.new()
|
||
var bm := BoxMesh.new()
|
||
bm.size = Vector3(0.98, 0.02, 0.98)
|
||
mi.mesh = bm
|
||
var mat := StandardMaterial3D.new()
|
||
mat.albedo_color = color
|
||
mat.transparency = BaseMaterial3D.TRANSPARENCY_ALPHA
|
||
mat.flags_transparent = true
|
||
mi.material_override = mat
|
||
mi.position = world_pos + Vector3(0, 0.52, 0)
|
||
_hi_root.add_child(mi)
|
||
|
||
|
||
func _highlight_tiles(coords: Array, color: Color) -> void:
|
||
for coord in coords:
|
||
if coord is Vector2i:
|
||
_add_highlight(coord.x, coord.y, color)
|
||
elif coord is Dictionary:
|
||
_add_highlight(coord.get("x", 0), coord.get("y", 0), color)
|
||
|
||
|
||
# Colors for highlight modes
|
||
const HI_GREEN := Color(0.20, 0.90, 0.20, 0.40)
|
||
const HI_RED := Color(1.00, 0.25, 0.25, 0.50)
|
||
const HI_BLUE := Color(0.20, 0.50, 1.00, 0.40)
|
||
const HI_YELLOW := Color(1.00, 0.85, 0.10, 0.50)
|
||
|
||
# ── Valid target computation ──────────────────────────────────────────────────
|
||
|
||
## Returns Array[Vector2i] of tiles where a hand card can be played.
|
||
func _valid_play_tiles(card: Dictionary) -> Array:
|
||
var results : Array = []
|
||
var card_type : String = card.get("cardType", "")
|
||
var my_player : Dictionary = Game.get_my_player()
|
||
var board : Dictionary = Game.get_board()
|
||
var bw : int = board.get("width", 8)
|
||
var bh : int = board.get("height", 8)
|
||
var arch : Dictionary = my_player.get("archSpirit", {})
|
||
var arch_pos : Dictionary = arch.get("position", {})
|
||
|
||
match card_type:
|
||
"Spirit", "Ruin":
|
||
var min_r : int = card.get("minChannelRange", 1)
|
||
var max_r : int = card.get("maxChannelRange", 2)
|
||
if arch_pos.is_empty():
|
||
return results
|
||
var ax : int = arch_pos.get("x", 0)
|
||
var ay : int = arch_pos.get("y", 0)
|
||
for tx in range(bw):
|
||
for ty in range(bh):
|
||
var dist := abs(tx - ax) + abs(ty - ay)
|
||
if dist < min_r or dist > max_r:
|
||
continue
|
||
# Check tile is empty.
|
||
var tile := Game.get_tile(tx, ty)
|
||
if tile.get("occupantId", "") != "":
|
||
continue
|
||
results.append(Vector2i(tx, ty))
|
||
|
||
"Terramorpha":
|
||
# Any tile is valid for the first target.
|
||
for tx in range(bw):
|
||
for ty in range(bh):
|
||
results.append(Vector2i(tx, ty))
|
||
|
||
"Repentance", "Instinct":
|
||
# No tile target needed — play directly (return empty, handle via click without target).
|
||
pass
|
||
|
||
return results
|
||
|
||
|
||
## Returns Array[Vector2i] of tiles the selected unit can move to.
|
||
func _valid_move_tiles(unit: Dictionary) -> Array:
|
||
var results : Array = []
|
||
var pos : Dictionary = unit.get("position", {})
|
||
if pos.is_empty():
|
||
return results
|
||
var ux : int = pos.get("x", 0)
|
||
var uy : int = pos.get("y", 0)
|
||
var mp : int = unit.get("currentMp", 0)
|
||
var bw : int = Game.board_width()
|
||
var bh : int = Game.board_height()
|
||
if mp <= 0 or unit.get("isExhausted", false):
|
||
return results
|
||
# BFS up to mp range (Manhattan, ignoring pathfinding for display).
|
||
for tx in range(bw):
|
||
for ty in range(bh):
|
||
var dist := abs(tx - ux) + abs(ty - uy)
|
||
if dist == 0 or dist > mp:
|
||
continue
|
||
var tile := Game.get_tile(tx, ty)
|
||
if tile.get("occupantId", "") != "":
|
||
continue
|
||
results.append(Vector2i(tx, ty))
|
||
return results
|
||
|
||
|
||
## Returns Array[String] (unit InstanceIds) this unit can attack.
|
||
func _valid_attack_targets(unit: Dictionary) -> Array:
|
||
var results : Array = []
|
||
var pos : Dictionary = unit.get("position", {})
|
||
if pos.is_empty():
|
||
return results
|
||
var ux : int = pos.get("x", 0)
|
||
var uy : int = pos.get("y", 0)
|
||
var min_r : int = unit.get("minAttackRange", 1)
|
||
var max_r : int = unit.get("maxAttackRange", 1)
|
||
if unit.get("isExhausted", false) and unit.get("extraAttacksRemaining", 0) == 0:
|
||
return results
|
||
|
||
# Check opponent field + arch.
|
||
var opp := Game.get_opponent()
|
||
var targets_to_check : Array = []
|
||
targets_to_check.append_array(opp.get("field", []))
|
||
var opp_arch : Dictionary = opp.get("archSpirit", {})
|
||
if not opp_arch.is_empty():
|
||
targets_to_check.append(opp_arch)
|
||
|
||
for t: Dictionary in targets_to_check:
|
||
if not t.get("canBeAttacked", true):
|
||
continue
|
||
var tp : Dictionary = t.get("position", {})
|
||
if tp.is_empty():
|
||
continue
|
||
var dist := abs(tp.get("x", 0) - ux) + abs(tp.get("y", 0) - uy)
|
||
if dist >= min_r and dist <= max_r:
|
||
results.append(t.get("instanceId", ""))
|
||
return results
|
||
|
||
|
||
## Returns Array[Vector2i] of placement tiles for ArchSpirit.
|
||
func _valid_placement_tiles() -> Array:
|
||
var results : Array = []
|
||
var my_id := Auth.user_id
|
||
var state := Game.state
|
||
var p1_id : String = str(state.get("playerOneAccountId", ""))
|
||
var bw : int = Game.board_width()
|
||
var bh : int = Game.board_height()
|
||
var row : int = 0 if my_id == p1_id else bh - 1
|
||
|
||
for tx in range(bw):
|
||
var tile := Game.get_tile(tx, row)
|
||
if tile.get("occupantId", "") == "":
|
||
results.append(Vector2i(tx, row))
|
||
return results
|
||
|
||
# ── Click handling ────────────────────────────────────────────────────────────
|
||
|
||
func _handle_board_click(coord: Vector2i) -> void:
|
||
# Check if a unit was clicked instead.
|
||
var unit_id := _mouse_to_unit_id(_last_mouse)
|
||
|
||
match _mode:
|
||
Mode.NONE:
|
||
if not unit_id.is_empty() and Game.is_my_turn():
|
||
_select_unit(unit_id)
|
||
elif coord.x >= 0:
|
||
# Deselect.
|
||
_set_mode(Mode.NONE)
|
||
|
||
Mode.CARD_FROM_HAND:
|
||
var card := _find_hand_card(_sel_card_id)
|
||
if card.is_empty():
|
||
_set_mode(Mode.NONE)
|
||
return
|
||
var card_type : String = card.get("cardType", "")
|
||
if card_type in ["Repentance", "Instinct"]:
|
||
# No target needed — play immediately.
|
||
Game.send_play_card(_sel_card_id, 0, 0)
|
||
_set_mode(Mode.NONE)
|
||
return
|
||
if coord.x < 0:
|
||
_set_mode(Mode.NONE)
|
||
return
|
||
Game.send_play_card(_sel_card_id, coord.x, coord.y)
|
||
_set_mode(Mode.NONE)
|
||
|
||
Mode.UNIT_SELECTED:
|
||
if not unit_id.is_empty():
|
||
# Clicking another unit — attack if it's an enemy, or switch selection.
|
||
var target_unit := Game.get_unit_by_id(unit_id)
|
||
var is_enemy := str(target_unit.get("ownerAccountId", "")) != Auth.user_id
|
||
if is_enemy:
|
||
Game.send_attack(_sel_unit_id, unit_id)
|
||
_set_mode(Mode.NONE)
|
||
else:
|
||
_select_unit(unit_id)
|
||
elif coord.x >= 0:
|
||
# Move to tile.
|
||
Game.send_move_unit(_sel_unit_id, coord.x, coord.y)
|
||
_set_mode(Mode.NONE)
|
||
|
||
Mode.PLACEMENT:
|
||
if coord.x >= 0:
|
||
Game.send_place_arch_spirit(coord.x, coord.y)
|
||
_set_mode(Mode.NONE)
|
||
|
||
|
||
func _select_unit(unit_id: String) -> void:
|
||
var unit := Game.get_unit_by_id(unit_id)
|
||
if unit.is_empty():
|
||
return
|
||
_sel_unit_id = unit_id
|
||
_set_mode(Mode.UNIT_SELECTED)
|
||
|
||
_clear_highlights()
|
||
# Highlight selected unit's tile.
|
||
var upos : Dictionary = unit.get("position", {})
|
||
if not upos.is_empty():
|
||
_add_highlight(upos.get("x", 0), upos.get("y", 0), HI_YELLOW)
|
||
|
||
# Movement range (green).
|
||
_highlight_tiles(_valid_move_tiles(unit), HI_GREEN)
|
||
|
||
# Attack targets: highlight their tiles red.
|
||
var attack_ids := _valid_attack_targets(unit)
|
||
for tid: String in attack_ids:
|
||
var tu := Game.get_unit_by_id(tid)
|
||
var tp : Dictionary = tu.get("position", {})
|
||
if not tp.is_empty():
|
||
_add_highlight(tp.get("x", 0), tp.get("y", 0), HI_RED)
|
||
|
||
|
||
func _set_mode(new_mode: Mode) -> void:
|
||
_mode = new_mode
|
||
_sel_card_id = ""
|
||
if new_mode == Mode.NONE or new_mode == Mode.PLACEMENT:
|
||
_sel_unit_id = ""
|
||
if new_mode == Mode.NONE:
|
||
_clear_highlights()
|
||
_refresh_hand_selection()
|
||
|
||
# ── Hold timer → context popup ────────────────────────────────────────────────
|
||
|
||
func _on_hold_timer_fired() -> void:
|
||
if _held_coord.x < 0:
|
||
return
|
||
var tile := Game.get_tile(_held_coord.x, _held_coord.y)
|
||
if tile.is_empty():
|
||
return
|
||
var aff : String = tile.get("resolvedAffinity", "Void")
|
||
_ctx_tile_lbl.text = "Tile (%d,%d) — %s" % [_held_coord.x, _held_coord.y, aff]
|
||
|
||
var occupant_id : String = tile.get("occupantId", "")
|
||
if occupant_id.is_empty():
|
||
_ctx_unit_lbl.text = ""
|
||
_ctx_abil_lbl.text = ""
|
||
else:
|
||
var unit := Game.get_unit_by_id(occupant_id)
|
||
var name : String = unit.get("name", "Unknown")
|
||
var hp : int = unit.get("currentHealth", 0)
|
||
var max_hp : int = unit.get("maxHealth", 0)
|
||
var def : int = unit.get("currentDefense", 0)
|
||
var max_def: int = unit.get("maxDefense", 0)
|
||
var dmg : int = unit.get("damage", 0)
|
||
_ctx_unit_lbl.text = "%s HP %d/%d DEF %d/%d ATK %d" % [
|
||
name, hp, max_hp, def, max_def, dmg]
|
||
|
||
# Parse ability names from JSON.
|
||
var abil_str : String = ""
|
||
var abil_json : String = unit.get("abilitiesJson", "")
|
||
if not abil_json.is_empty():
|
||
var parsed = JSON.parse_string(abil_json)
|
||
if parsed is Array:
|
||
for ab in parsed:
|
||
if ab is Dictionary:
|
||
abil_str += "• %s\n" % str(ab.get("name", ""))
|
||
_ctx_abil_lbl.text = abil_str.strip_edges()
|
||
|
||
_ctx_popup.visible = true
|
||
|
||
# ── Discard viewer ────────────────────────────────────────────────────────────
|
||
|
||
func _show_own_discard() -> void:
|
||
var discard : Array = Game.get_my_player().get("discard", [])
|
||
_show_discard_for("Your Discard", discard)
|
||
|
||
|
||
func _show_opp_discard() -> void:
|
||
var discard : Array = Game.get_opponent().get("discard", [])
|
||
_show_discard_for("Opponent Discard", discard)
|
||
|
||
|
||
func _show_discard_for(title: String, cards: Array) -> void:
|
||
_dv_title.text = "%s (%d)" % [title, cards.size()]
|
||
for c in _dv_list.get_children():
|
||
c.queue_free()
|
||
if cards.is_empty():
|
||
var lbl := Label.new()
|
||
lbl.text = "(empty)"
|
||
lbl.add_theme_color_override("font_color", Color(0.55, 0.55, 0.6))
|
||
_dv_list.add_child(lbl)
|
||
else:
|
||
for card: Dictionary in cards:
|
||
var row := _make_discard_row(card)
|
||
_dv_list.add_child(row)
|
||
_dv_overlay.visible = true
|
||
|
||
|
||
func _make_discard_row(card: Dictionary) -> Control:
|
||
var hbox := HBoxContainer.new()
|
||
hbox.add_theme_constant_override("separation", 8)
|
||
hbox.custom_minimum_size = Vector2(0, 32)
|
||
|
||
var type_lbl := Label.new()
|
||
type_lbl.text = "[%s]" % card.get("cardType", "?")
|
||
type_lbl.add_theme_font_size_override("font_size", 11)
|
||
type_lbl.add_theme_color_override("font_color", Color(0.6, 0.6, 0.7))
|
||
type_lbl.custom_minimum_size = Vector2(90, 0)
|
||
type_lbl.size_flags_vertical = Control.SIZE_SHRINK_CENTER
|
||
hbox.add_child(type_lbl)
|
||
|
||
var name_lbl := Label.new()
|
||
name_lbl.text = card.get("name", "Unknown")
|
||
name_lbl.add_theme_font_size_override("font_size", 13)
|
||
name_lbl.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
||
name_lbl.size_flags_vertical = Control.SIZE_SHRINK_CENTER
|
||
hbox.add_child(name_lbl)
|
||
|
||
var aff_lbl := Label.new()
|
||
aff_lbl.text = card.get("affinity", "")
|
||
aff_lbl.add_theme_font_size_override("font_size", 11)
|
||
aff_lbl.add_theme_color_override("font_color", Color(0.6, 0.7, 0.85))
|
||
aff_lbl.size_flags_vertical = Control.SIZE_SHRINK_CENTER
|
||
hbox.add_child(aff_lbl)
|
||
|
||
return hbox
|
||
|
||
# ── Hand display ──────────────────────────────────────────────────────────────
|
||
|
||
func _rebuild_hand(hand: Array) -> void:
|
||
for c in _hand_cards.get_children():
|
||
c.queue_free()
|
||
for card: Dictionary in hand:
|
||
var widget := _make_hand_card(card)
|
||
_hand_cards.add_child(widget)
|
||
|
||
|
||
func _make_hand_card(card: Dictionary) -> Control:
|
||
var iid : String = card.get("instanceId", "")
|
||
var name_str : String = card.get("name", "?")
|
||
var sp_cost : int = card.get("spCost", 0)
|
||
var card_type : String = card.get("cardType", "")
|
||
var affinity : String = card.get("affinity", "Void")
|
||
var aff_color : Color = AFFINITY_COLORS.get(affinity, Color(0.2, 0.2, 0.2))
|
||
|
||
# Root is a PanelContainer so we can set background + border.
|
||
var panel := PanelContainer.new()
|
||
panel.custom_minimum_size = Vector2(100, 155)
|
||
panel.set_meta("card_instance_id", iid)
|
||
|
||
var style := StyleBoxFlat.new()
|
||
style.bg_color = aff_color.darkened(0.35)
|
||
style.corner_radius_top_left = 6
|
||
style.corner_radius_top_right = 6
|
||
style.corner_radius_bottom_left = 6
|
||
style.corner_radius_bottom_right = 6
|
||
style.border_width_left = 2
|
||
style.border_width_right = 2
|
||
style.border_width_top = 2
|
||
style.border_width_bottom = 2
|
||
style.border_color = aff_color.lightened(0.2)
|
||
panel.add_theme_stylebox_override("panel", style)
|
||
|
||
var vbox := VBoxContainer.new()
|
||
vbox.add_theme_constant_override("separation", 2)
|
||
vbox.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
|
||
|
||
# Cost badge
|
||
var cost_lbl := Label.new()
|
||
cost_lbl.text = str(sp_cost) + " SP"
|
||
cost_lbl.add_theme_font_size_override("font_size", 10)
|
||
cost_lbl.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
|
||
cost_lbl.add_theme_color_override("font_color", Color(0.9, 0.85, 0.5))
|
||
vbox.add_child(cost_lbl)
|
||
|
||
# Art placeholder — colored rect.
|
||
var art := ColorRect.new()
|
||
art.color = aff_color.lightened(0.15)
|
||
art.custom_minimum_size = Vector2(0, 72)
|
||
art.size_flags_vertical = Control.SIZE_EXPAND_FILL
|
||
vbox.add_child(art)
|
||
|
||
# Card name
|
||
var name_lbl := Label.new()
|
||
name_lbl.text = name_str
|
||
name_lbl.add_theme_font_size_override("font_size", 10)
|
||
name_lbl.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
|
||
name_lbl.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
|
||
vbox.add_child(name_lbl)
|
||
|
||
# Type
|
||
var type_lbl := Label.new()
|
||
type_lbl.text = card_type
|
||
type_lbl.add_theme_font_size_override("font_size", 9)
|
||
type_lbl.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
|
||
type_lbl.add_theme_color_override("font_color", Color(0.65, 0.65, 0.72))
|
||
vbox.add_child(type_lbl)
|
||
|
||
# Key stat
|
||
var stat_str := ""
|
||
if card.get("damage") != null:
|
||
stat_str = "ATK %d" % int(card.get("damage"))
|
||
elif card.get("maxHealth") != null:
|
||
stat_str = "HP %d" % int(card.get("maxHealth"))
|
||
if not stat_str.is_empty():
|
||
var stat_lbl := Label.new()
|
||
stat_lbl.text = stat_str
|
||
stat_lbl.add_theme_font_size_override("font_size", 10)
|
||
stat_lbl.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
|
||
stat_lbl.add_theme_color_override("font_color", Color(0.8, 0.9, 1.0))
|
||
vbox.add_child(stat_lbl)
|
||
|
||
panel.add_child(vbox)
|
||
|
||
# Hover behaviour — scale up + move upward.
|
||
panel.mouse_entered.connect(func() -> void:
|
||
panel.pivot_offset = panel.size * 0.5
|
||
var tw := create_tween()
|
||
tw.tween_property(panel, "scale", Vector2(1.18, 1.18), 0.12)
|
||
var orig_y : float = panel.position.y
|
||
tw.parallel().tween_property(panel, "position:y", orig_y - 28.0, 0.12)
|
||
panel.z_index = 20)
|
||
|
||
panel.mouse_exited.connect(func() -> void:
|
||
var tw := create_tween()
|
||
tw.tween_property(panel, "scale", Vector2(1.0, 1.0), 0.10)
|
||
tw.parallel().tween_property(panel, "position:y",
|
||
_hand_cards.position.y, 0.10)
|
||
panel.z_index = 0)
|
||
|
||
# Click → select / deselect card.
|
||
var gui_input_callable := func(event: InputEvent) -> void:
|
||
if event is InputEventMouseButton:
|
||
var mb := event as InputEventMouseButton
|
||
if mb.button_index == MOUSE_BUTTON_LEFT and mb.pressed:
|
||
_on_hand_card_clicked(iid, panel)
|
||
panel.gui_input.connect(gui_input_callable)
|
||
|
||
return panel
|
||
|
||
|
||
func _on_hand_card_clicked(iid: String, panel: PanelContainer) -> void:
|
||
if not Game.is_my_turn():
|
||
return
|
||
if _mode == Mode.CARD_FROM_HAND and _sel_card_id == iid:
|
||
# Deselect.
|
||
_set_mode(Mode.NONE)
|
||
return
|
||
var card := _find_hand_card(iid)
|
||
if card.is_empty():
|
||
return
|
||
_sel_card_id = iid
|
||
_set_mode(Mode.CARD_FROM_HAND)
|
||
|
||
# Show valid play targets.
|
||
var valid := _valid_play_tiles(card)
|
||
_clear_highlights()
|
||
_highlight_tiles(valid, HI_GREEN)
|
||
|
||
|
||
func _refresh_hand_selection() -> void:
|
||
for widget in _hand_cards.get_children():
|
||
if not widget.has_meta("card_instance_id"):
|
||
continue
|
||
var iid : String = str(widget.get_meta("card_instance_id"))
|
||
var selected : bool = (_mode == Mode.CARD_FROM_HAND and _sel_card_id == iid)
|
||
# Adjust border color to show selection.
|
||
var style = widget.get_theme_stylebox("panel") as StyleBoxFlat
|
||
if style:
|
||
style.border_color = Color(1.0, 0.9, 0.2) if selected else \
|
||
AFFINITY_COLORS.get(
|
||
_find_hand_card(iid).get("affinity", "Void"),
|
||
Color(0.3, 0.3, 0.3)).lightened(0.2)
|
||
|
||
|
||
func _find_hand_card(iid: String) -> Dictionary:
|
||
for card: Dictionary in Game.get_hand():
|
||
if card.get("instanceId", "") == iid:
|
||
return card
|
||
return {}
|
||
|
||
# ── HUD update ────────────────────────────────────────────────────────────────
|
||
|
||
func _update_hud(state: Dictionary) -> void:
|
||
var phase : String = state.get("phase", "")
|
||
var turn : int = state.get("turnNumber", 1)
|
||
var active_id : String = str(state.get("activePlayerAccountId", ""))
|
||
var is_mine : bool = (active_id == Auth.user_id)
|
||
|
||
_phase_lbl.text = "Phase: %s" % phase
|
||
_turn_lbl.text = "Turn %d — %s" % [turn, "Your turn" if is_mine else "Opponent's turn"]
|
||
_turn_lbl.modulate = Color(0.4, 1.0, 0.5) if is_mine else Color(1.0, 0.6, 0.6)
|
||
|
||
var my : Dictionary = Game.get_my_player()
|
||
var opp : Dictionary = Game.get_opponent()
|
||
|
||
_sp_lbl.text = "SP: %d" % int(my.get("currentSp", 0))
|
||
_st_lbl.text = "ST: %d" % int(my.get("currentSt", 0))
|
||
_deck_lbl.text = "Deck: %d" % int(my.get("deckCount", 0))
|
||
|
||
_opp_hand_lbl.text = "Hand: %d" % int(opp.get("handCount", 0))
|
||
_opp_deck_lbl.text = "Deck: %d" % int(opp.get("deckCount", 0))
|
||
|
||
# Phase-based button visibility.
|
||
_draw_btn.visible = (phase == "Draw" and is_mine)
|
||
_end_turn_btn.visible = (phase == "Main" and is_mine)
|
||
_end_turn_btn.disabled = not is_mine
|
||
|
||
# Placement hint.
|
||
if phase == "Placement" and Game.get_my_player().get("archSpirit", {}).get("position") == null:
|
||
_phase_lbl.text = "Phase: Placement — place your ArchSpirit"
|
||
_set_mode(Mode.PLACEMENT)
|
||
_clear_highlights()
|
||
_highlight_tiles(_valid_placement_tiles(), HI_GREEN)
|
||
|
||
|
||
func _update_opponent_name() -> void:
|
||
var opp := Game.get_opponent()
|
||
# Display name isn't in PlayerStateDto directly; fall back to account ID trimmed.
|
||
var aid : String = str(opp.get("accountId", "Opponent"))
|
||
_opp_name_lbl.text = aid.substr(0, 8) + "…"
|
||
|
||
# ── Game over ─────────────────────────────────────────────────────────────────
|
||
|
||
func _on_game_over(winner_id: String, win_condition: String,
|
||
winner_delta: int, loser_delta: int) -> void:
|
||
var i_won : bool = (winner_id == Auth.user_id)
|
||
_go_result_lbl.text = "Victory!" if i_won else "Defeat"
|
||
_go_result_lbl.modulate = Color(0.4, 1.0, 0.5) if i_won else Color(1.0, 0.4, 0.4)
|
||
|
||
var my_delta : int = winner_delta if i_won else loser_delta
|
||
var sign_str : String = "+" if my_delta >= 0 else ""
|
||
_go_rank_lbl.text = "Win by %s | Rank %s%d" % [win_condition, sign_str, my_delta]
|
||
_go_overlay.visible = true
|
||
|
||
# ── State → scene ─────────────────────────────────────────────────────────────
|
||
|
||
func _on_state_updated(state: Dictionary) -> void:
|
||
var board : Dictionary = state.get("board", {})
|
||
var phase : String = state.get("phase", "")
|
||
|
||
# Rebuild board only if needed (first time or board changes).
|
||
if _tile_bodies.is_empty() and not board.is_empty():
|
||
_build_board(board)
|
||
elif not board.is_empty():
|
||
# Update tile affinities in case Terramorpha changed them.
|
||
for tile: Dictionary in board.get("tiles", []):
|
||
var c : Dictionary = tile.get("coord", {})
|
||
var aff : String = tile.get("resolvedAffinity", "Void")
|
||
_update_tile_affinity(c.get("x", 0), c.get("y", 0), aff)
|
||
|
||
_rebuild_units(state)
|
||
_rebuild_hand(Game.get_hand())
|
||
_update_hud(state)
|
||
_update_opponent_name()
|
||
|
||
# If we were in selection, clear it (state changed, re-evaluate).
|
||
if _mode == Mode.NONE or _mode == Mode.PLACEMENT:
|
||
pass
|
||
else:
|
||
_set_mode(Mode.NONE)
|
||
|
||
# Auto-draw on Draw phase.
|
||
if phase == "Draw" and Game.is_my_turn():
|
||
await get_tree().create_timer(0.3).timeout
|
||
Game.send_draw()
|
||
|
||
# ── Action buttons ────────────────────────────────────────────────────────────
|
||
|
||
func _on_draw_pressed() -> void:
|
||
Game.send_draw()
|
||
|
||
|
||
func _on_end_turn_pressed() -> void:
|
||
_set_mode(Mode.NONE)
|
||
Game.send_end_turn()
|
||
|
||
|
||
func _on_concede_pressed() -> void:
|
||
var dlg := ConfirmationDialog.new()
|
||
dlg.title = "Concede"
|
||
dlg.dialog_text = "Are you sure you want to concede?"
|
||
dlg.ok_button_text = "Concede"
|
||
dlg.cancel_button_text = "Cancel"
|
||
dlg.min_size = Vector2(280, 100)
|
||
add_child(dlg)
|
||
dlg.popup_centered()
|
||
dlg.confirmed.connect(func() -> void:
|
||
Game.send_concede()
|
||
dlg.queue_free())
|
||
dlg.canceled.connect(func() -> void: dlg.queue_free())
|