// Start is called before the first frame update void Start() { //WindowManager.instance.ScreenSizeChangeEvent += Instance_ScreenSizeChangeEvent; DetectPossibleMoves.SetBooleans(); GamePieces.FindGameBlocks(); GamePieces.FindGamePieces(); clickAndDropScript = gameObject.GetComponent <ClickAndDrop>(); gameOverCanvas = GameObject.FindGameObjectWithTag("GameOverCanvas"); restartButton = GameObject.FindGameObjectWithTag("RestartButton"); changeColorButton = GameObject.FindGameObjectWithTag("ChangeColor"); highScoreIcon = GameObject.FindGameObjectWithTag("HighScoreIcon"); background = GameObject.FindGameObjectWithTag("BlurBG"); gameOverCanvas.SetActive(false); restartButton.SetActive(false); changeColorButton.SetActive(true); background.SetActive(false); highScoreIcon.SetActive(true); // Change BG var camera = (Camera)FindObjectOfType(typeof(Camera)); camera.backgroundColor = SetColor.GetColor("Background"); // Change Grid AnimateGrid.SetGrid(); UpdateColor(); }
// Use this for initialization void Start () { canPick=false; CAD=GameObject.Find ("Main Camera").GetComponent<ClickAndDrop>(); }
// Main method that runs to find what objects are valid and plays it on the grid public static void WhereAreGamePiecesToPlay() { GamePieces.isAPlayValid = false; foreach (GameObject oneGamePiece in CollectionOfGamePieces) { bool isAGameBlockOutsideTheBoard = false; bool isAGameBlockOverLappingAValidPlay = false; bool isObjectClicked = false; gameObjectDetection = oneGamePiece.GetComponent <DetectObjectClicked>(); objectClickAndDrop = oneGamePiece.GetComponent <ClickAndDrop>(); if (gameObjectDetection.IsObjectClicked) { objectClickAndDrop.enabled = true; } // Save position of gamePiece if invalid placement if (Input.GetMouseButtonDown(0)) { // Detect if gamePiece has the script if (oneGamePiece.GetComponent <DetectObjectClicked>() != null) { isObjectClicked = gameObjectDetection.IsObjectClicked; // Get the Block ID if an object has been clicked. if (isObjectClicked && hasClickedObjectBeenReleased) { UUIDofObject = gameObjectDetection.UUID; hasClickedObjectBeenReleased = false; } } } // Get children Transform[] manyGameBlocks = oneGamePiece.GetComponentsInChildren <Transform>(); // Get children position for valid placement foreach (var oneGameBlock in manyGameBlocks) { // The main game block [Not the children of it] if (GetGameBlockTag(oneGameBlock)) { float x = oneGameBlock.position.x; float y = oneGameBlock.position.y; //Debug.Log("X: " + x + " Y: " + y); //Debug.Log(InsideBorder(x, y)); isAGameBlockOutsideTheBoard = InsideBorder(x, y) ? isAGameBlockOutsideTheBoard : true; //if (!InsideBorder(x, y)) // isAGameBlockOutsideTheBoard = true; Vector2 v = GameGrid.Vector2Round(oneGameBlock.position); // Mark true: if a game piece is overlapping if (GameGrid.IsAGamePieceInsideGridAt(v)) { isAGameBlockOverLappingAValidPlay = true; } } } // FOR VALID PLAY // If: children position is valid, destory gamepiece & leave gameblocks on board // When the mouse is released bool isAGameBlockInsideTheBoard = !isAGameBlockOutsideTheBoard; if (!isAGameBlockOverLappingAValidPlay && isAGameBlockInsideTheBoard && Input.GetMouseButtonUp(0)) { foreach (var oneGameBlock in manyGameBlocks) { // Align the gameblocks on the grid var pos = GameGrid.Vector2Round(oneGameBlock.position); oneGameBlock.position = pos; if (GetGameBlockTag(oneGameBlock)) { oneGameBlock.parent = null; Destroy(oneGamePiece); // Place valid piece on game grid Vector2 v = GameGrid.Vector2Round(oneGameBlock.position); GameGrid.grid[(int)v.x, (int)v.y] = oneGameBlock; // Change the background grid color AnimateGrid.ChangeBgGridColor((int)v.x, (int)v.y); hasClickedObjectBeenReleased = true; isAPlayValid = true; } } } // FOR INVALID PLAY // Else: restore gamepiece to it's orginal location for an invalid play else if (isAGameBlockOverLappingAValidPlay && Input.GetMouseButtonUp(0) || isAGameBlockOutsideTheBoard && Input.GetMouseButtonUp(0)) { if (UUIDofObject == gameObjectDetection.UUID) { oneGamePiece.transform.position = gameObjectDetection.PositionOfObject; var localScale = oneGamePiece.transform.localScale; localScale.x = OriginalScale; localScale.y = OriginalScale; oneGamePiece.transform.localScale = localScale; hasClickedObjectBeenReleased = true; //objectClickAndDrop.enabled = false; } } } }