From 5770d41a0c2c7c48b4b454cbbcb273c7afe05933 Mon Sep 17 00:00:00 2001 From: juliuse98 <91191144+juliuse98@users.noreply.github.com> Date: Wed, 22 Sep 2021 20:33:11 +0200 Subject: [PATCH] Add files via upload This adds the PathNode class and the PathMap class which generates a path for a NPC to follow to a desired position. --- Assets/Scripts/PathGenerator.cs | 17 ++++ Assets/Scripts/PathGenerator.cs.meta | 11 +++ Assets/Scripts/PathMap.cs | 119 +++++++++++++++++++++++++++ Assets/Scripts/PathMap.cs.meta | 11 +++ 4 files changed, 158 insertions(+) create mode 100644 Assets/Scripts/PathGenerator.cs create mode 100644 Assets/Scripts/PathGenerator.cs.meta create mode 100644 Assets/Scripts/PathMap.cs create mode 100644 Assets/Scripts/PathMap.cs.meta diff --git a/Assets/Scripts/PathGenerator.cs b/Assets/Scripts/PathGenerator.cs new file mode 100644 index 0000000..be77b95 --- /dev/null +++ b/Assets/Scripts/PathGenerator.cs @@ -0,0 +1,17 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class PathNode +{ + + private Vector3 position; + public Vector2 index; + private float score; + public PathNode(Vector3 Pos, float Score) { + + position = Pos; + score = Score; + } + public Vector3 Position { get => position; set => position = value; } +} diff --git a/Assets/Scripts/PathGenerator.cs.meta b/Assets/Scripts/PathGenerator.cs.meta new file mode 100644 index 0000000..1d6a35e --- /dev/null +++ b/Assets/Scripts/PathGenerator.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0f50f0a081672ea4a9bc9f13de003f0a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/PathMap.cs b/Assets/Scripts/PathMap.cs new file mode 100644 index 0000000..80d1ecd --- /dev/null +++ b/Assets/Scripts/PathMap.cs @@ -0,0 +1,119 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class PathMap : MonoBehaviour +{ + private Vector3 position; + private PathNode[,] map; + private GameObject[,] ball; + private int rows = 40; + private int cols = 40; + private float spacing = 1f; + private float height = 0; + + private PathNode[] uncheckedNodes; + private List openList; + private List closedList; + private List nextList; + + void Start() + { + map = new PathNode[40, 40]; + ball = new GameObject[rows, cols]; + openList = new List(); + closedList = new List(); + nextList = new List(); + + for (int r = 0; r < rows; r++) + { + for (int c = 0; c < cols; 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++) + { + GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); + sphere.transform.position = map[r,c].Position; + ball[r,c] = sphere; + + } + } + FindClosestNode(new Vector3(0.7f,2,0.7f)); + } + 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 && !openList.Contains(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 && !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.x - 1 >= 0 && !openList.Contains(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 - 1 >= 0 && !openList.Contains(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 && !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 <= cols && !openList.Contains(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 && !openList.Contains(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) { + 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)]); + return map[Mathf.RoundToInt(pos.x / 2), Mathf.RoundToInt(pos.z / 2)]; + } + else { + return null; + } + } + public void QueryNodes() { + PathNode currentNode; + for (int r = 0; r < rows; r++) + { + for (int c = 0; c < cols; c++) + { + currentNode = map[r,c]; + + } + } + + } + + // Update is called once per frame + void Update() + { + + + } +} diff --git a/Assets/Scripts/PathMap.cs.meta b/Assets/Scripts/PathMap.cs.meta new file mode 100644 index 0000000..d75c5c7 --- /dev/null +++ b/Assets/Scripts/PathMap.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 49bd52c676aea7140a49d1803a03926a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: