private void Start()
{
grid = new Grid(100, 100, 10);
}
if (Input.GetMouseButtonDown(0))
{
int width = grid.GetWidth(); <-- grid = null
}
if (Input.GetMouseButtonDown(0))
{
Debug.Log("2");
int width = grid.GetWidth();
Debug.Log(width + " width");
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GridBuildingSystem : MonoBehaviour
{
private Grid grid;
[SerializeField] private Camera cam;
private BuildingSO building;
public BuildingSO[] buildingsSO;
private void Start()
{
building = buildingsSO[0];
}
private void Update()
{
grid = GameManager.grid;
ChooseBuilding();
//if (GameManager.buildMode == true)
//{
//if (Input.touchCount >= 1)
if (Input.GetMouseButtonDown(0))
{
int width = grid.GetWidth();
int height = grid.GetHeight();
//grid.GetGridXZ(GetTouchWorldPosition(), out int x, out int z);
grid.GetGridXZ(GetMouseWorldPosition(), out int x, out int z);
if (x <= width && z <= height && x >= 0 && z >= 0)
{
bool hasBuilding = grid.GetBuilding(x, z, building.width, building.height);
if (hasBuilding == false)
{
Instantiate(building.prefab, grid.GetGridWorldPosition(x, z), Quaternion.identity);
grid.SetBuilding(x, z, building.width, building.height, building);
}
else
{
Debug.Log("You cannot build here!");
}
}
else
{
Debug.Log("You cannot build here!");
}
}
//}
}
private Vector3 GetTouchWorldPosition()
{
Ray ray = cam.ScreenPointToRay(Input.GetTouch(0).position);
if (Physics.Raycast(ray, out RaycastHit hit, 999f))
{
return hit.point;
}
else
{
return Vector3.zero;
}
}
private Vector3 GetMouseWorldPosition()
{
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit hit, 999f))
{
return hit.point;
}
else
{
return Vector3.zero;
}
}
// For PC.
private void ChooseBuilding()
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
building = buildingsSO[0];
}
else if (Input.GetKeyDown(KeyCode.Alpha2))
{
building = buildingsSO[1];
}
else if (Input.GetKeyDown(KeyCode.Alpha3))
{
building = buildingsSO[2];
}
else if (Input.GetKeyDown(KeyCode.Alpha4))
{
building = buildingsSO[3];
}
else if (Input.GetKeyDown(KeyCode.Alpha5))
{
building = buildingsSO[4];
}
else if (Input.GetKeyDown(KeyCode.Alpha6))
{
building = buildingsSO[5];
}
else if (Input.GetKeyDown(KeyCode.Alpha7))
{
building = buildingsSO[6];
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Grid : MonoBehaviour
{
public int width;
public int height;
private int cellSize;
private int hasPlayed;
private BuildingSO building;
private GameObject buildingSystem;
private GridBuildingSystem gridBuildingSystem;
private int[,] gridArray;
public bool[,] fullnessArray { get; private set; }
public string[,] namesArray { get; private set; }
public int[,] xPositionArray { get; private set; }
public int[,] zPositionArray { get; private set; }
public void Awake()
{
hasPlayed = 0;
}
public Grid(int width, int height, int cellSize)
{
this.width = width;
this.height = height;
this.cellSize = cellSize;
gridArray = new int[width, height];
fullnessArray = new bool[width, height];
namesArray = new string[width, height];
xPositionArray = new int[width, height];
zPositionArray = new int[width, height];
buildingSystem = GameObject.FindGameObjectWithTag("GridBuildingSystem");
gridBuildingSystem = buildingSystem.GetComponent<GridBuildingSystem>();
//PlayerPrefs.DeleteAll();
hasPlayed = PlayerPrefs.GetInt("FirstRun");
if (hasPlayed == 1)
{
GridData data = SaveSystem.LoadGrid();
for (int x = 0; x < gridArray.GetLength(0); x++)
{
for (int z = 0; z < gridArray.GetLength(1); z++)
{
fullnessArray[x, z] = data.fullnessArray[x, z];
namesArray[x, z] = data.namesArray[x, z];
xPositionArray[x, z] = data.xPositionArray[x, z];
zPositionArray[x, z] = data.zPositionArray[x, z];
if (namesArray[x, z] != null)
{
CreateBuildingOnLoad(namesArray[x, z], xPositionArray[x, z], zPositionArray[x, z]);
}
}
}
}
else
{
for (int x = 0; x < gridArray.GetLength(0); x++)
{
for (int z = 0; z < gridArray.GetLength(1); z++)
{
fullnessArray[x, z] = false;
namesArray[x, z] = null;
}
}
}
}
private void CreateBuildingOnLoad(string buildingName, int xPosition, int zPosition)
{
if (buildingName == gridBuildingSystem.buildingsSO[0].prefab.name)
{
building = gridBuildingSystem.buildingsSO[0];
CreateBuilding(xPosition, zPosition, building);
}
else if (buildingName == gridBuildingSystem.buildingsSO[1].prefab.name)
{
building = gridBuildingSystem.buildingsSO[1];
CreateBuilding(xPosition, zPosition, building);
}
else if (buildingName == gridBuildingSystem.buildingsSO[2].prefab.name)
{
building = gridBuildingSystem.buildingsSO[2];
CreateBuilding(xPosition, zPosition, building);
}
else if (buildingName == gridBuildingSystem.buildingsSO[3].prefab.name)
{
building = gridBuildingSystem.buildingsSO[3];
CreateBuilding(xPosition, zPosition, building);
}
else if (buildingName == gridBuildingSystem.buildingsSO[4].prefab.name)
{
building = gridBuildingSystem.buildingsSO[4];
CreateBuilding(xPosition, zPosition, building);
}
else if (buildingName == gridBuildingSystem.buildingsSO[5].prefab.name)
{
building = gridBuildingSystem.buildingsSO[5];
CreateBuilding(xPosition, zPosition, building);
}
else if (buildingName == gridBuildingSystem.buildingsSO[6].prefab.name)
{
building = gridBuildingSystem.buildingsSO[6];
CreateBuilding(xPosition, zPosition, building);
}
}
private void CreateBuilding(int xPosition, int zPosition, BuildingSO building)
{
Vector3 position = new Vector3(xPosition, 0, zPosition);
Instantiate(building.prefab, position, Quaternion.identity);
SetBuilding(xPosition / cellSize, zPosition / cellSize, building.width, building.height, building);
}
public Vector3 GetGridWorldPosition(int x, int z)
{
return new Vector3(x, 0, z) * cellSize;
}
public void GetGridXZ(Vector3 worldPosition, out int x, out int z)
{
x = Mathf.FloorToInt(worldPosition.x / cellSize);
z = Mathf.FloorToInt(worldPosition.z / cellSize);
}
public bool GetBuilding(int x, int z, int buildingWidth, int buildingHeight)
{
if (x >= 0 && x <= width && x + buildingWidth - 1 <= width && z >= 0 && z <= height && z + buildingHeight - 1 <= height)
{
int fullGridCells = 0;
for (int ix = 1; ix <= buildingWidth; ix++)
{
for (int iz = 1; iz <= buildingHeight; iz++)
{
if (fullnessArray[x + buildingWidth - ix, z + buildingHeight - iz] == false)
{
}
else
{
fullGridCells++;
}
}
}
if (fullGridCells == 0)
{
return false;
}
else
{
return true;
}
}
else
{
return true;
}
}
public void SetBuilding(int x, int z, int buildingWidth, int buildingHeight, BuildingSO building)
{
for (int ix = 1; ix <= buildingWidth; ix++)
{
for (int iz = 1; iz <= buildingHeight; iz++)
{
fullnessArray[x + buildingWidth - ix, z + buildingHeight - iz] = true;
}
}
namesArray[x, z] = building.prefab.name;
xPositionArray[x, z] = x * cellSize;
zPositionArray[x, z] = z * cellSize;
hasPlayed = 1;
PlayerPrefs.SetInt("FirstRun", hasPlayed);
SaveSystem.SaveGrid(this);
}
public int GetWidth()
{
return width;
}
public int GetHeight()
{
return height;
}
}