2023-08-15 20:36:04 +00:00
|
|
|
using System;
|
2023-08-21 18:08:11 +00:00
|
|
|
using Sirenix.OdinInspector;
|
2023-08-15 20:36:04 +00:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
// ReSharper disable once CheckNamespace
|
|
|
|
namespace BlueWaterProject
|
|
|
|
{
|
|
|
|
public class UnitController : MonoBehaviour
|
|
|
|
{
|
2023-08-21 18:08:11 +00:00
|
|
|
#region Property and variable
|
|
|
|
|
|
|
|
[PropertyOrder(-10)]
|
|
|
|
public Unit unit;
|
|
|
|
|
|
|
|
private bool isClickedTypeAllButton;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Unity built-in function
|
|
|
|
|
2023-08-15 20:36:04 +00:00
|
|
|
private void OnDrawGizmosSelected()
|
|
|
|
{
|
2023-08-28 19:52:23 +00:00
|
|
|
if (unit == null || unit.SailorCount <= 0) return;
|
2023-08-30 05:18:09 +00:00
|
|
|
|
|
|
|
var gridSize = 0;
|
2023-08-15 20:36:04 +00:00
|
|
|
|
2023-08-30 05:18:09 +00:00
|
|
|
switch (unit.SailorCount)
|
2023-08-15 20:36:04 +00:00
|
|
|
{
|
2023-08-30 05:18:09 +00:00
|
|
|
case 0:
|
|
|
|
gridSize = 1;
|
|
|
|
break;
|
|
|
|
case <= 3:
|
|
|
|
gridSize = 2;
|
|
|
|
break;
|
|
|
|
case <= 8:
|
|
|
|
gridSize = 3;
|
|
|
|
break;
|
|
|
|
case <= 15:
|
|
|
|
gridSize = 4;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
print("유닛의 병사 숫자 설정 에러");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < gridSize; i++)
|
|
|
|
{
|
|
|
|
for (var j = 0; j < gridSize; j++)
|
|
|
|
{
|
|
|
|
var currentPos = i * gridSize + j;
|
|
|
|
|
|
|
|
if (currentPos > unit.SailorCount) break;
|
|
|
|
|
|
|
|
var xOffset = (i - (gridSize - 1) / 2.0f) * UnitManager.Inst.UnitSpacing;
|
|
|
|
var zOffset = (j - (gridSize - 1) / 2.0f) * UnitManager.Inst.UnitSpacing;
|
|
|
|
var spawnPosition = transform.position + new Vector3(xOffset, 0, zOffset);
|
|
|
|
|
|
|
|
var ray = new Ray(spawnPosition, Vector3.down);
|
|
|
|
Gizmos.color = Physics.Raycast(ray, UnitManager.Inst.MaxGroundDistance, UnitManager.Inst.GroundLayer) ? Color.blue : Color.red;
|
|
|
|
Gizmos.DrawRay(ray.origin, ray.direction * UnitManager.Inst.MaxGroundDistance);
|
|
|
|
}
|
2023-08-15 20:36:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-29 03:41:24 +00:00
|
|
|
// private void Start()
|
|
|
|
// {
|
|
|
|
// SetIslandInfoTest();
|
|
|
|
// }
|
2023-08-21 18:08:11 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Custom function
|
|
|
|
|
|
|
|
[PropertyOrder(-9)]
|
|
|
|
[HorizontalGroup("Split", 0.5f)]
|
|
|
|
[GUIColor("GetCreateUnitButtonColor")]
|
2023-08-30 05:57:45 +00:00
|
|
|
[EnableIf("@DataManager.Inst.GetEnemyUnitSoKey(unit.Idx) != null || DataManager.Inst.GetUnitSoKey(unit.Idx) != null")]
|
2023-08-21 18:08:11 +00:00
|
|
|
[Button("유닛 생성")]
|
2023-08-15 20:36:04 +00:00
|
|
|
public void CreateUnit()
|
|
|
|
{
|
2023-08-30 05:18:09 +00:00
|
|
|
UnitManager.Inst.CreateUnitInEditor(this);
|
2023-08-15 20:36:04 +00:00
|
|
|
}
|
2023-08-22 06:48:37 +00:00
|
|
|
|
2023-08-21 18:08:11 +00:00
|
|
|
[PropertyOrder(-8)]
|
|
|
|
[HorizontalGroup("Split", 0.5f)]
|
2023-08-22 06:48:37 +00:00
|
|
|
[EnableIf("CanAssignUnit")]
|
|
|
|
[Button("유닛 배치")]
|
2023-08-30 05:18:09 +00:00
|
|
|
private void SetAssignUnitInEditor()
|
2023-08-22 06:48:37 +00:00
|
|
|
{
|
|
|
|
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)
|
2023-08-21 18:08:11 +00:00
|
|
|
{
|
2023-08-22 06:48:37 +00:00
|
|
|
if (UnitManager.Inst.CanAssignUnit(this, assignPos))
|
2023-08-21 18:08:11 +00:00
|
|
|
{
|
2023-08-22 06:48:37 +00:00
|
|
|
UnitManager.Inst.AssignUnit(this, assignPos);
|
2023-08-21 18:08:11 +00:00
|
|
|
}
|
2023-08-22 06:48:37 +00:00
|
|
|
else
|
2023-08-21 18:08:11 +00:00
|
|
|
{
|
2023-08-22 06:48:37 +00:00
|
|
|
print("병력이 땅과 맞닿아 있지 않아 배치할 수 없는 위치입니다.");
|
2023-08-21 18:08:11 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-22 06:48:37 +00:00
|
|
|
|
2023-08-21 18:08:11 +00:00
|
|
|
[PropertyOrder(-7)]
|
|
|
|
[GUIColor(1, 0, 0)]
|
2023-08-23 02:09:21 +00:00
|
|
|
[Button("유닛 초기화")]
|
|
|
|
private void ResetUnit()
|
2023-08-21 18:08:11 +00:00
|
|
|
{
|
2023-08-30 05:18:09 +00:00
|
|
|
var tempUnitIdx = unit.Idx;
|
|
|
|
UnitManager.Inst.DestroyDeployedUnits(this);
|
|
|
|
|
|
|
|
unit = new Unit
|
2023-08-21 18:08:11 +00:00
|
|
|
{
|
2023-08-30 05:18:09 +00:00
|
|
|
Idx = tempUnitIdx
|
|
|
|
};
|
|
|
|
isClickedTypeAllButton = false;
|
2023-08-21 18:08:11 +00:00
|
|
|
}
|
2023-08-15 20:36:04 +00:00
|
|
|
|
|
|
|
public void MoveCommand(Vector3 targetPos)
|
|
|
|
{
|
2023-08-28 19:52:23 +00:00
|
|
|
foreach (var soldier in unit.UnitList)
|
2023-08-15 20:36:04 +00:00
|
|
|
{
|
2023-08-17 07:57:46 +00:00
|
|
|
soldier.MoveTarget(targetPos);
|
2023-08-15 20:36:04 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-21 18:08:11 +00:00
|
|
|
|
2023-08-28 19:52:23 +00:00
|
|
|
private Color GetCreateUnitButtonColor() => unit.UnitList.Count > 0 ? Color.white : Color.green;
|
2023-08-21 18:08:11 +00:00
|
|
|
private Color GetTypeAllButtonColor() => isClickedTypeAllButton ? Color.white : Color.green;
|
|
|
|
private void OnTypeChanged() => isClickedTypeAllButton = false;
|
|
|
|
|
|
|
|
#endregion
|
2023-08-15 20:36:04 +00:00
|
|
|
}
|
|
|
|
}
|