using System; using UnityEngine; using UnityEngine.InputSystem; // ReSharper disable once CheckNamespace namespace BlueWaterProject { public class UnitSelection : MonoBehaviour { #region Property and variable [Tooltip("선택된 부대")] [SerializeField] private UnitController selectedUnitController; public UnitController SelectedUnitController { get => selectedUnitController; set { tempUnitController = selectedUnitController; selectedUnitController = value; if (selectedUnitController == null) { if (tempUnitController != null) { foreach (var soldier in tempUnitController.unit.soldierList) { soldier.ResetHighlight(); } tempUnitController = null; } GameManager.Inst.DefaultSpeedMode(); } else { GameManager.Inst.SlowSpeedMode(); } } } [SerializeField] private LayerMask unitLayer; [SerializeField] private LayerMask groundLayer; private UnitController tempUnitController; private Camera mainCamera; #endregion #region Unity built-in function private void Reset() { unitLayer = LayerMask.GetMask("Pirate") | LayerMask.GetMask("Enemy"); groundLayer = LayerMask.GetMask("Ground"); } private void Awake() { var controls = new BlueWater(); controls.Unit.LeftClick.performed += OnLeftClick; controls.Unit.RightClick.performed += OnRightClick; controls.Enable(); unitLayer = LayerMask.GetMask("Pirate") | LayerMask.GetMask("Enemy"); groundLayer = LayerMask.GetMask("Ground"); mainCamera = Camera.main; } #endregion #region New input system private void OnLeftClick(InputAction.CallbackContext context) { if (!context.performed) return; var ray = mainCamera.ScreenPointToRay(Mouse.current.position.ReadValue()); if (Physics.Raycast(ray, out var hit, Mathf.Infinity, unitLayer)) { var unitController = hit.collider.transform.parent.GetComponent(); if (unitController != null) { SelectedUnitController = unitController; foreach (var soldier in selectedUnitController.unit.soldierList) { soldier.SelectedHighlight(); } } } else { SelectedUnitController = null; } } private void OnRightClick(InputAction.CallbackContext context) { if (!context.performed || SelectedUnitController == null) return; var ray = mainCamera.ScreenPointToRay(Mouse.current.position.ReadValue()); if (Physics.Raycast(ray, out var hit, Mathf.Infinity, groundLayer)) { var targetPos = hit.point; SelectedUnitController.MoveCommand(targetPos); SelectedUnitController = null; } else { SelectedUnitController = null; } } #endregion } }