Advertisement
Guest User

Untitled

a guest
Sep 14th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Shape : MonoBehaviour {
  6.  
  7. public float speed = 1.0f;
  8.  
  9. float LastMoveDown = 0;
  10.  
  11. // Use this for initialization
  12. void Start () {
  13.  
  14. }
  15.  
  16. // Update is called once per frame
  17. void Update () {
  18.  
  19. if (Input.GetKeyDown("a"))
  20. {
  21. transform.position += new Vector3(-1, 0, 0);
  22.  
  23. Debug.Log(transform.position);
  24.  
  25.  
  26. if (!IsInGrid())
  27. {
  28. transform.position += new Vector3(1, 0, 0);
  29. }
  30. else
  31. {
  32. UpdateGameBoard();
  33. }
  34. }
  35.  
  36. if (Input.GetKeyDown("d"))
  37. {
  38. transform.position += new Vector3(1, 0, 0);
  39.  
  40. Debug.Log(transform.position);
  41.  
  42.  
  43. if (!IsInGrid())
  44. {
  45. transform.position += new Vector3(-1, 0, 0);
  46. }
  47. else
  48. {
  49. UpdateGameBoard();
  50. }
  51.  
  52. }
  53.  
  54. if (Input.GetKeyDown("s") || Time.time - LastMoveDown >= 1)
  55. {
  56. transform.position += new Vector3(0, -1, 0);
  57.  
  58. Debug.Log(transform.position);
  59.  
  60. if (!IsInGrid())
  61. {
  62. transform.position += new Vector3(0, 1, 0);
  63.  
  64. enabled = false;
  65.  
  66. FindObjectOfType<ShapeSpawner>().SpawnShape();
  67. }
  68. else
  69. {
  70. UpdateGameBoard();
  71. }
  72.  
  73. LastMoveDown = Time.time;
  74.  
  75. }
  76.  
  77. if (Input.GetKeyDown("w"))
  78. {
  79. transform.Rotate(0, 0, 90);
  80.  
  81. Debug.Log(transform.position);
  82.  
  83. if (!IsInGrid())
  84. {
  85. transform.Rotate(0, 0, -90);
  86. }
  87. else
  88. {
  89. UpdateGameBoard();
  90. }
  91.  
  92. }
  93.  
  94. if (Input.GetKeyDown("e"))
  95. {
  96. transform.Rotate(0, 0, -90);
  97.  
  98. Debug.Log(transform.position);
  99.  
  100. if (!IsInGrid())
  101. {
  102. transform.Rotate(0, 0, 90);
  103. }
  104. else
  105. {
  106. UpdateGameBoard();
  107. }
  108.  
  109. }
  110. }
  111.  
  112. public bool IsInGrid()
  113. {
  114.  
  115.  
  116. foreach(Transform childBlock in transform)
  117. {
  118. Vector2 vect = RoundVector(childBlock.position);
  119. if (!IsInBorder(vect))
  120. {
  121. return false;
  122. }
  123.  
  124. if(GameBoard.gameBoard[(int)vect.x, (int)vect.y] != null &&
  125. GameBoard.gameBoard[(int)vect.x, (int)vect.y].parent != transform)
  126. {
  127. return false;
  128. }
  129.  
  130. }
  131. return true;
  132. }
  133.  
  134. public Vector2 RoundVector(Vector2 vect)
  135. {
  136. return new Vector2(Mathf.Round(vect.x), Mathf.Round(vect.y));
  137. }
  138.  
  139. public static bool IsInBorder(Vector2 pos)
  140. {
  141. return ((int)pos.x >= -6.0 &&
  142. (int)pos.x <= 5.0 &&
  143. (int)pos.y >= -8.0);
  144. }
  145.  
  146. public void UpdateGameBoard()
  147. {
  148. for(int y = 0; y < 17; ++y)
  149. {
  150. for(int x = 0; x < 12; ++x)
  151. {
  152. if(GameBoard.gameBoard[x,y] != null &&
  153. GameBoard.gameBoard[x, y].parent != transform)
  154. {
  155. GameBoard.gameBoard[x, y] = null;
  156. }
  157. }
  158. }
  159.  
  160.  
  161. foreach(Transform childBlock in transform)
  162. {
  163. Vector2 vect = RoundVector (childBlock.position);
  164.  
  165. GameBoard.gameBoard[(int)vect.x, (int)vect.y] = childBlock;
  166.  
  167. Debug.Log("Cube At: " + vect.x + " " + vect.y);
  168.  
  169.  
  170.  
  171.  
  172.  
  173. }
  174.  
  175. GameBoard.PrintArray();
  176.  
  177. }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement