A 2D tile-based sandbox game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WGDefault.cc 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include "WGDefault.h"
  2. static int grassLevel(const siv::PerlinNoise &perlin, int x) {
  3. return (int)(perlin.noise(x / 50.0, 0) * 13);
  4. }
  5. static int stoneLevel(const siv::PerlinNoise &perlin, int x) {
  6. return (int)(perlin.noise(x / 50.0, 10) * 10) + 10;
  7. }
  8. Swan::Tile::ID WGDefault::genTile(Swan::TilePos pos) {
  9. int grass_level = grassLevel(perlin_, pos.x);
  10. int stone_level = stoneLevel(perlin_, pos.x);
  11. // Caves
  12. if (pos.y > grass_level + 7 && perlin_.noise(pos.x / 43.37, pos.y / 16.37) > 0.2)
  13. return tAir_;
  14. if (pos.y > stone_level)
  15. return tStone_;
  16. else if (pos.y > grass_level)
  17. return tDirt_;
  18. else if (pos.y == grass_level)
  19. return tGrass_;
  20. else
  21. return tAir_;
  22. }
  23. void WGDefault::genChunk(Swan::WorldPlane &plane, Swan::Chunk &chunk) {
  24. for (int cx = 0; cx < Swan::CHUNK_WIDTH; ++cx) {
  25. int tilex = chunk.pos_.x * Swan::CHUNK_WIDTH + cx;
  26. for (int cy = 0; cy < Swan::CHUNK_HEIGHT; ++cy) {
  27. int tiley = chunk.pos_.y * Swan::CHUNK_HEIGHT + cy;
  28. Swan::TilePos pos(tilex, tiley);
  29. Swan::Chunk::RelPos rel(cx, cy);
  30. chunk.setTileData(rel, genTile(pos));
  31. }
  32. }
  33. }
  34. Swan::BodyTrait::HasBody *WGDefault::spawnPlayer(Swan::WorldPlane &plane) {
  35. int x = 0;
  36. return dynamic_cast<Swan::BodyTrait::HasBody *>(
  37. plane.spawnEntity("core::player", Swan::SRFFloatArray{
  38. (float)x, (float)grassLevel(perlin_, x) - 4 }));
  39. }