removed lots of prints... fixed so pot positioning is synced

This commit is contained in:
2025-12-26 01:08:04 +01:00
parent 5f866b718f
commit b85b4fd447
12 changed files with 360 additions and 248 deletions

View File

@@ -11,11 +11,9 @@ var object_name = "bomb"
@export var is_being_lifted = false:
set(value):
is_being_lifted = value
Console.print("Pot is_being_lifted changed to: ", value, " on peer: ", multiplayer.get_unique_id())
@export var is_being_put_down = false:
set(value):
is_being_put_down = value
Console.print("Pot is_being_put_down changed to: ", value, " on peer: ", multiplayer.get_unique_id())
@export var is_being_grabbed = false
@export var is_moving = false
@export var is_spawning = false
@@ -49,24 +47,26 @@ var re_enable_collision_after_time = 0.0
var re_enable_time = 0.17
var previousFrameVel = Vector2.ZERO
var hasShownSmokePuffs = false
var grab_follow_speed = 15.0 # Speed at which pot follows holder when grabbed
var previous_holder_position = Vector2.ZERO # Track previous holder position to calculate movement delta
var previous_client_position = Vector2.ZERO # Track previous position on client for movement detection
var push_lead_factor = 1.1 # When pushing, pot moves slightly ahead (110% of player movement)
var min_push_distance = 12.0 # Minimum distance pot should maintain from player when pushing to avoid blocking
@export var holder_peer_id: int = 0:
set(value):
holder_peer_id = value
Console.print("Pot holder_peer_id changed to: ", value, " on peer: ", multiplayer.get_unique_id())
# Clear lifting state when holder is cleared, but only if we're not in the middle of lifting
if value == 0 and !is_being_lifted:
is_being_lifted = false
# Clear state when holder is cleared
if value == 0:
holder = null
# Always clear grabbed and lifted states when holder is cleared
is_being_grabbed = false
is_being_lifted = false
elif value != 0:
# Find the holder by peer ID
var spawn_root = get_tree().get_current_scene().get_node("SpawnRoot")
if spawn_root:
holder = spawn_root.get_node_or_null(str(value))
if holder:
Console.print("Pot found holder: ", holder.name, " for peer ID: ", value)
else:
Console.print("Pot failed to find holder for peer ID: ", value)
func _ready() -> void:
if is_spawning:
@@ -83,21 +83,21 @@ func _ready() -> void:
func _physics_process(delta: float) -> void:
# Update holder based on holder_peer_id for network sync
# CRITICAL: holder_peer_id is the source of truth - if it's 0, there's no holder
if holder_peer_id != 0:
var player = get_tree().get_current_scene().get_node("SpawnRoot").get_node_or_null(str(holder_peer_id))
if player and holder != player:
Console.print("Pot updating holder from ", holder, " to ", player.name)
holder = player
# Ensure lifting state is synced when holder changes
if !is_being_lifted and !is_being_grabbed:
is_being_lifted = true
lift_progress = 0.0
elif !player:
Console.print("Pot could not find player with peer_id: ", holder_peer_id)
elif holder_peer_id == 0 and holder != null:
Console.print("Pot clearing holder (holder_peer_id is 0)")
holder = null
is_being_lifted = false
elif holder_peer_id == 0:
# No holder - clear everything
if holder != null:
holder = null
if is_being_grabbed:
is_being_grabbed = false
if is_being_lifted:
is_being_lifted = false
if velocity != Vector2.ZERO:
velocity = Vector2.ZERO
# Handle lifted pot position on ALL clients for smooth following
if is_being_lifted and holder:
@@ -118,7 +118,6 @@ func _physics_process(delta: float) -> void:
else:
# Debug: Check why pot is not following
if is_being_lifted and !holder:
Console.print("Pot is_being_lifted but holder is null! holder_peer_id: ", holder_peer_id)
# Fix inconsistent state
if holder_peer_id == 0:
is_being_lifted = false
@@ -128,7 +127,21 @@ func _physics_process(delta: float) -> void:
update_sprite_scale()
if multiplayer.is_server():
if is_being_thrown:
# CRITICAL: If holder_peer_id is 0, ALWAYS clear grab state immediately
# This must happen before ANY logic runs to prevent movement after release
if holder_peer_id == 0:
is_being_grabbed = false
if holder != null:
holder = null
if velocity != Vector2.ZERO:
velocity = Vector2.ZERO
if is_moving:
is_moving = false
if $SfxDrag2.playing:
$SfxDrag2.stop()
# Skip all grab logic if holder_peer_id is 0
pass
elif is_being_thrown:
re_enable_collision_after_time -= delta
if re_enable_collision_after_time <= 0.0:
# enable collisions again
@@ -187,23 +200,96 @@ func _physics_process(delta: float) -> void:
positionZ = lift_height * lift_progress
update_sprite_scale()
elif is_being_grabbed:
#if velocity != Vector2.ZERO and velocity != previousFrameVel and hasShownSmokePuffs == false:
if velocity != Vector2.ZERO:
is_moving = true
#hasShownSmokePuffs = true
$GPUParticles2D.emitting = true
$GPUParticles2D/TimerSmokeParticles.start()
if !$SfxDrag2.playing:
$SfxDrag2.play()
# CRITICAL: Only follow if holder_peer_id is NOT 0 (this is the source of truth)
# Check holder_peer_id FIRST before is_being_grabbed to ensure we never follow when released
# ONLY run this on server (pot has authority 1 = server)
# DOUBLE CHECK: holder_peer_id must be non-zero AND holder must exist AND match
if holder_peer_id != 0 and is_being_grabbed and holder != null:
# Only follow if holder's authority matches holder_peer_id
if holder.get_multiplayer_authority() == holder_peer_id:
# Calculate how much the player has moved since last frame
var player_movement = holder.global_position - previous_holder_position
# Only move pot if player has moved
if player_movement.length() > 0.01:
# Get the locked grab direction to determine push/pull axis
var locked_dir = Vector2.ZERO
if "locked_grab_direction" in holder and holder.locked_grab_direction != Vector2.ZERO:
locked_dir = holder.locked_grab_direction.normalized()
if locked_dir != Vector2.ZERO:
# Project player's movement onto the push/pull direction
var movement_along_axis = player_movement.project(locked_dir)
# Determine if pushing (moving in locked_dir direction) or pulling (moving opposite)
var is_pushing = movement_along_axis.dot(locked_dir) > 0
if is_pushing:
# PUSHING: Pot moves first, ahead of player
# Move pot by slightly more than player movement so it leads (moves first)
var push_movement = movement_along_axis * push_lead_factor
global_position += push_movement
velocity = push_movement / delta if delta > 0 else Vector2.ZERO
else:
# PULLING: Player moves first, pot follows behind
# Move pot by the exact same amount as player (pot follows immediately)
global_position += movement_along_axis
velocity = movement_along_axis / delta if delta > 0 else Vector2.ZERO
else:
# No locked direction, just move pot by player's movement
global_position += player_movement
velocity = player_movement / delta if delta > 0 else Vector2.ZERO
# Update previous position for next frame
previous_holder_position = holder.global_position
if velocity.length() > 1.0:
is_moving = true
$GPUParticles2D.emitting = true
$GPUParticles2D/TimerSmokeParticles.start()
if !$SfxDrag2.playing:
$SfxDrag2.play()
else:
is_moving = false
if $SfxDrag2.playing:
$SfxDrag2.stop()
previousFrameVel = velocity
else:
# Player hasn't moved - keep pot at current position
velocity = Vector2.ZERO
is_moving = false
if $SfxDrag2.playing:
$SfxDrag2.stop()
# Still update previous position even if not moving
previous_holder_position = holder.global_position
else:
# No valid holder, clear grab state and stop movement immediately
is_being_grabbed = false
is_moving = false
if $SfxDrag2.playing:
$SfxDrag2.stop()
move_and_collide(velocity * delta)
previousFrameVel = velocity
velocity = Vector2.ZERO
holder = null
holder_peer_id = 0 # Force clear to ensure no following
pass
elif !is_being_lifted: # it just spawned or is free-falling:
# CRITICAL FINAL CHECK: If holder_peer_id is 0, STOP ALL MOVEMENT immediately
# This must run AFTER all grab logic to catch any cases where holder_peer_id was set to 0
# but the pot is still trying to move
if holder_peer_id == 0:
if is_being_grabbed:
is_being_grabbed = false
if holder != null:
holder = null
if velocity != Vector2.ZERO:
velocity = Vector2.ZERO
if is_moving:
is_moving = false
if $SfxDrag2.playing:
$SfxDrag2.stop()
# Only handle free-falling if we're not being held (holder_peer_id == 0 is the source of truth)
if holder_peer_id == 0 and !is_being_lifted: # it just spawned or is free-falling:
# Apply gravity to vertical movement
velocityZ += accelerationZ * delta
positionZ += velocityZ * delta
@@ -232,6 +318,13 @@ func _physics_process(delta: float) -> void:
update_sprite_scale()
pass
else:
# CRITICAL: If holder_peer_id is 0, ALWAYS clear grab state immediately (client side)
# This must happen before any grab logic runs
if holder_peer_id == 0:
is_being_grabbed = false
if holder != null:
holder = null
if is_fused and $AnimationPlayer.current_animation == "idle":
if $SfxBombFuse.playing:
$SfxBombFuse.play()
@@ -269,16 +362,44 @@ func _physics_process(delta: float) -> void:
global_position = target_position
positionZ = lift_height
update_sprite_scale()
elif is_being_grabbed:
if is_moving:
$GPUParticles2D.emitting = true
$GPUParticles2D/TimerSmokeParticles.start()
if !$SfxDrag2.playing:
$SfxDrag2.play()
# CRITICAL: On client, do NOT move the pot - only the server should move it
# The pot's position is synced via replication from the server
# We only handle visual/audio effects here
elif holder_peer_id == 0:
# No holder - ensure we're not in grabbed state
if is_being_grabbed:
is_being_grabbed = false
is_moving = false
if $SfxDrag2.playing:
$SfxDrag2.stop()
elif is_being_grabbed and holder_peer_id != 0:
# Only handle visual/audio effects on client, don't move the pot
# The server handles the actual movement and syncs position via replication
if holder != null and holder.get_multiplayer_authority() == holder_peer_id:
# Check if pot appears to be moving (based on position changes from server)
var pot_movement = global_position - previous_client_position
# If pot has moved, it's being pushed/pulled
if pot_movement.length() > 0.5:
is_moving = true
$GPUParticles2D.emitting = true
$GPUParticles2D/TimerSmokeParticles.start()
if !$SfxDrag2.playing:
$SfxDrag2.play()
else:
is_moving = false
if $SfxDrag2.playing:
$SfxDrag2.stop()
# Update previous position for next frame
previous_client_position = global_position
else:
# No valid holder, clear state
is_being_grabbed = false
is_moving = false
if $SfxDrag2.playing:
$SfxDrag2.stop()
else:
elif !is_being_grabbed:
if is_spawning:
if positionZ <= 0:
$SfxLand.play()
@@ -292,7 +413,6 @@ func _physics_process(delta: float) -> void:
pass
# Fix stuck put_down state
if is_being_put_down and lift_progress <= 0:
Console.print("Pot is_being_put_down but lift_progress is 0, clearing state")
is_being_put_down = false
update_sprite_scale()
if is_destroyed and !destroy_initiated:
@@ -341,7 +461,6 @@ func throw(direction: Vector2, initial_velocity: float = 200):
@rpc("any_peer", "reliable")
func throw_rpc(direction: Vector2, initial_velocity: float = 200):
Console.print("Pot throw_rpc called on peer: ", multiplayer.get_unique_id())
# Only execute on server to avoid conflicts
if multiplayer.is_server():
throw(direction, initial_velocity)
@@ -353,23 +472,70 @@ func grab(new_holder: CharacterBody2D) -> bool:
holder_peer_id = new_holder.get_multiplayer_authority()
is_being_grabbed = true
indicate(false)
# Initialize previous position to current position so we can track movement
previous_holder_position = new_holder.global_position
previous_client_position = global_position # Initialize client position tracking
# Disable pot's collision layer so grabber can't collide with it (prevents blocking when pushing)
# Note: This also means other players can't collide with pot while it's grabbed
# The pot uses direct position updates (not move_and_collide) so collisions aren't needed for movement
self.set_collision_layer_value(8, false) # Disable pot's collision layer
# Don't change pot's position - it should stay where it is and only move when player pushes/pulls
return true
return false
func release():
Console.print("Pot release() called on peer: ", multiplayer.get_unique_id())
holder = null
holder_peer_id = 0
is_being_grabbed = false
hasShownSmokePuffs = false
indicate(true)
if $SfxDrag2.playing:
$SfxDrag2.stop()
# Clear all grab-related state
# CRITICAL: If we're not the server, we need to notify the server to release
# The pot has authority 1 (server), so the server must be the one to clear holder_peer_id
if not multiplayer.is_server():
# Client requests server to release - use holder_peer_id directly
if holder_peer_id != 0:
request_release_pot.rpc_id(1, get_path(), holder_peer_id)
# Also clear locally for immediate visual feedback
holder = null
holder_peer_id = 0
is_being_grabbed = false
hasShownSmokePuffs = false
velocity = Vector2.ZERO
# Re-enable pot's collision layer when released
self.set_collision_layer_value(8, true) # Re-enable pot's collision layer
indicate(true)
if $SfxDrag2.playing:
$SfxDrag2.stop()
else:
# Server can release directly
holder = null
holder_peer_id = 0
is_being_grabbed = false
hasShownSmokePuffs = false
velocity = Vector2.ZERO
# Re-enable pot's collision layer when released
self.set_collision_layer_value(8, true) # Re-enable pot's collision layer
indicate(true)
if $SfxDrag2.playing:
$SfxDrag2.stop()
pass
@rpc("any_peer", "reliable")
func request_release_pot(pot_path: NodePath, peer_id: int):
if multiplayer.is_server():
var pot = get_node_or_null(pot_path)
var player = get_tree().get_current_scene().get_node("SpawnRoot").get_node_or_null(str(peer_id))
if pot and player:
# Check if the pot is being held by this player
if pot.holder_peer_id == peer_id or (pot.holder != null and pot.holder.get_multiplayer_authority() == peer_id):
pot.holder = null
pot.holder_peer_id = 0
pot.is_being_grabbed = false
pot.velocity = Vector2.ZERO
# Re-enable pot's collision layer when released
pot.set_collision_layer_value(8, true) # Re-enable pot's collision layer
pot.indicate(true)
if $SfxDrag2.playing:
$SfxDrag2.stop()
pass
func lift(new_holder: CharacterBody2D):
Console.print("Pot lift() called with holder: ", new_holder.name if new_holder else "null")
Console.print("Pot current authority: ", get_multiplayer_authority())
if (new_holder != holder and holder != null) and "lose_held_entity" in holder:
# steal from holder
holder.lose_held_entity(self)
@@ -393,20 +559,15 @@ func lift(new_holder: CharacterBody2D):
# Store initial position for smooth lifting - don't change current position yet
# The pot will smoothly glide from its current position to above the holder
$SfxLand.play()
Console.print("Pot lift() completed, is_being_lifted: ", is_being_lifted)
@rpc("any_peer", "reliable")
func lift_rpc(holder_path: NodePath):
Console.print("Pot lift_rpc called with holder_path: ", holder_path, " on peer: ", multiplayer.get_unique_id())
# Only execute on server to avoid conflicts
if multiplayer.is_server():
# Find the holder by path
var holder_node = get_node_or_null(holder_path)
if holder_node and holder_node is CharacterBody2D:
Console.print("Pot found holder, calling lift()")
lift(holder_node)
else:
Console.print("Pot failed to find holder at path: ", holder_path)
func put_down() -> bool:
if not is_being_lifted or is_being_put_down:
@@ -431,7 +592,6 @@ func put_down() -> bool:
var results = space_state.intersect_point(params)
if results.size() > 0:
# Found overlapping walls at target position
print("Cannot place pot: Wall in the way")
return false
# Second check: Line of sight between player and target position
@@ -446,7 +606,6 @@ func put_down() -> bool:
var result = space_state.intersect_ray(query)
if result:
# Hit something between player and target position
print("Cannot place pot: Path blocked")
return false
# Third check: Make sure we're not placing on top of another pot or object
@@ -454,7 +613,6 @@ func put_down() -> bool:
results = space_state.intersect_point(params)
if results.size() > 0:
# Found overlapping objects at target position
print("Cannot place pot: Object in the way")
return false
$Area2DCollision.set_deferred("monitoring", false)
@@ -554,7 +712,6 @@ func _on_area_2d_collision_body_entered(body: Node2D) -> void:
elif collider != self and "breakPot" in collider:
collider.take_damage(self, thrown_by)
# create particles from pot:
print("ye body is:", body)
take_damage.rpc(null, null)
pass
@@ -583,21 +740,20 @@ func request_grab_pot(pot_path: NodePath, peer_id: int):
var player = get_tree().get_current_scene().get_node("SpawnRoot").get_node_or_null(str(peer_id))
if pot and "grab" in pot and player:
if pot.grab(player):
# grab() function already disables collisions, but ensure it's done
player.grabbed_entity = pot
player.grabbed_entity_path = str(pot.get_path())
player.current_animation = "IDLE_PUSH"
# Sync animation and entity to all clients
#var all_players = get_tree().get_current_scene().get_node("SpawnRoot").get_children()
#for p in all_players:
#if p.has_method("sync_animation"):
#p.sync_animation.rpc("IDLE_PUSH")
#p.sync_grabbed_entity.rpc(str(pot.get_path()))
# Lock direction to current last_direction when grabbing
player.locked_grab_direction = player.last_direction
# Sync to all clients (including the joiner who requested it)
player.set_grabbed_entity_path_rpc.rpc(str(pot.get_path()))
player.sync_animation.rpc("IDLE_PUSH")
@rpc("any_peer", "reliable")
func request_lift_pot(pot_path: NodePath, peer_id: int):
func request_lift_pot(_pot_path: NodePath, _peer_id: int):
# This function is now handled by MultiplayerManager
# Keeping it for backward compatibility but it should not be called
print("Pot received request_lift_pot RPC - this should be handled by MultiplayerManager")
pass
@rpc("any_peer", "reliable")
@@ -605,11 +761,9 @@ func request_throw_pot(pot_path: NodePath, peer_id: int, direction: Vector2):
if multiplayer.is_server():
var pot = get_node_or_null(pot_path)
var player = get_tree().get_current_scene().get_node("SpawnRoot").get_node_or_null(str(peer_id))
print("Throw request: pot=", pot, " player=", player, " pot.holder_peer_id=", pot.holder_peer_id if pot else "null", " peer_id=", peer_id)
if pot and player:
# Check if the pot is being held by this player (either by holder_peer_id or by checking the holder directly)
if pot.holder_peer_id == peer_id or (pot.holder != null and pot.holder.get_multiplayer_authority() == peer_id):
print("Throw authorized for peer ", peer_id)
pot.throw(direction)
player.held_entity = null
player.held_entity_path = ""
@@ -622,8 +776,6 @@ func request_throw_pot(pot_path: NodePath, peer_id: int, direction: Vector2):
if p.has_method("sync_animation"):
p.sync_animation.rpc("THROW")
p.sync_held_entity.rpc("") # Clear held entity
else:
print("Throw denied: holder_peer_id mismatch or holder not found")
@rpc("call_local")
func take_damage(_iBody: Node2D, _iByWhoOrWhat: Node2D) -> void:
@@ -650,7 +802,6 @@ func sync_pot_state(lifted: bool, holder_id: int):
holder = null
else:
holder = null
print("Pot state synced: lifted=", lifted, " holder_id=", holder_id)
pass
func _on_area_2d_collision_body_exited(_body: Node2D) -> void: