using UnityEngine; using System.Collections; using UnityEngine.UI; public class GameMaster : MonoBehaviour { public enum State { idle, moveRoll, monster, flee }; public enum Tile { stone, pumpkin, candy, monster1, monster2, monster3 }; public Material[] mats; public GameObject player; public GameObject[] tiles; public Tile[] tileType; public State state; public Text btn1text, btn2text, btn3text, infoText, scoreText; public Camera mainCam; public Button btn1, btn2, btn3; int dieValue; public GameObject dieObject; static float[,] dieRotations = { { 0, 0, 0 }, { -90, 0, 0 }, { 0, 0, -90 }, { 0, 0, 90 }, { 90, 0, 0 }, { 180, 0, 0 } }; int playerPos; Vector3 playerOffset; // Use this for initialization void Start() { int i, j; tileType = new Tile[tiles.Length]; for (i = 0; i < tiles.Length; i++) { // use the name of the tiles to populate the array tiles[i] = GameObject.Find("tile" + i); if (tiles[i]) { // use the material's name to set the type Material mat = tiles[i].GetComponent().material; tileType[i] = Tile.stone; for (j = 0; j < mats.Length; j++) if (mat.name.IndexOf(mats[j].name) >= 0) tileType[i] = (Tile)j; } } RollDie(); playerOffset = tiles[0].transform.position - player.transform.position; PlacePlayer(0); // starting position for the game PlaceButtons(); // place the buttons based on the screen size state = State.idle; // initial state SetButtons(); infoText.text = ""; } // Places the player on a tile given by the index in the tiles array. // Only uses the x and z of the tile. public void PlacePlayer(int place) { player.transform.position = new Vector3(tiles[place].transform.position.x, player.transform.position.y, tiles[place].transform.position.z) + playerOffset; playerPos = place; // index in the array of tiles } // Update is called once per frame // Will probably need this to implement the actions void Update() { } // Sets the rotation of the die object based on the value of the die. void SetDie(int value) { float x, y, z; if (1 <= value && value <= 6) { x = dieRotations[value - 1, 0]; y = dieRotations[value - 1, 1]; z = dieRotations[value - 1, 2]; dieObject.transform.eulerAngles = new Vector3(x, y, z); } } // Roll the die randomly, then set the die object and the info text. void RollDie() { dieValue = Random.Range(1, 6); // random die SetDie(dieValue); infoText.text = "Roll: " + dieValue; // show it } //place the buttons based on the screen size void PlaceButtons() { //Camera cam = GameObject.Find("Main Camera"); //Button btn = GameObject.Find("Button1") as Button; float ymax = Screen.height; float xmax = Screen.width; float wid, hei; wid = btn1.GetComponent().rect.width; hei = btn1.GetComponent().rect.height; btn1.transform.position = new Vector3(xmax / 2 - 10f - 1.5f * wid, ymax - hei / 2 - 5f, btn1.transform.position.z); btn2.transform.position = new Vector3(xmax / 2 - wid / 2, ymax - hei / 2 - 5f, btn2.transform.position.z); btn3.transform.position = new Vector3(xmax / 2 + wid / 2 + 10f, ymax - hei / 2 - 5f, btn3.transform.position.z); wid = scoreText.GetComponent().rect.width; hei = scoreText.GetComponent().rect.height; scoreText.transform.position = new Vector3(5f + wid / 2, 5f, scoreText.transform.position.z); wid = infoText.GetComponent().rect.width; hei = infoText.GetComponent().rect.height; infoText.transform.position = new Vector3(xmax - wid / 2 - 5f, ymax - hei / 2 - 5f, infoText.transform.position.z); } // first button action public void Action1() { switch (state) { case State.idle: RollDie(); state = State.moveRoll; // next state break; } SetButtons(); } // Sets the text of the buttons based on the context. void SetButtons() { btn1text.text = ""; btn2text.text = ""; btn3text.text = ""; switch (state) { case State.idle: btn1text.text = "Roll"; break; } } }