네트워크 멀티플레이 대응
This commit is contained in:
103
Assets/Scripts/PlayerSpawnPositionSetter.cs
Normal file
103
Assets/Scripts/PlayerSpawnPositionSetter.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Northbound
|
||||
{
|
||||
public class PlayerSpawnPositionSetter : NetworkBehaviour
|
||||
{
|
||||
public static PlayerSpawnPositionSetter Instance { get; private set; }
|
||||
|
||||
[Header("Spawn Settings")]
|
||||
public List<Transform> spawnPoints = new List<Transform>();
|
||||
public bool useRandomSpawn = false;
|
||||
public bool findSpawnPointsAutomatically = true;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance != null && Instance != this)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
Instance = this;
|
||||
|
||||
if (findSpawnPointsAutomatically)
|
||||
{
|
||||
FindSpawnPoints();
|
||||
}
|
||||
}
|
||||
|
||||
private void FindSpawnPoints()
|
||||
{
|
||||
PlayerSpawnPoint[] points = FindObjectsByType<PlayerSpawnPoint>(FindObjectsSortMode.None);
|
||||
var sortedPoints = points.OrderBy(p => p.spawnIndex == -1 ? int.MaxValue : p.spawnIndex);
|
||||
|
||||
spawnPoints.Clear();
|
||||
foreach (var point in sortedPoints)
|
||||
{
|
||||
if (point.isAvailable)
|
||||
{
|
||||
spawnPoints.Add(point.transform);
|
||||
}
|
||||
}
|
||||
|
||||
Debug.Log($"<color=cyan>[SpawnPositionSetter] {spawnPoints.Count}개의 스폰 포인트를 찾았습니다.</color>");
|
||||
}
|
||||
|
||||
public Vector3 GetSpawnPosition(ulong clientId)
|
||||
{
|
||||
if (spawnPoints.Count == 0)
|
||||
{
|
||||
Debug.LogWarning("[SpawnPositionSetter] 스폰 포인트가 없습니다. 기본 위치 반환.");
|
||||
return Vector3.zero;
|
||||
}
|
||||
|
||||
int spawnIndex = GetSpawnIndexForClient(clientId);
|
||||
|
||||
Debug.Log($"<color=yellow>[SpawnPositionSetter] 클라이언트 {clientId}에게 스폰 인덱스 {spawnIndex} 할당</color>");
|
||||
return spawnPoints[spawnIndex].position;
|
||||
}
|
||||
|
||||
private int GetSpawnIndexForClient(ulong clientId)
|
||||
{
|
||||
if (useRandomSpawn)
|
||||
{
|
||||
return Random.Range(0, spawnPoints.Count);
|
||||
}
|
||||
|
||||
int spawnIndex;
|
||||
if (IsServer)
|
||||
{
|
||||
spawnIndex = GetAssignedSpawnIndexServer(clientId);
|
||||
}
|
||||
else
|
||||
{
|
||||
spawnIndex = (int)(clientId % (ulong)spawnPoints.Count);
|
||||
}
|
||||
|
||||
return spawnIndex;
|
||||
}
|
||||
|
||||
private int GetAssignedSpawnIndexServer(ulong clientId)
|
||||
{
|
||||
List<ulong> connectedClientIds = new List<ulong>(NetworkManager.Singleton.ConnectedClientsIds);
|
||||
connectedClientIds.Sort();
|
||||
|
||||
int index = connectedClientIds.IndexOf(clientId);
|
||||
if (index < 0) index = 0;
|
||||
|
||||
return index % spawnPoints.Count;
|
||||
}
|
||||
|
||||
public Quaternion GetSpawnRotation(ulong clientId)
|
||||
{
|
||||
if (spawnPoints.Count == 0)
|
||||
return Quaternion.identity;
|
||||
|
||||
int spawnIndex = GetSpawnIndexForClient(clientId);
|
||||
return spawnPoints[spawnIndex].rotation;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user