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