mirror of
https://github.com/DerTyp7/grow-ai-unity.git
synced 2025-10-30 04:47:10 +01:00
started work on place obj
This commit is contained in:
@@ -1,47 +1,66 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Grid
|
||||
public class Grid<TGridObject>
|
||||
{
|
||||
int width, height;
|
||||
float cellSize;
|
||||
Vector3 originPosition;
|
||||
int[,] gridArray;
|
||||
TGridObject[,] gridArray;
|
||||
|
||||
public Grid(int _width, int _height, float _cellSize, Vector3 _originPosition)
|
||||
bool showDebug = true;
|
||||
|
||||
public int GetWidth() => width;
|
||||
public int GetHeight() => height;
|
||||
public float GetCellSize() => cellSize;
|
||||
|
||||
public Grid(int _width, int _height, float _cellSize, Vector3 _originPosition, Func<Grid<TGridObject>, int, int, TGridObject> createGridObject)
|
||||
{
|
||||
width = _width;
|
||||
height = _height;
|
||||
cellSize = _cellSize;
|
||||
originPosition = _originPosition;
|
||||
|
||||
gridArray = new int[width, height];
|
||||
gridArray = new TGridObject[width, height];
|
||||
|
||||
for(int x = 0; x < gridArray.GetLength(0); x++)
|
||||
for (int x = 0; x < gridArray.GetLength(0); x++)
|
||||
{
|
||||
for (int y = 0; y < gridArray.GetLength(1); y++)
|
||||
{
|
||||
Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x, y+1), Color.white, 100f);
|
||||
Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x+1,y), Color.white, 100f);
|
||||
gridArray[x, y] = createGridObject(this, x, y);
|
||||
}
|
||||
}
|
||||
Debug.DrawLine(GetWorldPosition(0, height), GetWorldPosition(width, height), Color.white, 100f);
|
||||
Debug.DrawLine(GetWorldPosition(width, 0), GetWorldPosition(width, height), Color.white, 100f);
|
||||
|
||||
if (showDebug)
|
||||
{
|
||||
for (int x = 0; x < gridArray.GetLength(0); x++)
|
||||
{
|
||||
for (int y = 0; y < gridArray.GetLength(1); y++)
|
||||
{
|
||||
Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x, y + 1), Color.white, 100f);
|
||||
Debug.DrawLine(GetWorldPosition(x, y), GetWorldPosition(x + 1, y), Color.white, 100f);
|
||||
}
|
||||
}
|
||||
Debug.DrawLine(GetWorldPosition(0, height), GetWorldPosition(width, height), Color.white, 100f);
|
||||
Debug.DrawLine(GetWorldPosition(width, 0), GetWorldPosition(width, height), Color.white, 100f);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Vector3 GetWorldPosition(int x, int y)
|
||||
public Vector3 GetWorldPosition(int x, int y)
|
||||
{
|
||||
return new Vector3(x, y) * cellSize + originPosition;
|
||||
}
|
||||
|
||||
void GetXY(Vector3 worldPosition, out int x, out int y)
|
||||
public void GetXY(Vector3 worldPosition, out int x, out int y)
|
||||
{
|
||||
x = Mathf.FloorToInt((worldPosition - originPosition).x / cellSize);
|
||||
y = Mathf.FloorToInt((worldPosition - originPosition).y / cellSize);
|
||||
}
|
||||
|
||||
public void SetValue(int x, int y, int value)
|
||||
public void SetGridObject(int x, int y, TGridObject value)
|
||||
{
|
||||
if(x >= 0 && y >= 0 && x < width && y < height)
|
||||
{
|
||||
@@ -50,14 +69,14 @@ public class Grid
|
||||
}
|
||||
}
|
||||
|
||||
public void SetValue(Vector3 worldPosition, int value)
|
||||
public void SetGridObject(Vector3 worldPosition, TGridObject value)
|
||||
{
|
||||
int x, y;
|
||||
GetXY(worldPosition, out x, out y);
|
||||
SetValue(x, y, value);
|
||||
SetGridObject(x, y, value);
|
||||
}
|
||||
|
||||
public int GetValue(int x, int y)
|
||||
public TGridObject GetGridObject(int x, int y)
|
||||
{
|
||||
if (x >= 0 && y >= 0 && x < width && y < height)
|
||||
{
|
||||
@@ -65,14 +84,14 @@ public class Grid
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
return default(TGridObject);
|
||||
}
|
||||
}
|
||||
|
||||
public int GetValue(Vector3 worldPosition)
|
||||
public TGridObject GetGridObject(Vector3 worldPosition)
|
||||
{
|
||||
int x, y;
|
||||
GetXY(worldPosition, out x, out y);
|
||||
return GetValue(x, y);
|
||||
return GetGridObject(x, y);
|
||||
}
|
||||
}
|
||||
|
||||
57
Assets/Scripts/Grid/GridBuildingSystem.cs
Normal file
57
Assets/Scripts/Grid/GridBuildingSystem.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class GridBuildingSystem : MonoBehaviour
|
||||
{
|
||||
[SerializeField] Transform testTransform;
|
||||
|
||||
Grid<GridObject> grid;
|
||||
void Awake()
|
||||
{
|
||||
int gridWidth = 20;
|
||||
int gridHeight = 20;
|
||||
float cellSize = 2f;
|
||||
|
||||
grid = new Grid<GridObject>(gridWidth, gridHeight, cellSize, Vector3.zero, (Grid<GridObject> g, int x, int y) => new GridObject(g, x, y));
|
||||
}
|
||||
public class GridObject
|
||||
{
|
||||
int x, y;
|
||||
Grid<GridObject> grid;
|
||||
Transform transform;
|
||||
public GridObject(Grid<GridObject> _grid, int _x, int _y)
|
||||
{
|
||||
grid = _grid;
|
||||
x = _x;
|
||||
y = _y;
|
||||
}
|
||||
public void SetTransform(Transform _transform) => transform = _transform;
|
||||
public Transform GetTransform() => transform;
|
||||
public void ClearTransform() => transform = null;
|
||||
|
||||
public bool CanBuild()
|
||||
{
|
||||
return transform == null;
|
||||
}
|
||||
}
|
||||
void Update()
|
||||
{
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
{
|
||||
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);
|
||||
|
||||
GridObject gridObject = grid.GetGridObject(x, y);
|
||||
if (gridObject.CanBuild())
|
||||
{
|
||||
Transform builtTransform = Instantiate(testTransform, grid.GetWorldPosition(x, y), Quaternion.identity);
|
||||
gridObject.SetTransform(builtTransform);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("Cannot build here!" + " " + mousePosition);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 29730cb0d1ffeb247be75654297759f2
|
||||
guid: a9e8b2a885476754e9652c90137f91bc
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
18
Assets/Scripts/Grid/PlacedObjectTypeSO.cs
Normal file
18
Assets/Scripts/Grid/PlacedObjectTypeSO.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class PlacedObjectTypeSO : MonoBehaviour
|
||||
{
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Grid/PlacedObjectTypeSO.cs.meta
Normal file
11
Assets/Scripts/Grid/PlacedObjectTypeSO.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ed4c766e9f0dd454da548c672fa99b65
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -1,25 +0,0 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class TestGrid : MonoBehaviour
|
||||
{
|
||||
Grid grid;
|
||||
void Start()
|
||||
{
|
||||
grid = new Grid(100, 100, 1, new Vector3(0, 0));
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (Input.GetMouseButtonDown(0))
|
||||
{
|
||||
grid.SetValue(Camera.main.ScreenToWorldPoint(Input.mousePosition), 44);
|
||||
}
|
||||
|
||||
if (Input.GetMouseButtonDown(1))
|
||||
{
|
||||
Debug.Log(grid.GetValue(Camera.main.ScreenToWorldPoint(Input.mousePosition)));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user