using System; using Cinemachine; using Sirenix.OdinInspector; using Unity.VisualScripting; using UnityEngine; using UnityEngine.InputSystem; using UnityEngine.SceneManagement; using Random = UnityEngine.Random; // ReSharper disable once CheckNamespace namespace BlueWaterProject { public class SpawnController : MonoBehaviour { [SerializeField] private float instantiationDistance = 3f; [SerializeField] private float checkRadius = 1f; [SerializeField] private LayerMask checkLayer; [InlineButton("AddCrewmate", "Crewmate 추가")] [SerializeField] private int crewmateIndex; private Collider[] colliders = new Collider[MAX_COLLIDER]; private GameObject spawnPositionObj; private CinemachineVirtualCamera cinemachineVirtualCamera; private const string PLAYER_NAME = "Player"; private const string IN_ISLAND_PLAYER_NAME = "InIslandPlayer"; private const int MAX_COLLIDER = 10; private void Start() { spawnPositionObj = GameObject.Find("StageMap/SpawnPosition"); if (spawnPositionObj == null) { print("StageMap 또는 SpawnPosition 오브젝트를 찾을 수 없습니다."); return; } cinemachineVirtualCamera = GameObject.Find("Virtual Camera")?.GetComponent(); if (cinemachineVirtualCamera == null) { print("Virtual Camera 오브젝트를 찾을 수 없습니다."); return; } var currentSceneName = SceneManager.GetActiveScene().name; if (currentSceneName != "02.Main") { SpawnInIslandPlayer(spawnPositionObj.transform.position, GameManager.Inst.InIslandPlayerPrefab.transform.rotation); } } private void SpawnInIslandPlayer(Vector3 spawnPos, Quaternion spawnRotation) { var islandPlayer = Instantiate(GameManager.Inst.InIslandPlayerPrefab, spawnPos, spawnRotation); islandPlayer.name = IN_ISLAND_PLAYER_NAME; islandPlayer.gameObject.SetActive(true); var playerInput = islandPlayer.GetComponent(); if (playerInput == null) { playerInput = islandPlayer.transform.AddComponent(); } playerInput.actions = GameManager.Inst.PlayerAction; var desiredActionMap = playerInput.actions.FindActionMap(PLAYER_NAME); if (desiredActionMap == null) { print($"Action map named '{PLAYER_NAME}' not found in player actions!"); return; } playerInput.defaultActionMap = PLAYER_NAME; if (GameManager.Inst.shipPlayer != null) { GameManager.Inst.shipPlayer.GetComponent().enabled = false; } playerInput.SwitchCurrentActionMap(PLAYER_NAME); var inIslandPlayer = islandPlayer.GetComponent(); if (inIslandPlayer == null) { inIslandPlayer = islandPlayer.AddComponent(); } cinemachineVirtualCamera.Follow = islandPlayer.transform; cinemachineVirtualCamera.LookAt = islandPlayer.transform; GameManager.Inst.InIslandPlayer = inIslandPlayer; } private void AddCrewmate() { if (!GameManager.Inst.InIslandPlayer) return; if (crewmateIndex >= GameManager.Inst.CrewmatePrefabList.Count) { print("존재하지 않는 인덱스입니다."); return; } for (var i = 0; i < 50; i++) { var randomDirection = new Vector3(Random.Range(-1f, 1f), 0, Random.Range(-1f, 1f)).normalized; var spawnPos = GameManager.Inst.InIslandPlayer.transform.position + (randomDirection * instantiationDistance); Array.Clear(colliders, 0, MAX_COLLIDER); var size = Physics.OverlapSphereNonAlloc(spawnPos, checkRadius, colliders, checkLayer); if (size != 0) continue; var crewmate = Instantiate(GameManager.Inst.CrewmatePrefabList[crewmateIndex], spawnPos, GameManager.Inst.CrewmatePrefabList[crewmateIndex].transform.rotation); crewmate.gameObject.SetActive(true); GameManager.Inst.InIslandPlayer.CrewmateList.Add(crewmate); return; } print("소환할 수 있는 자리가 없습니다."); } } }