mirror of
https://github.com/DerTyp7/fps-citybuild-unity.git
synced 2025-10-30 12:37:08 +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;
|
private Vector3 position;
|
||||||
public Vector2 index;
|
public Vector2 index;
|
||||||
private float scoreF;
|
private float score;
|
||||||
private float scoreG;
|
public PathNode(Vector3 Pos, float Score) {
|
||||||
private float scoreH;
|
|
||||||
public List<PathNode> neigbors;
|
|
||||||
|
|
||||||
private PathNode previous;
|
|
||||||
|
|
||||||
public PathNode(Vector3 Pos) {
|
|
||||||
neigbors = new List<PathNode>();
|
|
||||||
position = Pos;
|
position = Pos;
|
||||||
scoreG = Mathf.Infinity;
|
score = Score;
|
||||||
scoreF = Mathf.Infinity;
|
|
||||||
scoreH = Mathf.Infinity;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Vector3 Position { get => position; set => position = value; }
|
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 Vector3 position;
|
||||||
private PathNode[,] map;
|
private PathNode[,] map;
|
||||||
private GameObject[,] ball;
|
private GameObject[,] ball;
|
||||||
private int rows = 200;
|
private int rows = 40;
|
||||||
private int cols = 200;
|
private int cols = 40;
|
||||||
private float spacing = 1f;
|
private float spacing = 1f;
|
||||||
private float height = 0;
|
private float height = 0;
|
||||||
|
|
||||||
@@ -17,12 +17,9 @@ public class PathMap : MonoBehaviour
|
|||||||
private List<PathNode> closedList;
|
private List<PathNode> closedList;
|
||||||
private List<PathNode> nextList;
|
private List<PathNode> nextList;
|
||||||
|
|
||||||
private List<PathNode> path;
|
|
||||||
|
|
||||||
void Start()
|
void Start()
|
||||||
{
|
{
|
||||||
path = new List<PathNode>();
|
map = new PathNode[40, 40];
|
||||||
map = new PathNode[1000, 1000];
|
|
||||||
ball = new GameObject[rows, cols];
|
ball = new GameObject[rows, cols];
|
||||||
openList = new List<PathNode>();
|
openList = new List<PathNode>();
|
||||||
closedList = new List<PathNode>();
|
closedList = new List<PathNode>();
|
||||||
@@ -32,194 +29,87 @@ public class PathMap : MonoBehaviour
|
|||||||
{
|
{
|
||||||
for (int c = 0; c < cols; c++)
|
for (int c = 0; c < cols; c++)
|
||||||
{
|
{
|
||||||
PathNode node = new PathNode(new Vector3(r * spacing, height, c * spacing));
|
PathNode node = new PathNode(new Vector3(r * spacing, height, c * spacing), 1f);
|
||||||
node.index = new Vector2(r, c);
|
node.index = new Vector2(r,c);
|
||||||
map[r, c] = node;
|
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++)
|
for (int r = 0; r < rows; r++)
|
||||||
{
|
{
|
||||||
for (int c = 0; c < cols; c++)
|
for (int c = 0; c < cols; c++)
|
||||||
{
|
{
|
||||||
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
|
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
|
||||||
sphere.transform.position = map[r, c].Position;
|
sphere.transform.position = map[r,c].Position;
|
||||||
ball[r, c] = sphere;
|
ball[r,c] = sphere;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
FindClosestNode(new Vector3(0.7f,2,0.7f));
|
||||||
QueryNodes(new Vector3(0.7f, 2, 0.7f), new Vector3(2.5f, 0, 2.5f));
|
|
||||||
}
|
}
|
||||||
private void AddAllNeighbors(Vector2 index)
|
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])) {
|
||||||
if ((int)index.x - 1 >= 0 && (int)index.y - 1 >= 0)
|
openList.Add(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]);
|
|
||||||
}
|
}
|
||||||
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)
|
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)];
|
return map[Mathf.RoundToInt(pos.x / 2), Mathf.RoundToInt(pos.z / 2)];
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
{
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void QueryNodes(Vector3 Vstart, Vector3 Vend)
|
public void QueryNodes() {
|
||||||
{
|
PathNode currentNode;
|
||||||
|
for (int r = 0; r < rows; r++)
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
d++;
|
for (int c = 0; c < cols; c++)
|
||||||
if (d > 10000)
|
|
||||||
{
|
|
||||||
Debug.Log("Mist");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
int winner = 0;
|
|
||||||
for (int i = 0; i < openList.Count; i++)
|
|
||||||
{
|
{
|
||||||
|
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
|
// Update is called once per frame
|
||||||
void Update()
|
void Update()
|
||||||
{
|
{
|
||||||
@@ -227,4 +117,3 @@ public class PathMap : MonoBehaviour
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user