using System; using UnityEngine; public class Stage { Tile[,] Tiles { get; set; } Wall[,] HorizontalWalls { get; set; } Wall[,] VerticalWalls { get; set; } public int Width { get; private set; } public int Height { get; private set; } public Action OnTileChanged { get; set; } public Action OnWallChanged { get; set; } public Stage(int width = 20, int height = 8) { Resize(width, height); Debug.Log($"Stage created with {Tiles.Length} tiles."); } public void Resize(int width, int heigh) { if (Tiles != null) throw new NotImplementedException(); Width = width; Height = heigh; Tiles = new Tile[Width, Height]; for(int x = 0; x < Width; x++) for(int y = 0; y < Height; y++) Tiles[x, y] = new Tile(this, x, y); HorizontalWalls = new Wall[Width, Height+1]; for (int x = 0; x < Width; x++) for (int y = 0; y <= Height; y++) HorizontalWalls[x, y] = new Wall(this, x, y, WallOrientation.Horizontal); VerticalWalls = new Wall[Width + 1, Height]; for (int x = 0; x <= Width; x++) for (int y = 0; y < Height; y++) VerticalWalls[x, y] = new Wall(this, x, y, WallOrientation.Vertical); } public bool HasTile(int x, int y) { return !(x < 0 || y < 0 || x >= Width || y >= Height); //TODO: implement better } public bool HasWall(WallOrientation orientation, int x, int y) { if(orientation == WallOrientation.Horizontal) return (x >= 0 && y >= 0 && x < Width && y <= Height); if(orientation == WallOrientation.Vertical) return (x >= 0 && y >= 0 && x <= Width && y < Height); throw new NotImplementedException($"Unknown orientation {orientation}."); } public bool IsBorderWall(WallOrientation orientation, int x, int y) { if(!HasWall(orientation, x, y)) return false; if (orientation == WallOrientation.Horizontal) return (y == 0 || y == Height); if (orientation == WallOrientation.Vertical) return (x == 0 || x == Width); throw new NotImplementedException($"Unknown orientation {orientation}."); } public Tile GetTileAt(int x, int y) { if (!HasTile(x, y)) throw new Exception($"Trying to get tile {x},{y} from world({Width},{Height})."); var tile = Tiles[x, y]; return tile; } public Wall GetWallAt(WallOrientation orientation, int x, int y) { if (!HasWall(orientation, x, y)) throw new Exception($"Trying to get wall {x},{y} from world({Width},{Height})."); if(orientation == WallOrientation.Horizontal) return HorizontalWalls[x, y]; if (orientation == WallOrientation.Vertical) return VerticalWalls[x, y]; throw new System.NotImplementedException(); } /*public void SetTile(int x, int y, Tile tile) { if(tile == null) throw new ArgumentNullException(tile.GetType().Name); if (!HasTile(x, y)) throw new Exception($"Trying to get tile {x},{y} from world({Width},{Height})."); Tiles[x, y] = tile; }*/ public Tile this[int x, int y] { get { return GetTileAt(x, y); } //set { SetTile(x, y, value); } } public Wall this[WallOrientation orientation, int x, int y] { get { return GetWallAt(orientation, x, y); } } public void Randomize() { for (int x = 0; x <= Width; x++) for (int y = 0; y <= Height; y++) { if(HasTile(x, y)) { if (UnityEngine.Random.Range(0, 2) == 0) this[x, y].Type = TileType.Floor; else this[x, y].Type = TileType.Empty; } if (HasWall(WallOrientation.Horizontal, x, y)) { if (UnityEngine.Random.Range(0, 2) == 1) this[WallOrientation.Horizontal, x, y].Type = WallType.Full; else this[WallOrientation.Horizontal, x, y].Type = WallType.Empty; } if (HasWall(WallOrientation.Vertical, x, y)) { if (UnityEngine.Random.Range(0, 2) == 1) this[WallOrientation.Vertical, x, y].Type = WallType.Full; else this[WallOrientation.Vertical, x, y].Type = WallType.Empty; } } Debug.Log($"Randomizing a stage..."); } public void Populate() { for (int x = 0; x < Width; x++) for (int y = 0; y < Height; y++) this[x, y].Type = TileType.Floor; for (int x = 0; x <= Width; x++) for (int y = 0; y <= Height; y++) { if(IsBorderWall(WallOrientation.Horizontal, x, y)) this[WallOrientation.Horizontal, x, y].Type = WallType.Full; if (IsBorderWall(WallOrientation.Vertical, x, y)) this[WallOrientation.Vertical, x, y].Type = WallType.Full; } } }