mirror of
https://github.com/DerTyp7/grow-ai-unity.git
synced 2025-10-30 12:57:09 +01:00
added pathfinding
This commit is contained in:
@@ -5,17 +5,21 @@ using UnityEngine;
|
||||
// Written with https://www.youtube.com/watch?v=dulosHPl82A
|
||||
public class GridBuildingSystem : MonoBehaviour
|
||||
{
|
||||
public static GridBuildingSystem instance;
|
||||
[SerializeField] List<PlacedObjectTypeSO> placedObjectTypeSOList = new List<PlacedObjectTypeSO>(); // https://youtu.be/dulosHPl82A?t=1192
|
||||
PlacedObjectTypeSO placedObjectTypeSO;
|
||||
|
||||
Grid<GridObject> grid;
|
||||
public Grid<GridObject> buildingGrid;
|
||||
void Awake()
|
||||
{
|
||||
if (instance == null)
|
||||
instance = this;
|
||||
|
||||
int gridWidth = 20;
|
||||
int gridHeight = 20;
|
||||
float cellSize = 2f;
|
||||
float cellSize = 1f;
|
||||
|
||||
grid = new Grid<GridObject>(gridWidth, gridHeight, cellSize, Vector3.zero, (Grid<GridObject> g, int x, int y) => new GridObject(g, x, y));
|
||||
buildingGrid = new Grid<GridObject>(gridWidth, gridHeight, cellSize, Vector3.zero, (Grid<GridObject> g, int x, int y) => new GridObject(g, x, y));
|
||||
placedObjectTypeSO = placedObjectTypeSOList[0];
|
||||
}
|
||||
public class GridObject
|
||||
@@ -40,17 +44,18 @@ public class GridBuildingSystem : MonoBehaviour
|
||||
}
|
||||
void Update()
|
||||
{
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
// PLACE
|
||||
if (Input.GetKeyDown(KeyCode.B))
|
||||
{
|
||||
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);
|
||||
buildingGrid.GetXY(mousePosition, out int x, out int y);
|
||||
|
||||
List<Vector2Int> gridPositionList = placedObjectTypeSO.GetGridPositionList(new Vector2Int(x, y), PlacedObjectTypeSO.Dir.Down);
|
||||
|
||||
bool canBuild = true;
|
||||
foreach (Vector2Int gridPosition in gridPositionList)
|
||||
{
|
||||
if(!grid.GetGridObject(gridPosition.x, gridPosition.y).CanBuild())
|
||||
if(!buildingGrid.GetGridObject(gridPosition.x, gridPosition.y).CanBuild())
|
||||
{
|
||||
// Cannot build here
|
||||
canBuild = false;
|
||||
@@ -60,12 +65,12 @@ public class GridBuildingSystem : MonoBehaviour
|
||||
|
||||
if (canBuild)
|
||||
{
|
||||
PlacedObject placedObject = PlacedObject.Create(grid.GetWorldPosition(x, y), new Vector2Int(x, y), PlacedObjectTypeSO.Dir.Down, placedObjectTypeSO);
|
||||
PlacedObject placedObject = PlacedObject.Create(buildingGrid.GetWorldPosition(x, y), new Vector2Int(x, y), PlacedObjectTypeSO.Dir.Down, placedObjectTypeSO);
|
||||
|
||||
|
||||
foreach(Vector2Int gridPosition in gridPositionList)
|
||||
{
|
||||
grid.GetGridObject(gridPosition.x, gridPosition.y).SetPlacedObject(placedObject);
|
||||
buildingGrid.GetGridObject(gridPosition.x, gridPosition.y).SetPlacedObject(placedObject);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -73,9 +78,10 @@ public class GridBuildingSystem : MonoBehaviour
|
||||
{
|
||||
Debug.Log("Cannot build here!" + " " + mousePosition);
|
||||
}
|
||||
}else if (Input.GetMouseButtonDown(1))
|
||||
|
||||
}else if (Input.GetKeyDown(KeyCode.N)) // DEMOLISH
|
||||
{
|
||||
GridObject gridObject = grid.GetGridObject(Camera.main.ScreenToWorldPoint(Input.mousePosition));
|
||||
GridObject gridObject = buildingGrid.GetGridObject(Camera.main.ScreenToWorldPoint(Input.mousePosition));
|
||||
PlacedObject placedObject = gridObject.GetPlacedObject();
|
||||
if (placedObject != null)
|
||||
{
|
||||
@@ -85,7 +91,7 @@ public class GridBuildingSystem : MonoBehaviour
|
||||
|
||||
foreach (Vector2Int gridPosition in gridPositionList)
|
||||
{
|
||||
grid.GetGridObject(gridPosition.x, gridPosition.y).ClearPlacedObject();
|
||||
buildingGrid.GetGridObject(gridPosition.x, gridPosition.y).ClearPlacedObject();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
49
Assets/Scripts/Grid/PlacedObject.cs
Normal file
49
Assets/Scripts/Grid/PlacedObject.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Tilemaps;
|
||||
|
||||
public class PlacedObject : MonoBehaviour
|
||||
{
|
||||
public static PlacedObject Create(Vector3 worldPosition, Vector2Int origin, PlacedObjectTypeSO.Dir dir, PlacedObjectTypeSO placedObjectTypeSO)
|
||||
{
|
||||
Transform placeObjectTransform = Instantiate(placedObjectTypeSO.prefab, worldPosition, Quaternion.identity);
|
||||
|
||||
PlacedObject placedObject = placeObjectTransform.GetComponent<PlacedObject>();
|
||||
placedObject.placedObjectTypeSO = placedObjectTypeSO;
|
||||
placedObject.origin = origin;
|
||||
placedObject.dir = dir;
|
||||
|
||||
if (placedObjectTypeSO.isWalkable)
|
||||
{
|
||||
foreach(Vector2Int position in placedObject.GetGridPositionList())
|
||||
{
|
||||
Pathfinding.Instance.GetNode(position.x, position.y).SetIsWalkable(true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return placedObject;
|
||||
}
|
||||
|
||||
PlacedObjectTypeSO placedObjectTypeSO;
|
||||
Vector2Int origin;
|
||||
PlacedObjectTypeSO.Dir dir;
|
||||
|
||||
public List<Vector2Int> GetGridPositionList()
|
||||
{
|
||||
return placedObjectTypeSO.GetGridPositionList(origin, PlacedObjectTypeSO.Dir.Down);
|
||||
}
|
||||
|
||||
public void DestroySelf()
|
||||
{
|
||||
if (placedObjectTypeSO.isWalkable)
|
||||
{
|
||||
foreach (Vector2Int position in GetGridPositionList())
|
||||
{
|
||||
Pathfinding.Instance.GetNode(position.x, position.y).SetIsWalkable(false);
|
||||
}
|
||||
}
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Grid/PlacedObject.cs.meta
Normal file
11
Assets/Scripts/Grid/PlacedObject.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 56fadc4f2b895ad46bda623c68601c7e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -17,7 +17,6 @@ public class PlacedObjectTypeSO : ScriptableObject
|
||||
case Dir.Right: return Dir.Down;
|
||||
}
|
||||
}
|
||||
|
||||
public enum Dir
|
||||
{
|
||||
Down,
|
||||
@@ -31,7 +30,7 @@ public class PlacedObjectTypeSO : ScriptableObject
|
||||
//public Transform visual;
|
||||
public int width;
|
||||
public int height;
|
||||
|
||||
public bool isWalkable = false;
|
||||
|
||||
public int GetRotationAngle(Dir dir)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user