using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; 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 float 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 angelimage; public Image demonimage; // Keep references of all the space GameObjects. private Button[] blackspaces = new Button[61]; private Image[] whitespaces = new Image[60]; // 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(); } 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(); } private Button SpawnBlackSpace(int i) { Button bsbutton; bsbutton = Instantiate(blackspaceasset, blackspacetransform); bsbutton.name += "" + i; bsbutton.onClick.AddListener(delegate{ButtonAction(bsbutton);}); return bsbutton; } private Image SpawnWhiteSpace() { Image wsimage; wsimage = Instantiate(whitespaceasset, whitespacetransform); return wsimage; } 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) { Image tempimage; tempimage = SpawnWhiteSpace(); 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] = tempimage; } } } 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]; 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] = tempimage; } } } // Update is called once per frame void Update () { UpdateBoardScale(); } public void ButtonAction(Button b) { int buttonindex = 0; int.TryParse(b.name.Replace("Move Selection(Clone)", ""), out buttonindex); Vector2 boardcoords = 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); ChangeSpaces(boardcoords); } private Vector2 ConvertButtonIndex(int i) { Vector2 temp; temp.x = (i*2)%11; temp.y = 0; switch(i) { case 0: case 1: case 2: case 3: case 4: case 5: return temp; case 6: case 7: case 8: case 9: case 10: temp.y = 1; return temp; case 11: case 12: case 13: case 14: case 15: case 16: temp.y = 2; return temp; case 17: case 18: case 19: case 20: case 21: temp.y = 3; return temp; case 22: case 23: case 24: case 25: case 26: case 27: temp.y = 4; return temp; case 28: case 29: case 30: case 31: case 32: temp.y = 5; return temp; case 33: case 34: case 35: case 36: case 37: case 38: temp.y = 6; return temp; case 39: case 40: case 41: case 42: case 43: temp.y = 7; return temp; case 44: case 45: case 46: case 47: case 48: case 49: temp.y = 8; return temp; case 50: case 51: case 52: case 53: case 54: temp.y = 9; return temp; case 55: case 56: case 57: case 58: case 59: case 60: temp.y = 10; return temp; default: return temp; } } private int ConvertImageIndex(Vector2 bc) { int i = 0; switch(Mathf.RoundToInt(bc.y)) { case 0: i = (Mathf.RoundToInt(bc.x) - 1)/2; break; case 1: i = (Mathf.RoundToInt(bc.x))/2; i += 5; break; case 2: i = (Mathf.RoundToInt(bc.x) - 1)/2; i += 11; break; case 3: i = (Mathf.RoundToInt(bc.x))/2; i += 16; break; case 4: i = (Mathf.RoundToInt(bc.x) - 1)/2; i += 22; break; case 5: i = (Mathf.RoundToInt(bc.x))/2; i += 27; break; case 6: i = (Mathf.RoundToInt(bc.x) - 1)/2; i += 33; break; case 7: i = (Mathf.RoundToInt(bc.x))/2; i += 38; break; case 8: i = (Mathf.RoundToInt(bc.x) - 1)/2; i += 44; break; case 9: i = (Mathf.RoundToInt(bc.x))/2; i += 49; break; case 10: i = (Mathf.RoundToInt(bc.x) - 1)/2; i += 55; break; default: break; } return i; } private void ChangeSpaces(Vector2 bc) { Vector2 tempbc = bc; int wsi = 0; if(bc.x > 0 && bc.x < (numberofspaces - 1)) { if(bc.y > 0) { Debug.Log( "changing space:" + (bc.x) + " " + (bc.y - 1) ); tempbc = bc; tempbc.y -= 1; wsi = ConvertImageIndex(tempbc); whitespaces[wsi].sprite = angelimage; } if(bc.y < (numberofspaces - 1)) { Debug.Log( "changing space:" + (bc.x) + " " + (bc.y + 1) ); tempbc = bc; tempbc.y += 1; wsi = ConvertImageIndex(tempbc); whitespaces[wsi].sprite = angelimage; } Debug.Log( "changing spaces:" + (bc.x - 1) + " " + (bc.y) + "\t" + (bc.x + 1) + " " + (bc.y) ); tempbc = bc; tempbc.x -= 1; wsi = ConvertImageIndex(tempbc); whitespaces[wsi].sprite = angelimage; tempbc.x += 2; wsi = ConvertImageIndex(tempbc); whitespaces[wsi].sprite = angelimage; } else if(bc.y > 0 && bc.y < (numberofspaces - 1)) { if(bc.x == 0) { Debug.Log( "changing space:" + (bc.x + 1) + " " + (bc.y) ); tempbc = bc; tempbc.x += 1; wsi = ConvertImageIndex(tempbc); whitespaces[wsi].sprite = angelimage; } if(bc.x == (numberofspaces - 1)) { Debug.Log( "changing space:" + (bc.x - 1) + " " + (bc.y) ); tempbc = bc; tempbc.x -= 1; wsi = ConvertImageIndex(tempbc); whitespaces[wsi].sprite = angelimage; } Debug.Log( "changing spaces:" + (bc.x) + " " + (bc.y - 1) + "\t" + (bc.x) + " " + (bc.y + 1) ); tempbc = bc; tempbc.y -= 1; wsi = ConvertImageIndex(tempbc); whitespaces[wsi].sprite = angelimage; tempbc.y += 2; wsi = ConvertImageIndex(tempbc); whitespaces[wsi].sprite = angelimage; } else { if(bc.x == 0) { Debug.Log( "changing space:" + (bc.x + 1) + " " + (bc.y) ); tempbc = bc; tempbc.x += 1; wsi = ConvertImageIndex(tempbc); whitespaces[wsi].sprite = angelimage; } if(bc.x == (numberofspaces - 1)) { Debug.Log( "changing space:" + (bc.x - 1) + " " + (bc.y) ); tempbc = bc; tempbc.x -= 1; wsi = ConvertImageIndex(tempbc); whitespaces[wsi].sprite = angelimage; } if(bc.y > 0) { Debug.Log( "changing space:" + (bc.x) + " " + (bc.y - 1) ); tempbc = bc; tempbc.y -= 1; wsi = ConvertImageIndex(tempbc); whitespaces[wsi].sprite = angelimage; } if(bc.y < (numberofspaces - 1)) { Debug.Log( "changing space:" + (bc.x) + " " + (bc.y + 1) ); tempbc = bc; tempbc.y += 1; wsi = ConvertImageIndex(tempbc); whitespaces[wsi].sprite = angelimage; } } } }