using UnityEngine; using UnityEngine.UI; using AngelsDemons; public class BoardManager : MonoBehaviour { // Global reference to the canvas' width and height. private float canvaswidth; private float canvasheight; // Variable to change spacing of the board from the edges of the canvas. private float canvasspacing = 80f; public Image board; // Board size references. private float boardwidth; private float boardheight; // Might want to make this part modular in the future. private int numberofspaces = 11; // Spaces for the board need to be spawned and resized based on canvas. private float spacespacing; public Button blackspaceasset; public Transform blackspacetransform; public Image whitespaceasset; public Transform whitespacetransform; // Image assets for the board spaces. public Sprite angelsprite; public Sprite demonsprite; public Sprite emptysprite; // Keep references of all the space GameObjects. private Button[] blackspaces = new Button[61]; private WhiteSpace[] whitespaces = new WhiteSpace[60]; // GameOver assets. Just to have a way to end the game. public GameObject gameoverobjects; // Use this for initialization void Start () { canvasheight = this.GetComponentInParent().rect.height; canvaswidth = this.GetComponentInParent().rect.width; boardheight = board.GetComponent().rect.height; boardwidth = board.GetComponent().rect.width; spacespacing = ((canvaswidth - canvasspacing)/11f); SpawnInitialBoard(); Board.ResetSpaces(); Board.ResetSprites(); // This should later be handled by Player.StartGame through MenuNav. Board.StartGame(); GameLogic.ResetTurn(); } // Grab the current canvas dimentions, used for dynamic resizing checks. private Vector2 GetCurrentCanvas() { Vector2 output; output.x = this.GetComponentInParent().rect.height; output.y = this.GetComponentInParent().rect.width; return output; } // Function call to return one Instantiated Button object. // Should be changed along with SpawnWhiteSpace to instead spawn the object and attach them // to a defined struct that can also hold the index of the spawned object. This will allow // for easier indexing of these prefabs in the future without the need for parsing. private Button SpawnBlackSpace(int i) { Button bsbutton; bsbutton = Instantiate(blackspaceasset, blackspacetransform); bsbutton.name += "" + i; bsbutton.onClick.AddListener(delegate{ButtonAction(bsbutton);}); return bsbutton; } // Function call to return one Instantiated Image object. private Image SpawnWhiteSpace() { Image wsimage; wsimage = Instantiate(whitespaceasset, whitespacetransform); return wsimage; } // Set up an empty board, sized to the current canvas. private void SpawnInitialBoard() { for(int i = 0; i < blackspaces.Length; ++i) { Button temp; temp = SpawnBlackSpace(i); temp.GetComponent().sizeDelta = new Vector2(spacespacing, spacespacing); float x = (spacespacing*i*2 + (temp.GetComponent().rect.width / 2f)) % (boardwidth) - ((boardwidth) / 2f); float y = (boardheight / 2f) - ((temp.GetComponent().rect.height / 2f) + spacespacing*(Mathf.Floor(i/(11f/2f)))); temp.transform.localPosition = new Vector3(x, y, 0); blackspaces[i] = temp; if(i < blackspaces.Length - 1) { WhiteSpace tempws; Image tempimage; tempimage = SpawnWhiteSpace(); tempimage.GetComponent().sizeDelta = new Vector2(spacespacing, spacespacing); tempimage.sprite = emptysprite; x = (spacespacing*i*2 + (tempimage.GetComponent().rect.width / 2f)*3) % (boardwidth) - ((boardwidth) / 2f); y = (boardheight / 2f) - ((tempimage.GetComponent().rect.height / 2f) + spacespacing*(Mathf.Floor((i + 1)/(11.01f/2f)))); tempimage.transform.localPosition = new Vector3(x, y, 0); tempws.img = tempimage; tempws.spriteindex = 2; whitespaces[i] = tempws; } } } // Update the board's scale according to the current canvas size, call UpdatePieceScale(). private void UpdateBoardScale() { canvasheight = this.GetComponentInParent().rect.height; canvaswidth = this.GetComponentInParent().rect.width; if(canvaswidth > canvasheight) { board.rectTransform.sizeDelta = new Vector2(canvasheight - canvasspacing, canvasheight - canvasspacing); } else { board.rectTransform.sizeDelta = new Vector2(canvaswidth - canvasspacing, canvaswidth - canvasspacing); } boardheight = board.GetComponent().rect.height; boardwidth = board.GetComponent().rect.width; spacespacing = ((boardwidth)/11f); UpdatePieceScale(); } // Update the pieces' scales according to the current canvas size. private void UpdatePieceScale() { for(int i = 0; i < blackspaces.Length; ++i) { Button temp = blackspaces[i]; temp.GetComponent().sizeDelta = new Vector2(spacespacing, spacespacing); float x = (spacespacing*i*2 + (temp.GetComponent().rect.width / 2f)) % (boardwidth) - ((boardwidth) / 2f); float y = (boardheight / 2f) - ((temp.GetComponent().rect.height / 2f) + spacespacing*(Mathf.Floor(i/(11f/2f)))); temp.transform.localPosition = new Vector3(x, y, 0); blackspaces[i] = temp; if(i < blackspaces.Length - 1) { Image tempimage = whitespaces[i].img; tempimage.GetComponent().sizeDelta = new Vector2(spacespacing, spacespacing); x = (spacespacing*i*2 + (tempimage.GetComponent().rect.width / 2f)*3) % (boardwidth) - ((boardwidth) / 2f); y = (boardheight / 2f) - ((tempimage.GetComponent().rect.height / 2f) + spacespacing*(Mathf.Floor((i + 1)/(11.01f/2f)))); tempimage.transform.localPosition = new Vector3(x, y, 0); whitespaces[i].img = tempimage; } } } // Change the passed in space indexes' sprites to the specified sprites. private void ChangeSpaces() { for(int i = 0; i < Board.GetSpacesLength(); ++i) { if(Board.GetSpaces(i) >= 0 && Board.GetSpaces(i) < whitespaces.Length) { switch(Board.GetSprites(i)) { case 0: whitespaces[Board.GetSpaces(i)].img.sprite = angelsprite; whitespaces[Board.GetSpaces(i)].spriteindex = 0; break; case 1: whitespaces[Board.GetSpaces(i)].img.sprite = demonsprite; whitespaces[Board.GetSpaces(i)].spriteindex = 1; break; case 2: whitespaces[Board.GetSpaces(i)].img.sprite = emptysprite; whitespaces[Board.GetSpaces(i)].spriteindex = 2; break; default: break; } } } // After a move has been made, since right now spaces are checked every frame, // change all spaces[] values to -1 to avoid sprites being updated every frame. Board.ResetSpaces(); Board.ResetSprites(); Board.SetMoveMade(false); } /* Update is called once per frame * Here we check if the current canvas has changed and if so, call UpdateBoardScale(), * then we also check if a move has been made, and call ChangeSpaces with the necessary parameters. */ void Update () { Vector2 currentcanvas = GetCurrentCanvas(); if(currentcanvas.x != canvaswidth || currentcanvas.y != canvasheight) { UpdateBoardScale(); } if(Board.GetMoveMade() && !Board.IsGameOver()) { ChangeSpaces(); CheckGameOver(); } } // Function definition to specify what the buttons should do when pressed, which is to call // the GameManager.MakeMove() function with the necessary parameters. public void ButtonAction(Button b) { if(!Board.IsGameOver()) { int buttonindex = 0; int.TryParse(b.name.Replace("Move Selection(Clone)", ""), out buttonindex); Vector2 boardcoords = Board.ConvertButtonIndex(buttonindex); // This is completely incorrect and will most likely need a switch case of some kind in the future. //Debug.Log("Board coordinates: " + boardcoords.x + " " + boardcoords.y); // Temporarily hardcoded. Should later be changed to a GameManager function call that determines // what to change spaces to. GameLogic.MakeMove(boardcoords, numberofspaces, whitespaces); } else { Debug.Log("The game is over."); } } public void CheckGameOver() { int turn = GameLogic.GetTurn(); int turnlimit = GameLogic.GetTurnLimit(); if(turn > turnlimit) { if(!gameoverobjects.activeSelf) { gameoverobjects.SetActive(true); } Board.EndGame(); } else { if(gameoverobjects.activeSelf) { gameoverobjects.SetActive(false); } } } }