From 6e7cec8959a6b169b9e3e5df1a08068dc1d7946a Mon Sep 17 00:00:00 2001 From: dingo Date: Wed, 11 Feb 2026 15:28:08 -0500 Subject: [PATCH] Ability work and portal fixes.... --- scenes/dungeon/mob.tscn | 10 +- scenes/dungeon/portal.tscn | 4 + scripts/autoload/game_state.gd | 52 +++++++- scripts/dungeon/dungeon_manager.gd | 104 ++++++++++------ scripts/dungeon/hazard_overlay_renderer.gd | 111 ++++++++++++++++++ .../dungeon/hazard_overlay_renderer.gd.uid | 1 + scripts/dungeon/projectile.gd | 35 +++++- scripts/ui/hub_ui.gd | 2 +- scripts/ui/main.gd | 8 +- 9 files changed, 274 insertions(+), 53 deletions(-) create mode 100644 scripts/dungeon/hazard_overlay_renderer.gd create mode 100644 scripts/dungeon/hazard_overlay_renderer.gd.uid diff --git a/scenes/dungeon/mob.tscn b/scenes/dungeon/mob.tscn index e0a4168..08f5c43 100644 --- a/scenes/dungeon/mob.tscn +++ b/scenes/dungeon/mob.tscn @@ -1,7 +1,10 @@ -[gd_scene load_steps=3 format=3 uid="uid://4it2xruff84i"] +[gd_scene load_steps=4 format=3 uid="uid://4it2xruff84i"] [ext_resource type="Script" uid="uid://b0rmvppejqbkq" path="res://scripts/dungeon/mob_visual.gd" id="1_hxp1t"] +[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_hxp1t"] +bg_color = Color(0.8542305, 0, 0.10823222, 1) + [sub_resource type="CircleShape2D" id="CircleShape2D_1"] radius = 14.0 @@ -22,9 +25,10 @@ vertical_alignment = 1 [node name="HealthBar" type="ProgressBar" parent="."] offset_left = -20.0 -offset_top = -28.0 +offset_top = -45.0 offset_right = 20.0 -offset_bottom = -23.0 +offset_bottom = -40.0 +theme_override_styles/fill = SubResource("StyleBoxFlat_hxp1t") value = 100.0 show_percentage = false diff --git a/scenes/dungeon/portal.tscn b/scenes/dungeon/portal.tscn index 0fbf133..dfd0f66 100644 --- a/scenes/dungeon/portal.tscn +++ b/scenes/dungeon/portal.tscn @@ -10,6 +10,7 @@ script/source = "extends Node2D var portal_data: Dictionary = {} var portal_id: int = 0 var portal_type: String = \"exit\" +var _portal_used: bool = false # Pulsing animation var pulse_time: float = 0.0 @@ -68,6 +69,9 @@ func _on_input_event(_viewport, event, _shape_idx): _use_portal() func _use_portal(): + if _portal_used: + return + _portal_used = true print(\"[Portal] Using portal %d (type: %s)\" % [portal_id, portal_type]) GameState.send_use_portal(portal_id) " diff --git a/scripts/autoload/game_state.gd b/scripts/autoload/game_state.gd index cf68e5c..9fcf061 100644 --- a/scripts/autoload/game_state.gd +++ b/scripts/autoload/game_state.gd @@ -39,6 +39,7 @@ const STATE_NAMES = ["AUTH", "HUB", "PLAYING", "DEAD", "PORTALUSED","DUNGEONCLEA var current_state: int = STATE_AUTH var account_id: int = 0 +var _hub_fallback_timer: SceneTreeTimer = null # Character data var character: Dictionary = { @@ -80,6 +81,8 @@ var chests: Dictionary = {} var dropped_items: Dictionary = {} var portals: Dictionary = {} var loot_chests: Dictionary = {} +var active_projectiles: Dictionary = {} # Projectiles from DUNGEON_STATE spatial tick +var hazards: Dictionary = {} # Active hazard zones from DUNGEON_STATE # Inventory var inventory: Array = [] @@ -232,6 +235,8 @@ func _handle_hub_state(data: Dictionary): "gold": int(c.get("gold", c.get("Gold", 0))) } if current_state != STATE_PLAYING and current_state != STATE_DEAD: + # Cancel the fallback timer — server responded in time + _hub_fallback_timer = null set_state(STATE_HUB) character_updated.emit() @@ -516,6 +521,22 @@ func _handle_dungeon_state(data: Dictionary): var lcid = lc.get("id", lc.get("Id")) loot_chests[lcid] = lc + # Parse active projectiles (spatially-filtered, sent each tick like mobs) + var projs = data.get("projectiles", data.get("Projectiles", [])) + active_projectiles.clear() + for proj in projs: + var pid = proj.get("id", proj.get("Id")) + if pid != null: + active_projectiles[pid] = proj + + # Parse active hazard zones (ability type 29 ground effects) + var hzs = data.get("hazards", data.get("Hazards", [])) + hazards.clear() + for hz in hzs: + var hid = hz.get("id", hz.get("Id")) + if hid != null: + hazards[hid] = hz + # Parse events (damage, deaths, projectile hits/expires) var events = data.get("events") if events: @@ -600,7 +621,7 @@ func _handle_levelup(data: Dictionary): func _handle_escape_success(_data: Dictionary): print("[GameState] Escape successful! Returning to hub...") is_escaping = false - + # Clear dungeon data players.clear() mobs.clear() @@ -608,6 +629,9 @@ func _handle_escape_success(_data: Dictionary): chests.clear() dropped_items.clear() portals.clear() + loot_chests.clear() + active_projectiles.clear() + hazards.clear() dungeon = { "id": "", "tier": 1, @@ -616,9 +640,23 @@ func _handle_escape_success(_data: Dictionary): "tiles": [], "phase": "active" } - + set_state(STATE_PORTALUSED) escaped.emit() - # Server will send HUB_STATE next which will call set_state(STATE_HUB) + # Server will send HUB_STATE next which will call set_state(STATE_HUB). + # Safety timeout: if HUB_STATE never arrives (network drop), force transition. + _start_hub_fallback_timer() + +func _start_hub_fallback_timer(): + # Cancel any existing fallback timer + if _hub_fallback_timer and _hub_fallback_timer.time_left > 0: + _hub_fallback_timer.timeout.disconnect(_on_hub_fallback_timeout) + _hub_fallback_timer = get_tree().create_timer(5.0) + _hub_fallback_timer.timeout.connect(_on_hub_fallback_timeout) + +func _on_hub_fallback_timeout(): + if current_state == STATE_PORTALUSED or current_state == STATE_DUNGEONCLEARED: + print("[GameState] HUB_STATE not received in time — forcing transition to HUB") + set_state(STATE_HUB) # Helper functions func send_move(dx: float, dy: float, aim_angle: float): @@ -687,7 +725,7 @@ func send_use_portal(portal_id: int): func _handle_portal_used(_data: Dictionary): print("[GameState] Portal used! Returning to hub...") is_escaping = false - + # Clear dungeon data players.clear() mobs.clear() @@ -696,6 +734,8 @@ func _handle_portal_used(_data: Dictionary): dropped_items.clear() portals.clear() loot_chests.clear() + active_projectiles.clear() + hazards.clear() dungeon = { "id": "", "tier": 1, @@ -706,7 +746,9 @@ func _handle_portal_used(_data: Dictionary): } set_state(STATE_PORTALUSED) escaped.emit() - # Server will send HUB_STATE next which will call set_state(STATE_HUB) + # Server will send HUB_STATE next which will call set_state(STATE_HUB). + # Safety timeout: if HUB_STATE never arrives (network drop), force transition. + _start_hub_fallback_timer() # Loot chest handlers func _handle_loot_chest_spawned(data: Dictionary): diff --git a/scripts/dungeon/dungeon_manager.gd b/scripts/dungeon/dungeon_manager.gd index 72380c6..e7c899f 100644 --- a/scripts/dungeon/dungeon_manager.gd +++ b/scripts/dungeon/dungeon_manager.gd @@ -34,6 +34,9 @@ var nearby_lootable_mob: Node = null # Visual tile renderer (for V2 dungeons) var visual_renderer: VisualTileMapRenderer = null +# Hazard zone overlay renderer +var hazard_renderer: HazardOverlayRenderer = null + # Scenes for entities var player_scene: PackedScene = null var mob_scene: PackedScene = null @@ -105,6 +108,11 @@ func _ready(): # Move it behind entities move_child(visual_renderer, 0) + # Create hazard overlay renderer (z_index 5: above floor, below entities) + hazard_renderer = HazardOverlayRenderer.new() + hazard_renderer.name = "HazardOverlay" + add_child(hazard_renderer) + # Defer GameState connections call_deferred("_connect_gamestate") @@ -139,9 +147,11 @@ func _on_game_state_changed(state: String): if crafting_panel: crafting_panel.visible = false - # Clear visual tiles when leaving dungeon + # Clear visual tiles and hazard overlays when leaving dungeon if not is_playing and visual_renderer: visual_renderer.clear_tiles() + if not is_playing and hazard_renderer: + hazard_renderer.clear() func _clear_entity_containers(): ## Remove all entity nodes from previous dungeon session. @@ -155,6 +165,9 @@ func _clear_entity_containers(): if is_instance_valid(proj): proj.queue_free() projectiles.clear() + # Clear hazard overlays + if hazard_renderer: + hazard_renderer.clear() func _on_dungeon_entered(): # Clear stale entity nodes from any previous dungeon session @@ -495,7 +508,7 @@ func _on_state_updated(): if player_scene and players_container: _update_entities(players_container, GameState.players, player_scene) if mob_scene and mobs_container: - await _prefetch_mob_textures_if_needed() + _prefetch_mob_textures_if_needed() _update_mob_entities() if chest_scene and chests_container: _update_entities(chests_container, GameState.chests, chest_scene) @@ -505,6 +518,10 @@ func _on_state_updated(): _update_entities(portals_container, GameState.portals, portal_scene) if loot_chest_scene and loot_chests_container: _update_loot_chests() + if projectile_scene and projectiles_container: + _update_projectile_entities() + if hazard_renderer: + hazard_renderer.update_hazards(GameState.hazards) func _collect_mob_sprite_paths(mobs: Dictionary) -> Array: var paths: Array = [] @@ -555,11 +572,17 @@ func _prefetch_mob_textures_if_needed() -> void: if missing.is_empty(): return + # Download in background — don't block the state tick. + # Mobs render with fallback sprites until textures arrive. _mob_prefetch_in_progress = true + _do_mob_prefetch(missing) + +func _do_mob_prefetch(missing: Array) -> void: await UpdateManager.prefetch_files(missing) for path in missing: _prefetched_mob_paths[path] = true _mob_prefetch_in_progress = false + # Mob nodes auto-rebuild sprites via _on_file_downloaded in mob_visual.gd func _update_entities(container: Node2D, data: Dictionary, scene: PackedScene): ## Generic entity updater for players, chests, items, portals. @@ -790,43 +813,50 @@ func _on_tiles_updated(tiles: Array): if visual_renderer: visual_renderer.update_tiles(tiles) +# ===== Projectile State-Tick Rendering ===== + +func _update_projectile_entities(): + ## Sync projectile nodes with GameState.active_projectiles (sent each tick). + var server_projs = GameState.active_projectiles + var seen_ids: Array = [] + + for id in server_projs: + seen_ids.append(id) + var proj_data = server_projs[id] + + var proj_node = projectiles.get(id) + if proj_node and not is_instance_valid(proj_node): + projectiles.erase(id) + proj_node = null + + if proj_node == null: + # New projectile — create and initialize + proj_node = projectile_scene.instantiate() + proj_node.name = "Projectile_" + str(id) + projectiles_container.add_child(proj_node) + proj_node.initialize(proj_data) + projectiles[id] = proj_node + else: + # Existing projectile — snap to server position + proj_node.update_from_state(proj_data) + + # Remove projectiles no longer in the server snapshot + var to_remove: Array = [] + for id in projectiles: + if id not in seen_ids: + to_remove.append(id) + for id in to_remove: + var proj_node = projectiles[id] + if is_instance_valid(proj_node): + proj_node.queue_free() + projectiles.erase(id) + # ===== Projectile Event Handlers ===== -func _on_projectile_spawned(data: Dictionary): - print("[DungeonManager] Projectile spawned event received: ", data) - - if projectile_scene == null: - print("[DungeonManager] ERROR: projectile_scene is null!") - return - - if projectiles_container == null: - print("[DungeonManager] ERROR: projectiles_container is null!") - return - - var proj_id = data.get("id", -1) - if proj_id == -1: - print("[DungeonManager] ERROR: Invalid projectile ID") - return - - print("[DungeonManager] Creating projectile instance for ID: ", proj_id) - - # Create projectile instance - var projectile = projectile_scene.instantiate() - projectile.name = "Projectile_" + str(proj_id) - projectiles_container.add_child(projectile) - - print("[DungeonManager] Projectile node created and added to container") - - # Initialize projectile with data - if projectile.has_method("initialize"): - projectile.initialize(data) - print("[DungeonManager] Projectile initialized") - else: - print("[DungeonManager] ERROR: Projectile has no initialize method!") - - # Store in tracking dictionary - projectiles[proj_id] = projectile - print("[DungeonManager] Projectile ", proj_id, " tracked. Total projectiles: ", projectiles.size()) +func _on_projectile_spawned(_data: Dictionary): + # Projectiles are now created from the DUNGEON_STATE tick. + # Standalone spawn events are no longer needed for rendering. + pass func _on_projectile_hit(data: Dictionary): var proj_id = data.get("projectileId", -1) diff --git a/scripts/dungeon/hazard_overlay_renderer.gd b/scripts/dungeon/hazard_overlay_renderer.gd new file mode 100644 index 0000000..f39a8e9 --- /dev/null +++ b/scripts/dungeon/hazard_overlay_renderer.gd @@ -0,0 +1,111 @@ +class_name HazardOverlayRenderer +extends Node2D + +## Renders colored tile overlays for active hazard zones (Ability Type 29). +## +## Each hazard has a world-space center (x, y), radius, tintColor, and tintOpacity. +## We compute which tiles fall within the radius and draw a tinted rectangle for each. +## Overlapping hazards stack by drawing in id-ascending order with alpha blending. + +const WORLD_TILE_SIZE := 32 # 16px tiles * 2x scale + +# Cached state: hazard id -> parsed data used for drawing +var _active_hazards: Dictionary = {} # id -> { tiles: Array[Vector2i], color: Color } + +func _ready(): + # Render above ground/floor tiles, below entities and projectiles + z_index = 5 + +func update_hazards(hazards: Dictionary) -> void: + ## Rebuild overlay state from the authoritative server hazard dict. + var changed := false + + # Detect additions / changes + for id in hazards: + var hz = hazards[id] + var center_x: float = hz.get("x", 0.0) + var center_y: float = hz.get("y", 0.0) + var radius: float = hz.get("radius", 0.0) + var tint_hex: String = hz.get("tintColor", "#ff0000") + var tint_opacity: float = hz.get("tintOpacity", 0.35) + + var color := Color.from_string(tint_hex, Color.RED) + color.a = clampf(tint_opacity, 0.0, 1.0) + + var tiles := _compute_tiles_in_radius(center_x, center_y, radius) + + if _active_hazards.has(id): + var prev = _active_hazards[id] + # Quick change check: color or tile count changed + if prev.color != color or prev.tiles.size() != tiles.size(): + _active_hazards[id] = { "tiles": tiles, "color": color } + changed = true + else: + _active_hazards[id] = { "tiles": tiles, "color": color } + changed = true + + # Detect removals + var to_remove: Array = [] + for id in _active_hazards: + if not hazards.has(id): + to_remove.append(id) + if to_remove.size() > 0: + changed = true + for id in to_remove: + _active_hazards.erase(id) + + if changed: + queue_redraw() + +func clear() -> void: + if _active_hazards.size() > 0: + _active_hazards.clear() + queue_redraw() + +func _draw(): + if _active_hazards.is_empty(): + return + + # Sort by id ascending for deterministic overlap blending + var sorted_ids = _active_hazards.keys() + sorted_ids.sort() + + for id in sorted_ids: + var entry = _active_hazards[id] + var color: Color = entry.color + var tiles: Array = entry.tiles + for tile_pos in tiles: + var rect := Rect2( + tile_pos.x * WORLD_TILE_SIZE, + tile_pos.y * WORLD_TILE_SIZE, + WORLD_TILE_SIZE, + WORLD_TILE_SIZE + ) + draw_rect(rect, color) + +func _compute_tiles_in_radius(cx: float, cy: float, radius: float) -> Array: + ## Return an array of Vector2i tile coordinates whose centers fall within the radius. + var tiles: Array = [] + if radius <= 0.0: + return tiles + + # Tile grid bounds that could possibly be in range + var half_tile := WORLD_TILE_SIZE * 0.5 + var min_tx := int(floor((cx - radius) / WORLD_TILE_SIZE)) + var max_tx := int(floor((cx + radius) / WORLD_TILE_SIZE)) + var min_ty := int(floor((cy - radius) / WORLD_TILE_SIZE)) + var max_ty := int(floor((cy + radius) / WORLD_TILE_SIZE)) + + var radius_sq := radius * radius + + for tx in range(min_tx, max_tx + 1): + for ty in range(min_ty, max_ty + 1): + # Tile center in world space + var tile_cx := tx * WORLD_TILE_SIZE + half_tile + var tile_cy := ty * WORLD_TILE_SIZE + half_tile + var dx := tile_cx - cx + var dy := tile_cy - cy + if dx * dx + dy * dy <= radius_sq: + tiles.append(Vector2i(tx, ty)) + + return tiles diff --git a/scripts/dungeon/hazard_overlay_renderer.gd.uid b/scripts/dungeon/hazard_overlay_renderer.gd.uid new file mode 100644 index 0000000..1e8320a --- /dev/null +++ b/scripts/dungeon/hazard_overlay_renderer.gd.uid @@ -0,0 +1 @@ +uid://b7fgpajxbqfw diff --git a/scripts/dungeon/projectile.gd b/scripts/dungeon/projectile.gd index d399333..3c33fdc 100644 --- a/scripts/dungeon/projectile.gd +++ b/scripts/dungeon/projectile.gd @@ -79,7 +79,7 @@ func _load_sprite(): _use_fallback_sprite() func _process(delta): - # Move projectile based on velocity (client-side prediction) + # Interpolate between server ticks using velocity if velocity.length() > 0: position += velocity * delta @@ -99,10 +99,10 @@ func initialize(data: Dictionary): position.x = data.get("x", 0) position.y = data.get("y", 0) - velocity.x = data.get("vx", 0) - velocity.y = data.get("vy", 0) + velocity.x = data.get("velocityX", data.get("vx", 0)) + velocity.y = data.get("velocityY", data.get("vy", 0)) - sprite_key = data.get("sprite", "") + sprite_key = data.get("spriteKey", data.get("sprite", "")) is_linear = data.get("isLinear", false) front_x = data.get("frontX", 0) front_y = data.get("frontY", 0) @@ -116,4 +116,29 @@ func initialize(data: Dictionary): # Load sprite after initialization _load_sprite() - print("[Projectile] Initialized projectile ", projectile_id, " at (", position.x, ", ", position.y, ") with velocity (", velocity.x, ", ", velocity.y, "), linear=", is_linear) +func update_from_state(data: Dictionary): + ## Snap to authoritative server position each tick, update velocity for interpolation. + position.x = data.get("x", position.x) + position.y = data.get("y", position.y) + + velocity.x = data.get("velocityX", data.get("vx", velocity.x)) + velocity.y = data.get("velocityY", data.get("vy", velocity.y)) + + # Update linear metadata if present + var new_linear = data.get("isLinear", null) + if new_linear != null: + is_linear = new_linear + front_x = data.get("frontX", front_x) + front_y = data.get("frontY", front_y) + back_x = data.get("backX", back_x) + back_y = data.get("backY", back_y) + + # Update sprite if it changed + var new_sprite = data.get("spriteKey", data.get("sprite", "")) + if new_sprite != "" and new_sprite != sprite_key: + sprite_key = new_sprite + _load_sprite() + + # Update rotation for linear projectiles + if is_linear and velocity.length() > 0: + rotation = velocity.angle() diff --git a/scripts/ui/hub_ui.gd b/scripts/ui/hub_ui.gd index 79b5315..0c8d511 100644 --- a/scripts/ui/hub_ui.gd +++ b/scripts/ui/hub_ui.gd @@ -296,7 +296,7 @@ func _on_logout_pressed(): # Reset GameState if GameState: - GameState.reset_state() + GameState.set_state(0) # Return to login screen get_tree().change_scene_to_file("res://scenes/ui/login.tscn") diff --git a/scripts/ui/main.gd b/scripts/ui/main.gd index b5e8985..8d05458 100644 --- a/scripts/ui/main.gd +++ b/scripts/ui/main.gd @@ -39,9 +39,13 @@ func _on_state_changed(new_state: String): func _show_screen(state: String): print("[Main] _show_screen called with: ", state) + # Transitional states (portal exit, dungeon cleared) show the loading screen + # while waiting for the server to send HUB_STATE. + var is_transitioning = state == "PORTALUSED" or state == "DUNGEONCLEARED" + if loading_screen: - _set_screen_visible(loading_screen, state == "LOADING") - print("[Main] LoadingScreen visible: ", state == "LOADING") + _set_screen_visible(loading_screen, state == "LOADING" or is_transitioning) + print("[Main] LoadingScreen visible: ", state == "LOADING" or is_transitioning) if auth_screen: _set_screen_visible(auth_screen, state == "AUTH") print("[Main] AuthScreen visible: ", state == "AUTH")