Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class GameController : MonoBehaviour {
  7.  
  8. public Button grassPrefab, waterPrefab, roadPrefab, housePrefab;
  9. public Button[,] buttonList = new Button[15,15];
  10. public Canvas canvas;
  11. public Text countText;
  12.  
  13. private int count;
  14.  
  15. private int[,] grid = new int[15, 15]
  16. {
  17. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  18. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  19. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  20. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0 },
  21. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0 },
  22. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 },
  23. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  24. { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  25. { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  26. { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  27. { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  28. { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  29. { 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 },
  30. { 0, 0, 0, 0, 0, 0, 0, 2, 3, 0, 0, 0, 0, 0, 0 },
  31. { 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0 }
  32. };
  33.  
  34. private int selectedItem;
  35. private int money = 0;
  36.  
  37. private void Awake()
  38. {
  39. selectedItem = 2;
  40. DrawBoard();
  41. SetCountText();
  42. }
  43.  
  44. private int GetOccurences(int[,] grid, int x)
  45. {
  46. var result = 0;
  47.  
  48. for(var i = 0; i < grid.GetLength(0); i++)
  49. {
  50. for (var j = 0; j < grid.GetLength(1); j++)
  51. {
  52. if (grid[i, j] == x)
  53. result++;
  54. }
  55. }
  56.  
  57. return result;
  58. }
  59.  
  60. Button GetSelectedPrefab(int cell)
  61. {
  62. Button selectedPrefab = roadPrefab;
  63. if(cell == 0)
  64. {
  65. selectedPrefab = grassPrefab;
  66. } else if(cell == 1)
  67. {
  68. selectedPrefab = waterPrefab;
  69. } else if(cell == 2)
  70. {
  71. selectedPrefab = roadPrefab;
  72. } else if(cell == 3)
  73. {
  74. selectedPrefab = housePrefab;
  75. }
  76.  
  77. return selectedPrefab;
  78. }
  79.  
  80. void DrawBoard ()
  81. {
  82. for(int y = 0; y < 15; y++)
  83. {
  84. for(int x = 0; x < 15; x++)
  85. {
  86. buttonList[y,x] = (Button) GameObject.Instantiate(GetSelectedPrefab(grid[y,x]), new Vector3(x * 36f - 256, y * 36f - 256, 0), Quaternion.identity);
  87. buttonList[y,x].transform.SetParent(canvas.transform, false);
  88. buttonList[y,x].GetComponentInParent<GridSpace>().SetXY(x, y);
  89. buttonList[y,x].GetComponentInParent<GridSpace>().SetGameControllerReference(this);
  90. }
  91. }
  92. }
  93.  
  94. public int GetSelectedItem ()
  95. {
  96. return selectedItem;
  97. }
  98.  
  99. public void SetSelectedItem(int id)
  100. {
  101. selectedItem = id;
  102. }
  103.  
  104. private void SetCountText()
  105. {
  106. countText.text = "Huizen: " + GetOccurences(this.grid, 3);
  107. }
  108.  
  109. public void SpaceSet(int x, int y)
  110. {
  111. if (IsAllowed(x, y, selectedItem, money))
  112. {
  113. grid[y, x] = selectedItem;
  114.  
  115. SetCountText();
  116.  
  117. foreach (Button child in canvas.GetComponents<Button>())
  118. {
  119. Destroy(child);
  120. }
  121.  
  122. DrawBoard();
  123. } else
  124. {
  125. Debug.Log("Not allowed!!");
  126. }
  127.  
  128. }
  129.  
  130. private bool IsAllowed(int x, int y, int selectedItem, int money)
  131. {
  132. // If the selected tile is water, return false
  133. if(grid[y,x] == 1)
  134. {
  135. return false;
  136. }
  137.  
  138. // Trying to place a road or house
  139. if(selectedItem == 2 || selectedItem == 3)
  140. {
  141. // If the selected tile is a road or house, return false
  142. if(grid[y,x] == 2 || grid[y,x] == 3)
  143. {
  144. return false;
  145. } else
  146. {
  147. // If the selected tile connects to another road, return true
  148. try
  149. {
  150. if (grid[y - 1, x] == 2 || grid[y + 1, x] == 2 || grid[y, x - 1] == 2 || grid[y, x + 1] == 2)
  151. {
  152. return true;
  153. }
  154. } catch
  155. {
  156. // Just to avoid array out of bounds exceptions 🤷
  157. }
  158. }
  159. }
  160.  
  161. return false;
  162. }
  163.  
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement