mirror of
				https://github.com/DerTyp7/grow-ai-unity.git
				synced 2025-10-30 21:07:09 +01:00 
			
		
		
		
	
		
			
				
	
	
		
			51 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			51 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using System.Collections;
 | |
| using System.Collections.Generic;
 | |
| using UnityEngine;
 | |
| using UnityEngine.Tilemaps;
 | |
| 
 | |
| public abstract class PlacedObject : MonoBehaviour
 | |
| {
 | |
|     public static PlacedObject Create(Vector3 worldPosition, Vector2Int origin, PlacedObjectTypeSO placedObjectTypeSO)
 | |
|     {
 | |
|         Transform placeObjectTransform = Instantiate(placedObjectTypeSO.prefab, worldPosition, Quaternion.identity);
 | |
| 
 | |
|         PlacedObject placedObject = placeObjectTransform.GetComponent<PlacedObject>();
 | |
|         placedObject.placedObjectTypeSO = placedObjectTypeSO;
 | |
|         placedObject.origin = origin;
 | |
| 
 | |
|         placedObject.OnPlace();
 | |
| 
 | |
|         if (placedObjectTypeSO.isWalkable) 
 | |
|         {
 | |
|             foreach(Vector2Int position in placedObject.GetGridPositionList())
 | |
|             {
 | |
|                 Pathfinding.Instance.GetNode(position.x, position.y).SetIsWalkable(true);
 | |
|             }
 | |
| 
 | |
|         }
 | |
| 
 | |
|         return placedObject;
 | |
|     }
 | |
| 
 | |
|     PlacedObjectTypeSO placedObjectTypeSO;
 | |
|     Vector2Int origin;
 | |
| 
 | |
|     public abstract void OnPlace();
 | |
| 
 | |
|     public List<Vector2Int> GetGridPositionList()
 | |
|     {
 | |
|         return placedObjectTypeSO.GetGridPositionList(origin);
 | |
|     }
 | |
| 
 | |
|     public void DestroySelf()
 | |
|     {
 | |
|         if (placedObjectTypeSO.isWalkable)
 | |
|         {
 | |
|             foreach (Vector2Int position in GetGridPositionList())
 | |
|             {
 | |
|                 Pathfinding.Instance.GetNode(position.x, position.y).SetIsWalkable(false);
 | |
|             }
 | |
|         }
 | |
|         Destroy(gameObject);
 | |
|     }
 | |
| } | 
