This repository has been archived on 2025-04-11. You can view files and clone it, but cannot push or open issues or pull requests.
angels-and-demons/Angels and Demons/Assets/Scripts/BoardManager.cs

277 lines
8.5 KiB
C#
Raw Normal View History

using UnityEngine;
2018-05-15 23:11:34 -05:00
using UnityEngine.UI;
using AngelsDemons;
2018-05-15 23:11:34 -05:00
public class BoardManager : MonoBehaviour
{
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.
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.
public Sprite angelsprite;
public Sprite demonsprite;
public Sprite emptysprite;
2018-05-15 23:11:34 -05:00
// 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;
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-15 23:11:34 -05:00
SpawnInitialBoard();
Board.ResetSpaces();
Board.ResetSprites();
// This should later be handled by Player.StartGame through MenuNav.
Board.StartGame();
GameLogic.ResetTurn();
2018-05-15 23:11:34 -05:00
}
// Grab the current canvas dimentions, used for dynamic resizing checks.
private Vector2 GetCurrentCanvas()
2018-05-15 23:11:34 -05:00
{
Vector2 output;
output.x = this.GetComponentInParent<RectTransform>().rect.height;
output.y = this.GetComponentInParent<RectTransform>().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.
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;
}
// 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;
}
// 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)
{
WhiteSpace tempws;
2018-05-15 23:11:34 -05:00
Image tempimage;
tempimage = SpawnWhiteSpace();
tempimage.GetComponent<RectTransform>().sizeDelta = new Vector2(spacespacing, spacespacing);
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);
tempws.img = tempimage;
tempws.spriteindex = 2;
whitespaces[i] = tempws;
2018-05-15 23:11:34 -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)
{
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);
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;
}
2018-05-15 23:11:34 -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.
Board.ResetSpaces();
Board.ResetSprites();
Board.SetMoveMade(false);
2018-05-15 23:11:34 -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 ()
{
Vector2 currentcanvas = GetCurrentCanvas();
if(currentcanvas.x != canvaswidth || currentcanvas.y != canvasheight)
{
UpdateBoardScale();
}
if(Board.GetMoveMade() && !Board.IsGameOver())
{
ChangeSpaces();
CheckGameOver();
}
2018-05-15 23:11:34 -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)
{
if(!Board.IsGameOver())
{
int buttonindex = 0;
int.TryParse(b.name.Replace("Move Selection(Clone)", ""), out buttonindex);
Vector2 boardcoords = Board.ConvertButtonIndex(buttonindex);
2018-05-15 23:11:34 -05:00
// 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);
2018-05-15 23:11:34 -05:00
// Temporarily hardcoded. Should later be changed to a GameManager function call that determines
// what to change spaces to.
GameLogic.MakeMove(boardcoords, numberofspaces, whitespaces);
}
else
2018-05-15 23:11:34 -05:00
{
Debug.Log("The game is over.");
2018-05-15 23:11:34 -05:00
}
}
public void CheckGameOver()
2018-05-15 23:11:34 -05:00
{
int turn = GameLogic.GetTurn();
int turnlimit = GameLogic.GetTurnLimit();
2018-05-15 23:11:34 -05:00
if(turn > turnlimit)
2018-05-15 23:11:34 -05:00
{
if(!gameoverobjects.activeSelf)
{
gameoverobjects.SetActive(true);
}
Board.EndGame();
}
else
{
if(gameoverobjects.activeSelf)
{
gameoverobjects.SetActive(false);
}
2018-05-15 23:11:34 -05:00
}
}
}