mirror of
https://github.com/DerTyp7/grow-ai-unity.git
synced 2025-10-29 12:32:10 +01:00
a
This commit is contained in:
@@ -15,6 +15,11 @@ public class GridBuildingSystem : MonoBehaviour
|
||||
PlacedObjectTypeSO selectedPlacedObjectTypeSO;
|
||||
Transform selectedGameObjectTransform;
|
||||
|
||||
Vector3 selectedWayStart;
|
||||
|
||||
public List<GameObject> blueprintWayList = new List<GameObject>();
|
||||
bool isPlacingWay = false;
|
||||
|
||||
public class GridObject
|
||||
{
|
||||
int x, y;
|
||||
@@ -51,63 +56,109 @@ public class GridBuildingSystem : MonoBehaviour
|
||||
|
||||
void Update()
|
||||
{
|
||||
UpdateSelectedGameObject();
|
||||
|
||||
// DEBUG
|
||||
if (Input.GetKeyDown(KeyCode.U))
|
||||
if (selectedGameObjectTransform != null)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
foreach(GameObject blueprint in blueprintWayList)
|
||||
{
|
||||
if (!dontDeleteBlueprintList.Contains(blueprint))
|
||||
{
|
||||
Destroy(blueprint);
|
||||
}
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.F))
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
{
|
||||
buildingGrid.GetGridObject(Camera.main.ScreenToWorldPoint(Input.mousePosition)).GetPlacedObject();
|
||||
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()
|
||||
{
|
||||
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);
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
selectedGameObjectTransform.gameObject.GetComponent<SpriteRenderer>().color = Color.red;
|
||||
}
|
||||
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)
|
||||
{
|
||||
GridObject gridObject = buildingGrid.GetGridObject(position); // Camera.main.ScreenToWorldPoint(Input.mousePosition)
|
||||
@@ -141,12 +192,11 @@ public class GridBuildingSystem : MonoBehaviour
|
||||
return canBuild;
|
||||
}
|
||||
|
||||
public void PlaceBuilding()
|
||||
public GameObject PlaceBuilding(Vector3 position)
|
||||
{
|
||||
Vector3 position = selectedGameObjectTransform.position;
|
||||
|
||||
Vector3 mousePosition = new Vector3(position.x, position.y);
|
||||
buildingGrid.GetXY(mousePosition, out int x, out int y);
|
||||
Debug.Log("HALLO");
|
||||
position = new Vector3(position.x, position.y);
|
||||
buildingGrid.GetXY(position, out int x, out int 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);
|
||||
}
|
||||
|
||||
return placedObject.gameObject;
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("Cannot build here!" + " " + mousePosition);
|
||||
Debug.Log("Cannot build here!" + " " + position);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void SelectBuilding(PlacedObjectTypeSO placedObjectTypeSO)
|
||||
@@ -180,6 +231,7 @@ public class GridBuildingSystem : MonoBehaviour
|
||||
Destroy(selectedGameObjectTransform.gameObject);
|
||||
selectedPlacedObjectTypeSO = null;
|
||||
selectedGameObjectTransform = null;
|
||||
isPlacingWay = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ public abstract class PlacedObject : MonoBehaviour
|
||||
placedObject.origin = origin;
|
||||
|
||||
placedObject.OnPlace();
|
||||
placedObject.isBlueprint = false;
|
||||
|
||||
if (placedObjectTypeSO.isWalkable)
|
||||
{
|
||||
@@ -30,6 +31,7 @@ public abstract class PlacedObject : MonoBehaviour
|
||||
PlacedObjectTypeSO placedObjectTypeSO;
|
||||
Vector2Int origin;
|
||||
|
||||
public bool isBlueprint = true;
|
||||
public abstract void OnPlace();
|
||||
|
||||
public List<Vector2Int> GetGridPositionList()
|
||||
|
||||
@@ -32,4 +32,6 @@ public class PlacedObjectTypeSO : ScriptableObject
|
||||
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;
|
||||
}
|
||||
|
||||
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(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) {
|
||||
return null;
|
||||
} 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 endNode = grid.GetGridObject(endX, endY);
|
||||
|
||||
@@ -75,7 +75,7 @@ public class Pathfinding {
|
||||
|
||||
foreach (PathNode neighbourNode in GetNeighbourList(currentNode)) {
|
||||
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);
|
||||
continue;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user