2018-05-15 23:11:34 -05:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class BoardManager : MonoBehaviour
|
|
|
|
|
{
|
2018-05-23 20:47:51 -05:00
|
|
|
|
|
2018-05-15 23:11:34 -05:00
|
|
|
|
// 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.
|
2018-05-23 20:47:51 -05:00
|
|
|
|
private int numberofspaces = 11;
|
2018-05-15 23:11:34 -05:00
|
|
|
|
|
|
|
|
|
// 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.
|
2018-05-23 20:47:51 -05:00
|
|
|
|
public Sprite angelsprite;
|
|
|
|
|
public Sprite demonsprite;
|
|
|
|
|
public Sprite emptysprite;
|
|
|
|
|
|
|
|
|
|
// Should later be defined in a separate namespace.
|
|
|
|
|
public struct WhiteSpace
|
|
|
|
|
{
|
|
|
|
|
public Image img;
|
|
|
|
|
public int spriteindex;
|
|
|
|
|
};
|
2018-05-15 23:11:34 -05:00
|
|
|
|
|
|
|
|
|
// Keep references of all the space GameObjects.
|
|
|
|
|
private Button[] blackspaces = new Button[61];
|
2018-05-23 20:47:51 -05:00
|
|
|
|
private WhiteSpace[] whitespaces = new WhiteSpace[60];
|
|
|
|
|
|
|
|
|
|
// Keep references for when MakeMove() is called.
|
|
|
|
|
private int[] spaces = new int[4]; // Indexes relative to whitespaces[] of the spaces being changed.
|
|
|
|
|
private int[] sprites = new int[4]; // Which sprite to change the space to.
|
|
|
|
|
public bool movemade = false; // Fix to keep ChangeSpaces() from being called every frame.
|
2018-05-15 23:11:34 -05:00
|
|
|
|
|
|
|
|
|
// Use this for initialization
|
|
|
|
|
void Start ()
|
|
|
|
|
{
|
|
|
|
|
canvasheight = this.GetComponentInParent<RectTransform>().rect.height;
|
|
|
|
|
canvaswidth = this.GetComponentInParent<RectTransform>().rect.width;
|
|
|
|
|
boardheight = board.GetComponent<RectTransform>().rect.height;
|
|
|
|
|
boardwidth = board.GetComponent<RectTransform>().rect.width;
|
|
|
|
|
spacespacing = ((canvaswidth - canvasspacing)/11f);
|
2018-05-23 20:47:51 -05:00
|
|
|
|
|
2018-05-15 23:11:34 -05:00
|
|
|
|
SpawnInitialBoard();
|
2018-05-23 20:47:51 -05:00
|
|
|
|
ResetSpaces();
|
|
|
|
|
ResetSprites();
|
2018-05-15 23:11:34 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-23 20:47:51 -05:00
|
|
|
|
// Grab the current canvas dimentions, used for dynamic resizing checks.
|
|
|
|
|
private Vector2 GetCurrentCanvas()
|
2018-05-15 23:11:34 -05:00
|
|
|
|
{
|
2018-05-23 20:47:51 -05:00
|
|
|
|
Vector2 output;
|
|
|
|
|
output.x = this.GetComponentInParent<RectTransform>().rect.height;
|
|
|
|
|
output.y = this.GetComponentInParent<RectTransform>().rect.width;
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Reset the spaces array to all -1 values. This means they will be ignored if they somehow
|
|
|
|
|
// get passed to a function that tries to change spaces.
|
|
|
|
|
private void ResetSpaces()
|
|
|
|
|
{
|
|
|
|
|
for(int i = 0; i < spaces.Length; ++i)
|
2018-05-15 23:11:34 -05:00
|
|
|
|
{
|
2018-05-23 20:47:51 -05:00
|
|
|
|
spaces[i] = -1;
|
2018-05-15 23:11:34 -05:00
|
|
|
|
}
|
2018-05-23 20:47:51 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// See above, but for the sprites array.
|
|
|
|
|
private void ResetSprites()
|
|
|
|
|
{
|
|
|
|
|
for(int i = 0; i < sprites.Length; ++i)
|
2018-05-15 23:11:34 -05:00
|
|
|
|
{
|
2018-05-23 20:47:51 -05:00
|
|
|
|
sprites[i] = -1;
|
2018-05-15 23:11:34 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-23 20:47:51 -05:00
|
|
|
|
// 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.
|
2018-05-15 23:11:34 -05:00
|
|
|
|
private Button SpawnBlackSpace(int i)
|
|
|
|
|
{
|
|
|
|
|
Button bsbutton;
|
|
|
|
|
bsbutton = Instantiate(blackspaceasset, blackspacetransform);
|
|
|
|
|
bsbutton.name += "" + i;
|
|
|
|
|
bsbutton.onClick.AddListener(delegate{ButtonAction(bsbutton);});
|
|
|
|
|
return bsbutton;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-23 20:47:51 -05:00
|
|
|
|
// Function call to return one Instantiated Image object.
|
2018-05-15 23:11:34 -05:00
|
|
|
|
private Image SpawnWhiteSpace()
|
|
|
|
|
{
|
|
|
|
|
Image wsimage;
|
|
|
|
|
wsimage = Instantiate(whitespaceasset, whitespacetransform);
|
|
|
|
|
return wsimage;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-23 20:47:51 -05:00
|
|
|
|
// Set up an empty board, sized to the current canvas.
|
2018-05-15 23:11:34 -05:00
|
|
|
|
private void SpawnInitialBoard()
|
|
|
|
|
{
|
|
|
|
|
for(int i = 0; i < blackspaces.Length; ++i)
|
|
|
|
|
{
|
|
|
|
|
Button temp;
|
|
|
|
|
temp = SpawnBlackSpace(i);
|
|
|
|
|
temp.GetComponent<RectTransform>().sizeDelta = new Vector2(spacespacing, spacespacing);
|
|
|
|
|
|
|
|
|
|
float x = (spacespacing*i*2 + (temp.GetComponent<RectTransform>().rect.width / 2f)) % (boardwidth) - ((boardwidth) / 2f);
|
|
|
|
|
float y = (boardheight / 2f) - ((temp.GetComponent<RectTransform>().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)
|
|
|
|
|
{
|
2018-05-23 20:47:51 -05:00
|
|
|
|
WhiteSpace tempws;
|
|
|
|
|
|
2018-05-15 23:11:34 -05:00
|
|
|
|
Image tempimage;
|
|
|
|
|
tempimage = SpawnWhiteSpace();
|
|
|
|
|
tempimage.GetComponent<RectTransform>().sizeDelta = new Vector2(spacespacing, spacespacing);
|
2018-05-23 20:47:51 -05:00
|
|
|
|
tempimage.sprite = emptysprite;
|
2018-05-15 23:11:34 -05:00
|
|
|
|
|
|
|
|
|
x = (spacespacing*i*2 + (tempimage.GetComponent<RectTransform>().rect.width / 2f)*3) % (boardwidth) - ((boardwidth) / 2f);
|
|
|
|
|
y = (boardheight / 2f) - ((tempimage.GetComponent<RectTransform>().rect.height / 2f) + spacespacing*(Mathf.Floor((i + 1)/(11.01f/2f))));
|
|
|
|
|
tempimage.transform.localPosition = new Vector3(x, y, 0);
|
|
|
|
|
|
2018-05-23 20:47:51 -05:00
|
|
|
|
tempws.img = tempimage;
|
|
|
|
|
tempws.spriteindex = 2;
|
|
|
|
|
|
|
|
|
|
whitespaces[i] = tempws;
|
2018-05-15 23:11:34 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-23 20:47:51 -05:00
|
|
|
|
// Update the board's scale according to the current canvas size, call UpdatePieceScale().
|
|
|
|
|
private void UpdateBoardScale()
|
|
|
|
|
{
|
|
|
|
|
canvasheight = this.GetComponentInParent<RectTransform>().rect.height;
|
|
|
|
|
canvaswidth = this.GetComponentInParent<RectTransform>().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<RectTransform>().rect.height;
|
|
|
|
|
boardwidth = board.GetComponent<RectTransform>().rect.width;
|
|
|
|
|
|
|
|
|
|
spacespacing = ((boardwidth)/11f);
|
|
|
|
|
|
|
|
|
|
UpdatePieceScale();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update the pieces' scales according to the current canvas size.
|
2018-05-15 23:11:34 -05:00
|
|
|
|
private void UpdatePieceScale()
|
|
|
|
|
{
|
|
|
|
|
for(int i = 0; i < blackspaces.Length; ++i)
|
|
|
|
|
{
|
|
|
|
|
Button temp = blackspaces[i];
|
|
|
|
|
temp.GetComponent<RectTransform>().sizeDelta = new Vector2(spacespacing, spacespacing);
|
|
|
|
|
float x = (spacespacing*i*2 + (temp.GetComponent<RectTransform>().rect.width / 2f)) % (boardwidth) - ((boardwidth) / 2f);
|
|
|
|
|
float y = (boardheight / 2f) - ((temp.GetComponent<RectTransform>().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)
|
|
|
|
|
{
|
2018-05-23 20:47:51 -05:00
|
|
|
|
Image tempimage = whitespaces[i].img;
|
2018-05-15 23:11:34 -05:00
|
|
|
|
tempimage.GetComponent<RectTransform>().sizeDelta = new Vector2(spacespacing, spacespacing);
|
|
|
|
|
|
|
|
|
|
x = (spacespacing*i*2 + (tempimage.GetComponent<RectTransform>().rect.width / 2f)*3) % (boardwidth) - ((boardwidth) / 2f);
|
|
|
|
|
y = (boardheight / 2f) - ((tempimage.GetComponent<RectTransform>().rect.height / 2f) + spacespacing*(Mathf.Floor((i + 1)/(11.01f/2f))));
|
|
|
|
|
tempimage.transform.localPosition = new Vector3(x, y, 0);
|
|
|
|
|
|
2018-05-23 20:47:51 -05:00
|
|
|
|
whitespaces[i].img = tempimage;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Change the passed in space indexes' sprites to the specified sprites.
|
|
|
|
|
private void ChangeSpaces(int[] spaces, int[] sprites)
|
|
|
|
|
{
|
|
|
|
|
for(int i = 0; i < spaces.Length; ++i)
|
|
|
|
|
{
|
|
|
|
|
if(spaces[i] >= 0 && spaces[i] < whitespaces.Length)
|
|
|
|
|
{
|
|
|
|
|
switch(sprites[i])
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
whitespaces[spaces[i]].img.sprite = angelsprite;
|
|
|
|
|
whitespaces[spaces[i]].spriteindex = 0;
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
whitespaces[spaces[i]].img.sprite = demonsprite;
|
|
|
|
|
whitespaces[spaces[i]].spriteindex = 1;
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
whitespaces[spaces[i]].img.sprite = emptysprite;
|
|
|
|
|
whitespaces[spaces[i]].spriteindex = 2;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2018-05-15 23:11:34 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-23 20:47:51 -05:00
|
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
ResetSpaces();
|
|
|
|
|
ResetSprites();
|
|
|
|
|
|
|
|
|
|
movemade = false;
|
2018-05-15 23:11:34 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-23 20:47:51 -05:00
|
|
|
|
/* 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.
|
|
|
|
|
*/
|
2018-05-15 23:11:34 -05:00
|
|
|
|
void Update ()
|
|
|
|
|
{
|
2018-05-23 20:47:51 -05:00
|
|
|
|
Vector2 currentcanvas = GetCurrentCanvas();
|
|
|
|
|
|
|
|
|
|
if(currentcanvas.x != canvaswidth || currentcanvas.y != canvasheight)
|
|
|
|
|
{
|
|
|
|
|
UpdateBoardScale();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(movemade)
|
|
|
|
|
{
|
|
|
|
|
ChangeSpaces(spaces, sprites);
|
|
|
|
|
}
|
2018-05-15 23:11:34 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-23 20:47:51 -05:00
|
|
|
|
// Function definition to specify what the buttons should do when pressed, which is to call
|
|
|
|
|
// the GameManager.MakeMove() function with the necessary parameters.
|
2018-05-15 23:11:34 -05:00
|
|
|
|
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.
|
2018-05-23 20:47:51 -05:00
|
|
|
|
//Debug.Log("Board coordinates: " + boardcoords.x + " " + boardcoords.y);
|
2018-05-15 23:11:34 -05:00
|
|
|
|
|
2018-05-23 20:47:51 -05:00
|
|
|
|
// Temporarily hardcoded. Should later be changed to a GameManager function call that determines
|
|
|
|
|
// what to change spaces to.
|
|
|
|
|
GameManager.MakeMove(boardcoords, numberofspaces, whitespaces, spaces, sprites, ref movemade);
|
2018-05-15 23:11:34 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-23 20:47:51 -05:00
|
|
|
|
// Convert a given button index in the blackspaces[] array to board coordinates.
|
|
|
|
|
// These need to made more modular in the future to support
|
|
|
|
|
// different board sizes.
|
|
|
|
|
public static Vector2 ConvertButtonIndex(int i)
|
2018-05-15 23:11:34 -05:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-23 20:47:51 -05:00
|
|
|
|
// Convert a given set of board coordinates to the corresponding whitespaces[] array index.
|
|
|
|
|
public static int ConvertImageIndex(Vector2 bc)
|
2018-05-15 23:11:34 -05:00
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|