MapMaker.cs 1.09 KB
Newer Older
Zac Olsen's avatar
Zac Olsen committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class MapMaker : MonoBehaviour {

	public GameObject emptyTile;

	private Text rowsText;
	private Text colsText;

	private Tile[,] map;

	void Start () {
	
		rowsText = GameObject.Find ("Rows").transform.FindChild("Text").GetComponent<Text> ();
		colsText = GameObject.Find ("Cols").transform.FindChild("Text").GetComponent<Text> ();
	}

	public void GenerateEmptyTiles () {

		int rows = 0;
		int cols = 0;

		int.TryParse (rowsText.text, out rows);
		int.TryParse (colsText.text, out cols);

		map = new Tile[cols, rows];

		float startx = -(float)cols / 2f * emptyTile.transform.lossyScale.x;
		float starty = (float)rows / 2f * emptyTile.transform.lossyScale.y;

		for (int y = 0; y < rows; y++) {
			for (int x = 0; x < cols; x++) {

				map[x, y] = ((GameObject) Instantiate (emptyTile, new Vector2(startx + x * emptyTile.GetComponent<BoxCollider2D>().size.x,
					starty - y * emptyTile.GetComponent<BoxCollider2D>().size.x), Quaternion.identity)).GetComponent<Tile> ();

				map [x, y].x = x;
				map [x, y].y = y;
			}
		}
	}
}