// Use this for initialization void Start() { //Initializes the grid grid = new gTile[100, 80]; for (int i = 0; i < 100; i++) { for (int j = 0; j < 80; j++) { grid[i, j] = new gTile(true, false); } } //initializes the textures baseTex = new Texture2D(200, 160, TextureFormat.ARGB32, false); currentTex = new Texture2D(200, 160, TextureFormat.ARGB32, false); Color[] Colors = baseTex.GetPixels(); Color C = new Color(0.161f, .407f, 0f); for (int i = 0; i < Colors.Length; i++) { Colors[i] = C; } baseTex.SetPixels(Colors); baseTex.Apply(); update = true; visible(false); Dimensions = new Vector2(grid.GetLength(0), grid.GetLength(1)); }
//clears the grid back to empty void clearGrid() { for (int i = 0; i < Dimensions.x; i++) { for (int j = 0; j < Dimensions.y; j++) { grid[i, j] = new gTile(true, true); } } }
//function ot update teh grid void updateGrid() { //resets the grid texture to be green. currentTex.SetPixels(baseTex.GetPixels()); currentTex.Apply(); //Iterates through the grid objects foreach (Transform child in transform) { //skips the object if it isn't active if (!child.gameObject.activeSelf) { continue; } //gets the objects dimensions and grid coordinates float x, y; Vector2 pos = convertCoordinates(child.transform.position); x = child.GetComponent <ObjectController>().length; y = child.GetComponent <ObjectController>().width; //checks the objects rotation and updates the grid and texture pixels based on it. if ((int)child.transform.rotation.eulerAngles.y == 0) { if (pos.x > Dimensions.x - 1 - (y - 1) || pos.x < 0 || pos.y > Dimensions.y - 1 - (x - 1) || pos.y < 0) { continue; } for (int i = (int)pos.x; i < (int)pos.x + (int)y; i++) { for (int j = (int)pos.y; j < (int)pos.y + (int)x; j++) { if (child.GetComponent <ObjectController>().placed) { grid[i, j] = new gTile(false, child.GetComponent <ObjectController>().walkable); } currentTex.SetPixel(2 * i, 2 * j, Color.red); currentTex.SetPixel(2 * i + 1, 2 * j, Color.red); currentTex.SetPixel(2 * i, 2 * j + 1, Color.red); currentTex.SetPixel(2 * i + 1, 2 * j + 1, Color.red); } } } else if ((int)child.transform.rotation.eulerAngles.y == 90) { if (pos.x > Dimensions.x - 1 - (x - 1) || pos.x < 0 || pos.y > 80 || pos.y < y) { continue; } for (int i = (int)pos.x; i < (int)pos.x + (int)x; i++) { for (int j = (int)pos.y - 1; j > (int)pos.y - (int)y - 1; j--) { if (child.GetComponent <ObjectController>().placed) { grid[i, j] = new gTile(false, child.GetComponent <ObjectController>().walkable); } currentTex.SetPixel(2 * i, 2 * j, Color.red); currentTex.SetPixel(2 * i - 1, 2 * j, Color.red); currentTex.SetPixel(2 * i, 2 * j - 1, Color.red); currentTex.SetPixel(2 * i - 1, 2 * j - 1, Color.red); } } } else if ((int)child.transform.rotation.eulerAngles.y == 180) { if (pos.x > Dimensions.x || pos.x < 0 + y || pos.y > Dimensions.y || pos.y < 0 + x) { continue; } for (int i = (int)pos.x - 1; i > (int)pos.x - 1 - (int)y; i--) { for (int j = (int)pos.y - 1; j > (int)pos.y - 1 - (int)x; j--) { if (child.GetComponent <ObjectController>().placed) { grid[i, j] = new gTile(false, child.GetComponent <ObjectController>().walkable); } currentTex.SetPixel(2 * i, 2 * j, Color.red); currentTex.SetPixel(2 * i + 1, 2 * j, Color.red); currentTex.SetPixel(2 * i, 2 * j + 1, Color.red); currentTex.SetPixel(2 * i + 1, 2 * j + 1, Color.red); } } } else if ((int)child.transform.rotation.eulerAngles.y == 270) { if (pos.x > Dimensions.x || pos.x < (x) || pos.y > Dimensions.y - y || pos.y < 0) { continue; } for (int i = (int)pos.x - (int)x; i < pos.x; i++) { for (int j = (int)pos.y; j < (int)pos.y + (int)y; j++) { if (child.GetComponent <ObjectController>().placed) { grid[i, j] = new gTile(false, child.GetComponent <ObjectController>().walkable); } currentTex.SetPixel(2 * i, 2 * j, Color.red); currentTex.SetPixel(2 * i + 1, 2 * j, Color.red); currentTex.SetPixel(2 * i, 2 * j + 1, Color.red); currentTex.SetPixel(2 * i + 1, 2 * j + 1, Color.red); } } } //applies the texture currentTex.Apply(); } }