OldBlueWater/BlueWater/Assets/02.Scripts/Ai/Unit/UnitController.cs

185 lines
5.8 KiB
C#

using System;
using Sirenix.OdinInspector;
using UnityEngine;
// ReSharper disable once CheckNamespace
namespace BlueWaterProject
{
public class UnitController : MonoBehaviour
{
#region Property and variable
[PropertyOrder(-11)]
[EnableIf("@unit.AttackerType == AttackerType.OFFENSE")]
[InlineButton("SetIslandInfoTest", "테스트 설정")]
[SerializeField] private IslandInfo attackIslandInfo;
[PropertyOrder(-10)]
public Unit unit;
private bool isClickedTypeAllButton;
#endregion
#region Unity built-in function
private void OnDrawGizmosSelected()
{
if (unit == null || unit.SoliderCount <= 0) return;
var unitManager = UnitManager.Inst != null ? UnitManager.Inst : FindObjectOfType<UnitManager>();
var matrix = unitManager.UnitMatrices.Find(um => um.soldiers == unit.SoliderCount);
if (matrix == null) return;
for (var i = 0; i < unit.SoliderCount; i++)
{
var row = i / matrix.columns;
var column = i % matrix.columns;
var xOffset = (column - (matrix.columns - 1) / 2.0f) * unitManager.SoldierSpacing;
var zOffset = (row - (matrix.rows - 1) / 2.0f) * unitManager.SoldierSpacing;
var spawnPosition = transform.position + new Vector3(xOffset, 0, zOffset);
var ray = new Ray(spawnPosition, Vector3.down);
Gizmos.color = Physics.Raycast(ray, unitManager.MaxGroundDistance, unitManager.GroundLayer) ? Color.blue : Color.red;
Gizmos.DrawRay(ray.origin, ray.direction * unitManager.MaxGroundDistance);
}
}
private void Awake()
{
SetIslandInfoTest();
}
#endregion
#region Custom function
[PropertyOrder(-9)]
[HorizontalGroup("Split", 0.5f)]
[GUIColor("GetCreateUnitButtonColor")]
[Button("유닛 생성")]
public void CreateUnit()
{
UnitManager.Inst.CreateUnit(this);
}
[PropertyOrder(-8)]
[HorizontalGroup("Split", 0.5f)]
[EnableIf("CanAssignUnit")]
[Button("유닛 배치")]
private void SetAssignUnitInScene()
{
if (UnitManager.Inst.CanAssignUnit(this, transform.position))
{
UnitManager.Inst.AssignUnit(this, transform.position);
}
}
private bool CanAssignUnit()
{
return UnitManager.Inst.CanAssignUnit(this, transform.position);
}
[PropertyOrder(2)]
[Button("테스트 배치")]
public void SetAssignUnit(Vector3 assignPos)
{
if (UnitManager.Inst.CanAssignUnit(this, assignPos))
{
UnitManager.Inst.AssignUnit(this, assignPos);
}
else
{
print("병력이 땅과 맞닿아 있지 않아 배치할 수 없는 위치입니다.");
}
}
[PropertyOrder(-7)]
[GUIColor(1, 0, 0)]
[Button("유닛 초기화")]
private void ResetUnit()
{
unit = new Unit();
attackIslandInfo = null;
isClickedTypeAllButton = false;
}
[PropertyOrder(1)]
[GUIColor("GetTypeAllButtonColor")]
[ShowIf("ShowTypeAllButton")]
[Button("타입 모두 적용")]
private void SetTypeAll()
{
foreach (var soldier in unit.soldierList)
{
soldier.SetAttackerType(unit.AttackerType);
soldier.SetOffenseType(unit.OffenseType);
soldier.SetDefenseType(unit.DefenseType);
}
isClickedTypeAllButton = true;
}
public void MoveCommand(Vector3 targetPos)
{
foreach (var soldier in unit.soldierList)
{
soldier.MoveTarget(targetPos);
}
}
private void SetIslandInfoTest()
{
if (unit.AttackerType != AttackerType.OFFENSE) return;
var islandInfo = FindObjectOfType<IslandInfo>();
attackIslandInfo = islandInfo;
foreach (var soldier in unit.soldierList)
{
soldier.IslandInfo = attackIslandInfo;
}
}
private bool ShowTypeAllButton()
{
switch (unit.AttackerType)
{
case AttackerType.NONE:
return false;
case AttackerType.OFFENSE:
if (unit.OffenseType == OffenseType.NONE)
{
return false;
}
break;
case AttackerType.DEFENSE:
if (unit.DefenseType == DefenseType.NONE)
{
return false;
}
break;
default:
throw new ArgumentOutOfRangeException();
}
return true;
}
private Color GetCreateUnitButtonColor() => unit.soldierList.Count > 0 ? Color.white : Color.green;
private Color GetAttackerTypeButtonColor()
{
if (unit.soldierList.Count > 0)
{
return unit.AttackerType == AttackerType.NONE ? Color.green : Color.white;
}
return Color.white;
}
private Color GetTypeAllButtonColor() => isClickedTypeAllButton ? Color.white : Color.green;
private void OnTypeChanged() => isClickedTypeAllButton = false;
public void SetAttackerType(AttackerType value) => unit.AttackerType = value;
#endregion
}
}