128 lines
4.9 KiB
C#
128 lines
4.9 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Cielonos.MainGame.Characters;
|
|
using UnityEngine;
|
|
|
|
namespace Cielonos.MainGame
|
|
{
|
|
public partial class BattleManager
|
|
{
|
|
public partial class EnemySubmodule : SubmoduleBase<BattleManager>
|
|
{
|
|
public enum SortingType
|
|
{
|
|
Nearest,
|
|
Farthest
|
|
}
|
|
|
|
public List<CharacterBase> activeEnemiesList;
|
|
|
|
public EnemySubmodule(BattleManager owner) : base(owner)
|
|
{
|
|
activeEnemiesList = new List<CharacterBase>();
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
activeEnemiesList.Clear();
|
|
}
|
|
|
|
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 enemyRadius = enemy.collisionSc.useCharacterController
|
|
? enemy.collisionSc.characterController.radius
|
|
: enemy.collisionSc.mainColliderRadius;
|
|
|
|
float distance = Vector3.Distance(origin, enemy.transform.position) - enemyRadius;
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取所有可见敌人
|
|
/// </summary>
|
|
public List<CharacterBase> GetVisibleEnemies(float radius = 50f)
|
|
{
|
|
List<CharacterBase> result = new List<CharacterBase>();
|
|
List<CharacterBase> enemies = GetEnemiesInRadius(Player.transform.position, radius);
|
|
|
|
foreach (CharacterBase enemy in enemies)
|
|
{
|
|
if (enemy == null || enemy.statusSm.isDead) continue;
|
|
|
|
Vector3 screenPos = Player.viewSc.playerCamera.WorldToScreenPoint(enemy.centerPoint.position);
|
|
bool isInScreen = screenPos.z > 0 &&
|
|
screenPos.x > 0 && screenPos.x < Screen.width &&
|
|
screenPos.y > 0 && screenPos.y < Screen.height;
|
|
|
|
if (isInScreen)
|
|
{
|
|
result.Add(enemy);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取按屏幕X坐标从左到右排序的可见敌人列表
|
|
/// </summary>
|
|
public List<CharacterBase> GetVisibleEnemiesSortedByScreenX(float radius = 50f)
|
|
{
|
|
List<CharacterBase> visibleEnemies = GetVisibleEnemies(radius);
|
|
|
|
visibleEnemies.Sort((a, b) =>
|
|
{
|
|
Vector3 screenA = Player.viewSc.playerCamera.WorldToScreenPoint(a.centerPoint.position);
|
|
Vector3 screenB = Player.viewSc.playerCamera.WorldToScreenPoint(b.centerPoint.position);
|
|
return screenA.x.CompareTo(screenB.x);
|
|
});
|
|
|
|
return visibleEnemies;
|
|
}
|
|
}
|
|
}
|
|
}
|