mirror of
https://github.com/DerTyp7/fps-citybuild-unity.git
synced 2025-10-29 12:22:07 +01:00
Merge remote-tracking branch 'origin/juliuse98-patch-1' into main
This commit is contained in:
@@ -7,24 +7,11 @@ public class PathNode
|
||||
|
||||
private Vector3 position;
|
||||
public Vector2 index;
|
||||
private float scoreF;
|
||||
private float scoreG;
|
||||
private float scoreH;
|
||||
public List<PathNode> neigbors;
|
||||
private float score;
|
||||
public PathNode(Vector3 Pos, float Score) {
|
||||
|
||||
private PathNode previous;
|
||||
|
||||
public PathNode(Vector3 Pos) {
|
||||
neigbors = new List<PathNode>();
|
||||
position = Pos;
|
||||
scoreG = Mathf.Infinity;
|
||||
scoreF = Mathf.Infinity;
|
||||
scoreH = Mathf.Infinity;
|
||||
score = Score;
|
||||
}
|
||||
|
||||
public Vector3 Position { get => position; set => position = value; }
|
||||
public float Hscore { get => scoreH; set => scoreH = value; }
|
||||
public float Gscore { get => scoreG; set => scoreG = value; }
|
||||
public float Fscore { get => scoreF; set => scoreF = value; }
|
||||
public PathNode Previous { get => previous; set => previous = value; }
|
||||
}
|
||||
|
||||
@@ -7,8 +7,8 @@ public class PathMap : MonoBehaviour
|
||||
private Vector3 position;
|
||||
private PathNode[,] map;
|
||||
private GameObject[,] ball;
|
||||
private int rows = 200;
|
||||
private int cols = 200;
|
||||
private int rows = 40;
|
||||
private int cols = 40;
|
||||
private float spacing = 1f;
|
||||
private float height = 0;
|
||||
|
||||
@@ -17,12 +17,9 @@ public class PathMap : MonoBehaviour
|
||||
private List<PathNode> closedList;
|
||||
private List<PathNode> nextList;
|
||||
|
||||
private List<PathNode> path;
|
||||
|
||||
void Start()
|
||||
{
|
||||
path = new List<PathNode>();
|
||||
map = new PathNode[1000, 1000];
|
||||
map = new PathNode[40, 40];
|
||||
ball = new GameObject[rows, cols];
|
||||
openList = new List<PathNode>();
|
||||
closedList = new List<PathNode>();
|
||||
@@ -32,21 +29,12 @@ public class PathMap : MonoBehaviour
|
||||
{
|
||||
for (int c = 0; c < cols; c++)
|
||||
{
|
||||
PathNode node = new PathNode(new Vector3(r * spacing, height, c * spacing));
|
||||
node.index = new Vector2(r, c);
|
||||
PathNode node = new PathNode(new Vector3(r * spacing, height, c * spacing), 1f);
|
||||
node.index = new Vector2(r,c);
|
||||
map[r, c] = node;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
for (int r = 0; r < rows; r++)
|
||||
{
|
||||
for (int c = 0; c < cols; c++)
|
||||
{
|
||||
AddAllNeighbors(new Vector2(r, c));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for (int r = 0; r < rows; r++)
|
||||
@@ -54,172 +42,74 @@ public class PathMap : MonoBehaviour
|
||||
for (int c = 0; c < cols; c++)
|
||||
{
|
||||
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
|
||||
sphere.transform.position = map[r, c].Position;
|
||||
ball[r, c] = sphere;
|
||||
sphere.transform.position = map[r,c].Position;
|
||||
ball[r,c] = sphere;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
QueryNodes(new Vector3(0.7f, 2, 0.7f), new Vector3(2.5f, 0, 2.5f));
|
||||
FindClosestNode(new Vector3(0.7f,2,0.7f));
|
||||
}
|
||||
private void AddAllNeighbors(Vector2 index)
|
||||
{
|
||||
if ((int)index.x - 1 >= 0 && (int)index.y - 1 >= 0)
|
||||
{
|
||||
map[(int)index.x, (int)index.y].neigbors.Add(map[(int)index.x - 1, (int)index.y - 1]);
|
||||
private void AddAllNeigbors(Vector2 index) {
|
||||
if ((int)index.x - 1 >= 0 && (int) index.y - 1 >= 0 && !openList.Contains(map[(int)index.x - 1, (int)index.y - 1])) {
|
||||
openList.Add(map[(int)index.x - 1, (int)index.y - 1]);
|
||||
}
|
||||
if ((int)index.y - 1 >= 0)
|
||||
if ((int)index.y - 1 >= 0 && !openList.Contains(map[(int)index.x, (int)index.y - 1]))
|
||||
{
|
||||
map[(int)index.x, (int)index.y].neigbors.Add(map[(int)index.x, (int)index.y - 1]);
|
||||
openList.Add(map[(int)index.x, (int)index.y - 1]);
|
||||
}
|
||||
if ((int)index.x + 1 < rows && (int)index.y - 1 >= 0)
|
||||
if ((int)index.x + 1 <= rows && (int)index.y - 1 >= 0 && !openList.Contains(map[(int)index.x + 1, (int)index.y - 1]))
|
||||
{
|
||||
map[(int)index.x, (int)index.y].neigbors.Add(map[(int)index.x + 1, (int)index.y - 1]);
|
||||
openList.Add(map[(int)index.x + 1, (int)index.y - 1]);
|
||||
}
|
||||
|
||||
if ((int)index.x - 1 >= 0)
|
||||
if ((int)index.x - 1 >= 0 && !openList.Contains(map[(int)index.x - 1, (int)index.y]))
|
||||
{
|
||||
map[(int)index.x, (int)index.y].neigbors.Add(map[(int)index.x - 1, (int)index.y]);
|
||||
openList.Add(map[(int)index.x - 1, (int)index.y]);
|
||||
}
|
||||
if ((int)index.x + 1 < rows && (int)index.y >= 0)
|
||||
if ((int)index.x + 1 <= rows && (int)index.y - 1 >= 0 && !openList.Contains(map[(int)index.x + 1, (int)index.y]))
|
||||
{
|
||||
map[(int)index.x, (int)index.y].neigbors.Add(map[(int)index.x + 1, (int)index.y]);
|
||||
openList.Add(map[(int)index.x + 1, (int)index.y]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
if ((int)index.x - 1 >= 0 && (int)index.y + 1 < cols)
|
||||
if ((int)index.x - 1 >= 0 && (int)index.y + 1 <= cols && !openList.Contains(map[(int)index.x - 1, (int)index.y + 1]))
|
||||
{
|
||||
map[(int)index.x, (int)index.y].neigbors.Add(map[(int)index.x - 1, (int)index.y + 1]);
|
||||
openList.Add(map[(int)index.x - 1, (int)index.y + 1]);
|
||||
}
|
||||
if ((int)index.y + 1 < cols)
|
||||
if ((int)index.y + 1 <= cols && !openList.Contains(map[(int)index.x, (int)index.y + 1]))
|
||||
{
|
||||
map[(int)index.x, (int)index.y].neigbors.Add(map[(int)index.x, (int)index.y + 1]);
|
||||
openList.Add(map[(int)index.x, (int)index.y + 1]);
|
||||
}
|
||||
if ((int)index.x + 1 < rows && (int)index.y + 1 < cols)
|
||||
if ((int)index.x + 1 <= rows && (int)index.y + 1 <= cols && !openList.Contains(map[(int)index.x + 1, (int)index.y + 1]))
|
||||
{
|
||||
map[(int)index.x, (int)index.y].neigbors.Add(map[(int)index.x + 1, (int)index.y + 1]);
|
||||
openList.Add(map[(int)index.x + 1, (int)index.y + 1]);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
private PathNode FindClosestNode(Vector3 pos)
|
||||
{
|
||||
private PathNode FindClosestNode(Vector3 pos) {
|
||||
if (pos.x > 0 && pos.x < rows * spacing && pos.z > 0 && pos.z < cols * spacing)
|
||||
{
|
||||
//Destroy(ball[Mathf.RoundToInt(pos.x / spacing), Mathf.RoundToInt(pos.z / spacing)]);
|
||||
Destroy(ball[Mathf.RoundToInt(pos.x / spacing), Mathf.RoundToInt(pos.z / spacing)]);
|
||||
return map[Mathf.RoundToInt(pos.x / 2), Mathf.RoundToInt(pos.z / 2)];
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public void QueryNodes(Vector3 Vstart, Vector3 Vend)
|
||||
{
|
||||
|
||||
bool finished = false;
|
||||
|
||||
PathNode start = FindClosestNode(Vstart);
|
||||
start.Gscore = 0;
|
||||
PathNode end = map[199, 150];
|
||||
Debug.Log(end.index);
|
||||
|
||||
openList.Add(start);
|
||||
|
||||
PathNode current;
|
||||
Debug.Log(Time.time);
|
||||
int d = 0;
|
||||
while (!finished)
|
||||
public void QueryNodes() {
|
||||
PathNode currentNode;
|
||||
for (int r = 0; r < rows; r++)
|
||||
{
|
||||
d++;
|
||||
if (d > 10000)
|
||||
{
|
||||
Debug.Log("Mist");
|
||||
return;
|
||||
}
|
||||
int winner = 0;
|
||||
for (int i = 0; i < openList.Count; i++)
|
||||
for (int c = 0; c < cols; c++)
|
||||
{
|
||||
currentNode = map[r,c];
|
||||
|
||||
if (openList[i].Fscore < openList[winner].Fscore) winner = i;
|
||||
}
|
||||
current = openList[winner];
|
||||
openList.RemoveAt(winner);
|
||||
closedList.Add(current);
|
||||
Destroy(ball[(int)current.index.x, (int)current.index.y]);
|
||||
|
||||
|
||||
|
||||
if (current != end)
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
foreach (PathNode p in current.neigbors)
|
||||
{
|
||||
if (!closedList.Contains(p))
|
||||
{
|
||||
|
||||
float tempG = current.Gscore + heuristic(p.Position, current.Position);
|
||||
bool newPath = false;
|
||||
if (openList.Contains(p))
|
||||
{
|
||||
|
||||
|
||||
if (tempG < p.Gscore)
|
||||
{
|
||||
p.Gscore = tempG;
|
||||
newPath = true;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
p.Gscore = tempG;
|
||||
newPath = true;
|
||||
openList.Add(p);
|
||||
|
||||
}
|
||||
if (newPath)
|
||||
{
|
||||
p.Hscore = heuristic(p.Position, end.Position);
|
||||
p.Fscore = p.Gscore + p.Hscore;
|
||||
p.Previous = current;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
PathNode temp = end;
|
||||
path.Add(temp);
|
||||
while (temp.Previous != null)
|
||||
{
|
||||
path.Add(temp.Previous);
|
||||
temp = temp.Previous;
|
||||
}
|
||||
Debug.Log(path.Count);
|
||||
Debug.Log("yeet it finished");
|
||||
Debug.Log(Time.time);
|
||||
for (int i = path.Count - 1; i >= 0;i-- )
|
||||
{
|
||||
//Debug.Log(path[i].index);
|
||||
}
|
||||
finished = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private float heuristic(Vector3 pos1, Vector3 pos2)
|
||||
{
|
||||
return Vector3.Distance(pos1, pos2);
|
||||
|
||||
}
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
@@ -227,4 +117,3 @@ public class PathMap : MonoBehaviour
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user