mirror of
				https://github.com/DerTyp7/fps-citybuild-unity.git
				synced 2025-10-31 13:17:07 +01:00 
			
		
		
		
	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.
This commit is contained in:
		
							
								
								
									
										17
									
								
								Assets/Scripts/PathGenerator.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								Assets/Scripts/PathGenerator.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -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; } | ||||
| } | ||||
							
								
								
									
										11
									
								
								Assets/Scripts/PathGenerator.cs.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								Assets/Scripts/PathGenerator.cs.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: 0f50f0a081672ea4a9bc9f13de003f0a | ||||
| MonoImporter: | ||||
|   externalObjects: {} | ||||
|   serializedVersion: 2 | ||||
|   defaultReferences: [] | ||||
|   executionOrder: 0 | ||||
|   icon: {instanceID: 0} | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
							
								
								
									
										119
									
								
								Assets/Scripts/PathMap.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										119
									
								
								Assets/Scripts/PathMap.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -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<PathNode> openList; | ||||
|     private List<PathNode> closedList; | ||||
|     private List<PathNode> nextList; | ||||
|  | ||||
|     void Start() | ||||
|     { | ||||
|         map = new PathNode[40, 40]; | ||||
|         ball = new GameObject[rows, cols]; | ||||
|         openList = new List<PathNode>(); | ||||
|         closedList = new List<PathNode>(); | ||||
|         nextList = new List<PathNode>(); | ||||
|  | ||||
|         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() | ||||
|     { | ||||
|  | ||||
|  | ||||
|     } | ||||
| } | ||||
							
								
								
									
										11
									
								
								Assets/Scripts/PathMap.cs.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								Assets/Scripts/PathMap.cs.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,11 @@ | ||||
| fileFormatVersion: 2 | ||||
| guid: 49bd52c676aea7140a49d1803a03926a | ||||
| MonoImporter: | ||||
|   externalObjects: {} | ||||
|   serializedVersion: 2 | ||||
|   defaultReferences: [] | ||||
|   executionOrder: 0 | ||||
|   icon: {instanceID: 0} | ||||
|   userData:  | ||||
|   assetBundleName:  | ||||
|   assetBundleVariant:  | ||||
		Reference in New Issue
	
	Block a user
	 juliuse98
					juliuse98