Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class GameController : MonoBehaviour {
- public Button grassPrefab, waterPrefab, roadPrefab, housePrefab;
- public Button[,] buttonList = new Button[15,15];
- public Canvas canvas;
- public Text countText;
- private int count;
- private int[,] grid = new int[15, 15]
- {
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 2, 3, 0, 0, 0, 0, 0, 0 },
- { 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 }
- };
- private int selectedItem;
- private int money = 0;
- private void Awake()
- {
- selectedItem = 2;
- DrawBoard();
- SetCountText();
- }
- private int GetOccurences(int[,] grid, int x)
- {
- var result = 0;
- for(var i = 0; i < grid.GetLength(0); i++)
- {
- for (var j = 0; j < grid.GetLength(1); j++)
- {
- if (grid[i, j] == x)
- result++;
- }
- }
- return result;
- }
- Button GetSelectedPrefab(int cell)
- {
- Button selectedPrefab = roadPrefab;
- if(cell == 0)
- {
- selectedPrefab = grassPrefab;
- } else if(cell == 1)
- {
- selectedPrefab = waterPrefab;
- } else if(cell == 2)
- {
- selectedPrefab = roadPrefab;
- } else if(cell == 3)
- {
- selectedPrefab = housePrefab;
- }
- return selectedPrefab;
- }
- void DrawBoard ()
- {
- for(int y = 0; y < 15; y++)
- {
- for(int x = 0; x < 15; x++)
- {
- buttonList[y,x] = (Button) GameObject.Instantiate(GetSelectedPrefab(grid[y,x]), new Vector3(x * 36f - 256, y * 36f - 256, 0), Quaternion.identity);
- buttonList[y,x].transform.SetParent(canvas.transform, false);
- buttonList[y,x].GetComponentInParent<GridSpace>().SetXY(x, y);
- buttonList[y,x].GetComponentInParent<GridSpace>().SetGameControllerReference(this);
- }
- }
- }
- public int GetSelectedItem ()
- {
- return selectedItem;
- }
- public void SetSelectedItem(int id)
- {
- selectedItem = id;
- }
- private void SetCountText()
- {
- countText.text = "Huizen: " + GetOccurences(this.grid, 3);
- }
- public void SpaceSet(int x, int y)
- {
- if (IsAllowed(x, y, selectedItem, money))
- {
- grid[y, x] = selectedItem;
- SetCountText();
- foreach (Button child in canvas.GetComponents<Button>())
- {
- Destroy(child);
- }
- DrawBoard();
- } else
- {
- Debug.Log("Not allowed!!");
- }
- }
- private bool IsAllowed(int x, int y, int selectedItem, int money)
- {
- // If the selected tile is water, return false
- if(grid[y,x] == 1)
- {
- return false;
- }
- // Trying to place a road or house
- if(selectedItem == 2 || selectedItem == 3)
- {
- // If the selected tile is a road or house, return false
- if(grid[y,x] == 2 || grid[y,x] == 3)
- {
- return false;
- } else
- {
- // If the selected tile connects to another road, return true
- try
- {
- if (grid[y - 1, x] == 2 || grid[y + 1, x] == 2 || grid[y, x - 1] == 2 || grid[y, x + 1] == 2)
- {
- return true;
- }
- } catch
- {
- // Just to avoid array out of bounds exceptions 🤷
- }
- }
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement