mirror of
https://github.com/DerTyp7/grow-ai-unity.git
synced 2025-10-30 04:47:10 +01:00
Grid
This commit is contained in:
8
Assets/Scripts/Grid.meta
Normal file
8
Assets/Scripts/Grid.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 899c6b51f58f4da42b8110dbab2ea916
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
78
Assets/Scripts/Grid/Grid.cs
Normal file
78
Assets/Scripts/Grid/Grid.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Grid
|
||||
{
|
||||
int width, height;
|
||||
float cellSize;
|
||||
Vector3 originPosition;
|
||||
int[,] gridArray;
|
||||
|
||||
public Grid(int _width, int _height, float _cellSize, Vector3 _originPosition)
|
||||
{
|
||||
width = _width;
|
||||
height = _height;
|
||||
cellSize = _cellSize;
|
||||
originPosition = _originPosition;
|
||||
|
||||
gridArray = new int[width, height];
|
||||
|
||||
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)
|
||||
{
|
||||
return new Vector3(x, y) * cellSize + originPosition;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if(x >= 0 && y >= 0 && x < width && y < height)
|
||||
{
|
||||
gridArray[x, y] = value;
|
||||
Debug.Log(x.ToString() + " " + y.ToString() + " -> " + value.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
public void SetValue(Vector3 worldPosition, int value)
|
||||
{
|
||||
int x, y;
|
||||
GetXY(worldPosition, out x, out y);
|
||||
SetValue(x, y, value);
|
||||
}
|
||||
|
||||
public int GetValue(int x, int y)
|
||||
{
|
||||
if (x >= 0 && y >= 0 && x < width && y < height)
|
||||
{
|
||||
return gridArray[x, y];
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public int GetValue(Vector3 worldPosition)
|
||||
{
|
||||
int x, y;
|
||||
GetXY(worldPosition, out x, out y);
|
||||
return GetValue(x, y);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Grid/Grid.cs.meta
Normal file
11
Assets/Scripts/Grid/Grid.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 410bfb52f88e7434a93933b875d268bb
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
25
Assets/Scripts/Grid/TestGrid.cs
Normal file
25
Assets/Scripts/Grid/TestGrid.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
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)));
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Grid/TestGrid.cs.meta
Normal file
11
Assets/Scripts/Grid/TestGrid.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 29730cb0d1ffeb247be75654297759f2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user