코어, 자원, 벽, 플레이어 시작 위치 설정
Flatkit 기능 일부 활용 테스트
This commit is contained in:
121
Assets/Scripts/NetworkConnectionHandler.cs
Normal file
121
Assets/Scripts/NetworkConnectionHandler.cs
Normal file
@@ -0,0 +1,121 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Northbound
|
||||
{
|
||||
/// <summary>
|
||||
/// 네트워크 연결 및 스폰 관리
|
||||
/// </summary>
|
||||
public class NetworkConnectionHandler : MonoBehaviour
|
||||
{
|
||||
[Header("Spawn Settings")]
|
||||
public List<Transform> spawnPoints = new List<Transform>();
|
||||
public bool useRandomSpawn = false;
|
||||
public bool findSpawnPointsAutomatically = true;
|
||||
|
||||
private Dictionary<ulong, int> _clientSpawnIndices = new Dictionary<ulong, int>();
|
||||
private int _nextSpawnIndex = 0;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (findSpawnPointsAutomatically)
|
||||
{
|
||||
FindSpawnPoints();
|
||||
}
|
||||
|
||||
if (NetworkManager.Singleton != null)
|
||||
{
|
||||
NetworkManager.Singleton.ConnectionApprovalCallback = ApprovalCheck;
|
||||
NetworkManager.Singleton.OnServerStarted += OnServerStarted;
|
||||
}
|
||||
}
|
||||
|
||||
private void FindSpawnPoints()
|
||||
{
|
||||
PlayerSpawnPoint[] points = FindObjectsByType<PlayerSpawnPoint>(FindObjectsSortMode.None);
|
||||
|
||||
// spawnIndex로 정렬
|
||||
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>[Connection] {spawnPoints.Count}개의 스폰 포인트를 찾았습니다.</color>");
|
||||
}
|
||||
|
||||
private void OnServerStarted()
|
||||
{
|
||||
Debug.Log("<color=green>[Connection] 서버 시작됨</color>");
|
||||
}
|
||||
|
||||
private void ApprovalCheck(
|
||||
NetworkManager.ConnectionApprovalRequest request,
|
||||
NetworkManager.ConnectionApprovalResponse response)
|
||||
{
|
||||
response.Approved = true;
|
||||
response.CreatePlayerObject = true;
|
||||
|
||||
// 스폰 위치 설정
|
||||
response.Position = GetSpawnPosition(request.ClientNetworkId);
|
||||
response.Rotation = GetSpawnRotation(request.ClientNetworkId);
|
||||
|
||||
Debug.Log($"<color=cyan>[Connection] 클라이언트 {request.ClientNetworkId} 승인됨. 스폰 위치: {response.Position}</color>");
|
||||
}
|
||||
|
||||
private Vector3 GetSpawnPosition(ulong clientId)
|
||||
{
|
||||
if (spawnPoints.Count == 0)
|
||||
{
|
||||
Debug.LogWarning("[Connection] 스폰 포인트가 없습니다. 기본 위치 반환.");
|
||||
return Vector3.zero;
|
||||
}
|
||||
|
||||
int spawnIndex;
|
||||
|
||||
if (useRandomSpawn)
|
||||
{
|
||||
spawnIndex = Random.Range(0, spawnPoints.Count);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!_clientSpawnIndices.ContainsKey(clientId))
|
||||
{
|
||||
_clientSpawnIndices[clientId] = _nextSpawnIndex;
|
||||
_nextSpawnIndex = (_nextSpawnIndex + 1) % spawnPoints.Count;
|
||||
}
|
||||
spawnIndex = _clientSpawnIndices[clientId];
|
||||
}
|
||||
|
||||
return spawnPoints[spawnIndex].position;
|
||||
}
|
||||
|
||||
private Quaternion GetSpawnRotation(ulong clientId)
|
||||
{
|
||||
if (spawnPoints.Count == 0)
|
||||
return Quaternion.identity;
|
||||
|
||||
int spawnIndex = _clientSpawnIndices.ContainsKey(clientId)
|
||||
? _clientSpawnIndices[clientId]
|
||||
: 0;
|
||||
|
||||
return spawnPoints[spawnIndex].rotation;
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (NetworkManager.Singleton != null)
|
||||
{
|
||||
NetworkManager.Singleton.ConnectionApprovalCallback = null;
|
||||
NetworkManager.Singleton.OnServerStarted -= OnServerStarted;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user