Skip to content

Instantly share code, notes, and snippets.

@stevetranby
Created January 17, 2018 04:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevetranby/b469e50116a6c8d0b5dfec52215e5d05 to your computer and use it in GitHub Desktop.
Save stevetranby/b469e50116a6c8d0b5dfec52215e5d05 to your computer and use it in GitHub Desktop.
Map Generation using setTileGID on TMXLayer-derived class of a TMXTiledMap-derived class.
// ... partial code ...
void SCMap::generateBaseMap(uint32_t seed, const PlanetGeneratorBiome& planetInfo)
{
int w = int(getMapSize().width);
int h = int(getMapSize().height);
// RANDOM stuff
PerlinNoise walkNoise(seed); // generator randomness
PerlinNoise floorNoise(seed + 1); // floor tile randomness
PerlinNoise objectNoise(seed + 2); // object randomness
double walkThreshold = 0.45;
// .5 .6 .5 = .1 * 5
double smooth = 6.0 * (double) (w + h) / 90.0;
// /// needs to be in range [0,1)
// auto headsOrTails = [](double r) { return (r > 0.5); };
//
// /// convert a double of range [0,1) into int range [min,max],
// auto choiceRange = [](double r, int min, int max) {
// auto base = min;
// auto val = base + (int)(round(r) * (max - min));
// return val;
// //return ((int)(r * (max + 1)) - min);
// };
// generate the map
auto metaLayer = getMetaLayer();
auto floorLayer = getFloorLayer();
auto wallLayer = getWallLayer();
int firstGidMeta = metaLayer->getTileSet()->_firstGid;
int firstGidFloor = floorLayer->getTileSet()->_firstGid;
int firstGidWall = wallLayer->getTileSet()->_firstGid;
// generate the collision map
/// why are walls = 1??
nextFloodIndex = 2;
const int tilesetCols = 16;
for (int r = 0; r < h; ++r)
{
for (int c = 0; c < w; ++c)
{
iVec2 tile(c, r);
auto tileIndex = r * w + c;
if (r == 0 || c == 0 || r == h - 1 || c == w - 1)
{
// collision
metaLayer->setTileGID(0, tile);
const auto& offset = planetInfo.wallOffset;
const auto& count = planetInfo.wallCount;
// TODO: refactor this stuff out (struct the offset,count,etc together)
if(count > 0) {
auto gid = firstGidWall + (offset.y * tilesetCols + offset.x) + STRandom(count);
wallLayer->setTileGID(gid, tile);
// edges should be tall
_walkMap[tileIndex] = TILE_WALL_TALL;
_fillMap[tileIndex] = TILE_WALL_TALL;
}
continue;
}
double p1 = walkNoise.noise(double(c) / smooth, double(r) / smooth, smooth);
if (p1 > walkThreshold)
{
// walkable ground
auto& offset = planetInfo.groundOffset;
auto& count = planetInfo.groundCount;
if(count > 0) {
auto gid = firstGidFloor + (offset.y * tilesetCols + offset.x) + STRandom(count);
floorLayer->setTileGID(gid, tile);
// walkable
metaLayer->setTileGID(firstGidMeta + kMetaWalkGID - 1, tile);
_walkMap[tileIndex] = TILE_WALK;
}
}
else if (p1 > (walkThreshold - 0.075))
{
// non-walkable fringe ground
auto& offset = planetInfo.nonwalkGroundOffset;
auto& count = planetInfo.nonwalkGroundCount;
if(count > 0) {
auto gid = firstGidFloor + (offset.y * tilesetCols + offset.x) + STRandom(count);
floorLayer->setTileGID(gid, tile);
// collision
metaLayer->setTileGID(0, tile);
// fringe is short
_walkMap[tileIndex] = TILE_WALL_SHORT;
_fillMap[tileIndex] = TILE_WALL_SHORT;
}
}
else
{
// non-walkable areas
if (STRandomChance(planetInfo.wallChance))
{
if (STRandomChance(planetInfo.rockChance)) {
const auto& offset = planetInfo.rockOffset;
const auto& count = planetInfo.rockCount;
if(count > 0) {
auto gid = firstGidWall + (offset.y * tilesetCols + offset.x) + STRandom(count);
wallLayer->setTileGID(gid, tile);
_walkMap[tileIndex] = TILE_WALL_SHORT;
_fillMap[tileIndex] = TILE_WALL_SHORT;
}
} else {
const auto& offset = planetInfo.wallOffset;
const auto& count = planetInfo.wallCount;
if(count > 0) {
auto gid = firstGidWall + (offset.y * tilesetCols + offset.x) + STRandom(count);
wallLayer->setTileGID(gid, tile);
_walkMap[tileIndex] = TILE_WALL_TALL;
_fillMap[tileIndex] = TILE_WALL_TALL;
}
}
}
else
{
// non-walkable fringe ground
auto& offset = planetInfo.nonwalkGroundOffset;
auto& count = planetInfo.nonwalkGroundCount;
auto gid = firstGidFloor + (offset.y * tilesetCols + offset.x) + STRandom(count);
floorLayer->setTileGID(gid, tile);
_walkMap[tileIndex] = TILE_WALL_SHORT;
_fillMap[tileIndex] = TILE_WALL_SHORT;
}
// collision
metaLayer->setTileGID(0, tile);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment