private void BuildArea() { int numAreas = _width * _height / 50; _mapAreas = new List <TDArea>(); int i = 1, counter = 0; while (i <= numAreas) { int areaSizeX = Random.Range(3, 5); int areaSizeY = Random.Range(3, 5); TDArea area = new TDArea(Random.Range(0, _width - areaSizeX), Random.Range(0, _height - areaSizeY), areaSizeX, areaSizeY); if (!AreaCollides(area)) { _mapAreas.Add(area); i++; MakeArea(area, Random.Range(1, 4)); } counter++; if (counter == 500) { break; // infinite loop safety } } }
private void MakeGrassArea(TDArea area) { for (int x = 0; x < area.width; x++) { for (int y = 0; y < area.height; y++) { _tiles[area.top + y, area.left + x].SetTileType((int)TILE_TYPE.GRASS); } } }
private bool AreaCollides(TDArea a) { foreach (TDArea b in _mapAreas) { if (a.CollidesWith(b)) { return(true); } } return(false); }
private void MakeWallArea(TDArea area) { for (int x = 0; x < area.width; x++) { for (int y = 0; y < area.height; y++) { //if (x == 0 || x == area.width - 1 || y == 0 || y == area.height - 1) // _tiles[area.top+y, area.left+x].SetTileType((int)TILE_TYPE.WALL); //else // _tiles[area.top+y, area.left+x].SetTileType((int)TILE_TYPE.GROUND); _tiles[area.top + y, area.left + x].SetTileType((int)TILE_TYPE.WALL); } } }
private void MakeArea(TDArea area, int random) { switch (random) { case (int)AREA_STYLE.WALL: MakeWallArea(area); break; case (int)AREA_STYLE.GRASS: MakeGrassArea(area); break; case (int)AREA_STYLE.WATER: MakeWaterArea(area); break; } }
public bool CollidesWith(TDArea other) { if (left > other.right + 1) { return(false); } if (top > other.bottom + 1) { return(false); } if (right < other.left - 1) { return(false); } if (bottom < other.top - 1) { return(false); } return(true); }