97 lines
3.2 KiB
C#
97 lines
3.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Cielonos.MainGame.Characters;
|
|
using Sirenix.OdinInspector;
|
|
using SLSFramework.General;
|
|
using UnityEngine;
|
|
|
|
namespace Cielonos.MainGame
|
|
{
|
|
public partial class BattleManager : Singleton<BattleManager>
|
|
{
|
|
private static Player Player => MainGameManager.Player;
|
|
|
|
[ShowInInspector]
|
|
private EnemySubmodule enemySm;
|
|
|
|
protected override void Awake()
|
|
{
|
|
base.Awake();
|
|
enemySm ??= new EnemySubmodule(this);
|
|
}
|
|
}
|
|
|
|
public partial class BattleManager
|
|
{
|
|
public static EnemySubmodule EnemySm => Instance.enemySm;
|
|
}
|
|
|
|
public partial class BattleManager
|
|
{
|
|
public class EnemySubmodule : SubmoduleBase<BattleManager>
|
|
{
|
|
public enum SortingType
|
|
{
|
|
Nearest,
|
|
Farthest
|
|
}
|
|
|
|
public List<CharacterBase> activeEnemiesList;
|
|
|
|
public EnemySubmodule(BattleManager owner) : base(owner)
|
|
{
|
|
activeEnemiesList = new List<CharacterBase>();
|
|
}
|
|
|
|
public CharacterBase GetNearestEnemy(float maximumDistance = float.MaxValue, Transform origin = null)
|
|
{
|
|
if (origin == null)
|
|
{
|
|
origin = Player.transform;
|
|
}
|
|
|
|
List<CharacterBase> nearestEnemies = GetEnemiesInRadius(origin.position, maximumDistance);
|
|
CharacterBase nearestEnemy = nearestEnemies.Count > 0 ? nearestEnemies[0] : null;
|
|
|
|
return nearestEnemy;
|
|
}
|
|
|
|
public List<CharacterBase> GetNearestEnemies(float maximumDistance = float.MaxValue, int enemyCount = 1, Transform origin = null)
|
|
{
|
|
if (origin == null)
|
|
{
|
|
origin = Player.transform;
|
|
}
|
|
|
|
List<CharacterBase> nearestEnemies = GetEnemiesInRadius(origin.position, maximumDistance).Take(enemyCount).ToList();
|
|
|
|
return nearestEnemies;
|
|
}
|
|
|
|
public List<CharacterBase> GetEnemiesInRadius(Vector3 origin, float radius, SortingType sortingType = SortingType.Nearest)
|
|
{
|
|
List<CharacterBase> enemiesInRadius = new List<CharacterBase>();
|
|
foreach (CharacterBase enemy in activeEnemiesList)
|
|
{
|
|
float distance = Vector3.Distance(origin, enemy.transform.position);
|
|
if (distance <= radius)
|
|
{
|
|
enemiesInRadius.Add(enemy);
|
|
}
|
|
}
|
|
|
|
if (sortingType == SortingType.Nearest)
|
|
{
|
|
enemiesInRadius = enemiesInRadius.OrderBy(x => Vector3.Distance(origin, x.transform.position)).ToList();
|
|
}
|
|
else if (sortingType == SortingType.Farthest)
|
|
{
|
|
enemiesInRadius = enemiesInRadius.OrderByDescending(x => Vector3.Distance(origin, x.transform.position)).ToList();
|
|
}
|
|
|
|
return enemiesInRadius;
|
|
}
|
|
}
|
|
}
|
|
} |