You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
383 lines
11 KiB
383 lines
11 KiB
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using UnityEngine;
|
|
|
|
|
|
//currentGroup为1时调用该模块中的Start函数
|
|
public class AIManager : MonoBehaviour
|
|
{
|
|
|
|
public List<Cell> OpenList;
|
|
public List<Cell> ClosedList;
|
|
|
|
public Cell StartNode;
|
|
public Cell EndNode;
|
|
|
|
public Character AttTrath;
|
|
|
|
GameManager gameManager;
|
|
|
|
public List<Character> Gamer;
|
|
public List<Character_AI> AI;
|
|
|
|
|
|
public List<Cell> AllList;
|
|
|
|
public int movementCost = 3;
|
|
public int poiseDamgage = 1;
|
|
|
|
|
|
private void Awake()
|
|
{
|
|
AllList = GameObject.FindObjectsOfType<Cell>().ToList();
|
|
}
|
|
// Start is called before the first frame update
|
|
public void AIStart()
|
|
{
|
|
Gamer = new List<Character>();
|
|
AI = new List<Character_AI>();
|
|
gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
|
|
|
|
//获得双方阵营棋子
|
|
|
|
foreach (var piece in gameManager.pieces)
|
|
{
|
|
if (piece.GetComponent<Character>().groupNumber==1)
|
|
{
|
|
//Debug.Log(55);
|
|
AI.Add(piece.GetComponent<Character_AI>());
|
|
}
|
|
else
|
|
{
|
|
Gamer.Add(piece.GetComponent<Character>());
|
|
}
|
|
|
|
|
|
}
|
|
|
|
SearchPieces();
|
|
|
|
foreach (var ai in AI)
|
|
{
|
|
// ai.GetComponent<GameManager>().SetSelected(ai);
|
|
|
|
var temp = Gamer.Find(v =>v!=null&& v.currentHealth >= 0);
|
|
if (temp != null)
|
|
{
|
|
StartNode = ai.mCell;
|
|
EndNode = Gamer[0].mCell;
|
|
AttTrath = temp;
|
|
Excecute(ai);
|
|
}
|
|
else
|
|
{
|
|
break;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
|
|
public void Excecute(Character_AI x)
|
|
{
|
|
try
|
|
{
|
|
Debug.Log("Excecuted");
|
|
var temp = FindPath(x);
|
|
MoveTo(x, temp);
|
|
Attack(x);
|
|
}
|
|
catch (System.Exception)
|
|
{
|
|
|
|
throw;
|
|
}
|
|
|
|
|
|
// x.weaponManager.SetCurrnetWeaponActive(false);
|
|
}
|
|
|
|
public void SearchPieces()
|
|
{
|
|
GameObject[] pieces = GameObject.FindGameObjectsWithTag("Pieces");
|
|
}
|
|
|
|
public void ClearNode()
|
|
{
|
|
|
|
}
|
|
|
|
public Cell FindCell(Vector2 v2)
|
|
{
|
|
return AllList.Find(v => Vector2.Distance(v.GetPos, v2) < 0.1);
|
|
}
|
|
|
|
public List<Cell> FindPath(Character AI)
|
|
{
|
|
var tempList = new List<Cell>();
|
|
|
|
|
|
{
|
|
Cell temp = FindCell(AttTrath.mCell.GetPos + Vector2.right);
|
|
if (temp!=null)
|
|
{
|
|
if (Vector2.Distance(temp.GetPos, AI.mCell.GetPos) < 0.2f)
|
|
{
|
|
Debug.Log(44222222222222222);
|
|
return new List<Cell>();
|
|
}
|
|
if (temp.currentCharacter == null)
|
|
{
|
|
tempList.Add(temp);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return new List<Cell>();
|
|
}
|
|
}
|
|
{
|
|
Cell temp = FindCell(AttTrath.mCell.GetPos + Vector2.up);
|
|
if (temp!=null)
|
|
{
|
|
if (Vector2.Distance(temp.GetPos, AI.mCell.GetPos) < 0.2f)
|
|
{
|
|
Debug.Log(44222222222222222);
|
|
return new List<Cell>();
|
|
}
|
|
if (temp.currentCharacter == null)
|
|
{
|
|
tempList.Add(temp);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return new List<Cell>();
|
|
}
|
|
}
|
|
{
|
|
Cell temp = FindCell(AttTrath.mCell.GetPos + Vector2.left);
|
|
if (temp!=null)
|
|
{
|
|
if (Vector2.Distance(temp.GetPos, AI.mCell.GetPos) < 0.2f)
|
|
{
|
|
Debug.Log(44222222222222222);
|
|
return new List<Cell>();
|
|
}
|
|
if (temp.currentCharacter == null)
|
|
{
|
|
tempList.Add(temp);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return new List<Cell>();
|
|
}
|
|
}
|
|
{
|
|
Cell temp = FindCell(AttTrath.mCell.GetPos + Vector2.down);
|
|
if (temp!=null)
|
|
{
|
|
if (Vector2.Distance(temp.GetPos, AI.mCell.GetPos) < 0.2f)
|
|
{
|
|
Debug.Log(44222222222222222);
|
|
return new List<Cell>();
|
|
}
|
|
if (temp.currentCharacter == null)
|
|
{
|
|
tempList.Add(temp);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return new List<Cell>();
|
|
}
|
|
}
|
|
//if (tempList.Find(v=>Vector2.Distance(v.GetPos,AI.mCell.GetPos) < 0.2f)!=null)
|
|
//{
|
|
// Debug.Log(44222222222222222);
|
|
// return new List<Cell>();
|
|
//}
|
|
Debug.Log(tempList.Contains(AI.mCell));
|
|
//ClosedList = new List<Cell>();
|
|
EndNode = tempList[Random.Range(0, tempList.Count)];
|
|
// EndNodecell=
|
|
Vector2 AiDis = EndNode.GetPos - StartNode.GetPos;
|
|
Debug.Log(AiDis);
|
|
List<Cell> Path = new List<Cell>();
|
|
Path.Add(EndNode);
|
|
while (AiDis.x != 0 || AiDis.y != 0)
|
|
{
|
|
if (AiDis.x != 0 && AiDis.y != 0)
|
|
{
|
|
if (Random.Range(0, 2) == 1)
|
|
{
|
|
if (AiDis.y<0)
|
|
{
|
|
AiDis -= Vector2.down;
|
|
// Debug.Log(FindCell(StartNode.GetPos + AiDis));
|
|
Path.Add(FindCell(StartNode.GetPos + AiDis));
|
|
}
|
|
else
|
|
{
|
|
AiDis -= Vector2.up;
|
|
// Debug.Log(FindCell(StartNode.GetPos + AiDis));
|
|
Path.Add(FindCell(StartNode.GetPos + AiDis));
|
|
}
|
|
|
|
// EndNode.parentNode
|
|
}
|
|
else
|
|
{
|
|
if (AiDis.x<0)
|
|
{
|
|
// Debug.Log(FindCell(StartNode.GetPos + AiDis));
|
|
AiDis -= Vector2.left;
|
|
Path.Add(FindCell(StartNode.GetPos + AiDis));
|
|
}
|
|
else
|
|
{
|
|
// Debug.Log(FindCell(StartNode.GetPos + AiDis));
|
|
AiDis -= Vector2.right;
|
|
Path.Add(FindCell(StartNode.GetPos + AiDis));
|
|
}
|
|
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (AiDis.x != 0)
|
|
{
|
|
if (AiDis.x < 0)
|
|
{
|
|
// Debug.Log(FindCell(StartNode.GetPos + AiDis));
|
|
AiDis -= Vector2.left;
|
|
Path.Add(FindCell(StartNode.GetPos + AiDis));
|
|
}
|
|
else
|
|
{
|
|
// Debug.Log(FindCell(StartNode.GetPos + AiDis));
|
|
AiDis -= Vector2.right;
|
|
Path.Add(FindCell(StartNode.GetPos + AiDis));
|
|
}
|
|
}
|
|
else if (AiDis.y != 0)
|
|
{
|
|
if (AiDis.y < 0)
|
|
{
|
|
AiDis -= Vector2.down;
|
|
// Debug.Log(FindCell(StartNode.GetPos + AiDis));
|
|
Path.Add(FindCell(StartNode.GetPos + AiDis));
|
|
}
|
|
else
|
|
{
|
|
AiDis -= Vector2.up;
|
|
// Debug.Log(FindCell(StartNode.GetPos + AiDis));
|
|
Path.Add(FindCell(StartNode.GetPos + AiDis));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// Path = Path.OrderByDescending(v => v).ToList();
|
|
return Path;
|
|
}
|
|
// public List<Cell> AiMovePath;
|
|
public void MoveTo(Character AI, List<Cell> Path_r)
|
|
{
|
|
List<Cell> Path = new List<Cell>();
|
|
|
|
int len = Path_r.Count;
|
|
for (int i = Path_r.Count - 1; i > 0; i--)
|
|
{
|
|
Path.Add(Path_r[i]);
|
|
}
|
|
|
|
Cell begin = StartNode;
|
|
Debug.Log(len);
|
|
int c = 0;
|
|
while (AI.GetCurrentMovement() >= 1) //曼哈顿距离大于攻击距离,行动点足够
|
|
{
|
|
if (c + 1 >= Path.Count)
|
|
{
|
|
break;
|
|
}
|
|
// Debug.Log(c + 1+",,,"+Path.Count);
|
|
if (Path[c + 1].transform.position.x > Path[c].transform.position.x)
|
|
{
|
|
StartCoroutine(DelayTest.DelayToInvoke(() => AI.Move(Vector2.right), 1f));
|
|
}
|
|
else if (Path[c + 1].transform.position.x < Path[c].transform.position.x)
|
|
{
|
|
StartCoroutine(DelayTest.DelayToInvoke(() => AI.Move(Vector2.left), 1f));
|
|
}
|
|
else if (Path[c + 1].transform.position.y < Path[c].transform.position.y)
|
|
{
|
|
StartCoroutine(DelayTest.DelayToInvoke(() => AI.Move(Vector2.down), 1f));
|
|
}
|
|
else if (Path[c + 1].transform.position.y > Path[c].transform.position.y)
|
|
{
|
|
StartCoroutine(DelayTest.DelayToInvoke(() => AI.Move(Vector2.up), 1f));
|
|
}
|
|
c++;
|
|
}
|
|
|
|
}
|
|
|
|
// public async void Attack(Character_AI _AI)
|
|
public void Attack(Character_AI _AI)
|
|
{
|
|
// _AI.weaponManager.SetCurrnetWeaponActive(true);
|
|
_AI.Turn((AttTrath.mCell.GetPos - _AI.mCell.GetPos));
|
|
// await Task.Delay(System.TimeSpan.FromSeconds(0.2f));
|
|
// bool judge = _AI.AiAtt();
|
|
// while (judge)
|
|
// {
|
|
// judge = _AI.AiAtt();
|
|
// await Task.Delay(System.TimeSpan.FromSeconds(0.2f));
|
|
// }
|
|
|
|
// _AI.weaponManager.SetCurrnetWeaponActive(false);
|
|
|
|
|
|
if(_AI.GetCurrentMovement() >= movementCost)
|
|
{
|
|
_AI.ChangeMovement(-movementCost);
|
|
RaycastHit2D hit = Physics2D.Raycast(_AI.transform.position,Vector2.up,1.0f,LayerMask.GetMask("Cells"));
|
|
Cell currentCell = hit.collider.GetComponent<Cell>();
|
|
if(currentCell.damage > 0)
|
|
_AI.ChangeHealth(-currentCell.damage);
|
|
|
|
//实施攻击并结算
|
|
AttTrath.ChangeHealth(AISettlement.DamageSettlement(AttTrath.AIDamage, _AI, AttTrath));
|
|
AttTrath.ChangePoise(-poiseDamgage);
|
|
if(AttTrath.GetCurrentHealth() == 0)
|
|
{
|
|
if(_AI.GetExecute())
|
|
{
|
|
_AI.ChangeHealth(10);
|
|
_AI.ChangeMana(5);
|
|
}
|
|
}
|
|
|
|
}
|
|
else
|
|
_AI.OutOfMovement();
|
|
|
|
|
|
}
|
|
|
|
public bool FindIn(Cell c, List<Cell> _cells)
|
|
{
|
|
foreach (var cell in _cells)
|
|
{
|
|
if (c == cell)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|