mirror of
https://github.com/DerTyp7/grow-ai-unity.git
synced 2025-10-30 04:47:10 +01:00
a
This commit is contained in:
@@ -15,6 +15,11 @@ public class GridBuildingSystem : MonoBehaviour
|
|||||||
PlacedObjectTypeSO selectedPlacedObjectTypeSO;
|
PlacedObjectTypeSO selectedPlacedObjectTypeSO;
|
||||||
Transform selectedGameObjectTransform;
|
Transform selectedGameObjectTransform;
|
||||||
|
|
||||||
|
Vector3 selectedWayStart;
|
||||||
|
|
||||||
|
public List<GameObject> blueprintWayList = new List<GameObject>();
|
||||||
|
bool isPlacingWay = false;
|
||||||
|
|
||||||
public class GridObject
|
public class GridObject
|
||||||
{
|
{
|
||||||
int x, y;
|
int x, y;
|
||||||
@@ -51,63 +56,109 @@ public class GridBuildingSystem : MonoBehaviour
|
|||||||
|
|
||||||
void Update()
|
void Update()
|
||||||
{
|
{
|
||||||
UpdateSelectedGameObject();
|
if (selectedGameObjectTransform != null)
|
||||||
|
|
||||||
// DEBUG
|
|
||||||
if (Input.GetKeyDown(KeyCode.U))
|
|
||||||
{
|
{
|
||||||
buildingGrid.GetGridObject(Camera.main.ScreenToWorldPoint(Input.mousePosition)).SwitchIsAccessable();
|
if (selectedPlacedObjectTypeSO.placedObjectType == PlacedObjectTypeSO.PlacedObjectType.WAY)
|
||||||
|
{
|
||||||
|
UpdateWayDrawing();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
UpdateSelectedGameObject();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void UpdateWayDrawing()
|
||||||
|
{
|
||||||
|
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||||
|
List<GameObject> dontDeleteBlueprintList = new List<GameObject>();
|
||||||
|
|
||||||
|
if (isPlacingWay)
|
||||||
|
{
|
||||||
|
foreach (Vector3 position in Pathfinding.Instance.FindPath(selectedWayStart, mousePosition, true))
|
||||||
|
{
|
||||||
|
GameObject placedBuilding = PlaceBuilding(position);
|
||||||
|
if (placedBuilding != null)
|
||||||
|
{
|
||||||
|
placedBuilding.GetComponent<PlacedObject>().isBlueprint = true;
|
||||||
|
dontDeleteBlueprintList.Add(placedBuilding);
|
||||||
|
blueprintWayList.Add(placedBuilding);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
buildingGrid.GetXY(position, out int x, out int y);
|
||||||
|
PlacedObject tempPlacedObject = buildingGrid.GetGridObject(x, y).GetPlacedObject();
|
||||||
|
if (tempPlacedObject.isBlueprint)
|
||||||
|
{
|
||||||
|
dontDeleteBlueprintList.Add(tempPlacedObject.gameObject);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Input.GetKeyDown(KeyCode.F))
|
foreach(GameObject blueprint in blueprintWayList)
|
||||||
{
|
{
|
||||||
buildingGrid.GetGridObject(Camera.main.ScreenToWorldPoint(Input.mousePosition)).GetPlacedObject();
|
if (!dontDeleteBlueprintList.Contains(blueprint))
|
||||||
|
{
|
||||||
|
Destroy(blueprint);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (Input.GetMouseButtonDown(0))
|
||||||
|
{
|
||||||
|
if (isPlacingWay)
|
||||||
|
{
|
||||||
|
// PLACE EVERYTHING
|
||||||
|
foreach (GameObject blueprint in blueprintWayList)
|
||||||
|
{
|
||||||
|
blueprint.GetComponent<PlacedObject>().isBlueprint = false;
|
||||||
|
blueprintWayList.Remove(blueprint);
|
||||||
|
}
|
||||||
|
isPlacingWay = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
isPlacingWay = true;
|
||||||
|
selectedWayStart = mousePosition;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void UpdateSelectedGameObject()
|
void UpdateSelectedGameObject()
|
||||||
{
|
{
|
||||||
if (selectedGameObjectTransform != null)
|
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||||
|
|
||||||
|
buildingGrid.GetXY(mousePosition, out int x, out int y);
|
||||||
|
|
||||||
|
selectedGameObjectTransform.position = buildingGrid.GetWorldPosition(x, y);
|
||||||
|
|
||||||
|
List<Vector2Int> gridPositionList = selectedPlacedObjectTypeSO.GetGridPositionList(new Vector2Int(x, y));
|
||||||
|
if (!CanBuild(gridPositionList))
|
||||||
{
|
{
|
||||||
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
selectedGameObjectTransform.gameObject.GetComponent<SpriteRenderer>().color = Color.red;
|
||||||
|
|
||||||
buildingGrid.GetXY(mousePosition, out int x, out int y);
|
|
||||||
|
|
||||||
selectedGameObjectTransform.position = buildingGrid.GetWorldPosition(x, y);
|
|
||||||
|
|
||||||
List<Vector2Int> gridPositionList = selectedPlacedObjectTypeSO.GetGridPositionList(new Vector2Int(x, y));
|
|
||||||
if (!CanBuild(gridPositionList))
|
|
||||||
{
|
|
||||||
selectedGameObjectTransform.gameObject.GetComponent<SpriteRenderer>().color = Color.red;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
selectedGameObjectTransform.gameObject.GetComponent<SpriteRenderer>().color = Color.white;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(selectedPlacedObjectTypeSO.placedObjectType == PlacedObjectTypeSO.PlacedObjectType.WAY)
|
|
||||||
{
|
|
||||||
if (Input.GetMouseButton(0))
|
|
||||||
{
|
|
||||||
PlaceBuilding();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (Input.GetMouseButtonDown(0))
|
|
||||||
{
|
|
||||||
PlaceBuilding();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (Input.GetMouseButtonDown(1) || Input.GetKeyDown(KeyCode.Escape))
|
|
||||||
{
|
|
||||||
DeselectBuilding();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
else if (selectedPlacedObjectTypeSO.placedObjectType != PlacedObjectTypeSO.PlacedObjectType.WAY)
|
||||||
|
{
|
||||||
|
selectedGameObjectTransform.gameObject.GetComponent<SpriteRenderer>().color = Color.white;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Input.GetMouseButtonDown(0))
|
||||||
|
{
|
||||||
|
|
||||||
|
PlaceBuilding(selectedGameObjectTransform.position);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (Input.GetMouseButtonDown(1) || Input.GetKeyDown(KeyCode.Escape))
|
||||||
|
{
|
||||||
|
DeselectBuilding();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DemolishBuilding(Vector3 position)
|
public void DemolishBuilding(Vector3 position)
|
||||||
{
|
{
|
||||||
GridObject gridObject = buildingGrid.GetGridObject(position); // Camera.main.ScreenToWorldPoint(Input.mousePosition)
|
GridObject gridObject = buildingGrid.GetGridObject(position); // Camera.main.ScreenToWorldPoint(Input.mousePosition)
|
||||||
@@ -141,12 +192,11 @@ public class GridBuildingSystem : MonoBehaviour
|
|||||||
return canBuild;
|
return canBuild;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void PlaceBuilding()
|
public GameObject PlaceBuilding(Vector3 position)
|
||||||
{
|
{
|
||||||
Vector3 position = selectedGameObjectTransform.position;
|
Debug.Log("HALLO");
|
||||||
|
position = new Vector3(position.x, position.y);
|
||||||
Vector3 mousePosition = new Vector3(position.x, position.y);
|
buildingGrid.GetXY(position, out int x, out int y);
|
||||||
buildingGrid.GetXY(mousePosition, out int x, out int y);
|
|
||||||
|
|
||||||
List<Vector2Int> gridPositionList = selectedPlacedObjectTypeSO.GetGridPositionList(new Vector2Int(x, y));
|
List<Vector2Int> gridPositionList = selectedPlacedObjectTypeSO.GetGridPositionList(new Vector2Int(x, y));
|
||||||
|
|
||||||
@@ -160,12 +210,13 @@ public class GridBuildingSystem : MonoBehaviour
|
|||||||
{
|
{
|
||||||
buildingGrid.GetGridObject(gridPosition.x, gridPosition.y).SetPlacedObject(placedObject);
|
buildingGrid.GetGridObject(gridPosition.x, gridPosition.y).SetPlacedObject(placedObject);
|
||||||
}
|
}
|
||||||
|
return placedObject.gameObject;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Debug.Log("Cannot build here!" + " " + mousePosition);
|
Debug.Log("Cannot build here!" + " " + position);
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SelectBuilding(PlacedObjectTypeSO placedObjectTypeSO)
|
public void SelectBuilding(PlacedObjectTypeSO placedObjectTypeSO)
|
||||||
@@ -180,6 +231,7 @@ public class GridBuildingSystem : MonoBehaviour
|
|||||||
Destroy(selectedGameObjectTransform.gameObject);
|
Destroy(selectedGameObjectTransform.gameObject);
|
||||||
selectedPlacedObjectTypeSO = null;
|
selectedPlacedObjectTypeSO = null;
|
||||||
selectedGameObjectTransform = null;
|
selectedGameObjectTransform = null;
|
||||||
|
isPlacingWay = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ public abstract class PlacedObject : MonoBehaviour
|
|||||||
placedObject.origin = origin;
|
placedObject.origin = origin;
|
||||||
|
|
||||||
placedObject.OnPlace();
|
placedObject.OnPlace();
|
||||||
|
placedObject.isBlueprint = false;
|
||||||
|
|
||||||
if (placedObjectTypeSO.isWalkable)
|
if (placedObjectTypeSO.isWalkable)
|
||||||
{
|
{
|
||||||
@@ -30,6 +31,7 @@ public abstract class PlacedObject : MonoBehaviour
|
|||||||
PlacedObjectTypeSO placedObjectTypeSO;
|
PlacedObjectTypeSO placedObjectTypeSO;
|
||||||
Vector2Int origin;
|
Vector2Int origin;
|
||||||
|
|
||||||
|
public bool isBlueprint = true;
|
||||||
public abstract void OnPlace();
|
public abstract void OnPlace();
|
||||||
|
|
||||||
public List<Vector2Int> GetGridPositionList()
|
public List<Vector2Int> GetGridPositionList()
|
||||||
|
|||||||
@@ -32,4 +32,6 @@ public class PlacedObjectTypeSO : ScriptableObject
|
|||||||
return gridPositionList;
|
return gridPositionList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
9
Assets/Scripts/Grid/PlacedObjectWayTypeSO.cs
Normal file
9
Assets/Scripts/Grid/PlacedObjectWayTypeSO.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
public class PlacedObjectWayTypeSO : PlacedObjectTypeSO
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
11
Assets/Scripts/Grid/PlacedObjectWayTypeSO.cs.meta
Normal file
11
Assets/Scripts/Grid/PlacedObjectWayTypeSO.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 7294d731521cbb34b968ca0855ad1e74
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
@@ -22,11 +22,11 @@ public class Pathfinding {
|
|||||||
return grid;
|
return grid;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Vector3> FindPath(Vector3 startWorldPosition, Vector3 endWorldPosition) {
|
public List<Vector3> FindPath(Vector3 startWorldPosition, Vector3 endWorldPosition, bool ignoreIsWalkable = false) {
|
||||||
grid.GetXY(startWorldPosition, out int startX, out int startY);
|
grid.GetXY(startWorldPosition, out int startX, out int startY);
|
||||||
grid.GetXY(endWorldPosition, out int endX, out int endY);
|
grid.GetXY(endWorldPosition, out int endX, out int endY);
|
||||||
|
|
||||||
List<PathNode> path = FindPath(startX, startY, endX, endY);
|
List<PathNode> path = FindPath(startX, startY, endX, endY, ignoreIsWalkable);
|
||||||
if (path == null) {
|
if (path == null) {
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
@@ -38,7 +38,7 @@ public class Pathfinding {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<PathNode> FindPath(int startX, int startY, int endX, int endY) {
|
public List<PathNode> FindPath(int startX, int startY, int endX, int endY, bool ignoreIsWalkable = false) {
|
||||||
PathNode startNode = grid.GetGridObject(startX, startY);
|
PathNode startNode = grid.GetGridObject(startX, startY);
|
||||||
PathNode endNode = grid.GetGridObject(endX, endY);
|
PathNode endNode = grid.GetGridObject(endX, endY);
|
||||||
|
|
||||||
@@ -75,7 +75,7 @@ public class Pathfinding {
|
|||||||
|
|
||||||
foreach (PathNode neighbourNode in GetNeighbourList(currentNode)) {
|
foreach (PathNode neighbourNode in GetNeighbourList(currentNode)) {
|
||||||
if (closedList.Contains(neighbourNode)) continue;
|
if (closedList.Contains(neighbourNode)) continue;
|
||||||
if (!neighbourNode.isWalkable) {
|
if (!neighbourNode.isWalkable && !ignoreIsWalkable) { // If neighbouring node is not walkable instantly add them to closed
|
||||||
closedList.Add(neighbourNode);
|
closedList.Add(neighbourNode);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user