using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using TMPro;
using Unity.Profiling.Editor;
using Unity.VisualScripting;
using UnityEngine;
public class Djikstras : MonoBehaviour
{
public GameObject A;
public GameObject B;
public GameObject C;
public GameObject D;
public GameObject E;
public GameObject F;
public GameObject G;
public GameObject startingNode;
public GameObject finalNode;
public bool isTravelling = false;
public void djikstras()
{
isTravelling = true;
GameObject[] a = { B, C };
GameObject[] b = { A, C, D };
GameObject[] c = { A, B, C };
GameObject[] d = { B, C, E, F };
GameObject[] e = { D, F, G };
GameObject[] f = { E, D, G };
GameObject[] g = { F, E, };
GameObject[][] nodesRelations = { a, b, c, d, e, f, g };
GameObject[] nodes = { A, B, C, D, E, F, G };
float[] shortdist = { 0, 0, 0, 0, 0, 0, 0 };
GameObject[] prevNodes = { A, B, C, D, E, F, G };
List<GameObject> visitedNodes = new List<GameObject>();
GameObject currentNode;
float dist = 0;
GameObject lowestNode = A;
currentNode = startingNode;
while (visitedNodes.Count < 7)
{
while (currentNode != finalNode)
{
GameObject[] currentArray = a;
int count = 0;
for (int i = 0; i < nodes.Length; i++)
{
if (currentNode == nodes[i]){}
else
{
count = count + 1;
}
}
currentArray = nodesRelations[count];
nodes.RemoveAt(0);
float lowestnodedis = 1000;
for (int i = 0; i < currentArray.Length; i++)
{
float dis = Mathf.Abs(Mathf.Sqrt(Mathf.Pow((currentNode.transform.position.x - currentArray[i].transform.position.x), 2) + Mathf.Pow(currentNode.transform.position.y - currentArray[i].transform.position.y, 2)));
if (dis < lowestnodedis)
{
lowestnodedis = dis;
lowestNode = currentArray[i];
}
dis = dis + dist;
int index = 0;
for (int j = 0; j < nodes.Length; j++)
{
if (nodes[j] == currentArray[i]){}
else
{
index = index + 1;
}
}
if (dis < shortdist[index])
{
shortdist[index] = dis;
prevNodes[index] = currentNode;
}
}
visitedNodes.Add(currentNode);
currentNode = lowestNode;
}
}
Stack<GameObject> shortestPath = new Stack<GameObject>();
GameObject followthroughnode = finalNode;
while (followthroughnode != startingNode)
{
int i = 0;
for (int z = 0; z < nodes.Length; z++)
{
if (nodes[z] == followthroughnode){}
else
{
i = i + 1;
}
}
shortestPath.Push(followthroughnode);
followthroughnode = prevNodes[i];
}
enemyMovement(shortestPath, finalNode);
}
void enemyMovement(Stack<GameObject> stack, GameObject final)
{
float speed = 1f;
while(transform.position != final.transform.position)
{
GameObject nextNode = stack.Pop();
Vector3 nextPosition = new Vector3(nextNode.transform.position.x, nextNode.transform.position.y, -1);
transform.position = Vector3.MoveTowards(transform.position,nextPosition, speed*Time.deltaTime);
}
isTravelling = false;
}
void Update()
{
while(isTravelling == false)
{
djikstras();
}
}
}
这里的错误信息是:
OutOfMemoryException: Out of memory
System.Collections.Generic.List`1[T].set_Capacity (System.Int32 value) (at <17d9ce77f27a4bd2afb5ba32c9bea976>:0)
System.Collections.Generic.List`1[T].EnsureCapacity (System.Int32 min) (at <17d9ce77f27a4bd2afb5ba32c9bea976>:0)
System.Collections.Generic.List`1[T].AddWithResize (T item) (at <17d9ce77f27a4bd2afb5ba32c9bea976>:0)
Djikstras.djikstras () (at Assets/Scripts/Djikstras.cs:109)
Djikstras.Update () (at Assets/Scripts/Djikstras.cs:156)
我是一位学生,目前正在制作一个团结的录像。 这是我为敌人在我的游戏中使用的一种文字,这种游戏是建立在最低道路算法的基础之上的。
算法应当从目前敌对特性的起始点和最后需要达到的终点。 算法通过使用阵列对 no进行模拟和模拟,每一阵列均符合节点,并持有与其相关的节点。 实际游戏物体可与作为成分的敌方特性(绿地)一起看待。
( https://i.stack.imgur.com/abxF4.png)
之后,该方案采用分界线算法,在两条点之间找到最短的道路,将订单推向一个分点,然后将分界线移至移动功能。 之后,该运动的功能冲淡了每一条 no子,把敌人的特性移至那个 no子,直到它到达最后的终点。
我确信,这一错误的产生是由于正在创建的猎物阵阵阵阵列的数量,但并不肯定如何接手。