Files
tekton/scripts/managers/player_movement_manager.gd
T

542 lines
20 KiB
GDScript

extends Node
var player: Node3D
var enhanced_gridmap: Node
# Movement settings
var movement_range: int = 1
var use_diagonal_movement: bool = false
var is_moving: bool = false
var rotation_speed: float = 10.0
var target_rotation: float = 0.0
var speed_multiplier: float = 1.0 # For slow-mo effects
func initialize(p_player: Node3D, p_gridmap: Node):
player = p_player
enhanced_gridmap = p_gridmap
# Initialize settings from player if available
if "movement_range" in player:
movement_range = player.movement_range
if "use_diagonal_movement" in player:
use_diagonal_movement = player.use_diagonal_movement
signal movement_finished
var movement_queue: Array[Vector2i] = [] # Queue of target grid positions
var current_move_direction: Vector2i = Vector2i.ZERO
var last_move_direction: Vector2i = Vector2i(0, 1) # Default forward (towards +Z)
func _process(delta):
if player:
_handle_rotation(delta)
func _handle_rotation(delta):
if player.rotation.y != target_rotation:
player.rotation.y = lerp_angle(player.rotation.y, target_rotation, delta * rotation_speed)
func rotate_towards_target(target_pos: Vector2i):
var direction = Vector3(target_pos.x - player.current_position.x, 0, target_pos.y - player.current_position.y)
if direction != Vector3.ZERO:
target_rotation = atan2(direction.x, direction.z)
if player.is_multiplayer_authority() and _can_rpc():
player.rpc("sync_rotation", target_rotation)
func _can_rpc() -> bool:
if not player or not player.is_inside_tree() or not player.multiplayer.has_multiplayer_peer():
return false
if player.multiplayer.multiplayer_peer.get_connection_status() != MultiplayerPeer.CONNECTION_CONNECTED:
return false
return true
func simple_move_to(grid_position: Vector2i) -> bool:
if is_moving:
return false
if not player.is_multiplayer_authority():
print("[Move] Failed: Not authority for %s (Authority: %d, My Peer: %d)" % [player.name, player.get_multiplayer_authority(), player.multiplayer.get_unique_id()])
return false
if player.get("is_frozen") or player.get("is_stop_frozen"):
print("[Move] Failed: Player is frozen")
return false
# BLOCK MOVEMENT IF MATCH NOT ACTIVE (Countdown)
var main = player.get_tree().root.get_node_or_null("Main")
if main and main.goals_cycle_manager and not main.goals_cycle_manager.is_match_active:
return false
var distance: int
if use_diagonal_movement:
distance = max(abs(grid_position.x - player.current_position.x), abs(grid_position.y - player.current_position.y))
else:
distance = abs(grid_position.x - player.current_position.x) + abs(grid_position.y - player.current_position.y)
if distance > movement_range:
return false
if not enhanced_gridmap.is_position_valid(grid_position):
# print("[Move] Failed: Position not valid on GridMap %s" % grid_position)
return false
if player.has_method("can_move_to_finish") and not player.can_move_to_finish(grid_position):
print("[Move] Failed: Cannot move to finish yet")
return false
var cell_floor = enhanced_gridmap.get_cell_item(Vector3i(grid_position.x, 0, grid_position.y))
var cell_item = enhanced_gridmap.get_cell_item(Vector3i(grid_position.x, 1, grid_position.y))
# Allow passing through Walls (Item 4 or 16) if Invisible
var is_wall_passable = player.get("is_invisible") and (cell_floor == 4 or cell_item == 16 or cell_item == 4)
# Check Floor 0 (Basic Walkability/Void)
if (cell_floor == -1 or cell_floor in enhanced_gridmap.non_walkable_items) and not is_wall_passable:
print("[Move] Failed: Floor Item %d is non-walkable" % cell_floor)
return false
# Check Floor 1 (Obstacles/Walls)
if (cell_item != -1 and cell_item in [4, 13, 16]) and not is_wall_passable:
print("[Move] Failed: Blocked by Item %d on Floor 1" % cell_item)
return false
# PHYSICS CHECK: Ensure no static obstacles (like Stands) are blocking the path
if _is_position_blocked_by_physics(grid_position):
print("[Move] Failed: Blocked by physics raycast at %s" % grid_position)
return false
if player.is_position_occupied(grid_position):
var push_dir = grid_position - player.current_position
if not try_push(grid_position, push_dir):
return false
# Check for Tekton interaction (Knock Mode)
# If moving into a Tekton's space while in Knock Mode, trigger knock
if player.get("is_knock_mode"):
# Find Tekton at grid_position
var tektons = player.get_tree().get_nodes_in_group("Tektons")
for t in tektons:
if t.current_position == grid_position and not t.is_carried:
# Trigger Knock
player.knock_tekton()
return false # Don't move into the tile, just knock
rotate_towards_target(grid_position)
rotate_towards_target(grid_position)
if player.is_multiplayer_authority():
if player.has_method("sync_walk_animation"):
if _can_rpc():
player.rpc("sync_walk_animation")
else:
player.sync_walk_animation()
var path = [Vector2(player.current_position.x, player.current_position.y), Vector2(grid_position.x, grid_position.y)]
path.pop_front()
current_move_direction = grid_position - player.current_position
if current_move_direction != Vector2i.ZERO:
last_move_direction = current_move_direction
if player.is_multiplayer_authority():
# Authority starts their own tween locally
var is_bot = player.is_bot or player.is_in_group("Bots")
player.start_movement_along_path(path, not is_bot)
# Authority sends RPC to others (call_remote) to start their tweens
if _can_rpc():
player.rpc("start_movement_along_path", path, not is_bot)
return true
func try_push(target_pos: Vector2i, direction: Vector2i) -> bool:
if not player.has_method("get_player_at_position"):
return false
var other_player = player.get_player_at_position(target_pos)
if not other_player:
return false
# === INVULNERABILITY CHECK ===
if other_player.get("is_carrying_tekton"):
print("[Move] Push blocked: Target is carrying a Tekton and is invulnerable.")
NotificationManager.send_message(player, "Target is Immune!", NotificationManager.MessageType.WARNING)
return false
# === NEW LOGIC: Only allow push if in ATTACK MODE and NOT GHOST ===
if not player.get("is_attack_mode") or player.get("is_invisible"):
# Standard bumping effect (Visual only)
print("[Move] Push blocked: Not in attack mode or is Ghost (%s trying to push %s)" % [player.name, other_player.name])
if _can_rpc():
player.rpc("sync_bump", target_pos, true) # Soft bump
elif player.has_method("sync_bump"):
player.sync_bump(target_pos, true)
return false
# SAFE ZONE PROTECTION (Only in Stop n Go)
if LobbyManager.is_game_mode(GameMode.Mode.STOP_N_GO):
var safe_columns = [6, 7, 8, 14, 15, 16]
# 1. Prevent attacker from attacking IF THEY ARE in a Safe Zone
if player.current_position.x in safe_columns:
print(" - Attack BLOCKED: Attacker is in Safe Zone!")
NotificationManager.send_message(player, "Cannot Attack while in Safe Zone!", NotificationManager.MessageType.WARNING)
return false
# 2. Prevent attacking players WHO ARE in a Safe Zone (existing logic)
if target_pos.x in safe_columns:
print(" - Attack BLOCKED: Target is in Safe Zone!")
NotificationManager.send_message(player, "Target is in Safe Zone!", NotificationManager.MessageType.WARNING)
return false
# === SUPER PUSH (Attack Mode) ===
print("Player %s SUPER PUSHING %s!" % [player.name, other_player.name])
# Visual Feedback: Attack Bump
if _can_rpc():
player.rpc("sync_bump", target_pos, false) # Attack bump
elif player.has_method("sync_bump"):
player.sync_bump(target_pos, false)
# 1. 3-Floor Knockback towards Starting Line (X=0)
var push_direction = Vector2i(-1, 0) # Backwards
var pushed_to_pos = target_pos
var push_path = []
# Try to push up to 3 tiles back, building the path as we go
for i in range(3):
var next_back = pushed_to_pos + push_direction
if _can_push_to(next_back):
pushed_to_pos = next_back
push_path.append(Vector2(pushed_to_pos.x, pushed_to_pos.y))
else:
break # Blocked by wall or edge
if push_path.size() > 0:
# Valid push movement
if _can_rpc():
# Pass 'true' for 'force' parameter to interrupt active movements
other_player.rpc("start_movement_along_path", push_path, false, true)
# Authority Check: If we are already the authority for the victim (e.g. Host hitting a Bot),
# the 'call_remote' RPC above won't execute locally. We MUST call it manually.
if other_player.is_multiplayer_authority():
other_player.start_movement_along_path(push_path, false, true)
other_player.target_position = pushed_to_pos # Logical update
# 2. Apply freeze/stun effect (blue tint)
if _can_rpc():
other_player.rpc("apply_stagger", 1.5)
else:
# Handle local execution (e.g. offline or host-only logic)
other_player.apply_stagger(1.5)
# 4. Consume Boost (Full) - One hit per charge
if player.powerup_manager:
# Consume all available boost to force a full recharge cycle
player.powerup_manager.consume_boost(100.0)
# SCORING: 200 Points for successful attack (ONLY in Free Mode)
if player.is_multiplayer_authority():
var is_sng = LobbyManager.is_game_mode(GameMode.Mode.STOP_N_GO)
if not is_sng:
var main = player.get_tree().get_root().get_node_or_null("Main")
if main:
var gcm = main.get_node_or_null("GoalsCycleManager")
if gcm:
if multiplayer.is_server():
# Server/Bot: Directly add score to specific player ID
gcm.add_score(player.name.to_int(), 200)
else:
# Client: Request score add (sender ID used)
gcm.rpc("request_add_score", 200)
NotificationManager.send_message(player, "Successful Attack! +200 Pts", NotificationManager.MessageType.GOAL)
else:
NotificationManager.send_message(player, "Successful Attack!", NotificationManager.MessageType.GOAL)
# 5. Block the attacker from moving into the victim's space to prevent overlapping
return false
func set_speed_multiplier(multiplier: float):
speed_multiplier = multiplier
# If we are currently moving, we might need to adjust the speed of the active tween?
# However, the movement is handled via RPC 'start_movement_along_path' which
# likely uses a fixed speed or duration on all clients.
# Let's check how 'start_movement_along_path' is implemented in player.gd.
func _on_movement_finished():
if not movement_queue.is_empty():
var next_target = movement_queue.pop_front()
# Use a small delay or call_deferred to avoid recursion issues,
# but keep it snappy by executing immediately if possible.
if not simple_move_to(next_target):
# If next move failed, clear queue and signal finished
movement_queue.clear()
current_move_direction = Vector2i.ZERO
emit_signal("movement_finished")
else:
current_move_direction = Vector2i.ZERO
emit_signal("movement_finished")
func move_to_clicked_position(grid_position: Vector2i) -> bool:
if not player.is_multiplayer_authority() or is_moving or player.action_points <= 0:
return false
# Check if player is frozen
if player.get("is_frozen") or player.get("is_stop_frozen"):
return false
# BLOCK MOVEMENT IF MATCH NOT ACTIVE (Countdown)
var main = player.get_tree().root.get_node_or_null("Main")
if main and main.goals_cycle_manager and not main.goals_cycle_manager.is_match_active:
return false
# Validate grid position is within bounds
if not enhanced_gridmap.is_position_valid(grid_position):
return false
# Check finish line logic
if player.has_method("can_move_to_finish") and not player.can_move_to_finish(grid_position):
return false
# This function seems to rely on pathfinding or just direct move if adjacent?
# The original code for move_player_to_clicked_position called simple_move_to if adjacent?
# Actually original code for move_player_to_clicked_position wasn't fully shown in previous view_file.
# Let's assume it uses pathfinding if not adjacent, or just validates and moves.
# For now, let's just try simple move if it's adjacent
return simple_move_to(grid_position)
func is_within_movement_range(target_position: Vector2i) -> bool:
var distance: int
if use_diagonal_movement:
distance = max(abs(target_position.x - player.current_position.x), abs(target_position.y - player.current_position.y))
else:
distance = abs(target_position.x - player.current_position.x) + abs(target_position.y - player.current_position.y)
return distance <= movement_range
# Update highlight_movement_range to respect the expanded obstacle blocking
func highlight_movement_range():
if not player.is_multiplayer_authority() or player.is_bot or player.is_in_group("Bots"):
return
# Prevent recursive highlighting
if player._is_highlighting:
return
player._is_highlighting = true
player.clear_highlights()
var cells_to_highlight = []
# First, identify all cells that are blocked by obstacles
var blocked_cells = []
# Obstacle blocking logic removed
# Now highlight all cells within movement range that aren't blocked
for x in range(max(0, player.current_position.x - movement_range),
min(enhanced_gridmap.columns, player.current_position.x + movement_range + 1)):
for z in range(max(0, player.current_position.y - movement_range),
min(enhanced_gridmap.rows, player.current_position.y + movement_range + 1)):
var test_pos = Vector2i(x, z)
# Skip current position
if test_pos == player.current_position:
continue
# Check if within movement range
if is_within_movement_range(test_pos):
# Skip if blocked by obstacle
if test_pos in blocked_cells:
continue
# Check basic walkability
var cell_item = enhanced_gridmap.get_cell_item(Vector3i(x, 0, z))
# Allow passing through Walls (Item 4) if Invisible
var is_wall_passable = player.get("is_invisible") and cell_item == 4
if (cell_item == -1 or cell_item in enhanced_gridmap.non_walkable_items) and not is_wall_passable:
continue
if player.is_position_occupied(test_pos):
continue
# Check if there's a valid path to this cell
if can_reach_cell(test_pos, blocked_cells):
cells_to_highlight.append(test_pos)
# At the end of the function:
player.highlight_cells_if_authorized(cells_to_highlight)
player._is_highlighting = false
# Helper function to check if a cell can be reached given the blocked cells
func can_reach_cell(target_pos: Vector2i, blocked_cells: Array) -> bool:
# Simple BFS to find if there's a path
var queue = [player.current_position]
var visited = {player.current_position: true}
var steps = {player.current_position: 0}
while not queue.is_empty():
var current = queue.pop_front()
# If we've found the target, check if it's within movement range
if current == target_pos:
return steps[current] <= movement_range
# If we've used all movement, don't explore further
if steps[current] >= movement_range:
continue
# Try all adjacent cells
var directions = [
Vector2i(0, -1), # North
Vector2i(1, 0), # East
Vector2i(0, 1), # South
Vector2i(-1, 0), # West
]
# Add diagonal directions if enabled
if enhanced_gridmap.diagonal_movement:
directions.append(Vector2i(-1, -1)) # Northwest
directions.append(Vector2i(1, -1)) # Northeast
directions.append(Vector2i(-1, 1)) # Southwest
directions.append(Vector2i(1, 1)) # Southeast
for dir in directions:
var next_pos = current + dir
# Skip if already visited, blocked, or not valid
if visited.has(next_pos) or next_pos in blocked_cells:
continue
# Custom Walkable Check incorporating invisibility
var cell_item = enhanced_gridmap.get_cell_item(Vector3i(next_pos.x, 0, next_pos.y))
var is_wall_passable = player.get("is_invisible") and cell_item == 4
if (not enhanced_gridmap.is_position_valid(next_pos) or \
(cell_item in enhanced_gridmap.non_walkable_items and not is_wall_passable) or \
cell_item == -1):
continue
if player.is_position_occupied(next_pos) and next_pos != target_pos:
continue
# Check if movement between cells is blocked by an obstacle
# if not is_diagonal_direction(dir) and enhanced_gridmap.is_movement_blocked(current, next_pos, 3):
# continue
# For diagonal movement, check if both orthogonal paths are blocked
if is_diagonal_direction(dir):
var mid1 = Vector2i(next_pos.x, current.y)
var mid2 = Vector2i(current.x, next_pos.y)
var path1_blocked = mid1 in blocked_cells # or enhanced_gridmap.is_movement_blocked(current, mid1, 3)
var path2_blocked = mid2 in blocked_cells # or enhanced_gridmap.is_movement_blocked(current, mid2, 3)
if path1_blocked and path2_blocked:
continue
# Add to queue
queue.append(next_pos)
visited[next_pos] = true
steps[next_pos] = steps[current] + 1
return false
# Helper function to check if a direction is diagonal
func is_diagonal_direction(direction: Vector2i) -> bool:
return direction.x != 0 and direction.y != 0
func highlight_adjacent_cells():
if not player.is_multiplayer_authority() or player.is_bot or player.is_in_group("Bots"):
return
var cells_to_highlight = []
# Add current position if item exists
var current_cell = Vector3i(player.current_position.x, 1, player.current_position.y)
if enhanced_gridmap.get_cell_item(current_cell) != -1:
cells_to_highlight.append(player.current_position)
# Add valid neighbors
var neighbors = enhanced_gridmap.get_neighbors(player.current_position, 0)
for neighbor in neighbors:
if neighbor.is_walkable:
var cell_pos = neighbor.position
if enhanced_gridmap.get_cell_item(Vector3i(cell_pos.x, 1, cell_pos.y)) != -1:
cells_to_highlight.append(cell_pos)
player.highlight_cells_if_authorized(cells_to_highlight)
func _is_position_in_static_stand_area(pos: Vector2i) -> bool:
# Check against all known Static Tekton Stands (3x3 areas)
var stands = player.get_tree().get_nodes_in_group("StaticTektonStands")
print("[Debug] Checking Push Prevention for %s. Found %d stands." % [pos, stands.size()])
for stand in stands:
if not enhanced_gridmap: continue
# Convert world to grid. Use global_position just to be safe.
var local_pos = enhanced_gridmap.to_local(stand.global_position)
var stand_grid_pos = enhanced_gridmap.local_to_map(local_pos)
# Stand is centered, so key check is 3x3 around it
var center = Vector2i(stand_grid_pos.x, stand_grid_pos.z)
# Check if pos is right on top of stand (distance 0) or adjacent (distance 1)
# Chebyshev distance <= 1 means 3x3 square
if abs(pos.x - center.x) <= 1 and abs(pos.y - center.y) <= 1:
print(" - BLOCKED by Stand at %s (Center: %s)" % [stand.name, center])
return true
return false
func _can_push_to(pos: Vector2i) -> bool:
"""Helper to validate if a grid position is a safe landing spot for a push."""
if not enhanced_gridmap or not enhanced_gridmap.is_position_valid(pos):
return false
var cell_item = enhanced_gridmap.get_cell_item(Vector3i(pos.x, 0, pos.y))
# Must be walkable and NOT in non_walkable_items (to prevent getting stuck on walls/blocks)
if cell_item == -1 or cell_item in enhanced_gridmap.non_walkable_items:
return false
if player.is_position_occupied(pos):
return false
if _is_position_in_static_stand_area(pos):
return false
if _is_position_blocked_by_physics(pos):
return false
return true
func _is_position_blocked_by_physics(target_pos: Vector2i) -> bool:
if not player.is_inside_tree(): return false
var space_state = player.get_world_3d().direct_space_state
var center_x = target_pos.x + 0.5
var center_z = target_pos.y + 0.5
var from = Vector3(center_x, 1.0, center_z)
var to = Vector3(center_x, 0.1, center_z)
var query = PhysicsRayQueryParameters3D.create(from, to)
query.collide_with_areas = false
query.collide_with_bodies = true
var result = space_state.intersect_ray(query)
if result:
if result.collider != player:
# ONLY block if it's a Static Tekton Stand
# Ignore GridMap floors/walls, which are handled by get_cell_item rules
if result.collider.name.find("StaticTektonStand") != -1 or result.collider.is_in_group("StaticTektonStands") or result.collider.has_method("is_stand"):
return true
return false