2023-10-17 07:31:10 +00:00
|
|
|
using System;
|
2023-10-05 07:56:53 +00:00
|
|
|
using Cinemachine;
|
2023-10-17 07:31:10 +00:00
|
|
|
using Sirenix.OdinInspector;
|
|
|
|
using Unity.VisualScripting;
|
2023-10-05 07:56:53 +00:00
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.InputSystem;
|
|
|
|
using UnityEngine.SceneManagement;
|
2023-10-17 07:31:10 +00:00
|
|
|
using Random = UnityEngine.Random;
|
2023-10-05 07:56:53 +00:00
|
|
|
|
|
|
|
// ReSharper disable once CheckNamespace
|
|
|
|
namespace BlueWaterProject
|
|
|
|
{
|
|
|
|
public class SpawnController : MonoBehaviour
|
|
|
|
{
|
2023-10-18 07:28:15 +00:00
|
|
|
[SerializeField] private float instantiationDistance = 3f;
|
|
|
|
[SerializeField] private float checkRadius = 1f;
|
2023-10-17 07:31:10 +00:00
|
|
|
[SerializeField] private LayerMask checkLayer;
|
|
|
|
|
2023-10-19 09:15:14 +00:00
|
|
|
[Title("Crewmate 생성")]
|
|
|
|
[InfoBox("$CrewmateIndexInfoMessage")]
|
2023-10-17 07:31:10 +00:00
|
|
|
[SerializeField] private int crewmateIndex;
|
|
|
|
|
|
|
|
private Collider[] colliders = new Collider[MAX_COLLIDER];
|
|
|
|
|
2023-10-05 07:56:53 +00:00
|
|
|
private GameObject spawnPositionObj;
|
|
|
|
private CinemachineVirtualCamera cinemachineVirtualCamera;
|
2023-10-23 06:11:28 +00:00
|
|
|
|
|
|
|
private Transform spawnLocation;
|
2023-10-19 09:15:14 +00:00
|
|
|
private string CrewmateIndexInfoMessage => $"0 ~ {GameManager.Inst.CrewmatePrefabList.Count - 1} 숫자를 입력해주세요.";
|
|
|
|
|
2023-10-05 07:56:53 +00:00
|
|
|
private const string PLAYER_NAME = "Player";
|
|
|
|
private const string IN_ISLAND_PLAYER_NAME = "InIslandPlayer";
|
2023-10-17 07:31:10 +00:00
|
|
|
private const int MAX_COLLIDER = 10;
|
2023-10-05 07:56:53 +00:00
|
|
|
|
|
|
|
private void Start()
|
|
|
|
{
|
|
|
|
spawnPositionObj = GameObject.Find("StageMap/SpawnPosition");
|
|
|
|
if (spawnPositionObj == null)
|
|
|
|
{
|
|
|
|
print("StageMap 또는 SpawnPosition 오브젝트를 찾을 수 없습니다.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cinemachineVirtualCamera = GameObject.Find("Virtual Camera")?.GetComponent<CinemachineVirtualCamera>();
|
|
|
|
if (cinemachineVirtualCamera == null)
|
|
|
|
{
|
|
|
|
print("Virtual Camera 오브젝트를 찾을 수 없습니다.");
|
|
|
|
return;
|
|
|
|
}
|
2023-10-23 06:11:28 +00:00
|
|
|
|
|
|
|
spawnLocation = GameObject.Find("Characters")?.transform;
|
|
|
|
if (spawnLocation == null)
|
|
|
|
{
|
|
|
|
spawnLocation = new GameObject("Characters").transform;
|
|
|
|
}
|
2023-10-05 07:56:53 +00:00
|
|
|
|
|
|
|
var currentSceneName = SceneManager.GetActiveScene().name;
|
|
|
|
if (currentSceneName != "02.Main")
|
|
|
|
{
|
2023-10-17 07:31:10 +00:00
|
|
|
SpawnInIslandPlayer(spawnPositionObj.transform.position, GameManager.Inst.InIslandPlayerPrefab.transform.rotation);
|
2023-10-05 07:56:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void SpawnInIslandPlayer(Vector3 spawnPos, Quaternion spawnRotation)
|
|
|
|
{
|
2023-10-23 06:11:28 +00:00
|
|
|
var islandPlayer = Instantiate(GameManager.Inst.InIslandPlayerPrefab, spawnPos, spawnRotation, spawnLocation);
|
2023-10-05 07:56:53 +00:00
|
|
|
islandPlayer.name = IN_ISLAND_PLAYER_NAME;
|
2023-10-12 06:32:02 +00:00
|
|
|
islandPlayer.gameObject.SetActive(true);
|
2023-10-05 07:56:53 +00:00
|
|
|
|
|
|
|
var playerInput = islandPlayer.GetComponent<PlayerInput>();
|
|
|
|
if (playerInput == null)
|
|
|
|
{
|
2023-10-17 07:31:10 +00:00
|
|
|
playerInput = islandPlayer.transform.AddComponent<PlayerInput>();
|
2023-10-05 07:56:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2023-10-23 03:12:13 +00:00
|
|
|
if (GameManager.Inst.ShipPlayer != null)
|
2023-10-05 07:56:53 +00:00
|
|
|
{
|
2023-10-23 03:12:13 +00:00
|
|
|
GameManager.Inst.ShipPlayer.GetComponent<PlayerInput>().enabled = false;
|
2023-10-05 07:56:53 +00:00
|
|
|
}
|
2023-10-23 06:11:28 +00:00
|
|
|
|
|
|
|
playerInput.enabled = true;
|
2023-10-05 07:56:53 +00:00
|
|
|
|
2023-10-11 20:26:45 +00:00
|
|
|
var inIslandPlayer = islandPlayer.GetComponent<InIslandPlayer>();
|
|
|
|
if (inIslandPlayer == null)
|
|
|
|
{
|
|
|
|
inIslandPlayer = islandPlayer.AddComponent<InIslandPlayer>();
|
|
|
|
}
|
2023-10-05 07:56:53 +00:00
|
|
|
|
2023-10-20 05:34:30 +00:00
|
|
|
inIslandPlayer.UseAgentMovement();
|
|
|
|
|
2023-10-23 04:33:27 +00:00
|
|
|
InIslandCamera.Inst.SetTarget(islandPlayer.transform);
|
2023-10-17 07:31:10 +00:00
|
|
|
|
2023-10-23 06:11:28 +00:00
|
|
|
GameManager.Inst.SetCurrentInIslandPlayer(inIslandPlayer);
|
2023-10-17 07:31:10 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 09:15:14 +00:00
|
|
|
[DisableIf("@crewmateIndex >= GameManager.Inst.CrewmatePrefabList.Count || crewmateIndex < 0")]
|
|
|
|
[Button("Crewmate 추가")]
|
2023-10-17 07:31:10 +00:00
|
|
|
private void AddCrewmate()
|
|
|
|
{
|
2023-10-23 06:11:28 +00:00
|
|
|
if (!GameManager.Inst.CurrentInIslandPlayer.GameObject) return;
|
2023-10-17 07:31:10 +00:00
|
|
|
|
2023-10-19 09:15:14 +00:00
|
|
|
if (crewmateIndex >= GameManager.Inst.CrewmatePrefabList.Count || crewmateIndex < 0)
|
2023-10-17 07:31:10 +00:00
|
|
|
{
|
|
|
|
print("존재하지 않는 인덱스입니다.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-18 07:28:15 +00:00
|
|
|
for (var i = 0; i < 50; i++)
|
2023-10-17 07:31:10 +00:00
|
|
|
{
|
2023-10-18 07:28:15 +00:00
|
|
|
var randomDirection = new Vector3(Random.Range(-1f, 1f), 0, Random.Range(-1f, 1f)).normalized;
|
2023-10-23 06:11:28 +00:00
|
|
|
var spawnPos = GameManager.Inst.CurrentInIslandPlayer.Transform.position + (randomDirection * instantiationDistance);
|
2023-10-17 07:31:10 +00:00
|
|
|
|
|
|
|
Array.Clear(colliders, 0, MAX_COLLIDER);
|
2023-10-18 07:28:15 +00:00
|
|
|
var size = Physics.OverlapSphereNonAlloc(spawnPos, checkRadius, colliders, checkLayer);
|
2023-10-17 07:31:10 +00:00
|
|
|
|
|
|
|
if (size != 0) continue;
|
|
|
|
|
|
|
|
var crewmate = Instantiate(GameManager.Inst.CrewmatePrefabList[crewmateIndex], spawnPos,
|
2023-10-23 06:11:28 +00:00
|
|
|
GameManager.Inst.CrewmatePrefabList[crewmateIndex].transform.rotation, spawnLocation);
|
2023-10-17 07:31:10 +00:00
|
|
|
crewmate.gameObject.SetActive(true);
|
|
|
|
|
2023-10-23 06:11:28 +00:00
|
|
|
GameManager.Inst.CurrentCrewmateList.Add(crewmate);
|
2023-10-17 07:31:10 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-10-18 07:28:15 +00:00
|
|
|
|
|
|
|
print("소환할 수 있는 자리가 없습니다.");
|
2023-10-05 07:56:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|