45 lines
1.7 KiB
C#
45 lines
1.7 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class AIUtils {
|
|
public static BattleHero GetNearestHero(MonsterViewpoint monsterViewpoint,
|
|
BattleParticipant owner) {
|
|
int ownerPosition = GetMonsterPosition(monsterViewpoint, owner);
|
|
|
|
int nearestHeroIndex = -1;
|
|
int nearestDistance = int.MaxValue;
|
|
for (int i = 0; i < monsterViewpoint.Heroes.Length; i++) {
|
|
if (!monsterViewpoint.Heroes[i].IsAlive()) continue;
|
|
int distance = System.Math.Abs(i - ownerPosition);
|
|
if (distance < nearestDistance) {
|
|
nearestHeroIndex = i;
|
|
nearestDistance = distance;
|
|
}
|
|
}
|
|
if (nearestHeroIndex == -1) return null;
|
|
return monsterViewpoint.Heroes[nearestHeroIndex];
|
|
}
|
|
|
|
public static int GetMonsterPosition(MonsterViewpoint monsterViewpoint, BattleParticipant monster) {
|
|
for (int i = 0; i < monsterViewpoint.Monsters.Length; i++) {
|
|
if (monsterViewpoint.Monsters[i] == monster) return i;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
public static BattleHero GetHeroWithHighestHP(MonsterViewpoint monsterViewpoint) {
|
|
int highestHPHeroIndex = -1;
|
|
int highestHP = -1;
|
|
for (int i = 0; i < monsterViewpoint.Heroes.Length; i++) {
|
|
if (!monsterViewpoint.Heroes[i].IsAlive()) continue;
|
|
int heroHP = monsterViewpoint.Heroes[i].GetHP();
|
|
if (heroHP > highestHP) {
|
|
highestHPHeroIndex = i;
|
|
highestHP = heroHP;
|
|
}
|
|
}
|
|
if (highestHPHeroIndex == -1) return null;
|
|
return monsterViewpoint.Heroes[highestHPHeroIndex];
|
|
}
|
|
}
|