Created
July 1, 2013 21:04
-
-
Save AtkinsSJ/5904617 to your computer and use it in GitHub Desktop.
The map generation code from Beard of Bees.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package uk.co.samatkins.beebeard; | |
import java.util.List; | |
import com.badlogic.gdx.math.MathUtils; | |
import com.badlogic.gdx.math.Rectangle; | |
import uk.co.samatkins.Tilemap; | |
import uk.co.samatkins.beebeard.PlayScene.Tile; | |
public class MapGenerator { | |
private PlayScene playScene; | |
private Tilemap tilemap; | |
private List<Rectangle> solids; | |
private static final float beehiveChance = 0.2f; | |
public MapGenerator(PlayScene playScene) { | |
this.playScene = playScene; | |
} | |
/** | |
* | |
*/ | |
public void generate() { | |
playScene.clearMap(); | |
tilemap = playScene.getTilemap(); | |
solids = playScene.getSolids(); | |
int tilesAcross = tilemap.getTilesAcross(), | |
tilesUp = tilemap.getTilesDown(); | |
// Fill with sky | |
tilemap.setTileRect(0, tilesAcross-1, 0, tilesUp-1, Tile.SKY.getIndex()); | |
// Add ground | |
tilemap.setTileRect(0, tilesAcross-1, 0, 0, Tile.DIRT.getIndex()); | |
tilemap.setSolidRect(0, tilesAcross-1, 0, 0, true); | |
solids.add(new Rectangle(0,0, tilesAcross*PlayScene.tileWidth, PlayScene.tileHeight )); | |
// Grass | |
tilemap.setTileRect(0, tilesAcross-1, 1, 1, Tile.GRASS.getIndex()); | |
for (int x=3; x<tilesAcross-3; x+=5) { | |
placeTree(x); | |
} | |
} | |
/** | |
* Generate a tree with the trunk on the Xth tile across. | |
* @param x | |
*/ | |
private void placeTree(int x) { | |
int rootLevel = 1, | |
treeHeight = MathUtils.random(5, tilemap.getTilesDown()-3), | |
canopyHeight = (treeHeight < 6) ? 2 : 3, | |
canopyWidth = (canopyHeight == 2) ? 3 : 5, | |
trunkHeight = treeHeight - canopyHeight; | |
// Roots | |
tilemap.setTile(x, rootLevel, Tile.ROOT.getIndex()); | |
tilemap.setTile(x-1, rootLevel, Tile.ROOT_LEFT.getIndex()); | |
tilemap.setTile(x+1, rootLevel, Tile.ROOT_RIGHT.getIndex()); | |
// Trunk | |
tilemap.setTileRect(x, x, rootLevel+1, rootLevel+trunkHeight, Tile.TRUNK.getIndex()); | |
// Canopy | |
int canopyLeft = x - (canopyWidth-1)/2, | |
canopyRight = canopyLeft + canopyWidth -1, | |
canopyBottom = rootLevel+trunkHeight, | |
canopyTop = canopyBottom + canopyHeight-1; | |
// Corners | |
tilemap.setTile(canopyLeft, canopyBottom, Tile.CANOPY_BOTTOM_LEFT.getIndex()); | |
tilemap.setTile(canopyRight, canopyBottom, Tile.CANOPY_BOTTOM_RIGHT.getIndex()); | |
tilemap.setTile(canopyLeft, canopyTop, Tile.CANOPY_TOP_LEFT.getIndex()); | |
tilemap.setTile(canopyRight, canopyTop, Tile.CANOPY_TOP_RIGHT.getIndex()); | |
// Canopy centre | |
tilemap.setTileRect( | |
canopyLeft+1, canopyRight-1, | |
canopyBottom+1, canopyTop-1, | |
Tile.CANOPY.getIndex() | |
); | |
// Top & sides | |
tilemap.setTileRect(canopyLeft+1, canopyRight-1, | |
canopyTop, canopyTop, Tile.CANOPY_TOP.getIndex()); | |
tilemap.setTileRect(canopyLeft, canopyLeft, | |
canopyBottom+1, canopyTop-1, Tile.CANOPY_LEFT.getIndex()); | |
tilemap.setTileRect(canopyRight, canopyRight, | |
canopyBottom+1, canopyTop-1, Tile.CANOPY_RIGHT.getIndex()); | |
// Bottom | |
tilemap.setTileRect(canopyLeft+1, canopyRight-1, | |
canopyBottom, canopyBottom, Tile.CANOPY_BOTTOM.getIndex()); | |
tilemap.setTile(x, canopyBottom, Tile.TRUNK_CANOPY.getIndex()); | |
// Branches! | |
boolean left = MathUtils.randomBoolean(); | |
int branchLength = (canopyWidth-1)/2; | |
for (int i=0; i<trunkHeight-1; i+=2) { | |
int y = i + 2; | |
if (left) { | |
tilemap.setTile(x-branchLength, y, Tile.BRANCH_TIP_LEFT.getIndex()); | |
tilemap.setTile(x, y, Tile.TRUNK_BRANCH_LEFT.getIndex()); | |
tilemap.setTileRect(x-branchLength+1, x-1, y, y, Tile.BRANCH.getIndex()); | |
if (Math.random() < beehiveChance) { | |
playScene.addBeehive(x-branchLength+1, y); | |
} | |
solids.add(new Rectangle( | |
(x-branchLength)*PlayScene.tileWidth, y*PlayScene.tileHeight, | |
branchLength * PlayScene.tileWidth, PlayScene.tileHeight | |
)); | |
} else { | |
tilemap.setTile(x+branchLength, y, Tile.BRANCH_TIP_RIGHT.getIndex()); | |
tilemap.setTile(x, y, Tile.TRUNK_BRANCH_RIGHT.getIndex()); | |
tilemap.setTileRect(x+branchLength-1, x+1, y, y, Tile.BRANCH.getIndex()); | |
if (Math.random() < beehiveChance) { | |
playScene.addBeehive(x+branchLength-1, y); | |
} | |
solids.add(new Rectangle( | |
(x+1)*PlayScene.tileWidth, y*PlayScene.tileHeight, | |
branchLength * PlayScene.tileWidth, PlayScene.tileHeight | |
)); | |
} | |
left = !left; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment