mirror of
https://github.com/DerTyp7/grow-ai-unity.git
synced 2025-10-30 12:57:09 +01:00
a
This commit is contained in:
@@ -8,24 +8,67 @@ public class GridBuildingSystem : MonoBehaviour
|
||||
public static GridBuildingSystem instance;
|
||||
public Grid<GridObject> buildingGrid;
|
||||
|
||||
public int gridWidth;
|
||||
public int gridHeight;
|
||||
public float cellSize;
|
||||
|
||||
PlacedObjectTypeSO selectedPlacedObjectTypeSO;
|
||||
Transform selectedGameObjectTransform;
|
||||
|
||||
void Start()
|
||||
public class GridObject
|
||||
{
|
||||
int x, y;
|
||||
bool isAccessable;
|
||||
Grid<GridObject> grid;
|
||||
PlacedObject placedObject;
|
||||
public GridObject(Grid<GridObject> _grid, int _x, int _y, bool _isAccessable = true) // FOR DEBUG TRUE
|
||||
{
|
||||
grid = _grid;
|
||||
x = _x;
|
||||
y = _y;
|
||||
isAccessable = _isAccessable;
|
||||
}
|
||||
public void SetPlacedObject(PlacedObject _placedObject) => placedObject = _placedObject;
|
||||
public PlacedObject GetPlacedObject() => placedObject;
|
||||
public void ClearPlacedObject() => placedObject = null;
|
||||
public void SetIsAccessable(bool _isAccessable) => isAccessable = _isAccessable;
|
||||
public void SwitchIsAccessable() => isAccessable = !isAccessable;
|
||||
public bool IsAccessable() => isAccessable;
|
||||
public bool CanBuild()
|
||||
{
|
||||
return placedObject == null && isAccessable;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (instance == null)
|
||||
instance = this;
|
||||
|
||||
int gridWidth = GridInfo.instance.gridWidth;
|
||||
int gridHeight = GridInfo.instance.gridHeight;
|
||||
float cellSize = GridInfo.instance.cellSize;
|
||||
|
||||
buildingGrid = new Grid<GridObject>(gridWidth, gridHeight, cellSize, Vector3.zero, (Grid<GridObject> g, int x, int y) => new GridObject(g, x, y));
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if(selectedGameObjectTransform != null)
|
||||
UpdateSelectedGameObject();
|
||||
|
||||
// DEBUG
|
||||
if (Input.GetKeyDown(KeyCode.U))
|
||||
{
|
||||
buildingGrid.GetGridObject(Camera.main.ScreenToWorldPoint(Input.mousePosition)).SwitchIsAccessable();
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown(KeyCode.F))
|
||||
{
|
||||
buildingGrid.GetGridObject(Camera.main.ScreenToWorldPoint(Input.mousePosition)).GetPlacedObject();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void UpdateSelectedGameObject()
|
||||
{
|
||||
if (selectedGameObjectTransform != null)
|
||||
{
|
||||
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||
|
||||
@@ -33,7 +76,7 @@ public class GridBuildingSystem : MonoBehaviour
|
||||
|
||||
selectedGameObjectTransform.position = buildingGrid.GetWorldPosition(x, y);
|
||||
|
||||
List<Vector2Int> gridPositionList = selectedPlacedObjectTypeSO.GetGridPositionList(new Vector2Int(x, y), PlacedObjectTypeSO.Dir.Down);
|
||||
List<Vector2Int> gridPositionList = selectedPlacedObjectTypeSO.GetGridPositionList(new Vector2Int(x, y));
|
||||
if (!CanBuild(gridPositionList))
|
||||
{
|
||||
selectedGameObjectTransform.gameObject.GetComponent<SpriteRenderer>().color = Color.red;
|
||||
@@ -46,34 +89,13 @@ public class GridBuildingSystem : MonoBehaviour
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
{
|
||||
PlaceBuilding();
|
||||
}else if (Input.GetMouseButtonDown(1) || Input.GetKeyDown(KeyCode.Escape))
|
||||
}
|
||||
else if (Input.GetMouseButtonDown(1) || Input.GetKeyDown(KeyCode.Escape))
|
||||
{
|
||||
DeselectBuilding();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class GridObject
|
||||
{
|
||||
int x, y;
|
||||
Grid<GridObject> grid;
|
||||
PlacedObject placedObject;
|
||||
public GridObject(Grid<GridObject> _grid, int _x, int _y)
|
||||
{
|
||||
grid = _grid;
|
||||
x = _x;
|
||||
y = _y;
|
||||
}
|
||||
public void SetPlacedObject(PlacedObject _placedObject) => placedObject = _placedObject;
|
||||
public PlacedObject GetPlacedObject() => placedObject;
|
||||
public void ClearPlacedObject() => placedObject = null;
|
||||
|
||||
public bool CanBuild()
|
||||
{
|
||||
return placedObject == null;
|
||||
}
|
||||
}
|
||||
|
||||
public void DemolishBuilding(Vector3 position)
|
||||
{
|
||||
GridObject gridObject = buildingGrid.GetGridObject(position); // Camera.main.ScreenToWorldPoint(Input.mousePosition)
|
||||
@@ -114,13 +136,13 @@ public class GridBuildingSystem : MonoBehaviour
|
||||
Vector3 mousePosition = new Vector3(position.x, position.y);
|
||||
buildingGrid.GetXY(mousePosition, out int x, out int y);
|
||||
|
||||
List<Vector2Int> gridPositionList = selectedPlacedObjectTypeSO.GetGridPositionList(new Vector2Int(x, y), PlacedObjectTypeSO.Dir.Down);
|
||||
List<Vector2Int> gridPositionList = selectedPlacedObjectTypeSO.GetGridPositionList(new Vector2Int(x, y));
|
||||
|
||||
|
||||
|
||||
if (CanBuild(gridPositionList))
|
||||
{
|
||||
PlacedObject placedObject = PlacedObject.Create(buildingGrid.GetWorldPosition(x, y), new Vector2Int(x, y), PlacedObjectTypeSO.Dir.Down, selectedPlacedObjectTypeSO);
|
||||
PlacedObject placedObject = PlacedObject.Create(buildingGrid.GetWorldPosition(x, y), new Vector2Int(x, y), selectedPlacedObjectTypeSO);
|
||||
|
||||
foreach (Vector2Int gridPosition in gridPositionList)
|
||||
{
|
||||
|
||||
58
Assets/Scripts/Grid/PathfindingSystem.cs
Normal file
58
Assets/Scripts/Grid/PathfindingSystem.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class PathfindingSystem : MonoBehaviour
|
||||
{
|
||||
public static PathfindingSystem instance { get; private set; }
|
||||
public Pathfinding pathfinding;
|
||||
|
||||
|
||||
|
||||
void Start()
|
||||
{
|
||||
instance = this;
|
||||
|
||||
int gridWidth = GridBuildingSystem.instance.gridWidth;
|
||||
int gridHeight = GridBuildingSystem.instance.gridHeight;
|
||||
float cellSize = GridBuildingSystem.instance.cellSize;
|
||||
|
||||
pathfinding = new Pathfinding(gridWidth, gridHeight, cellSize);
|
||||
}
|
||||
|
||||
void Update()
|
||||
{/*
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
{
|
||||
Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||
pathfinding.GetGrid().GetXY(mouseWorldPosition, out int x, out int y);
|
||||
List<PathNode> path = pathfinding.FindPath(originX, originY, x, y);
|
||||
if (path != null)
|
||||
{
|
||||
float cellSize = pathfinding.GetGrid().GetCellSize();
|
||||
for (int i = 0; i < path.Count - 1; i++)
|
||||
{
|
||||
Debug.DrawLine(new Vector3(path[i].x, path[i].y) * cellSize + Vector3.one * cellSize / 2, new Vector3(path[i + 1].x, path[i + 1].y) * cellSize + Vector3.one * cellSize / 2, Color.green, 5f);
|
||||
}
|
||||
}
|
||||
|
||||
//characterPathfinding.SetTargetPosition(mouseWorldPosition);
|
||||
}
|
||||
|
||||
if (Input.GetMouseButtonDown(1))
|
||||
{
|
||||
Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||
pathfinding.GetGrid().GetXY(mouseWorldPosition, out int x, out int y);
|
||||
pathfinding.GetNode(x, y).SetIsWalkable(!pathfinding.GetNode(x, y).isWalkable);
|
||||
originX = x;
|
||||
originY = y;
|
||||
}
|
||||
|
||||
if (Input.GetMouseButtonDown(2))
|
||||
{
|
||||
Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
|
||||
pathfinding.GetGrid().GetXY(mouseWorldPosition, out int x, out int y);
|
||||
pathfinding.GetNode(x, y).SetIsWalkable(!pathfinding.GetNode(x, y).isWalkable);
|
||||
}*/
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Grid/PathfindingSystem.cs.meta
Normal file
11
Assets/Scripts/Grid/PathfindingSystem.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: db134add0ff65f04aa54ed5edf6d6df2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -3,16 +3,17 @@ using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Tilemaps;
|
||||
|
||||
public class PlacedObject : MonoBehaviour
|
||||
public abstract class PlacedObject : MonoBehaviour
|
||||
{
|
||||
public static PlacedObject Create(Vector3 worldPosition, Vector2Int origin, PlacedObjectTypeSO.Dir dir, PlacedObjectTypeSO placedObjectTypeSO)
|
||||
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.dir = dir;
|
||||
|
||||
placedObject.OnPlace();
|
||||
|
||||
if (placedObjectTypeSO.isWalkable)
|
||||
{
|
||||
@@ -28,11 +29,12 @@ public class PlacedObject : MonoBehaviour
|
||||
|
||||
PlacedObjectTypeSO placedObjectTypeSO;
|
||||
Vector2Int origin;
|
||||
PlacedObjectTypeSO.Dir dir;
|
||||
|
||||
public abstract void OnPlace();
|
||||
|
||||
public List<Vector2Int> GetGridPositionList()
|
||||
{
|
||||
return placedObjectTypeSO.GetGridPositionList(origin, PlacedObjectTypeSO.Dir.Down);
|
||||
return placedObjectTypeSO.GetGridPositionList(origin);
|
||||
}
|
||||
|
||||
public void DestroySelf()
|
||||
|
||||
@@ -6,25 +6,6 @@ using UnityEngine;
|
||||
public class PlacedObjectTypeSO : ScriptableObject
|
||||
{
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
public enum Dir
|
||||
{
|
||||
Down,
|
||||
Left,
|
||||
Up,
|
||||
Right,
|
||||
}
|
||||
|
||||
public string nameString;
|
||||
public Transform prefab;
|
||||
//public Transform visual;
|
||||
@@ -33,56 +14,16 @@ public class PlacedObjectTypeSO : ScriptableObject
|
||||
public bool isWalkable = false;
|
||||
public Sprite iconSprite;
|
||||
|
||||
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)
|
||||
public List<Vector2Int> GetGridPositionList(Vector2Int offset)
|
||||
{
|
||||
List<Vector2Int> gridPositionList = new List<Vector2Int>();
|
||||
switch (dir)
|
||||
for (int x = 0; x < width; x++)
|
||||
{
|
||||
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;
|
||||
for (int y = 0; y < height; y++)
|
||||
{
|
||||
gridPositionList.Add(offset + new Vector2Int(x, y));
|
||||
}
|
||||
}
|
||||
return gridPositionList;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user