New player model with anims!!!!!!

This commit is contained in:
Twirpytherobot
2025-11-16 18:42:06 +00:00
parent 9d5feb6f0e
commit 3431c12dc2
10 changed files with 301 additions and 49 deletions
+94 -9
View File
@@ -11,6 +11,8 @@ extends Node3D
# Weapon spawning counter (server-side only)
var _weapon_spawn_counter: int = 0
# Track active weapons for late-join sync (server-side only)
var _active_weapons: Dictionary = {} # weapon_id -> WorldWeapon reference
# multiplayer chat
@onready var message: LineEdit = $MultiplayerChat/VBoxContainer/HBoxContainer/Message
@@ -53,8 +55,11 @@ func _spawn_initial_weapons():
# Wait a frame for everything to be ready
await get_tree().process_frame
print("[Server] _spawn_initial_weapons - Connected peers: ", multiplayer.get_peers())
# Spawn a sword
_weapon_spawn_counter += 1
print("[Server] Calling RPC to spawn sword with ID: ", _weapon_spawn_counter)
rpc("spawn_world_weapon",
"res://level/resources/weapon_sword.tres",
Vector3(5, 1, 0),
@@ -64,6 +69,7 @@ func _spawn_initial_weapons():
# Spawn a shield
_weapon_spawn_counter += 1
print("[Server] Calling RPC to spawn shield with ID: ", _weapon_spawn_counter)
rpc("spawn_world_weapon",
"res://level/resources/weapon_shield.tres",
Vector3(-5, 1, 0),
@@ -72,13 +78,24 @@ func _spawn_initial_weapons():
)
func _on_player_connected(peer_id, player_info):
# for id in Network.players.keys():
# var player_data = Network.players[id]
# if id != peer_id:
# rpc_id(peer_id, "sync_player_skin", id, player_data["skin"])
_add_player(peer_id, player_info)
# Sync existing weapons to the newly joined player (but not to server itself)
if multiplayer.is_server() and peer_id != 1:
print("[Server] Syncing weapons to newly connected peer: ", peer_id)
print("[Server] Active weapons count: ", _active_weapons.size())
for weapon_id in _active_weapons.keys():
var weapon = _active_weapons[weapon_id]
if is_instance_valid(weapon) and weapon.weapon_data:
print("[Server] Sending weapon ", weapon_id, " to peer ", peer_id)
# Send current position and zero velocity for syncing
rpc_id(peer_id, "_client_spawn_weapon",
weapon.weapon_data.resource_path,
weapon.global_position,
Vector3.ZERO,
weapon_id
)
func _on_host_pressed():
menu.hide()
Network.start_host(nick_input.text.strip_edges(), skin_input.text.strip_edges().to_lower())
@@ -206,8 +223,10 @@ func _on_jemz_preset():
# ---------- WEAPON SPAWNING ----------
## Spawn a weapon in the world (called from server, syncs to all clients)
@rpc("authority", "call_local", "reliable")
@rpc("any_peer", "call_local", "reliable")
func spawn_world_weapon(weapon_data_path: String, spawn_position: Vector3, initial_velocity: Vector3, weapon_id: int):
print("[Client ", multiplayer.get_unique_id(), "] spawn_world_weapon called for weapon_id: ", weapon_id)
if not weapons_container:
push_error("WeaponsContainer not found in level!")
return
@@ -221,15 +240,81 @@ func spawn_world_weapon(weapon_data_path: String, spawn_position: Vector3, initi
# Create WorldWeapon instance
var world_weapon = WorldWeapon.new()
world_weapon.weapon_data = weapon_data
print("[DEBUG] About to set weapon_id. Parameter weapon_id = ", weapon_id)
world_weapon.weapon_id = weapon_id # Store the ID
print("[DEBUG] After setting weapon_id. world_weapon.weapon_id = ", world_weapon.weapon_id)
world_weapon.name = "WorldWeapon_" + str(weapon_id) # Deterministic name
print("[DEBUG] Set weapon name to: ", world_weapon.name, " using weapon_id: ", weapon_id)
world_weapon.position = spawn_position
# Add to weapons container
weapons_container.add_child(world_weapon, true)
# Remove existing weapon with same name if it exists (prevents duplicates)
var weapon_path = NodePath(world_weapon.name)
if weapons_container.has_node(weapon_path):
print("[DEBUG] Weapon ", world_weapon.name, " already exists, removing old one first")
var old_weapon = weapons_container.get_node(weapon_path)
weapons_container.remove_child(old_weapon)
old_weapon.queue_free()
print("Spawned weapon: ", world_weapon.name, " at ", spawn_position)
# Add to weapons container
weapons_container.add_child(world_weapon)
print("[DEBUG] After add_child, weapon name in tree: ", world_weapon.name, " weapon_id: ", world_weapon.weapon_id)
# Track this weapon on the server (AFTER adding to tree so signals work)
if multiplayer.is_server():
_active_weapons[weapon_id] = world_weapon
print("[Server] Added weapon ", weapon_id, " to _active_weapons. Total: ", _active_weapons.size())
# Connect to the weapon's removal signal
world_weapon.tree_exiting.connect(_on_weapon_removed.bind(weapon_id))
print("[Peer ", multiplayer.get_unique_id(), "] Spawned weapon: ", world_weapon.name, " at ", spawn_position)
# Apply velocity after physics is ready
if initial_velocity != Vector3.ZERO:
await get_tree().process_frame
world_weapon.linear_velocity = initial_velocity
## Remove a world weapon from all clients (called by WorldWeapon when picked up)
func remove_world_weapon(weapon_id: int):
print("[Server] remove_world_weapon called for weapon_id: ", weapon_id)
if not multiplayer.is_server():
print("[ERROR] remove_world_weapon called on client!")
return
# Broadcast removal to all clients
print("[Server] Broadcasting removal RPC to all clients")
rpc("_remove_weapon_on_clients", weapon_id)
## RPC to remove weapon on all clients
@rpc("any_peer", "call_local", "reliable")
func _remove_weapon_on_clients(weapon_id: int):
print("[Peer ", multiplayer.get_unique_id(), "] _remove_weapon_on_clients called for weapon_id: ", weapon_id)
var weapon_name = "WorldWeapon_" + str(weapon_id)
if weapons_container and weapons_container.has_node(weapon_name):
var weapon = weapons_container.get_node(weapon_name)
print("[Peer ", multiplayer.get_unique_id(), "] Removing weapon ", weapon_name)
weapon.queue_free()
else:
print("[Peer ", multiplayer.get_unique_id(), "] WARNING: Weapon ", weapon_name, " not found in WeaponsContainer")
## Called when a weapon is removed (picked up or destroyed)
func _on_weapon_removed(weapon_id: int):
if not multiplayer.is_server():
return
# Remove from tracking
if _active_weapons.has(weapon_id):
_active_weapons.erase(weapon_id)
print("Removed weapon ", weapon_id, " from active tracking")
## Client-only spawn (called via rpc_id for late-join sync)
@rpc("any_peer", "reliable")
func _client_spawn_weapon(weapon_data_path: String, spawn_position: Vector3, initial_velocity: Vector3, weapon_id: int):
print("[Client ", multiplayer.get_unique_id(), "] _client_spawn_weapon received for weapon_id: ", weapon_id)
# This only runs on clients, not server (no call_local)
if multiplayer.is_server():
print("[ERROR] _client_spawn_weapon called on server!")
return
# Call the regular spawn function to create the weapon
print("[Client ", multiplayer.get_unique_id(), "] Calling spawn_world_weapon locally")
spawn_world_weapon(weapon_data_path, spawn_position, initial_velocity, weapon_id)