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); return GetNearestEnemy(ownerPosition, viewpoint.Heroes); } public static BattleMonster GetNearestMonster(MonsterViewpoint viewpoint, BattleParticipant owner) { int ownerPosition = GetHeroPosition(viewpoint, owner); return GetNearestEnemy(ownerPosition, viewpoint.Monsters); } private static ParticipantT GetNearestEnemy( int ownerPosition, ParticipantT[] otherTeam) where ParticipantT : BattleParticipant{ int nearestIndex = -1; int nearestDistance = int.MaxValue; for (int i = 0; i < otherTeam.Length; i++) { if (!otherTeam[i].IsAlive()) continue; int distance = System.Math.Abs(i - ownerPosition); if (distance < nearestDistance) { nearestIndex = i; nearestDistance = distance; } } return ParticipantAtIndex(nearestIndex, otherTeam); } 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 ParticipantAtIndex(farthestHeroIndex, viewpoint.Heroes); } 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; } private static int GetHeroPosition(MonsterViewpoint viewpoint, BattleParticipant hero) { for (int i = 0; i < viewpoint.Heroes.Length; i++) { if (viewpoint.Heroes[i] == hero) 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 ParticipantAtIndex(highestHPHeroIndex, viewpoint.Heroes); } private static ParticipantT ParticipantAtIndex(int index, ParticipantT[] team) where ParticipantT : BattleParticipant { if (index == -1) return null; return team[index]; } }