Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.46 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3.  
  4. public class GameBoard : MonoBehaviour
  5. {
  6. const ushort CELL_COUNT_X = 10;
  7. const ushort CELL_COUNT_Y = 24; // only 20 will be visible to player, but we will use 24
  8. const ushort CELL_SIZE = 1;
  9.  
  10. GameObject brick;
  11. List<GameObject> bricks = new List<GameObject>();
  12. private Sprite brickSprite;
  13. private Sprite wallSprite;
  14. GameObject[] debugWallBricks = new GameObject[CELL_COUNT_X * CELL_COUNT_Y];
  15. TetrisPiece fallingPiece;
  16. const ushort BLOCKTYPE_COUNT = 7;
  17.  
  18. const short DEFAULT_POSITION_X = CELL_COUNT_X / 2 - 2;
  19. const short DEFAULT_POSITION_Y = CELL_COUNT_Y - 4;
  20.  
  21. //bool[,] frozenCells = new bool[CELL_COUNT_X, CELL_COUNT_Y]; // ill use this to store record of all fixed/settled blocks no longer falling
  22. FrozenPiece[,] frozenCells = new FrozenPiece[CELL_COUNT_X, CELL_COUNT_Y];
  23. List<GameObject> frozenCellBricks = new List<GameObject>();
  24.  
  25. float dropTimer = 0f;
  26. float dropTimerBaseSpeed = 1f;
  27. float dropTimerSuperSpeed = 15f;
  28. float dropDelay = 0.7f;
  29. float moveTimer = 0f;
  30. float moveToDropSpeedFactor = 5f;
  31. float moveDelay; // this value depends on what value the dropDelay is and is set in Update
  32. float rotTimer = 0f;
  33. float rotDelay = 0.15f;
  34.  
  35.  
  36. private void Start()
  37. {
  38. brickSprite = Resources.Load<Sprite>("Tex/tetris_brick");
  39. wallSprite = Resources.Load<Sprite>("Tex/border_rounded");
  40.  
  41. CreateDebugWall();
  42. MakeNewFallingPiece();
  43.  
  44. for (int y = 0; y < frozenCells.GetLength(1); y++)
  45. {
  46. for (int x = 0; x < frozenCells.GetLength(0); x++)
  47. {
  48. FrozenPiece f = new FrozenPiece(Color.grey, false);
  49. f.isFilled = false;
  50. frozenCells[x,y] = f;
  51. }
  52. }
  53. }
  54.  
  55. // i clear the visual board every frame and re-create a new brick, spriterenderer etc every frame (probably a shit way !) todo
  56. void ClearBoard()
  57. {
  58. foreach (GameObject go in bricks)
  59. Destroy(go);
  60. foreach (GameObject go in frozenCellBricks)
  61. Destroy(go);
  62.  
  63. }
  64.  
  65. void CreateDebugWall()
  66. {
  67. int i = 0;
  68. for (int x = 0; x < CELL_COUNT_X; x++)
  69. {
  70. for (int y = 0; y < CELL_COUNT_Y; y++)
  71. {
  72. brick = new GameObject();
  73. brick.name = "Debug_Wall_Brick";
  74. brick.AddComponent<SpriteRenderer>();
  75. brick.GetComponent<SpriteRenderer>().sprite = wallSprite;
  76. brick.transform.position = new Vector2(x * CELL_SIZE, y * CELL_SIZE);
  77. brick.GetComponent<SpriteRenderer>().color = Color.grey;
  78. debugWallBricks[i] = brick;
  79.  
  80. }
  81. }
  82. }
  83.  
  84. void MakeNewFallingPiece()
  85. {
  86. TetrisPiece.BlockType blockType = (TetrisPiece.BlockType)Random.Range(0, BLOCKTYPE_COUNT);
  87. dropTimer = 0f;
  88. fallingPiece = new TetrisPiece(blockType, DEFAULT_POSITION_X, DEFAULT_POSITION_Y);
  89. DrawFallingPiece();
  90. }
  91.  
  92. void DropDown()
  93. {
  94. // check cell directly below a block:
  95. for (int y = 0; y < fallingPiece.GetCurrentOrientation().orientations.GetLength(0); y++)
  96. {
  97. for (int x = 0; x < fallingPiece.GetCurrentOrientation().orientations.GetLength(0); x++)
  98. {
  99. if (fallingPiece.GetCurrentOrientation().orientations[y, x] == 1)
  100. {
  101. if (fallingPiece.pos_y + y == 0)
  102. {
  103. Debug.Log("HIT FLOOR!");
  104. ConvertFallingPieceToFrozenBricks();
  105. return;
  106. }
  107. if (frozenCells[fallingPiece.pos_x + x, fallingPiece.pos_y + y - 1].isFilled == true)
  108. {
  109. Debug.Log("HIT FROZEN CELLS!");
  110. ConvertFallingPieceToFrozenBricks();
  111. return;
  112. }
  113. }
  114. }
  115. }
  116. fallingPiece.pos_y--;
  117. }
  118.  
  119. void MoveLeft()
  120. {
  121. // check cell directly to left of a block:
  122. for (int y = 0; y < fallingPiece.GetCurrentOrientation().orientations.GetLength(0); y++)
  123. {
  124. for (int x = 0; x < fallingPiece.GetCurrentOrientation().orientations.GetLength(0); x++)
  125. {
  126. if (fallingPiece.GetCurrentOrientation().orientations[y, x] == 1)
  127. {
  128. if (fallingPiece.pos_x + x == 0)
  129. {
  130. Debug.Log("hit side wall");
  131. return;
  132. }
  133. if(frozenCells[fallingPiece.pos_x - 1 + x, fallingPiece.pos_y + y].isFilled == true)
  134. {
  135. Debug.Log("tried moving left but there was a frozen brick");
  136. return;
  137. }
  138. }
  139. }
  140. }
  141. fallingPiece.pos_x--;
  142. }
  143.  
  144. void MoveRight()
  145. {
  146. // check cell directly to left of a block:
  147. for (int y = 0; y < fallingPiece.GetCurrentOrientation().orientations.GetLength(0); y++)
  148. {
  149. for (int x = 0; x < fallingPiece.GetCurrentOrientation().orientations.GetLength(0); x++)
  150. {
  151. if (fallingPiece.GetCurrentOrientation().orientations[y, x] == 1)
  152. {
  153. if (fallingPiece.pos_x + x == CELL_COUNT_X - 1)
  154. {
  155. Debug.Log("hit side wall");
  156. return;
  157. }
  158. if (frozenCells[fallingPiece.pos_x + 1 + x, fallingPiece.pos_y + y].isFilled == true)
  159. {
  160. Debug.Log("tried moving right but there was a frozen brick");
  161. return;
  162. }
  163. }
  164. }
  165. }
  166. fallingPiece.pos_x++;
  167. }
  168.  
  169. void RotatePositive()
  170. {
  171. for (int y = 0; y < fallingPiece.GetCurrentOrientation().orientations.GetLength(0); y++)
  172. {
  173. for (int x = 0; x < fallingPiece.GetCurrentOrientation().orientations.GetLength(0); x++)
  174. {
  175. if (fallingPiece.GetNextOrientation().orientations[y, x] == 1)
  176. {
  177. if (fallingPiece.pos_x + x < 0)
  178. {
  179. Debug.Log("tried rotation but hit a wall");
  180. return;
  181. }
  182. if (fallingPiece.pos_x + x >= CELL_COUNT_X)
  183. {
  184. Debug.Log("tried rotation but hit a wall");
  185. return;
  186. }
  187. if (fallingPiece.pos_y + y == 0)
  188. {
  189. Debug.Log("tried rotation but hit floor");
  190. return;
  191. }
  192. }
  193. }
  194. }
  195. fallingPiece.GotoNextOrientation();
  196. }
  197.  
  198. void ConvertFallingPieceToFrozenBricks()
  199. {
  200. for (int y = 0; y < fallingPiece.GetCurrentOrientation().orientations.GetLength(0); y++)
  201. {
  202. for (int x = 0; x < fallingPiece.GetCurrentOrientation().orientations.GetLength(0); x++)
  203. {
  204. if (fallingPiece.GetCurrentOrientation().orientations[y,x] == 1)
  205. frozenCells[fallingPiece.pos_x + x, fallingPiece.pos_y + y] = new FrozenPiece(fallingPiece.color, true);
  206. }
  207. }
  208. fallingPiece = null;
  209. }
  210.  
  211. void DrawFallingPiece()
  212. {
  213. if (fallingPiece == null)
  214. return;
  215.  
  216. for (int y = 0; y < fallingPiece.GetCurrentOrientation().orientations.GetLength(0); y++)
  217. {
  218. for (int x = 0; x < fallingPiece.GetCurrentOrientation().orientations.GetLength(0); x++)
  219. {
  220. if (fallingPiece.GetCurrentOrientation().orientations[y, x] == 1)
  221. {
  222. brick = new GameObject();
  223. brick.name = "TetrisPiece_" + fallingPiece.blockType;
  224. brick.AddComponent<SpriteRenderer>();
  225. brick.GetComponent<SpriteRenderer>().sprite = brickSprite;
  226. brick.transform.position = new Vector2((fallingPiece.pos_x + x) * CELL_SIZE, (fallingPiece.pos_y + y) * CELL_SIZE);
  227. brick.GetComponent<SpriteRenderer>().color = fallingPiece.color;
  228. bricks.Add(brick);
  229. }
  230. }
  231. }
  232. }
  233.  
  234. void DrawFrozenBricks()
  235. {
  236. for (int x = 0; x < frozenCells.GetLength(0); x++)
  237. {
  238. for (int y = 0; y < frozenCells.GetLength(1); y++)
  239. {
  240. if (frozenCells[x, y].isFilled == true)
  241. {
  242. brick = new GameObject();
  243. brick.name = "Frozen Cell Brick";
  244. brick.AddComponent<SpriteRenderer>();
  245. brick.GetComponent<SpriteRenderer>().sprite = brickSprite;
  246. brick.transform.position = new Vector2(x * CELL_SIZE, y * CELL_SIZE);
  247. brick.GetComponent<SpriteRenderer>().color = frozenCells[x,y].color;
  248. frozenCellBricks.Add(brick);
  249. }
  250. }
  251. }
  252. }
  253.  
  254. void HandleMovementAndBlockCreation()
  255. {
  256. float input_x = Input.GetAxis("Horizontal");
  257. float input_y = Input.GetAxis("Vertical");
  258. bool rotateButton = Input.GetButtonDown("Rotate_Positive");
  259. moveDelay = dropDelay / moveToDropSpeedFactor;
  260.  
  261. moveTimer += Time.deltaTime;
  262. rotTimer += Time.deltaTime;
  263.  
  264. if (fallingPiece != null)
  265. {
  266. if (input_y < 0)
  267. dropTimer += dropTimerSuperSpeed * Time.deltaTime;
  268. else
  269. dropTimer += dropTimerBaseSpeed * Time.deltaTime;
  270.  
  271.  
  272. if (dropTimer >= dropDelay)
  273. {
  274. DropDown();
  275. dropTimer = 0f;
  276. }
  277. else
  278. {
  279. if (moveTimer >= moveDelay)
  280. {
  281. moveTimer = 0f;
  282.  
  283. if (input_x < 0)
  284. MoveLeft();
  285. else if (input_x > 0)
  286. MoveRight();
  287. }
  288. if (rotateButton)
  289. RotatePositive();
  290. }
  291. }
  292. else
  293. {
  294. MakeNewFallingPiece();
  295. }
  296. }
  297.  
  298. private void Update()
  299. {
  300. ClearBoard();
  301.  
  302. HandleMovementAndBlockCreation();
  303.  
  304.  
  305.  
  306. DrawFallingPiece();
  307. DrawFrozenBricks();
  308.  
  309. }
  310. }
  311.  
  312. public class TetrisPiece
  313. {
  314. public enum BlockType { A, B, C, D, E, F, G };
  315. public BlockType blockType;
  316. private Orientation[] possibleOrientations;
  317. private int currentOrientationIndex = 0;
  318. public short pos_x, pos_y;
  319. public Color color;
  320.  
  321. public TetrisPiece(BlockType blockType, short PosX, short PosY)
  322. {
  323. pos_x = PosX;
  324. pos_y = PosY;
  325.  
  326. this.blockType = blockType;
  327. switch (blockType)
  328. {
  329. case BlockType.A:
  330. color = Color.red;
  331. possibleOrientations = new Orientation[2];
  332. possibleOrientations[0] = new Orientation(new int[4, 4] { { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 } });
  333. possibleOrientations[1] = new Orientation(new int[4, 4] { { 0, 0, 0, 0 }, { 1, 1, 1, 1 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } });
  334. break;
  335. case BlockType.B:
  336. color = Color.blue;
  337. possibleOrientations = new Orientation[1];
  338. possibleOrientations[0] = new Orientation(new int[4, 4] { { 0, 0, 0, 0 }, { 0, 1, 1, 0 }, { 0, 1, 1, 0 }, { 0, 0, 0, 0 } });
  339. break;
  340. case BlockType.C:
  341. color = Color.green;
  342. possibleOrientations = new Orientation[4];
  343. possibleOrientations[0] = new Orientation(new int[4, 4] { { 0, 0, 0, 0 }, { 0, 1, 0, 0 }, { 1, 1, 1, 0 }, { 0, 0, 0, 0 } });
  344. possibleOrientations[1] = new Orientation(new int[4, 4] { { 0, 0, 0, 0 }, { 0, 1, 0, 0 }, { 1, 1, 0, 0 }, { 0, 1, 0, 0 } });
  345. possibleOrientations[2] = new Orientation(new int[4, 4] { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 1, 1, 1, 0 }, { 0, 1, 0, 0 } });
  346. possibleOrientations[3] = new Orientation(new int[4, 4] { { 0, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 1, 0 }, { 0, 1, 0, 0 } });
  347. break;
  348. case BlockType.D:
  349. color = Color.cyan;
  350. possibleOrientations = new Orientation[2];
  351. possibleOrientations[0] = new Orientation(new int[4, 4] { { 0, 0, 0, 0 }, { 0, 0, 1, 0 }, { 0, 1, 1, 0 }, { 0, 1, 0, 0 } });
  352. possibleOrientations[1] = new Orientation(new int[4, 4] { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 1, 1, 0, 0 }, { 0, 1, 1, 0 } });
  353. break;
  354. case BlockType.E:
  355. color = ColorTools.Color255(128f, 41f, 11f); // brown
  356. possibleOrientations = new Orientation[2];
  357. possibleOrientations[0] = new Orientation(new int[4, 4] { { 0, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 1, 0 }, { 0, 0, 1, 0 } });
  358. possibleOrientations[1] = new Orientation(new int[4, 4] { { 0, 0, 0, 0 }, { 0, 0, 1, 1 }, { 0, 1, 1, 0 }, { 0, 0, 0, 0 } });
  359. break;
  360. case BlockType.F:
  361. color = ColorTools.Color255(255f, 255f, 0f); // yellow
  362. possibleOrientations = new Orientation[4];
  363. possibleOrientations[0] = new Orientation(new int[4, 4] { { 0, 0, 0, 0 }, { 0, 0, 1, 0 }, { 0, 0, 1, 0 }, { 0, 1, 1, 0 } });
  364. possibleOrientations[1] = new Orientation(new int[4, 4] { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 1, 1, 1 }, { 0, 0, 0, 1 } });
  365. possibleOrientations[2] = new Orientation(new int[4, 4] { { 0, 0, 0, 0 }, { 0, 1, 1, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 } });
  366. possibleOrientations[3] = new Orientation(new int[4, 4] { { 0, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 1, 1}, { 0, 0, 0, 0 } });
  367. break;
  368. case BlockType.G:
  369. color = ColorTools.Color255(128f, 0f, 128f); // purple
  370. possibleOrientations = new Orientation[4];
  371. possibleOrientations[0] = new Orientation(new int[4, 4] { { 0, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 1, 0 } });
  372. possibleOrientations[1] = new Orientation(new int[4, 4] { { 0, 0, 0, 0 }, { 0, 0, 1, 0 }, { 1, 1, 1, 0 }, { 0, 0, 0, 0 } });
  373. possibleOrientations[2] = new Orientation(new int[4, 4] { { 0, 0, 0, 0 }, { 1, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 } });
  374. possibleOrientations[3] = new Orientation(new int[4, 4] { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 1, 1, 1, 0 }, { 1, 0, 0, 0 } });
  375. break;
  376. }
  377. }
  378.  
  379.  
  380. public Orientation GetCurrentOrientation()
  381. {
  382. return possibleOrientations[currentOrientationIndex];
  383. }
  384.  
  385. public Orientation GetNextOrientation()
  386. {
  387. Debug.Log("index = " + currentOrientationIndex);
  388. if (possibleOrientations.Length == 1)
  389. return possibleOrientations[0];
  390. if (currentOrientationIndex == possibleOrientations.Length - 1)
  391. return possibleOrientations[0];
  392. else
  393. return possibleOrientations[currentOrientationIndex + 1];
  394. }
  395.  
  396. public void GotoNextOrientation()
  397. {
  398. currentOrientationIndex++;
  399. if (currentOrientationIndex >= possibleOrientations.Length)
  400. currentOrientationIndex = 0;
  401. }
  402. }
  403.  
  404. public struct Orientation
  405. {
  406. public int[,] orientations;
  407.  
  408. public Orientation(int[,] Orientations)
  409. {
  410. orientations = Orientations;
  411. }
  412. }
  413.  
  414. public class FrozenPiece
  415. {
  416. public Color color;
  417. public bool isFilled = false;
  418.  
  419. public FrozenPiece(Color Color, bool IsFilled)
  420. {
  421. color = Color;
  422. isFilled = IsFilled;
  423. }
  424. }
  425.  
  426. public static class ColorTools
  427. {
  428. public static Color Color255(float r, float g, float b)
  429. {
  430. return new Color(r / 255f, g / 255f, b / 255f);
  431. }
  432. public static Color Color255_Alpha(float r, float g, float b, float a)
  433. {
  434. return new Color(r / 255f, g / 255f, b / 255f, a / 255f);
  435. }
  436. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement