Ability work and portal fixes....
This commit is contained in:
@@ -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):
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
@@ -0,0 +1 @@
|
||||
uid://b7fgpajxbqfw
|
||||
@@ -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()
|
||||
|
||||
@@ -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")
|
||||
|
||||
+6
-2
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user