Advertisement
CreedWN

Untitled

Mar 24th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Square : MonoBehaviour {
  6. int moveamount;
  7. int cubesize;
  8. Vector2 movevector;
  9. Vector4 boundries;
  10. void Start(){
  11. moveamount = GameObject.FindWithTag("Grid").GetComponent<GridBG>().moveamount;
  12. boundries = new Vector4(7,-7,7,-7);
  13. movevector = Vector2.up * moveamount;
  14. cubesize = 1;
  15. }
  16. bool DoBoundCheck(GameObject square){
  17. Vector3 movvec = square.transform.position+(this.transform.up*moveamount);
  18. if ((movvec.x <=boundries[0] & movvec.x >=boundries[1] & movvec.y <=boundries[2] & movvec.y >=boundries[3])) {
  19. return true;
  20. } else {
  21. return false;
  22. }
  23. }
  24.  
  25. GameObject GetSquare(GameObject square){
  26. Vector2 movevec = (Vector2)(this.transform.up* moveamount);
  27. Vector2 lb = new Vector2 (square.transform.position.x,square.transform.position.y)+movevec;
  28. Vector2 rt = new Vector2 (square.transform.position.x+cubesize, square.transform.position.y+cubesize)+movevec;
  29.  
  30. Collider2D[] colliderslist = Physics2D.OverlapAreaAll(lb,rt,Physics.DefaultRaycastLayers);
  31. foreach (Collider2D coll in colliderslist){
  32. if (coll != null) { // It could just not collide with anything
  33. if (coll.gameObject.tag != null & coll.gameObject.tag == "Square") {
  34. return coll.gameObject;
  35. }
  36. }
  37. }
  38. return null;
  39. }
  40.  
  41. List<GameObject> GetOnWhat(GameObject square){
  42. Vector2 lb = new Vector2 (square.transform.position.x, square.transform.position.y);
  43. Vector2 rt = new Vector2 (square.transform.position.x + 1, square.transform.position.y + 1);
  44. List<GameObject> objlist = new List<GameObject> ();
  45. Collider2D[] colliderslist = Physics2D.OverlapAreaAll(lb,rt,Physics.DefaultRaycastLayers);
  46. foreach (Collider2D coll in colliderslist){
  47. if (coll != null) { // It could just not collide with anything
  48. if (coll.gameObject.tag != null) {
  49. objlist.Add (coll.gameObject);
  50. }
  51. }
  52. }
  53. return objlist;
  54. }
  55. void DoArrowCheck(GameObject square){
  56. List<GameObject> onwhatlist = GetOnWhat (square);
  57. foreach (GameObject item in onwhatlist) {
  58. if (item.tag == "Arrow") {
  59. square.transform.rotation = item.transform.rotation;
  60. }
  61. }
  62. }
  63. bool DoCircleCheck(GameObject square){
  64. List<GameObject> onwhatlist = GetOnWhat (square);
  65. foreach (GameObject item in onwhatlist) {
  66. if (item.tag == "Circle") {
  67. if (square.GetComponent<SpriteRenderer> ().color == item.GetComponent<SpriteRenderer> ().color) {
  68. return true;
  69. }
  70. }
  71. }
  72. return false;
  73. }
  74. void CheckWin(){
  75. foreach (GameObject square in GameObject.FindGameObjectsWithTag("Square")) {
  76. if (!DoCircleCheck (square)) {
  77. return;
  78. }
  79. }
  80. Debug.Log ("YOU WIN!");
  81. }
  82. bool DoCollisions(){
  83. bool NextSpaceFree = false;
  84. GameObject square = this.gameObject;
  85. List<GameObject> squarelist = new List<GameObject>();
  86. squarelist.Add (square);
  87. while(!NextSpaceFree)
  88. { if (!DoBoundCheck (square)) {
  89. NextSpaceFree = true;
  90. return false;
  91. }else{
  92. square = GetSquare (square);
  93. if (square == null) {
  94. NextSpaceFree = true;
  95. break;
  96. }else{
  97. squarelist.Add (square);
  98. }
  99. }
  100. }
  101. foreach (GameObject item in squarelist) {
  102. Vector3 pos = this.transform.up * moveamount;
  103. pos = new Vector3 (Mathf.Round (pos.x), Mathf.Round (pos.y), Mathf.Round (pos.z));
  104. item.transform.position += pos;
  105. DoArrowCheck (item);
  106. }
  107. return true;
  108. }
  109. void OnMouseDown(){
  110. DoCollisions ();
  111. CheckWin ();
  112. }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement