mirror of
				https://github.com/DerTyp7/grow-ai-unity.git
				synced 2025-10-30 21:07:09 +01:00 
			
		
		
		
	placing ways
This commit is contained in:
		
							
								
								
									
										18
									
								
								Assets/Objects/way_normal.asset
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								Assets/Objects/way_normal.asset
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,18 @@ | |||||||
|  | %YAML 1.1 | ||||||
|  | %TAG !u! tag:unity3d.com,2011: | ||||||
|  | --- !u!114 &11400000 | ||||||
|  | MonoBehaviour: | ||||||
|  |   m_ObjectHideFlags: 0 | ||||||
|  |   m_CorrespondingSourceObject: {fileID: 0} | ||||||
|  |   m_PrefabInstance: {fileID: 0} | ||||||
|  |   m_PrefabAsset: {fileID: 0} | ||||||
|  |   m_GameObject: {fileID: 0} | ||||||
|  |   m_Enabled: 1 | ||||||
|  |   m_EditorHideFlags: 0 | ||||||
|  |   m_Script: {fileID: 11500000, guid: ed4c766e9f0dd454da548c672fa99b65, type: 3} | ||||||
|  |   m_Name: way_normal | ||||||
|  |   m_EditorClassIdentifier:  | ||||||
|  |   nameString: Way | ||||||
|  |   prefab: {fileID: 3420327930372171566, guid: 1b0d9211d89253549b71b121b268c59b, type: 3} | ||||||
|  |   width: 1 | ||||||
|  |   height: 1 | ||||||
							
								
								
									
										8
									
								
								Assets/Objects/way_normal.asset.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								Assets/Objects/way_normal.asset.meta
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,8 @@ | |||||||
|  | fileFormatVersion: 2 | ||||||
|  | guid: b08e32832db84ca489b3dd705df7eff7 | ||||||
|  | NativeFormatImporter: | ||||||
|  |   externalObjects: {} | ||||||
|  |   mainObjectFileID: 11400000 | ||||||
|  |   userData:  | ||||||
|  |   assetBundleName:  | ||||||
|  |   assetBundleVariant:  | ||||||
| @@ -223,7 +223,7 @@ MonoBehaviour: | |||||||
|   m_Script: {fileID: 11500000, guid: a9e8b2a885476754e9652c90137f91bc, type: 3} |   m_Script: {fileID: 11500000, guid: a9e8b2a885476754e9652c90137f91bc, type: 3} | ||||||
|   m_Name:  |   m_Name:  | ||||||
|   m_EditorClassIdentifier:  |   m_EditorClassIdentifier:  | ||||||
|   testTransform: {fileID: 3420327930372171566, guid: 1b0d9211d89253549b71b121b268c59b, type: 3} |   placedObjectTypeSO: {fileID: 11400000, guid: b08e32832db84ca489b3dd705df7eff7, type: 2} | ||||||
| --- !u!4 &1522634446 | --- !u!4 &1522634446 | ||||||
| Transform: | Transform: | ||||||
|   m_ObjectHideFlags: 0 |   m_ObjectHideFlags: 0 | ||||||
|   | |||||||
| @@ -4,7 +4,7 @@ using UnityEngine; | |||||||
|  |  | ||||||
| public class GridBuildingSystem : MonoBehaviour | public class GridBuildingSystem : MonoBehaviour | ||||||
| { | { | ||||||
|     [SerializeField] Transform testTransform; |     [SerializeField] PlacedObjectTypeSO placedObjectTypeSO; | ||||||
|  |  | ||||||
|     Grid<GridObject> grid; |     Grid<GridObject> grid; | ||||||
|     void Awake() |     void Awake() | ||||||
| @@ -42,11 +42,28 @@ public class GridBuildingSystem : MonoBehaviour | |||||||
|             Vector3 mousePosition = new Vector3(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y); |             Vector3 mousePosition = new Vector3(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y); | ||||||
|             grid.GetXY(mousePosition, out int x, out int y); |             grid.GetXY(mousePosition, out int x, out int y); | ||||||
|  |  | ||||||
|             GridObject gridObject = grid.GetGridObject(x, y); |             List<Vector2Int> gridPositionList = placedObjectTypeSO.GetGridPositionList(new Vector2Int(x, y), PlacedObjectTypeSO.Dir.Down); | ||||||
|             if (gridObject.CanBuild()) |  | ||||||
|  |             bool canBuild = true; | ||||||
|  |             foreach (Vector2Int gridPosition in gridPositionList) | ||||||
|             { |             { | ||||||
|                 Transform builtTransform = Instantiate(testTransform, grid.GetWorldPosition(x, y), Quaternion.identity); |                 if(!grid.GetGridObject(gridPosition.x, gridPosition.y).CanBuild()) | ||||||
|                 gridObject.SetTransform(builtTransform); |                 { | ||||||
|  |                     // Cannot build here | ||||||
|  |                     canBuild = false; | ||||||
|  |                     break; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             if (canBuild) | ||||||
|  |             { | ||||||
|  |                 Transform builtTransform = Instantiate(placedObjectTypeSO.prefab, grid.GetWorldPosition(x, y), Quaternion.identity); | ||||||
|  |                  | ||||||
|  |                 foreach(Vector2Int gridPosition in gridPositionList) | ||||||
|  |                 { | ||||||
|  |                     grid.GetGridObject(gridPosition.x, gridPosition.y).SetTransform(builtTransform); | ||||||
|  |                 } | ||||||
|  |                  | ||||||
|             } |             } | ||||||
|             else |             else | ||||||
|             { |             { | ||||||
|   | |||||||
| @@ -2,17 +2,89 @@ using System.Collections; | |||||||
| using System.Collections.Generic; | using System.Collections.Generic; | ||||||
| using UnityEngine; | using UnityEngine; | ||||||
|  |  | ||||||
| public class PlacedObjectTypeSO : MonoBehaviour | [CreateAssetMenu()] | ||||||
|  | public class PlacedObjectTypeSO : ScriptableObject | ||||||
| { | { | ||||||
|     // Start is called before the first frame update |  | ||||||
|     void Start() |  | ||||||
|     { |  | ||||||
|  |  | ||||||
|  |     public static Dir GetNextDir(Dir dir) | ||||||
|  |     { | ||||||
|  |         switch (dir) | ||||||
|  |         { | ||||||
|  |             default: | ||||||
|  |             case Dir.Down: return Dir.Left; | ||||||
|  |             case Dir.Left: return Dir.Up; | ||||||
|  |             case Dir.Up: return Dir.Right; | ||||||
|  |             case Dir.Right: return Dir.Down; | ||||||
|  |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     // Update is called once per frame |     public enum Dir | ||||||
|     void Update() |  | ||||||
|     { |     { | ||||||
|          |         Down, | ||||||
|  |         Left, | ||||||
|  |         Up, | ||||||
|  |         Right, | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     public string nameString; | ||||||
|  |     public Transform prefab; | ||||||
|  |     //public Transform visual; | ||||||
|  |     public int width; | ||||||
|  |     public int height; | ||||||
|  |  | ||||||
|  |  | ||||||
|  |     public int GetRotationAngle(Dir dir) | ||||||
|  |     { | ||||||
|  |         switch (dir) | ||||||
|  |         { | ||||||
|  |             default: | ||||||
|  |             case Dir.Down: return 0; | ||||||
|  |             case Dir.Left: return 90; | ||||||
|  |             case Dir.Up: return 180; | ||||||
|  |             case Dir.Right: return 270; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public Vector2Int GetRotationOffset(Dir dir) | ||||||
|  |     { | ||||||
|  |         switch (dir) | ||||||
|  |         { | ||||||
|  |             default: | ||||||
|  |             case Dir.Down: return new Vector2Int(0, 0); | ||||||
|  |             case Dir.Left: return new Vector2Int(0, width); | ||||||
|  |             case Dir.Up: return new Vector2Int(width, height); | ||||||
|  |             case Dir.Right: return new Vector2Int(height, 0); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     public List<Vector2Int> GetGridPositionList(Vector2Int offset, Dir dir) | ||||||
|  |     { | ||||||
|  |         List<Vector2Int> gridPositionList = new List<Vector2Int>(); | ||||||
|  |         switch (dir) | ||||||
|  |         { | ||||||
|  |             default: | ||||||
|  |             case Dir.Down: | ||||||
|  |             case Dir.Up: | ||||||
|  |                 for (int x = 0; x < width; x++) | ||||||
|  |                 { | ||||||
|  |                     for (int y = 0; y < height; y++) | ||||||
|  |                     { | ||||||
|  |                         gridPositionList.Add(offset + new Vector2Int(x, y)); | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|  |                 break; | ||||||
|  |             case Dir.Left: | ||||||
|  |             case Dir.Right: | ||||||
|  |                 for (int x = 0; x < height; x++) | ||||||
|  |                 { | ||||||
|  |                     for (int y = 0; y < width; y++) | ||||||
|  |                     { | ||||||
|  |                         gridPositionList.Add(offset + new Vector2Int(x, y)); | ||||||
|  |                     } | ||||||
|  |                 } | ||||||
|  |                 break; | ||||||
|  |         } | ||||||
|  |         return gridPositionList; | ||||||
|  |     } | ||||||
|  |  | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 j.mei7
					j.mei7