Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using System.Net;
- using Unity.Mathematics;
- using UnityEngine;
- using UnityEngine.UI;
- using Random = UnityEngine.Random;
- public class Ball : MonoBehaviour
- {
- public GameFieldManager gfm;
- public Color color;
- public Vector2 position;
- public Image image;
- public BoxCollider2D boxCollider2D;
- public RectTransform rectTransform;
- public Button button;
- private void Awake()
- {
- gfm = GameObject.Find("/GameManager").GetComponent<GameFieldManager>();
- image = GetComponent<Image>();
- boxCollider2D = GetComponent<BoxCollider2D>();
- rectTransform = GetComponent<RectTransform>();
- button = GetComponent<Button>();
- }
- private void Start()
- {
- OnEnable();
- boxCollider2D.offset = new Vector2(gfm.ballSize.x / -2, gfm.ballSize.y / 2);
- boxCollider2D.size = gfm.ballSize;
- button.onClick.AddListener(delegate { gfm.CollectBallsToDestroy(this); });
- }
- private void OnEnable()
- {
- image.color = gfm.colors[Random.Range(0, gfm.colors.Count)];
- color = image.color;
- position = new Vector2(Mathf.Round(rectTransform.anchoredPosition.x / gfm.ballSize.x), Mathf.Round(rectTransform.anchoredPosition.y / gfm.ballSize.y));
- name = string.Format("{0}/{1} Ball", position.x, position.y);
- }
- public List<Ball> GetBallsAround()
- {
- List<Ball> ballsList = new List<Ball>();
- 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);
- foreach (Collider2D item in hit)
- {
- Ball ball = item.GetComponent<Ball>();
- if ((ball.position + Vector2.up == position ||
- ball.position + Vector2.down == position ||
- ball.position + Vector2.left == position ||
- ball.position + Vector2.right == position) && ball.color == color)
- {
- ballsList.Add(ball);
- }
- }
- return ballsList;
- }
- public void MoveDown()
- {
- if (position.y > 0)
- {
- RaycastHit2D hit = Physics2D.Raycast(transform.position + Vector3.down + Vector3.left, Vector2.down, gfm.gameFieldSize.y);
- Vector2 posToMove = new Vector2(-1, -1);
- if (hit)
- {
- if (hit.transform.gameObject.GetComponent<Ball>().position.y + 1 != position.y)
- {
- posToMove = hit.transform.gameObject.GetComponent<Ball>().position + Vector2.up ;
- }
- }
- else
- {
- posToMove = new Vector2(position.x, 0);
- }
- if (posToMove != new Vector2(-1, -1))
- {
- Move(posToMove);
- }
- }
- }
- public void MoveRight()
- {
- if (position.x < 0)
- {
- RaycastHit2D hit = Physics2D.Raycast(transform.position + Vector3.up + Vector3.right, Vector2.right, gfm.gameFieldSize.x);
- Vector2 posToMove = new Vector2(-1, -1);
- if (hit)
- {
- if (hit.transform.gameObject.GetComponent<Ball>().position.x - 1 != position.x)
- {
- posToMove = hit.transform.gameObject.GetComponent<Ball>().position + Vector2.left;
- }
- }
- else
- {
- posToMove = new Vector2(0, position.y);
- }
- if (posToMove != new Vector2(-1, -1))
- {
- Move(posToMove);
- }
- }
- }
- private void Move(Vector2 pos)
- {
- //Debug.Log(string.Format("Move {0} to {1}", position, pos));
- rectTransform.anchoredPosition = pos * gfm.ballSize;
- position = pos;
- name = string.Format("{0}/{1} Ball", position.x, position.y);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement