Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Square : MonoBehaviour {
- int moveamount;
- int cubesize;
- Vector2 movevector;
- Vector4 boundries;
- void Start(){
- moveamount = GameObject.FindWithTag("Grid").GetComponent<GridBG>().moveamount;
- boundries = new Vector4(7,-7,7,-7);
- movevector = Vector2.up * moveamount;
- cubesize = 1;
- }
- bool DoBoundCheck(GameObject square){
- Vector3 movvec = square.transform.position+(this.transform.up*moveamount);
- if ((movvec.x <=boundries[0] & movvec.x >=boundries[1] & movvec.y <=boundries[2] & movvec.y >=boundries[3])) {
- return true;
- } else {
- return false;
- }
- }
- GameObject GetSquare(GameObject square){
- Vector2 movevec = (Vector2)(this.transform.up* moveamount);
- Vector2 lb = new Vector2 (square.transform.position.x,square.transform.position.y)+movevec;
- Vector2 rt = new Vector2 (square.transform.position.x+cubesize, square.transform.position.y+cubesize)+movevec;
- Collider2D[] colliderslist = Physics2D.OverlapAreaAll(lb,rt,Physics.DefaultRaycastLayers);
- foreach (Collider2D coll in colliderslist){
- if (coll != null) { // It could just not collide with anything
- if (coll.gameObject.tag != null & coll.gameObject.tag == "Square") {
- return coll.gameObject;
- }
- }
- }
- return null;
- }
- List<GameObject> GetOnWhat(GameObject square){
- Vector2 lb = new Vector2 (square.transform.position.x, square.transform.position.y);
- Vector2 rt = new Vector2 (square.transform.position.x + 1, square.transform.position.y + 1);
- List<GameObject> objlist = new List<GameObject> ();
- Collider2D[] colliderslist = Physics2D.OverlapAreaAll(lb,rt,Physics.DefaultRaycastLayers);
- foreach (Collider2D coll in colliderslist){
- if (coll != null) { // It could just not collide with anything
- if (coll.gameObject.tag != null) {
- objlist.Add (coll.gameObject);
- }
- }
- }
- return objlist;
- }
- void DoArrowCheck(GameObject square){
- List<GameObject> onwhatlist = GetOnWhat (square);
- foreach (GameObject item in onwhatlist) {
- if (item.tag == "Arrow") {
- square.transform.rotation = item.transform.rotation;
- }
- }
- }
- bool DoCircleCheck(GameObject square){
- List<GameObject> onwhatlist = GetOnWhat (square);
- foreach (GameObject item in onwhatlist) {
- if (item.tag == "Circle") {
- if (square.GetComponent<SpriteRenderer> ().color == item.GetComponent<SpriteRenderer> ().color) {
- return true;
- }
- }
- }
- return false;
- }
- void CheckWin(){
- foreach (GameObject square in GameObject.FindGameObjectsWithTag("Square")) {
- if (!DoCircleCheck (square)) {
- return;
- }
- }
- Debug.Log ("YOU WIN!");
- }
- bool DoCollisions(){
- bool NextSpaceFree = false;
- GameObject square = this.gameObject;
- List<GameObject> squarelist = new List<GameObject>();
- squarelist.Add (square);
- while(!NextSpaceFree)
- { if (!DoBoundCheck (square)) {
- NextSpaceFree = true;
- return false;
- }else{
- square = GetSquare (square);
- if (square == null) {
- NextSpaceFree = true;
- break;
- }else{
- squarelist.Add (square);
- }
- }
- }
- foreach (GameObject item in squarelist) {
- Vector3 pos = this.transform.up * moveamount;
- pos = new Vector3 (Mathf.Round (pos.x), Mathf.Round (pos.y), Mathf.Round (pos.z));
- item.transform.position += pos;
- DoArrowCheck (item);
- }
- return true;
- }
- void OnMouseDown(){
- DoCollisions ();
- CheckWin ();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement