Advertisement
Korvix

Ball.cs

Jul 15th, 2020
897
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.97 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using Unity.Mathematics;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. using Random = UnityEngine.Random;
  8.  
  9. public class Ball : MonoBehaviour
  10. {
  11.     public GameFieldManager gfm;
  12.     public Color color;
  13.     public Vector2 position;
  14.  
  15.     public Image image;
  16.     public BoxCollider2D boxCollider2D;
  17.     public RectTransform rectTransform;
  18.     public Button button;
  19.  
  20.     private void Awake()
  21.     {
  22.         gfm = GameObject.Find("/GameManager").GetComponent<GameFieldManager>();
  23.         image = GetComponent<Image>();
  24.         boxCollider2D = GetComponent<BoxCollider2D>();
  25.         rectTransform = GetComponent<RectTransform>();
  26.         button = GetComponent<Button>();
  27.     }
  28.  
  29.     private void Start()
  30.     {
  31.         OnEnable();
  32.         boxCollider2D.offset = new Vector2(gfm.ballSize.x / -2, gfm.ballSize.y / 2);
  33.         boxCollider2D.size = gfm.ballSize;
  34.         button.onClick.AddListener(delegate { gfm.CollectBallsToDestroy(this); });
  35.     }
  36.  
  37.     private void OnEnable()
  38.     {
  39.         image.color = gfm.colors[Random.Range(0, gfm.colors.Count)];
  40.         color = image.color;
  41.         position = new Vector2(Mathf.Round(rectTransform.anchoredPosition.x / gfm.ballSize.x), Mathf.Round(rectTransform.anchoredPosition.y / gfm.ballSize.y));
  42.         name = string.Format("{0}/{1} Ball", position.x, position.y);
  43.     }
  44.  
  45.     public List<Ball> GetBallsAround()
  46.     {
  47.         List<Ball> ballsList = new List<Ball>();
  48.  
  49.         Collider2D[] hit = Physics2D.OverlapBoxAll(new Vector2(transform.position.x - (gfm.ballSize.x / 2), transform.position.y + (gfm.ballSize.y / 2)), gfm.ballSize * 1.2f, 0);
  50.         foreach (Collider2D item in hit)
  51.         {
  52.             Ball ball = item.GetComponent<Ball>();
  53.             if ((ball.position + Vector2.up == position ||
  54.                  ball.position + Vector2.down == position ||
  55.                  ball.position + Vector2.left == position ||
  56.                  ball.position + Vector2.right == position) && ball.color == color)
  57.             {
  58.                 ballsList.Add(ball);
  59.             }
  60.         }
  61.         return ballsList;
  62.     }
  63.  
  64.     public void MoveDown()
  65.     {
  66.         if (position.y > 0)
  67.         {
  68.             RaycastHit2D hit = Physics2D.Raycast(transform.position + Vector3.down + Vector3.left, Vector2.down, gfm.gameFieldSize.y);
  69.             Vector2 posToMove = new Vector2(-1, -1);
  70.             if (hit)
  71.             {
  72.                 if (hit.transform.gameObject.GetComponent<Ball>().position.y + 1 != position.y)
  73.                 {
  74.                     posToMove = hit.transform.gameObject.GetComponent<Ball>().position + Vector2.up ;
  75.                 }
  76.             }
  77.             else
  78.             {
  79.                 posToMove = new Vector2(position.x, 0);
  80.             }
  81.             if (posToMove != new Vector2(-1, -1))
  82.             {
  83.                 Move(posToMove);
  84.             }
  85.         }
  86.     }
  87.  
  88.     public void MoveRight()
  89.     {
  90.         if (position.x < 0)
  91.         {
  92.             RaycastHit2D hit = Physics2D.Raycast(transform.position + Vector3.up + Vector3.right, Vector2.right, gfm.gameFieldSize.x);
  93.             Vector2 posToMove = new Vector2(-1, -1);
  94.             if (hit)
  95.             {
  96.                 if (hit.transform.gameObject.GetComponent<Ball>().position.x - 1 != position.x)
  97.                 {
  98.                     posToMove = hit.transform.gameObject.GetComponent<Ball>().position + Vector2.left;
  99.                 }
  100.             }
  101.             else
  102.             {
  103.                 posToMove = new Vector2(0, position.y);
  104.             }
  105.             if (posToMove != new Vector2(-1, -1))
  106.             {
  107.                 Move(posToMove);
  108.             }
  109.         }
  110.     }
  111.  
  112.     private void Move(Vector2 pos)
  113.     {
  114.         //Debug.Log(string.Format("Move {0} to {1}", position, pos));
  115.         rectTransform.anchoredPosition = pos * gfm.ballSize;
  116.         position = pos;
  117.         name = string.Format("{0}/{1} Ball", position.x, position.y);
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement