using System.Collections.Generic; using Sirenix.OdinInspector; using UnityEngine; // ReSharper disable once CheckNamespace namespace BlueWaterProject { public class IslandInfo : MonoBehaviour { #region Property and variable [field: SerializeField] public string IslandName { get; private set; } [field: SerializeField] public List HouseList { get; private set; } [field: SerializeField] public List UnitList { get; private set; } [field: SerializeField] public List EnemyList { get; private set; } [field: SerializeField] public List TargetAllList { get; private set; } public IslandInfo() { IslandName = null; HouseList = null; UnitList = null; EnemyList = null; TargetAllList = null; } public IslandInfo(string islandName, List houseList, List unitList, List enemyList, List targetAllList) { IslandName = islandName; HouseList = houseList; UnitList = unitList; EnemyList = enemyList; TargetAllList = targetAllList; } #endregion #region Unity built-in Function private void Awake() { InitIslandInfo(); } #endregion #region Custom function [GUIColor(0, 1, 0)] [Button("섬 정보 추출")] private void InitIslandInfo() { HouseList = new List(5); var houses = transform.Find("Houses"); if (houses) { foreach (Transform house in houses) { if (!house.CompareTag("House") || !house.gameObject.activeSelf) continue; var houseInfo = house.GetComponent(); houseInfo.SetIslandInfo(this); HouseList.Add(houseInfo.transform); } } UnitList = new List(20); var units = transform.Find("Units"); if (units) { foreach (Transform unit in units) { if (!unit.CompareTag("Unit") || !unit.gameObject.activeSelf) continue; UnitList.Add(unit.GetComponent()); } } EnemyList = new List(UnitList.Capacity * 16); foreach (var unit in UnitList) { foreach (Transform enemy in unit.transform) { if (!unit.gameObject.activeSelf) continue; var combatAi = enemy.GetComponent(); combatAi.SetDefendingIslandInfo(this); EnemyList.Add(enemy); } } TargetAllList = new List(HouseList.Capacity + EnemyList.Capacity); foreach (var enemy in EnemyList) { TargetAllList.Add(enemy); } foreach (var house in HouseList) { TargetAllList.Add(house); } } public void RemoveListElement(List list, Transform element) { if (list.Contains(element)) { list.Remove(element); } if (TargetAllList.Contains(element)) { TargetAllList.Remove(element); } //CleanupList(list); //CleanupList(TargetAllList); } private void CleanupList(List list) { for (var i = list.Count - 1; i >= 0; i--) { if (list[i] == null || list[i].gameObject == null) { list.RemoveAt(i); } } } #endregion } }