ProjectDDD/Assets/Scirpts/CellManager.cs
2025-06-25 20:20:15 +09:00

68 lines
1.7 KiB (Stored with Git LFS)
C#

using UnityEngine;
public class CellManager : MonoBehaviour
{
public RectTransform cellCanvas;
public RectTransform cell;
// 0 == 비어있음, 1 == 차있음, 2 == 잠김
private const int mapSize = 30;
private int[] tiles;
private RectTransform[] tileUIs;
void Start()
{
tiles = new int [mapSize * mapSize];
// TODO: 데이터 입력
for (int i = 0; i < mapSize * mapSize; i++)
{
tiles[i] = 0;
}
cell.SetParent(null);
cell.gameObject.SetActive(false);
tileUIs = new RectTransform[mapSize * mapSize];
for (int i = 0; i < mapSize * mapSize; i++)
{
var newCell = Instantiate(cell, cellCanvas);
newCell.gameObject.SetActive(true);
tileUIs[i] = newCell.GetComponent<RectTransform>();
}
UpdateCells();
}
Vector3 CellToWorld(Vector2Int cellPos)
{
var worldX = (cellPos.x - cellPos.y) * 0.5f * 1.414f;
var worldZ = -(cellPos.x + cellPos.y) * 0.5f * 1.414f;
return new Vector3(worldX, 0f, worldZ);
}
Vector2Int WorldToCell(Vector3 worldPos)
{
var cellX = Mathf.RoundToInt((worldPos.x - worldPos.z)/ 1.414f);
var cellY = Mathf.RoundToInt((-worldPos.z - worldPos.x) / 1.414f);
return new Vector2Int(cellX, cellY);
}
int GetCellID(Vector2Int cellPos)
{
return cellPos.x + cellPos.y * mapSize;
}
Vector3 GetCellWorldPos(int cellPos)
{
return CellToWorld(new Vector2Int(cellPos / mapSize, cellPos % mapSize));
}
public void UpdateCells()
{
for (int i = 0; i < mapSize * mapSize; i++)
{
tileUIs[i].anchoredPosition3D = GetCellWorldPos(i);
}
}
}