2023-08-15 20:36:04 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2023-09-18 00:21:55 +00:00
|
|
|
using BehaviorDesigner.Runtime;
|
2023-08-22 06:48:37 +00:00
|
|
|
using Sirenix.OdinInspector;
|
2023-08-29 03:41:24 +00:00
|
|
|
using UnityEditor.Animations;
|
2023-08-15 20:36:04 +00:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
// ReSharper disable once CheckNamespace
|
|
|
|
namespace BlueWaterProject
|
|
|
|
{
|
2023-08-17 02:56:45 +00:00
|
|
|
public class UnitManager : Singleton<UnitManager>
|
2023-08-15 20:36:04 +00:00
|
|
|
{
|
|
|
|
#region Property and variable
|
2023-08-29 03:41:24 +00:00
|
|
|
|
2023-09-13 07:05:21 +00:00
|
|
|
[Tooltip("Pirate 프리팹")]
|
|
|
|
[field: SerializeField] public GameObject PiratePrefab { get; private set; }
|
2023-08-29 03:41:24 +00:00
|
|
|
|
|
|
|
[Tooltip("캐릭터 기초 프리팹")]
|
2023-09-13 07:05:21 +00:00
|
|
|
[field: SerializeField] public GameObject BaseHumanPrefab { get; private set; }
|
2023-08-29 03:41:24 +00:00
|
|
|
|
|
|
|
[Tooltip("화살 프리팹")]
|
|
|
|
[field: SerializeField] public GameObject ArrowPrefab { get; private set; }
|
2023-09-18 00:21:55 +00:00
|
|
|
|
|
|
|
[Tooltip("Enemy 비헤이비어 트리")]
|
|
|
|
[field: SerializeField] public ExternalBehaviorTree EnemyBehaviorTree { get; private set; }
|
|
|
|
|
|
|
|
[Tooltip("Pirate 비헤이비어 트리")]
|
|
|
|
[field: SerializeField] public ExternalBehaviorTree PirateBehaviorTree { get; private set; }
|
2023-08-15 20:36:04 +00:00
|
|
|
|
|
|
|
[Tooltip("바닥 레이어")]
|
|
|
|
[field: SerializeField] public LayerMask GroundLayer { get; private set; }
|
|
|
|
|
|
|
|
[Tooltip("바닥과의 최대 허용 거리")]
|
2023-09-13 07:05:21 +00:00
|
|
|
[field: SerializeField] public float MaxGroundDistance { get; private set; } = 4f;
|
2023-08-15 20:36:04 +00:00
|
|
|
|
|
|
|
[Tooltip("병력 간의 간격")]
|
2023-08-31 07:38:08 +00:00
|
|
|
[field: SerializeField] public float UnitSpacing { get; private set; } = 2.5f;
|
2023-08-15 20:36:04 +00:00
|
|
|
|
2023-08-29 03:41:24 +00:00
|
|
|
[field: Tooltip("병력들의 애니메이터 컨트롤러 리스트")]
|
|
|
|
[field: SerializeField] public List<AnimatorController> AIAnimatorControllerList { get; private set; } = new(GlobalValue.AI_ANIMATOR_CAPACITY);
|
2023-09-12 14:46:57 +00:00
|
|
|
|
|
|
|
private Transform pirateUnits;
|
2023-09-13 07:05:21 +00:00
|
|
|
|
2023-08-30 05:57:45 +00:00
|
|
|
private const int ANIMATOR_CONTROLLER_PREFAB_CAPACITY = 6;
|
2023-09-12 14:46:57 +00:00
|
|
|
private const int PIRATE_UNIT_CAPACITY = 50;
|
2023-08-15 20:36:04 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Unity built-in function
|
|
|
|
|
2023-08-23 02:09:21 +00:00
|
|
|
protected override void OnAwake()
|
2023-08-15 20:36:04 +00:00
|
|
|
{
|
|
|
|
GroundLayer = LayerMask.GetMask("Ground");
|
2023-09-13 07:05:21 +00:00
|
|
|
//InitPlayerUnitList();
|
2023-08-15 20:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void Reset()
|
|
|
|
{
|
|
|
|
GroundLayer = LayerMask.GetMask("Ground");
|
2023-09-13 07:05:21 +00:00
|
|
|
MaxGroundDistance = 4f;
|
2023-08-31 07:38:08 +00:00
|
|
|
UnitSpacing = 2.5f;
|
2023-08-29 03:41:24 +00:00
|
|
|
InitCharacterPrefabList();
|
2023-09-13 07:05:21 +00:00
|
|
|
//InitPlayerUnitList();
|
2023-08-15 20:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Custom function
|
|
|
|
|
2023-08-22 06:48:37 +00:00
|
|
|
#if UNITY_EDITOR
|
|
|
|
/// <summary>
|
|
|
|
/// 프리팹 초기화 함수
|
|
|
|
/// </summary>
|
|
|
|
[GUIColor(0, 1, 0)]
|
2023-09-18 00:21:55 +00:00
|
|
|
[ShowIf("@PiratePrefab == null || BaseHumanPrefab == null || ArrowPrefab == null || EnemyBehaviorTree == null || PirateBehaviorTree == null ||" +
|
2023-09-13 07:05:21 +00:00
|
|
|
"AIAnimatorControllerList == null || AIAnimatorControllerList.Count != ANIMATOR_CONTROLLER_PREFAB_CAPACITY")]
|
2023-08-22 06:48:37 +00:00
|
|
|
[Button("프리팹 초기화")]
|
2023-08-29 03:41:24 +00:00
|
|
|
private void InitCharacterPrefabList()
|
2023-08-22 06:48:37 +00:00
|
|
|
{
|
2023-09-18 00:21:55 +00:00
|
|
|
PiratePrefab = Utils.LoadFromFolder<GameObject>("Assets/05.Prefabs/Character/Unit", "PirateUnit", ".prefab");
|
|
|
|
BaseHumanPrefab = Utils.LoadFromFolder<GameObject>("Assets/05.Prefabs/Character", "BaseHuman", ".prefab");
|
|
|
|
ArrowPrefab = Utils.LoadFromFolder<GameObject>("Assets/05.Prefabs", "Arrow_01", ".prefab");
|
|
|
|
EnemyBehaviorTree = Utils.LoadFromFolder<ExternalBehaviorTree>("Assets/09.BehaviorTree/Enemy", "BaseEnemyAi", ".asset");
|
|
|
|
PirateBehaviorTree = Utils.LoadFromFolder<ExternalBehaviorTree>("Assets/09.BehaviorTree/Pirate", "BasePirateAi", ".asset");
|
2023-08-30 05:57:45 +00:00
|
|
|
AIAnimatorControllerList = new List<AnimatorController>(ANIMATOR_CONTROLLER_PREFAB_CAPACITY)
|
|
|
|
{
|
2023-09-18 00:21:55 +00:00
|
|
|
Utils.LoadFromFolder<AnimatorController>("Assets/07.Animation", "Archer", ".controller"),
|
|
|
|
Utils.LoadFromFolder<AnimatorController>("Assets/07.Animation", "Axeman", ".controller"),
|
|
|
|
Utils.LoadFromFolder<AnimatorController>("Assets/07.Animation", "SpearKnight", ".controller"),
|
|
|
|
Utils.LoadFromFolder<AnimatorController>("Assets/07.Animation", "Spearman", ".controller"),
|
|
|
|
Utils.LoadFromFolder<AnimatorController>("Assets/07.Animation", "SwordKnight", ".controller"),
|
|
|
|
Utils.LoadFromFolder<AnimatorController>("Assets/07.Animation", "Swordman", ".controller")
|
2023-08-30 05:57:45 +00:00
|
|
|
};
|
2023-08-22 06:48:37 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-09-13 07:05:21 +00:00
|
|
|
// /// <summary>
|
|
|
|
// /// 플레이어가 가진 유닛 리스트 초기화
|
|
|
|
// /// </summary>
|
|
|
|
// [GUIColor(0, 1, 0)]
|
|
|
|
// [Button("플레이어 유닛 가져오기")]
|
|
|
|
// private void InitPlayerUnitList()
|
|
|
|
// {
|
|
|
|
// SetPlayerUnits();
|
|
|
|
//
|
|
|
|
// pirateUnitList = new List<PirateUnit>(PIRATE_UNIT_CAPACITY);
|
|
|
|
//
|
|
|
|
// foreach (Transform item in pirateUnits)
|
|
|
|
// {
|
|
|
|
// if (!item.gameObject.activeSelf) continue;
|
|
|
|
//
|
|
|
|
// pirateUnitList.Add(item.GetComponent<PirateUnit>());
|
|
|
|
// }
|
|
|
|
// }
|
2023-08-29 03:41:24 +00:00
|
|
|
|
|
|
|
private void SetPlayerUnits()
|
2023-08-22 06:48:37 +00:00
|
|
|
{
|
2023-09-12 14:46:57 +00:00
|
|
|
var pirateUnitsObj = GameObject.Find("PirateUnits");
|
|
|
|
if (pirateUnitsObj)
|
2023-08-22 06:48:37 +00:00
|
|
|
{
|
2023-09-12 14:46:57 +00:00
|
|
|
pirateUnits = pirateUnitsObj.transform;
|
2023-08-22 06:48:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-09-12 14:46:57 +00:00
|
|
|
pirateUnitsObj = new GameObject("PirateUnits");
|
|
|
|
pirateUnitsObj.transform.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
|
|
|
|
pirateUnits = pirateUnitsObj.transform;
|
2023-08-22 06:48:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-12 14:46:57 +00:00
|
|
|
private void SetUnitName(BaseUnit baseUnit, string unitName, string baseName)
|
2023-08-15 20:36:04 +00:00
|
|
|
{
|
2023-09-12 14:46:57 +00:00
|
|
|
if (string.IsNullOrEmpty(unitName))
|
2023-08-17 07:57:46 +00:00
|
|
|
{
|
|
|
|
const int maxIterations = 100;
|
|
|
|
var namingNum = 0;
|
|
|
|
|
|
|
|
while (namingNum < maxIterations)
|
|
|
|
{
|
|
|
|
var newUnitName = $"{baseName}_Unit_{namingNum + 1:00}";
|
|
|
|
var checkGameObject = GameObject.Find(newUnitName);
|
2023-09-12 14:46:57 +00:00
|
|
|
if (checkGameObject && checkGameObject != baseUnit.gameObject)
|
2023-08-17 07:57:46 +00:00
|
|
|
{
|
|
|
|
namingNum++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-09-12 14:46:57 +00:00
|
|
|
baseUnit.gameObject.name = newUnitName;
|
2023-08-17 07:57:46 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-09-12 14:46:57 +00:00
|
|
|
baseUnit.gameObject.name = unitName;
|
2023-08-17 07:57:46 +00:00
|
|
|
}
|
2023-08-29 03:41:24 +00:00
|
|
|
}
|
2023-08-30 07:20:31 +00:00
|
|
|
|
2023-09-12 14:46:57 +00:00
|
|
|
public void PirateUnitCreateAndAssign(string cardIdx, Vector3 assignPos)
|
2023-08-30 07:20:31 +00:00
|
|
|
{
|
2023-09-12 14:46:57 +00:00
|
|
|
var newUnitController = CreatePirateUnit(cardIdx, EAttackerType.OFFENSE);
|
|
|
|
AssignPirateUnit(newUnitController, assignPos, true);
|
2023-08-30 07:20:31 +00:00
|
|
|
}
|
2023-08-29 03:41:24 +00:00
|
|
|
|
2023-08-30 02:10:16 +00:00
|
|
|
/// <summary>
|
|
|
|
/// 동적 생성용 부대 생성 함수
|
|
|
|
/// </summary>
|
2023-09-12 14:46:57 +00:00
|
|
|
public PirateUnit CreatePirateUnit(string cardIdx, EAttackerType attackerType)
|
2023-08-29 03:41:24 +00:00
|
|
|
{
|
2023-08-30 15:36:22 +00:00
|
|
|
var card = DataManager.Inst.GetCardDictionaryFromKey(cardIdx);
|
2023-09-12 14:46:57 +00:00
|
|
|
var unit = DataManager.Inst.GetPirateUnitStatDictionaryFromKey(card.UnitIdx);
|
2023-09-13 07:05:21 +00:00
|
|
|
var captainStat = DataManager.Inst.GetPirateStatDictionaryFromKey(unit.CaptainStatIdx);
|
|
|
|
var sailorStat = DataManager.Inst.GetPirateStatDictionaryFromKey(unit.SailorStatIdx);
|
2023-08-29 03:41:24 +00:00
|
|
|
|
|
|
|
SetPlayerUnits();
|
|
|
|
|
2023-09-13 07:05:21 +00:00
|
|
|
var newUnitController = Instantiate(PiratePrefab, Vector3.zero, Quaternion.identity, pirateUnits).GetComponent<PirateUnit>();
|
2023-09-12 14:46:57 +00:00
|
|
|
newUnitController.pirateUnitStat = new PirateUnitStat(unit);
|
2023-08-29 03:41:24 +00:00
|
|
|
|
|
|
|
DestroyDeployedUnits(newUnitController);
|
2023-08-30 02:10:16 +00:00
|
|
|
|
2023-09-12 14:46:57 +00:00
|
|
|
var pirateStat = DataManager.Inst.GetPirateStatDictionaryFromKey(newUnitController.pirateUnitStat.SailorStatIdx);
|
|
|
|
var baseName = pirateStat.UnitType.ToString();
|
|
|
|
SetUnitName(newUnitController, newUnitController.pirateUnitStat.UnitName, baseName);
|
2023-08-29 03:41:24 +00:00
|
|
|
|
2023-09-13 07:05:21 +00:00
|
|
|
newUnitController.pirateUnitStat.PirateAiList = new List<PirateAi>(newUnitController.pirateUnitStat.SailorCount + 1);
|
2023-08-29 03:41:24 +00:00
|
|
|
|
|
|
|
var unitControllerTransform = newUnitController.transform;
|
|
|
|
var unitControllerRotation = unitControllerTransform.rotation;
|
|
|
|
unitControllerTransform.rotation = Quaternion.identity;
|
|
|
|
|
|
|
|
var gridSize = 0;
|
|
|
|
|
2023-09-12 14:46:57 +00:00
|
|
|
switch (newUnitController.pirateUnitStat.SailorCount)
|
2023-08-29 03:41:24 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
var heroPosition = (gridSize * gridSize) / 2;
|
|
|
|
|
|
|
|
for (var i = 0; i < gridSize; i++)
|
|
|
|
{
|
|
|
|
for (var j = 0; j < gridSize; j++)
|
|
|
|
{
|
|
|
|
var currentPos = i * gridSize + j;
|
|
|
|
|
2023-09-12 14:46:57 +00:00
|
|
|
if (currentPos > newUnitController.pirateUnitStat.SailorCount) break;
|
2023-08-29 03:41:24 +00:00
|
|
|
|
2023-08-30 05:18:09 +00:00
|
|
|
var zOffset = (i - (gridSize - 1) / 2.0f) * UnitSpacing;
|
|
|
|
var xOffset = (j - (gridSize - 1) / 2.0f) * UnitSpacing;
|
2023-08-29 03:41:24 +00:00
|
|
|
var spawnPosition = unitControllerTransform.position + new Vector3(xOffset, 0, zOffset);
|
|
|
|
|
2023-09-13 07:05:21 +00:00
|
|
|
var baseObj = Instantiate(BaseHumanPrefab, spawnPosition,
|
2023-08-29 03:41:24 +00:00
|
|
|
Quaternion.identity, newUnitController.transform);
|
|
|
|
|
|
|
|
var newSoldierName = $"{baseName}_{currentPos + 1:00}";
|
|
|
|
|
|
|
|
baseObj.name = newSoldierName;
|
|
|
|
baseObj.gameObject.SetActive(false);
|
|
|
|
|
2023-09-13 07:05:21 +00:00
|
|
|
var pirateAi = GetPirateAi(baseObj, pirateStat.UnitType, currentPos == heroPosition ? captainStat : sailorStat);
|
2023-08-30 02:10:16 +00:00
|
|
|
|
2023-09-13 07:05:21 +00:00
|
|
|
pirateAi.SetAttackerType(attackerType);
|
|
|
|
pirateAi.SetOffenseType(newUnitController.pirateUnitStat.OffenseType);
|
|
|
|
pirateAi.SetDefenseType(newUnitController.pirateUnitStat.DefenseType);
|
|
|
|
newUnitController.pirateUnitStat.PirateAiList.Add(pirateAi);
|
2023-08-29 03:41:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
newUnitController.transform.rotation *= unitControllerRotation;
|
2023-08-30 07:20:31 +00:00
|
|
|
|
|
|
|
return newUnitController;
|
2023-08-29 03:41:24 +00:00
|
|
|
}
|
2023-08-30 02:10:16 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 에디터용 부대 생성 함수
|
|
|
|
/// </summary>
|
2023-09-12 14:46:57 +00:00
|
|
|
public void CreateEnemyUnitInEditor(EnemyUnit enemyUnit)
|
2023-08-30 02:10:16 +00:00
|
|
|
{
|
2023-09-12 14:46:57 +00:00
|
|
|
var unit = DataManager.Inst.GetEnemyUnitStatSoFromKey(enemyUnit.enemyUnitStat.Idx);
|
2023-08-30 05:57:45 +00:00
|
|
|
|
2023-08-30 02:10:16 +00:00
|
|
|
SetPlayerUnits();
|
|
|
|
|
2023-09-12 14:46:57 +00:00
|
|
|
enemyUnit.enemyUnitStat = new EnemyUnitStat(unit);
|
2023-08-30 02:10:16 +00:00
|
|
|
|
2023-09-12 14:46:57 +00:00
|
|
|
var captainStat = DataManager.Inst.GetEnemyStatSoFromKey(enemyUnit.enemyUnitStat.CaptainStatIdx);
|
|
|
|
var sailorStat = DataManager.Inst.GetEnemyStatSoFromKey(enemyUnit.enemyUnitStat.SailorStatIdx);
|
2023-08-30 02:10:16 +00:00
|
|
|
|
2023-09-12 14:46:57 +00:00
|
|
|
DestroyDeployedUnits(enemyUnit);
|
2023-08-30 02:10:16 +00:00
|
|
|
|
2023-09-13 07:05:21 +00:00
|
|
|
var enemyStat = DataManager.Inst.GetEnemyStatSoFromKey(enemyUnit.enemyUnitStat.SailorStatIdx);
|
2023-09-12 14:46:57 +00:00
|
|
|
var baseName = enemyStat.UnitType.ToString();
|
|
|
|
SetUnitName(enemyUnit, enemyUnit.enemyUnitStat.UnitName, baseName);
|
2023-08-30 02:10:16 +00:00
|
|
|
|
2023-09-13 07:05:21 +00:00
|
|
|
enemyUnit.enemyUnitStat.EnemyAiList = new List<EnemyAi>(enemyUnit.enemyUnitStat.SailorCount + 1);
|
2023-08-30 02:10:16 +00:00
|
|
|
|
2023-09-12 14:46:57 +00:00
|
|
|
var unitControllerTransform = enemyUnit.transform;
|
2023-08-30 02:10:16 +00:00
|
|
|
var unitControllerRotation = unitControllerTransform.rotation;
|
|
|
|
unitControllerTransform.rotation = Quaternion.identity;
|
|
|
|
|
|
|
|
var gridSize = 0;
|
|
|
|
|
2023-09-12 14:46:57 +00:00
|
|
|
switch (enemyUnit.enemyUnitStat.SailorCount)
|
2023-08-30 02:10:16 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
var heroPosition = (gridSize * gridSize) / 2;
|
|
|
|
|
|
|
|
for (var i = 0; i < gridSize; i++)
|
|
|
|
{
|
|
|
|
for (var j = 0; j < gridSize; j++)
|
|
|
|
{
|
|
|
|
var currentPos = i * gridSize + j;
|
|
|
|
|
2023-09-12 14:46:57 +00:00
|
|
|
if (currentPos > enemyUnit.enemyUnitStat.SailorCount) break;
|
2023-08-30 02:10:16 +00:00
|
|
|
|
2023-08-30 05:18:09 +00:00
|
|
|
var zOffset = (i - (gridSize - 1) / 2.0f) * UnitSpacing;
|
|
|
|
var xOffset = (j - (gridSize - 1) / 2.0f) * UnitSpacing;
|
2023-08-30 02:10:16 +00:00
|
|
|
var spawnPosition = unitControllerTransform.position + new Vector3(xOffset, 0, zOffset);
|
|
|
|
|
2023-09-13 07:05:21 +00:00
|
|
|
var baseObj = Instantiate(BaseHumanPrefab, spawnPosition,
|
2023-09-12 14:46:57 +00:00
|
|
|
Quaternion.identity, enemyUnit.transform);
|
2023-08-30 02:10:16 +00:00
|
|
|
|
|
|
|
var newSoldierName = $"{baseName}_{currentPos + 1:00}";
|
|
|
|
|
|
|
|
baseObj.name = newSoldierName;
|
|
|
|
baseObj.gameObject.SetActive(false);
|
|
|
|
|
2023-09-13 07:05:21 +00:00
|
|
|
var currentStat = currentPos == heroPosition ? captainStat : sailorStat;
|
|
|
|
var enemyAi = GetEnemyAi(baseObj, enemyStat.UnitType, currentStat);
|
2023-08-30 02:10:16 +00:00
|
|
|
|
2023-09-13 07:05:21 +00:00
|
|
|
enemyAi.SetAttackerType(enemyUnit.enemyUnitStat.AttackerType);
|
|
|
|
enemyAi.SetOffenseType(enemyUnit.enemyUnitStat.OffenseType);
|
|
|
|
enemyAi.SetDefenseType(enemyUnit.enemyUnitStat.DefenseType);
|
|
|
|
enemyAi.InitStartInEditor();
|
|
|
|
enemyUnit.enemyUnitStat.EnemyAiList.Add(enemyAi);
|
2023-08-30 02:10:16 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-12 14:46:57 +00:00
|
|
|
enemyUnit.transform.rotation *= unitControllerRotation;
|
2023-08-30 02:10:16 +00:00
|
|
|
}
|
2023-08-29 03:41:24 +00:00
|
|
|
|
2023-09-13 07:05:21 +00:00
|
|
|
private EnemyAi GetEnemyAi(GameObject baseObj, GlobalValue.UnitType unitType, EnemyStat enemyStat)
|
2023-08-29 03:41:24 +00:00
|
|
|
{
|
2023-09-13 07:05:21 +00:00
|
|
|
EnemyAi enemyAi = unitType switch
|
2023-08-29 03:41:24 +00:00
|
|
|
{
|
2023-09-13 07:05:21 +00:00
|
|
|
GlobalValue.UnitType.ARCHER_E => baseObj.AddComponent<EnemyArcher>(),
|
|
|
|
GlobalValue.UnitType.SPEAR_KNIGHT_E => baseObj.AddComponent<EnemySpearKnight>(),
|
|
|
|
GlobalValue.UnitType.SPEARMAN_E => baseObj.AddComponent<EnemySpearman>(),
|
|
|
|
GlobalValue.UnitType.SWORD_KNIGHT_E => baseObj.AddComponent<EnemySwordKnight>(),
|
|
|
|
GlobalValue.UnitType.SWORDMAN_E => baseObj.AddComponent<EnemySwordman>(),
|
|
|
|
_ => throw new ArgumentOutOfRangeException(nameof(unitType), unitType, null)
|
|
|
|
};
|
2023-08-29 03:41:24 +00:00
|
|
|
|
2023-09-13 07:05:21 +00:00
|
|
|
if (enemyAi == null) return null;
|
2023-08-29 03:41:24 +00:00
|
|
|
|
2023-09-13 07:05:21 +00:00
|
|
|
enemyAi.EnemyStat = new EnemyStat(enemyStat);
|
|
|
|
return enemyAi;
|
|
|
|
}
|
|
|
|
|
|
|
|
private PirateAi GetPirateAi(GameObject baseObj, GlobalValue.UnitType unitType, PirateStat pirateStat)
|
|
|
|
{
|
|
|
|
PirateAi pirateAi = unitType switch
|
|
|
|
{
|
|
|
|
GlobalValue.UnitType.ARCHER_P => baseObj.AddComponent<PirateArcher>(),
|
|
|
|
GlobalValue.UnitType.AXEMAN_P => baseObj.AddComponent<PirateAxeman>(),
|
|
|
|
GlobalValue.UnitType.SPEARMAN_P => baseObj.AddComponent<PirateSpearman>(),
|
|
|
|
GlobalValue.UnitType.SWORD_KNIGHT_P => baseObj.AddComponent<PirateSwordKnight>(),
|
|
|
|
GlobalValue.UnitType.SWORDMAN_P => baseObj.AddComponent<PirateSwordman>(),
|
|
|
|
_ => throw new ArgumentOutOfRangeException(nameof(unitType), unitType, null)
|
|
|
|
};
|
|
|
|
|
|
|
|
if (pirateAi == null) return null;
|
|
|
|
|
|
|
|
pirateAi.PirateStat = new PirateStat(pirateStat);
|
|
|
|
return pirateAi;
|
2023-08-22 06:48:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 유닛 배치 함수
|
|
|
|
/// </summary>
|
2023-09-12 14:46:57 +00:00
|
|
|
public bool CanAssignUnit(EnemyUnit enemyUnit, Vector3 assignPos)
|
2023-08-22 06:48:37 +00:00
|
|
|
{
|
2023-09-13 07:05:21 +00:00
|
|
|
if (enemyUnit.enemyUnitStat.EnemyAiList.Count <= 0) return false;
|
2023-08-22 06:48:37 +00:00
|
|
|
|
2023-09-12 14:46:57 +00:00
|
|
|
enemyUnit.transform.position = assignPos;
|
2023-08-22 06:48:37 +00:00
|
|
|
|
2023-09-12 14:46:57 +00:00
|
|
|
for (var i = 0; i < enemyUnit.enemyUnitStat.SailorCount; i++)
|
2023-08-22 06:48:37 +00:00
|
|
|
{
|
2023-09-13 07:05:21 +00:00
|
|
|
var unitPos = enemyUnit.enemyUnitStat.EnemyAiList[i].transform.position;
|
2023-09-11 03:41:02 +00:00
|
|
|
var ray = new Ray(unitPos + Vector3.up, Vector3.down);
|
2023-08-15 20:36:04 +00:00
|
|
|
if (Physics.Raycast(ray, out var hit, MaxGroundDistance, GroundLayer))
|
|
|
|
{
|
2023-09-11 03:41:02 +00:00
|
|
|
unitPos.y = hit.point.y;
|
2023-08-15 20:36:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-08-22 06:48:37 +00:00
|
|
|
return false;
|
2023-08-15 20:36:04 +00:00
|
|
|
}
|
2023-08-22 06:48:37 +00:00
|
|
|
}
|
2023-08-17 07:57:46 +00:00
|
|
|
|
2023-08-22 06:48:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-09-12 14:46:57 +00:00
|
|
|
public void AssignEnemyUnit(EnemyUnit enemyUnit, Vector3 assignPos)
|
|
|
|
{
|
|
|
|
enemyUnit.transform.position = assignPos;
|
|
|
|
|
2023-09-13 07:05:21 +00:00
|
|
|
foreach (var unit in enemyUnit.enemyUnitStat.EnemyAiList)
|
2023-09-12 14:46:57 +00:00
|
|
|
{
|
|
|
|
var myPos = unit.transform.position;
|
|
|
|
var ray = new Ray(myPos + Vector3.up, Vector3.down);
|
|
|
|
if (Physics.Raycast(ray, out var hit, MaxGroundDistance, GroundLayer))
|
|
|
|
{
|
|
|
|
unit.transform.position = new Vector3(myPos.x, hit.point.y, myPos.z);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-13 07:05:21 +00:00
|
|
|
foreach (var unit in enemyUnit.enemyUnitStat.EnemyAiList)
|
2023-09-12 14:46:57 +00:00
|
|
|
{
|
|
|
|
unit.gameObject.SetActive(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void AssignPirateUnit(PirateUnit pirateUnit, Vector3 assignPos, bool isOffense)
|
2023-08-22 06:48:37 +00:00
|
|
|
{
|
2023-09-12 14:46:57 +00:00
|
|
|
pirateUnit.transform.position = assignPos;
|
2023-09-04 07:31:04 +00:00
|
|
|
IslandInfo hitIslandInfo = null;
|
|
|
|
|
2023-09-13 07:05:21 +00:00
|
|
|
foreach (var unit in pirateUnit.pirateUnitStat.PirateAiList)
|
2023-08-22 06:48:37 +00:00
|
|
|
{
|
2023-09-04 07:31:04 +00:00
|
|
|
var myPos = unit.transform.position;
|
2023-08-30 07:20:31 +00:00
|
|
|
var ray = new Ray(myPos + Vector3.up, Vector3.down);
|
2023-08-30 05:18:09 +00:00
|
|
|
if (Physics.Raycast(ray, out var hit, MaxGroundDistance, GroundLayer))
|
|
|
|
{
|
2023-09-04 07:31:04 +00:00
|
|
|
unit.transform.position = new Vector3(myPos.x, hit.point.y, myPos.z);
|
|
|
|
|
|
|
|
if (isOffense && hitIslandInfo == null)
|
|
|
|
{
|
|
|
|
hitIslandInfo = hit.transform.root.GetComponent<IslandInfo>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-13 07:05:21 +00:00
|
|
|
foreach (var unit in pirateUnit.pirateUnitStat.PirateAiList)
|
2023-09-04 07:31:04 +00:00
|
|
|
{
|
|
|
|
if (isOffense)
|
|
|
|
{
|
2023-09-18 00:21:55 +00:00
|
|
|
unit.SetAttackingIslandInfo(hitIslandInfo);
|
2023-08-30 05:18:09 +00:00
|
|
|
}
|
2023-09-04 07:31:04 +00:00
|
|
|
unit.gameObject.SetActive(true);
|
2023-08-15 20:36:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 기존에 생성된 부대 병력들이 있으면 확인해서 삭제해주는 함수
|
|
|
|
/// </summary>
|
2023-09-12 14:46:57 +00:00
|
|
|
public void DestroyDeployedUnits(BaseUnit baseUnit)
|
2023-08-15 20:36:04 +00:00
|
|
|
{
|
2023-09-12 14:46:57 +00:00
|
|
|
if (baseUnit.transform.childCount <= 0) return;
|
2023-08-15 20:36:04 +00:00
|
|
|
|
2023-09-12 14:46:57 +00:00
|
|
|
for (var i = baseUnit.transform.childCount - 1; i >= 0; i--)
|
2023-08-15 20:36:04 +00:00
|
|
|
{
|
|
|
|
if (Application.isPlaying)
|
|
|
|
{
|
2023-09-12 14:46:57 +00:00
|
|
|
Destroy(baseUnit.transform.GetChild(i).gameObject);
|
2023-08-15 20:36:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-09-12 14:46:57 +00:00
|
|
|
DestroyImmediate(baseUnit.transform.GetChild(i).gameObject);
|
2023-08-15 20:36:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-13 07:05:21 +00:00
|
|
|
// /// <summary>
|
|
|
|
// /// pirateUnitList 내의 속성
|
|
|
|
// /// </summary>
|
|
|
|
// public void RemovePlayerUnitListElement(PirateUnit pirateUnit)
|
|
|
|
// {
|
|
|
|
// if (pirateUnitList.Contains(pirateUnit))
|
|
|
|
// {
|
|
|
|
// pirateUnitList.Remove(pirateUnit);
|
|
|
|
// }
|
|
|
|
// else
|
|
|
|
// {
|
|
|
|
// Debug.Log("제거하려는 속성이 리스트 내에 존재하지 않습니다.");
|
|
|
|
// }
|
|
|
|
// }
|
2023-08-22 06:48:37 +00:00
|
|
|
|
2023-08-15 20:36:04 +00:00
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|