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

184 lines
5.8 KiB
C#
Raw Normal View History

using System;
using Sirenix.OdinInspector;
using UnityEngine;
// ReSharper disable once CheckNamespace
namespace BlueWaterProject
{
public class UnitController : MonoBehaviour
{
#region Property and variable
[PropertyOrder(-11)]
2023-08-29 03:41:24 +00:00
//[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.SailorCount <= 0) return;
var unitManager = UnitManager.Inst != null ? UnitManager.Inst : FindObjectOfType<UnitManager>();
2023-08-29 03:41:24 +00:00
var matrix = unitManager.UnitMatrices.Find(um => um.units == unit.SailorCount);
if (matrix == null) return;
for (var i = 0; i < unit.SailorCount; i++)
{
var row = i / matrix.columns;
var column = i % matrix.columns;
2023-08-29 03:41:24 +00:00
var xOffset = (column - (matrix.columns - 1) / 2.0f) * unitManager.UnitSpacing;
var zOffset = (row - (matrix.rows - 1) / 2.0f) * unitManager.UnitSpacing;
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);
}
}
2023-08-29 03:41:24 +00:00
// private void Start()
// {
// 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)]
2023-08-23 02:09:21 +00:00
[Button("유닛 초기화")]
private void ResetUnit()
{
2023-08-23 02:09:21 +00:00
unit = new Unit();
attackIslandInfo = null;
isClickedTypeAllButton = false;
}
[PropertyOrder(1)]
[GUIColor("GetTypeAllButtonColor")]
2023-08-29 03:41:24 +00:00
//[ShowIf("ShowTypeAllButton")]
[Button("타입 모두 적용")]
private void SetTypeAll()
{
foreach (var soldier in unit.UnitList)
{
soldier.SetOffenseType(unit.OffenseType);
soldier.SetDefenseType(unit.DefenseType);
}
isClickedTypeAllButton = true;
}
public void MoveCommand(Vector3 targetPos)
{
foreach (var soldier in unit.UnitList)
{
soldier.MoveTarget(targetPos);
}
}
2023-08-29 03:41:24 +00:00
// private void SetIslandInfoTest()
// {
// if (unit.AttackerType != AttackerType.OFFENSE) return;
//
// var islandInfo = FindObjectOfType<IslandInfo>();
// attackIslandInfo = islandInfo;
//
// foreach (var soldier in unit.UnitList)
// {
// soldier.IslandInfo = attackIslandInfo;
// }
// }
2023-08-29 03:41:24 +00:00
// 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.UnitList.Count > 0 ? Color.white : Color.green;
2023-08-29 03:41:24 +00:00
// private Color GetAttackerTypeButtonColor()
// {
// if (unit.UnitList.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;
2023-08-29 03:41:24 +00:00
//public void SetAttackerType(AttackerType value) => unit.AttackerType = value;
#endregion
}
}