From 112d471533f7940b14851c4d61ca0eb5709254aa Mon Sep 17 00:00:00 2001 From: dingo Date: Mon, 16 Mar 2026 16:04:30 -0400 Subject: [PATCH] Fixed Stalagmites --- .claude/plan.md | 125 +++++++ .../game/GameScreen.tscn210116835248.tmp | 315 ++++++++++++++++++ .../origin-client/scenes/game/game_screen.gd | 37 +- 3 files changed, 466 insertions(+), 11 deletions(-) create mode 100644 .claude/plan.md create mode 100644 Client/origin-client/scenes/game/GameScreen.tscn210116835248.tmp diff --git a/.claude/plan.md b/.claude/plan.md new file mode 100644 index 0000000..cc5eb42 --- /dev/null +++ b/.claude/plan.md @@ -0,0 +1,125 @@ +# Plan: Deferred Rebuild + Movement Path Animation + +## Goal +1. Let attack/damage/death animations play to completion before `_rebuild_units` destroys the nodes. +2. Move units along a BFS path (avoiding occupied tiles) while playing the Moving animation, before the rebuild snaps them to their new positions. +3. Hand and HUD update immediately; only the board (unit nodes) is delayed. + +--- + +## New constants (near ANIM_ALIASES) +```gdscript +const MOVE_STEP_SECS : float = 0.28 # seconds per grid step during move anim +const POST_ANIM_SECS : float = 1.2 # seconds to hold after one-shot anims +``` + +## New state variables (near _prev_unit_hp) +```gdscript +var _prev_unit_pos : Dictionary = {} # instanceId → Vector2i +var _move_anims : Dictionary = {} # instanceId → Tween +var _pending_rebuild : Dictionary = {} # latest state awaiting delayed rebuild +var _rebuild_tween : Tween = null +``` + +--- + +## New functions + +### `_update_prev_state(state)` — replaces HP tracking in _rebuild_units +Called immediately in `_on_state_updated` (before the delayed rebuild) so the +next state update has correct baselines for both HP and position. + +```gdscript +func _update_prev_state(state: Dictionary) -> void: + _prev_unit_hp.clear() + _prev_unit_pos.clear() + for player: Dictionary in state.get("players", []): + var arch := player.get("archSpirit", {}) + for unit: Dictionary in ([arch] + player.get("field", [])): + if unit.is_empty(): continue + var iid := str(unit.get("instanceId", "")) + if iid.is_empty(): continue + _prev_unit_hp[iid] = int(unit.get("currentHealth", 0)) + var pd = unit.get("position") + if pd is Dictionary and not pd.is_empty(): + _prev_unit_pos[iid] = Vector2i(pd.get("x",0), pd.get("y",0)) +``` + +### `_bfs_path(from, to, blocked)` → `Array[Vector2i]` +4-directional BFS. `blocked` is `Dictionary[Vector2i, true]` of impassable tiles; +the destination is always treated as passable. +Returns the list of steps NOT including `from`, INCLUDING `to`. +Falls back to `[to]` if no path found (shouldn't happen on a connected grid). + +### `_animate_unit_move(iid, from_coord, to_coord, blocked)` → `float` +1. Look up `_unit_bodies[iid]`; guard with `is_instance_valid`. +2. BFS path from `from_coord` to `to_coord` using `blocked`. +3. Kill any existing tween in `_move_anims[iid]`. +4. Call `_play_unit_anim(iid, "moving", true)` (looping). +5. Create Tween (EASE_IN_OUT / TRANS_SINE). For each step in path, tween + `body.position` to `Vector3(wp.x, body.position.y, wp.z)` over `MOVE_STEP_SECS`. + (Keep body's current Y so placeholder and model heights both work.) +6. Chain a callback: erase from `_move_anims`, call `_play_unit_anim(iid,"idle",true)`. +7. Store tween in `_move_anims[iid]`. +8. Return `path.size() * MOVE_STEP_SECS`. + +### `_play_move_animations(state)` → `float` +Called before rebuild, using `_prev_unit_pos` as the "before" snapshot. +- Build `blocked: Dictionary` from all values in `_prev_unit_pos`. +- For each unit in `state` that has a position change vs `_prev_unit_pos`: + - Erase the mover's old coord from `blocked` (they've left it). + - Call `_animate_unit_move(iid, old, new, blocked)`. + - Track max duration. +- Returns max duration across all movers. + +### `_schedule_rebuild(state, delay)` → `void` +- Store `state` in `_pending_rebuild`. +- Kill + null `_rebuild_tween` if one exists. +- If `delay <= 0`: call `_rebuild_units(state)` synchronously and return. +- Else: `_rebuild_tween = create_tween()`, `.tween_callback(λ).set_delay(delay)`. + The lambda: `_rebuild_tween = null; _rebuild_units(_pending_rebuild)`. + +--- + +## Modified functions + +### `_play_damage_animations` — returns `bool` +Change return type from `void` to `bool`. +Return `true` if any `_play_unit_anim` call was made (HP drop or missing unit). + +### `_rebuild_units` +- **Remove** the HP tracking block at the bottom (moved to `_update_prev_state`). +- **Remove** `_prev_unit_hp.clear()` — no longer owned here. +- **Add** at the top: kill all tweens in `_move_anims`, then `.clear()` it. + +### `_add_unit_mesh` — model load callback +In the model load callback, before `b.position = wp + Vector3(0,0.5,0)`, check: +```gdscript +if not _move_anims.has(iid): # only reset pos if not currently moving + b.position = wp + Vector3(0, 0.5, 0) +``` +This prevents a loaded model from snapping back to the original tile mid-tween. + +### `_on_state_updated` — new structure +``` +1. print / _rebuild_ownership / camera / board [unchanged, immediate] +2. _play_move_animations(state) → move_dur +3. _play_damage_animations(state) → dmg_played +4. [immediate UI] _rebuild_hand(Game.get_hand()), _update_hud(state), print +5. _update_prev_state(state) [update tracking now, before delayed rebuild] +6. delay = move_dur; if dmg_played: delay = maxf(delay, POST_ANIM_SECS) +7. _schedule_rebuild(state, delay) +8. [selection restoration match block — immediate, same as before] +9. [auto-draw await — same as before] +``` + +--- + +## What this achieves +- Attack animation fires on click → plays for ~1-2s → state update arrives → + damage anim plays → 1.2s delay → rebuild. Total: seamless. +- Unit moves → move animation plays cell-by-cell along BFS path → + rebuild fires after path completes. Hand/HUD update immediately. +- If a new state arrives during the delay (rapid opponent actions), the + pending rebuild is replaced with the latest state; old animations may + be cut short, which is acceptable. diff --git a/Client/origin-client/scenes/game/GameScreen.tscn210116835248.tmp b/Client/origin-client/scenes/game/GameScreen.tscn210116835248.tmp new file mode 100644 index 0000000..3ab35cf --- /dev/null +++ b/Client/origin-client/scenes/game/GameScreen.tscn210116835248.tmp @@ -0,0 +1,315 @@ +[gd_scene load_steps=3 format=3 uid="uid://b17evi1hw4dqu"] + +[ext_resource type="Script" uid="uid://b8x84lb58h6gx" path="res://scenes/game/game_screen.gd" id="1_gscr"] +[ext_resource type="PackedScene" path="res://scenes/game/InGameSettings.tscn" id="2_igs"] + +[node name="GameScreen" type="Node3D"] +script = ExtResource("1_gscr") + +[node name="WorldEnvironment" type="WorldEnvironment" parent="."] + +[node name="DirectionalLight3D" type="DirectionalLight3D" parent="."] +transform = Transform3D(0.707107, -0.5, 0.5, 0, 0.707107, 0.707107, -0.707107, -0.5, 0.5, 0, 8, 0) +light_energy = 1.2 +shadow_enabled = true + +[node name="CameraRig" type="Node3D" parent="."] + +[node name="Camera3D" type="Camera3D" parent="CameraRig"] +transform = Transform3D(1, 0, 0, 0, 0.766044, 0.642788, 0, -0.642788, 0.766044, 0, 10, 9) + +[node name="BoardRoot" type="Node3D" parent="."] + +[node name="TilesRoot" type="Node3D" parent="BoardRoot"] + +[node name="UnitsRoot" type="Node3D" parent="BoardRoot"] + +[node name="HighlightsRoot" type="Node3D" parent="BoardRoot"] + +[node name="HUD" type="CanvasLayer" parent="."] +layer = 10 + +[node name="TopBar" type="HBoxContainer" parent="HUD"] +anchors_preset = 10 +anchor_right = 1.0 +offset_bottom = 48.0 +theme_override_constants/separation = 0 + +[node name="PhaseLabel" type="Label" parent="HUD/TopBar"] +unique_name_in_owner = true +custom_minimum_size = Vector2(140, 48) +layout_mode = 2 +theme_override_font_sizes/font_size = 13 +text = "Phase: —" +vertical_alignment = 1 + +[node name="TurnLabel" type="Label" parent="HUD/TopBar"] +unique_name_in_owner = true +layout_mode = 2 +size_flags_horizontal = 3 +theme_override_font_sizes/font_size = 13 +text = "Turn 1 — Your turn" +horizontal_alignment = 1 +vertical_alignment = 1 + +[node name="OpponentPanel" type="HBoxContainer" parent="HUD/TopBar"] +unique_name_in_owner = true +custom_minimum_size = Vector2(240, 48) +layout_mode = 2 +theme_override_constants/separation = 8 + +[node name="OppNameLabel" type="Label" parent="HUD/TopBar/OpponentPanel"] +unique_name_in_owner = true +modulate = Color(1, 0.5, 0.5, 1) +layout_mode = 2 +theme_override_font_sizes/font_size = 12 +text = "Opponent" +vertical_alignment = 1 + +[node name="OppHandLabel" type="Label" parent="HUD/TopBar/OpponentPanel"] +unique_name_in_owner = true +layout_mode = 2 +theme_override_font_sizes/font_size = 12 +text = "Hand: 0" +vertical_alignment = 1 + +[node name="OppDeckLabel" type="Label" parent="HUD/TopBar/OpponentPanel"] +unique_name_in_owner = true +layout_mode = 2 +theme_override_font_sizes/font_size = 12 +text = "Deck: 0" +vertical_alignment = 1 + +[node name="OppDiscardBtn" type="Button" parent="HUD/TopBar/OpponentPanel"] +unique_name_in_owner = true +layout_mode = 2 +theme_override_font_sizes/font_size = 11 +text = "Discard" + +[node name="HandArea" type="PanelContainer" parent="HUD"] +anchors_preset = -1 +anchor_top = 1.0 +anchor_right = 0.75 +anchor_bottom = 1.0 +offset_top = -250.0 + +[node name="HandScroll" type="ScrollContainer" parent="HUD/HandArea"] +unique_name_in_owner = true +layout_mode = 2 +size_flags_horizontal = 3 +size_flags_vertical = 3 +vertical_scroll_mode = 0 + +[node name="HandCards" type="HBoxContainer" parent="HUD/HandArea/HandScroll"] +unique_name_in_owner = true +layout_mode = 2 +size_flags_vertical = 4 +theme_override_constants/separation = 6 + +[node name="RightPanel" type="PanelContainer" parent="HUD"] +anchors_preset = -1 +anchor_left = 0.75 +anchor_top = 1.0 +anchor_right = 1.0 +anchor_bottom = 1.0 +offset_left = -1.0 +offset_top = -241.0 +offset_right = -1.0 +offset_bottom = 9.0 +scale = Vector2(1, 0.96000004) + +[node name="RightMargin" type="MarginContainer" parent="HUD/RightPanel"] +layout_mode = 2 +theme_override_constants/margin_left = 10 +theme_override_constants/margin_top = 8 +theme_override_constants/margin_right = 10 +theme_override_constants/margin_bottom = 8 + +[node name="RightVBox" type="VBoxContainer" parent="HUD/RightPanel/RightMargin"] +layout_mode = 2 +theme_override_constants/separation = 6 + +[node name="SpLabel" type="Label" parent="HUD/RightPanel/RightMargin/RightVBox"] +unique_name_in_owner = true +layout_mode = 2 +theme_override_font_sizes/font_size = 14 +text = "SP: 0" + +[node name="StLabel" type="Label" parent="HUD/RightPanel/RightMargin/RightVBox"] +unique_name_in_owner = true +layout_mode = 2 +theme_override_font_sizes/font_size = 14 +text = "ST: 0" + +[node name="DeckLabel" type="Label" parent="HUD/RightPanel/RightMargin/RightVBox"] +unique_name_in_owner = true +layout_mode = 2 +theme_override_font_sizes/font_size = 12 +text = "Deck: 0" + +[node name="DiscardBtn" type="Button" parent="HUD/RightPanel/RightMargin/RightVBox"] +unique_name_in_owner = true +layout_mode = 2 +theme_override_font_sizes/font_size = 12 +text = "View Discard" + +[node name="DrawBtn" type="Button" parent="HUD/RightPanel/RightMargin/RightVBox"] +unique_name_in_owner = true +custom_minimum_size = Vector2(0, 36) +layout_mode = 2 +theme_override_font_sizes/font_size = 13 +text = "Draw Card" + +[node name="EndTurnBtn" type="Button" parent="HUD/RightPanel/RightMargin/RightVBox"] +unique_name_in_owner = true +custom_minimum_size = Vector2(0, 36) +layout_mode = 2 +theme_override_font_sizes/font_size = 13 +text = "End Turn" + +[node name="SettingsBtn" type="Button" parent="HUD/RightPanel/RightMargin/RightVBox"] +unique_name_in_owner = true +layout_mode = 2 +theme_override_font_sizes/font_size = 12 +text = "Settings" + +[node name="ContextPopup" type="PanelContainer" parent="HUD"] +unique_name_in_owner = true +visible = false +anchors_preset = 8 +anchor_left = 0.5 +anchor_top = 0.5 +anchor_right = 0.5 +anchor_bottom = 0.5 +offset_left = -160.0 +offset_top = -120.0 +offset_right = 160.0 +offset_bottom = 120.0 + +[node name="ContextMargin" type="MarginContainer" parent="HUD/ContextPopup"] +layout_mode = 2 +theme_override_constants/margin_left = 12 +theme_override_constants/margin_top = 8 +theme_override_constants/margin_right = 12 +theme_override_constants/margin_bottom = 8 + +[node name="ContextVBox" type="VBoxContainer" parent="HUD/ContextPopup/ContextMargin"] +layout_mode = 2 +theme_override_constants/separation = 6 + +[node name="ContextTileLabel" type="Label" parent="HUD/ContextPopup/ContextMargin/ContextVBox"] +unique_name_in_owner = true +layout_mode = 2 +theme_override_font_sizes/font_size = 13 +text = "Tile (0,0) — Void" + +[node name="ContextUnitLabel" type="Label" parent="HUD/ContextPopup/ContextMargin/ContextVBox"] +unique_name_in_owner = true +layout_mode = 2 +theme_override_font_sizes/font_size = 12 +autowrap_mode = 3 + +[node name="ContextAbilitiesLabel" type="Label" parent="HUD/ContextPopup/ContextMargin/ContextVBox"] +unique_name_in_owner = true +modulate = Color(0.7, 0.7, 0.85, 1) +layout_mode = 2 +theme_override_font_sizes/font_size = 11 +autowrap_mode = 3 + +[node name="ContextClose" type="Button" parent="HUD/ContextPopup/ContextMargin/ContextVBox"] +unique_name_in_owner = true +layout_mode = 2 +theme_override_font_sizes/font_size = 12 +text = "Close" + +[node name="DiscardViewer" type="PanelContainer" parent="HUD"] +unique_name_in_owner = true +visible = false +anchors_preset = -1 +anchor_left = 0.1 +anchor_top = 0.05 +anchor_right = 0.9 +anchor_bottom = 0.95 + +[node name="DVMargin" type="MarginContainer" parent="HUD/DiscardViewer"] +layout_mode = 2 +theme_override_constants/margin_left = 16 +theme_override_constants/margin_top = 12 +theme_override_constants/margin_right = 16 +theme_override_constants/margin_bottom = 12 + +[node name="DVVBox" type="VBoxContainer" parent="HUD/DiscardViewer/DVMargin"] +layout_mode = 2 +theme_override_constants/separation = 8 + +[node name="DVTitle" type="Label" parent="HUD/DiscardViewer/DVMargin/DVVBox"] +unique_name_in_owner = true +layout_mode = 2 +theme_override_font_sizes/font_size = 16 +text = "Discard Pile" + +[node name="DVScroll" type="ScrollContainer" parent="HUD/DiscardViewer/DVMargin/DVVBox"] +layout_mode = 2 +size_flags_vertical = 3 +horizontal_scroll_mode = 0 + +[node name="DVList" type="VBoxContainer" parent="HUD/DiscardViewer/DVMargin/DVVBox/DVScroll"] +unique_name_in_owner = true +layout_mode = 2 +size_flags_horizontal = 3 +theme_override_constants/separation = 4 + +[node name="DVCloseBtn" type="Button" parent="HUD/DiscardViewer/DVMargin/DVVBox"] +unique_name_in_owner = true +custom_minimum_size = Vector2(0, 36) +layout_mode = 2 +theme_override_font_sizes/font_size = 13 +text = "Close" + +[node name="GameOverOverlay" type="PanelContainer" parent="HUD"] +unique_name_in_owner = true +visible = false +anchors_preset = -1 +anchor_left = 0.25 +anchor_top = 0.25 +anchor_right = 0.75 +anchor_bottom = 0.75 + +[node name="GOMargin" type="MarginContainer" parent="HUD/GameOverOverlay"] +layout_mode = 2 +theme_override_constants/margin_left = 24 +theme_override_constants/margin_top = 24 +theme_override_constants/margin_right = 24 +theme_override_constants/margin_bottom = 24 + +[node name="GOVBox" type="VBoxContainer" parent="HUD/GameOverOverlay/GOMargin"] +layout_mode = 2 +theme_override_constants/separation = 16 + +[node name="GOResultLabel" type="Label" parent="HUD/GameOverOverlay/GOMargin/GOVBox"] +unique_name_in_owner = true +layout_mode = 2 +theme_override_font_sizes/font_size = 28 +text = "Game Over" +horizontal_alignment = 1 + +[node name="GORankLabel" type="Label" parent="HUD/GameOverOverlay/GOMargin/GOVBox"] +unique_name_in_owner = true +layout_mode = 2 +theme_override_font_sizes/font_size = 15 +horizontal_alignment = 1 + +[node name="GOMainMenuBtn" type="Button" parent="HUD/GameOverOverlay/GOMargin/GOVBox"] +unique_name_in_owner = true +custom_minimum_size = Vector2(0, 44) +layout_mode = 2 +theme_override_font_sizes/font_size = 15 +text = "Return to Main Menu" + +[node name="InGameSettings" parent="HUD" instance=ExtResource("2_igs")] +unique_name_in_owner = true + +[node name="HoldTimer" type="Timer" parent="."] +unique_name_in_owner = true +wait_time = 0.5 +one_shot = true diff --git a/Client/origin-client/scenes/game/game_screen.gd b/Client/origin-client/scenes/game/game_screen.gd index ea2bdb0..0d58328 100644 --- a/Client/origin-client/scenes/game/game_screen.gd +++ b/Client/origin-client/scenes/game/game_screen.gd @@ -512,11 +512,17 @@ func _try_load_tile_model(body: StaticBody3D, affinity: String) -> void: child.queue_free() b.add_child(model_node) - # Scale so the XZ footprint fits the 0.98-unit tile square. + # Scale so the XZ footprint fits the 0.98-unit tile square, then lift + # the model so its base sits on the tile's TOP surface. + # _fit_model_to_tile seats the AABB bottom at body-local Y=0, which is + # world Y = tile_h/2 (the tile centre). Shifting by +tile_h/2 moves the + # base up to body-local Y = tile_h/2 = world Y = tile_h (tile top). _fit_model_to_tile(model_node, 0.98) + var tile_h_m : float = float(b.get_meta("tile_height", 1.0)) + model_node.position.y += tile_h_m * 0.5 # Attach a random stalagmite bottom model below the tile (if any are configured). - var bottom_url := ContentCache.get_random_tile_bottom_url() + var bottom_url : String = ContentCache.get_random_tile_bottom_url() if not bottom_url.is_empty(): _try_load_tile_bottom(body_ref, bottom_url) ) @@ -532,14 +538,18 @@ func _try_load_tile_bottom(body_ref: Variant, url: String) -> void: return if bottom_node == null: return - # The body centre is at world Y = h/2. The bottom of the tile is at - # body-local Y = -h/2. Scale the stalagmite to fit the tile footprint, - # then flip it upside-down (scale Y = -1) so it hangs downward, and - # seat its origin at the tile-bottom local position. + # The body centre is at world Y = tile_h/2. The bottom of the tile is at + # body-local Y = -tile_h/2 (world Y = 0). + # Fit the model to the tile XZ footprint, then rotate 180° around X + # (equivalent to a Y-flip but preserves mesh normals, unlike scale.y=-1). + # After the rotation the wide base is still at body-local Y=0 and the + # narrow spike is at -scaled_height. Setting position.y = -tile_h/2 + # shifts the whole thing so the base lands at the tile bottom (world Y=0) + # and the spike hangs further downward. var tile_h : float = float(b.get_meta("tile_height", 1.0)) _fit_model_to_tile(bottom_node, 0.98) - bottom_node.scale.y = -absf(bottom_node.scale.y) # flip to hang down - bottom_node.position.y = -tile_h * 0.5 # move to tile bottom + bottom_node.rotate_x(PI) # flip upside-down, normals intact + bottom_node.position.y = -tile_h * 0.5 # seat flipped base at tile bottom b.add_child(bottom_node) ) @@ -764,11 +774,16 @@ func _add_unit_mesh(unit: Dictionary, team_color: Color, is_enemy: bool = false) # Apply multiplicative tint to all StandardMaterial3D surfaces. _apply_model_tint(model_node, model_tint) - # Position model so its local origin sits at the body's position. - # Skip reset if a move tween is driving this body — it controls position. + # Restore the body to its correct world height (uses y_offset captured + # from the outer scope, which accounts for tile_h and the unit's + # half-height). Skip if a move tween is currently driving the body. if not _move_anims.has(iid): - b.position = wp + Vector3(0, 0.5, 0) + b.position = wp + Vector3(0, y_offset, 0) b.add_child(model_node) + # Seat the model's local-Y=0 (feet) on the tile's top surface. + # The body centre is at tile_h + half_height; tile_h - y_offset = -half_height + # shifts the model so its feet land exactly at world Y = tile_h. + model_node.position.y = tile_h - y_offset _unit_meshes[iid] = model_node # Find and store the AnimationPlayer, then start the Idle loop.