2023-08-15 20:36:04 +00:00
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.InputSystem;
|
|
|
|
|
|
|
|
// ReSharper disable once CheckNamespace
|
|
|
|
namespace BlueWaterProject
|
|
|
|
{
|
|
|
|
public class UnitSelection : MonoBehaviour
|
|
|
|
{
|
|
|
|
#region Property and variable
|
2023-09-13 07:05:21 +00:00
|
|
|
|
2023-08-15 20:36:04 +00:00
|
|
|
[Tooltip("선택된 부대")]
|
2023-09-13 07:05:21 +00:00
|
|
|
[field: SerializeField] public bool IsSelectable { get; private set; } = true;
|
|
|
|
[field: SerializeField] public PirateUnit SelectedPirateUnit { get; private set; }
|
2023-08-15 20:36:04 +00:00
|
|
|
|
|
|
|
[SerializeField] private LayerMask unitLayer;
|
|
|
|
[SerializeField] private LayerMask groundLayer;
|
|
|
|
|
2023-09-12 14:46:57 +00:00
|
|
|
private PirateUnit previousUnitController;
|
2023-08-15 20:36:04 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Unity built-in function
|
|
|
|
|
|
|
|
private void Reset()
|
|
|
|
{
|
2023-08-31 07:38:08 +00:00
|
|
|
unitLayer = LayerMask.GetMask("Pirate");
|
2023-08-15 20:36:04 +00:00
|
|
|
groundLayer = LayerMask.GetMask("Ground");
|
|
|
|
}
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
{
|
|
|
|
var controls = new BlueWater();
|
|
|
|
|
2023-09-20 04:54:25 +00:00
|
|
|
controls.Unit.SelectUnit.performed += OnSelectUnit;
|
|
|
|
controls.Unit.CancelSelectedUnit.performed += OnCancelSelectedUnit;
|
|
|
|
controls.Unit.MoveUnit.performed += OnMoveUnit;
|
2023-08-15 20:36:04 +00:00
|
|
|
controls.Enable();
|
|
|
|
|
2023-08-31 07:38:08 +00:00
|
|
|
unitLayer = LayerMask.GetMask("Pirate");
|
2023-08-15 20:36:04 +00:00
|
|
|
groundLayer = LayerMask.GetMask("Ground");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region New input system
|
|
|
|
|
2023-09-20 04:54:25 +00:00
|
|
|
private void OnSelectUnit(InputAction.CallbackContext context)
|
2023-08-15 20:36:04 +00:00
|
|
|
{
|
2023-09-13 07:05:21 +00:00
|
|
|
if (!context.performed || !IsSelectable) return;
|
2023-08-15 20:36:04 +00:00
|
|
|
|
2023-10-23 03:32:29 +00:00
|
|
|
var ray = CameraManager.Inst.MainCam.ScreenPointToRay(Mouse.current.position.ReadValue());
|
2023-08-15 20:36:04 +00:00
|
|
|
|
2023-08-17 07:57:46 +00:00
|
|
|
// 부대를 클릭 했을 때,
|
2023-09-13 07:05:21 +00:00
|
|
|
if (Physics.Raycast(ray, out var hit, Mathf.Infinity, unitLayer, QueryTriggerInteraction.Collide))
|
2023-08-15 20:36:04 +00:00
|
|
|
{
|
2023-09-13 07:05:21 +00:00
|
|
|
var pirateUnit = hit.collider.transform.parent.GetComponent<PirateUnit>();
|
|
|
|
if (pirateUnit == null) return;
|
2023-08-17 07:57:46 +00:00
|
|
|
|
|
|
|
// 선택된 부대가 없었을 때,
|
2023-09-13 07:05:21 +00:00
|
|
|
if (SelectedPirateUnit == null)
|
2023-08-15 20:36:04 +00:00
|
|
|
{
|
2023-09-13 07:05:21 +00:00
|
|
|
foreach (var pirateAi in pirateUnit.pirateUnitStat.PirateAiList)
|
2023-08-17 07:57:46 +00:00
|
|
|
{
|
2023-09-13 07:05:21 +00:00
|
|
|
pirateAi.SelectedHighlight();
|
2023-08-17 07:57:46 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 07:05:21 +00:00
|
|
|
SelectedPirateUnit = pirateUnit;
|
2023-08-17 07:57:46 +00:00
|
|
|
GameManager.Inst.SlowSpeedMode();
|
|
|
|
}
|
|
|
|
// 선택된 부대가 이미 선택된 부대일 때,
|
2023-09-13 07:05:21 +00:00
|
|
|
else if (SelectedPirateUnit == pirateUnit)
|
2023-08-17 07:57:46 +00:00
|
|
|
{
|
2023-09-13 07:05:21 +00:00
|
|
|
foreach (var pirateAi in SelectedPirateUnit.pirateUnitStat.PirateAiList)
|
2023-08-17 07:57:46 +00:00
|
|
|
{
|
2023-09-13 07:05:21 +00:00
|
|
|
pirateAi.ResetHighlight();
|
2023-08-17 07:57:46 +00:00
|
|
|
}
|
2023-08-15 20:36:04 +00:00
|
|
|
|
2023-09-13 07:05:21 +00:00
|
|
|
SelectedPirateUnit = null;
|
2023-08-17 07:57:46 +00:00
|
|
|
GameManager.Inst.DefaultSpeedMode();
|
|
|
|
}
|
|
|
|
// 다른 부대가 선택될 때,
|
|
|
|
else
|
|
|
|
{
|
2023-09-13 07:05:21 +00:00
|
|
|
foreach (var pirateAi in pirateUnit.pirateUnitStat.PirateAiList)
|
2023-08-15 20:36:04 +00:00
|
|
|
{
|
2023-09-13 07:05:21 +00:00
|
|
|
pirateAi.SelectedHighlight();
|
2023-08-15 20:36:04 +00:00
|
|
|
}
|
2023-08-17 07:57:46 +00:00
|
|
|
|
2023-09-13 07:05:21 +00:00
|
|
|
foreach (var pirateAi in SelectedPirateUnit.pirateUnitStat.PirateAiList)
|
2023-08-17 07:57:46 +00:00
|
|
|
{
|
2023-09-13 07:05:21 +00:00
|
|
|
pirateAi.ResetHighlight();
|
2023-08-17 07:57:46 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 07:05:21 +00:00
|
|
|
SelectedPirateUnit = pirateUnit;
|
2023-08-15 20:36:04 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-17 07:57:46 +00:00
|
|
|
// 부대를 클릭하지 않았을 때,
|
2023-08-15 20:36:04 +00:00
|
|
|
else
|
|
|
|
{
|
2023-08-17 07:57:46 +00:00
|
|
|
// 선택된 부대가 없었을 때,
|
2023-09-13 07:05:21 +00:00
|
|
|
if (SelectedPirateUnit == null) return;
|
2023-08-17 07:57:46 +00:00
|
|
|
|
|
|
|
// 선택된 부대가 있었을 때,
|
2023-09-13 07:05:21 +00:00
|
|
|
foreach (var pirateAi in SelectedPirateUnit.pirateUnitStat.PirateAiList)
|
2023-08-17 07:57:46 +00:00
|
|
|
{
|
2023-09-13 07:05:21 +00:00
|
|
|
pirateAi.ResetHighlight();
|
2023-08-17 07:57:46 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 07:05:21 +00:00
|
|
|
SelectedPirateUnit = null;
|
2023-08-17 07:57:46 +00:00
|
|
|
GameManager.Inst.DefaultSpeedMode();
|
2023-08-15 20:36:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-20 04:54:25 +00:00
|
|
|
private void OnCancelSelectedUnit(InputAction.CallbackContext context)
|
|
|
|
{
|
|
|
|
if (!context.performed || !IsSelectable || SelectedPirateUnit == null) return;
|
|
|
|
|
|
|
|
foreach (var pirateAi in SelectedPirateUnit.pirateUnitStat.PirateAiList)
|
|
|
|
{
|
|
|
|
pirateAi.ResetHighlight();
|
|
|
|
}
|
|
|
|
|
|
|
|
SelectedPirateUnit = null;
|
|
|
|
GameManager.Inst.DefaultSpeedMode();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnMoveUnit(InputAction.CallbackContext context)
|
2023-08-15 20:36:04 +00:00
|
|
|
{
|
2023-09-13 07:05:21 +00:00
|
|
|
if (!context.performed || !IsSelectable || SelectedPirateUnit == null) return;
|
2023-08-15 20:36:04 +00:00
|
|
|
|
2023-10-23 03:32:29 +00:00
|
|
|
var ray = CameraManager.Inst.MainCam.ScreenPointToRay(Mouse.current.position.ReadValue());
|
2023-08-15 20:36:04 +00:00
|
|
|
|
2023-09-13 07:05:21 +00:00
|
|
|
if (Physics.Raycast(ray, out var hit, Mathf.Infinity, groundLayer, QueryTriggerInteraction.Collide))
|
2023-08-15 20:36:04 +00:00
|
|
|
{
|
|
|
|
var targetPos = hit.point;
|
2023-09-13 07:05:21 +00:00
|
|
|
SelectedPirateUnit.MoveCommand(targetPos);
|
2023-08-15 20:36:04 +00:00
|
|
|
}
|
2023-08-17 07:57:46 +00:00
|
|
|
|
2023-09-13 07:05:21 +00:00
|
|
|
foreach (var pirateAi in SelectedPirateUnit.pirateUnitStat.PirateAiList)
|
2023-08-15 20:36:04 +00:00
|
|
|
{
|
2023-09-13 07:05:21 +00:00
|
|
|
pirateAi.ResetHighlight();
|
2023-08-15 20:36:04 +00:00
|
|
|
}
|
2023-08-17 07:57:46 +00:00
|
|
|
|
2023-09-13 07:05:21 +00:00
|
|
|
SelectedPirateUnit = null;
|
2023-08-17 07:57:46 +00:00
|
|
|
GameManager.Inst.DefaultSpeedMode();
|
2023-08-15 20:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|