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.

DefaultWorldGen.cc 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include "DefaultWorldGen.h"
  2. #include <algorithm>
  3. #include <cstdint>
  4. #include "entities/PlayerEntity.h"
  5. static int getGrassLevel(const siv::PerlinNoise &perlin, int x) {
  6. return (int)(perlin.noise(x / 50.0, 0) * 13);
  7. }
  8. static int getStoneLevel(const siv::PerlinNoise &perlin, int x) {
  9. return (int)(perlin.noise(x / 50.0, 10) * 10) + 10;
  10. }
  11. void DefaultWorldGen::drawBackground(
  12. const Swan::Context &ctx, Cygnet::Renderer &rnd, Swan::Vec2 pos) {
  13. int texmin = 10;
  14. //int texmax = 20;
  15. if (pos.y > texmin) {
  16. /*
  17. SDL_Texture *tex = bgCave_.texture_.get();
  18. Uint8 alpha = std::clamp(
  19. (pos.y - texmin) / (texmax - texmin), 0.0f, 1.0f) * 255;
  20. Swan::TexAlphaMod amod(tex, alpha);
  21. Swan::Draw::parallaxBackground(
  22. win, tex, std::nullopt, std::nullopt,
  23. pos.x * Swan::TILE_SIZE, pos.y * Swan::TILE_SIZE, 0.7);
  24. TODO */
  25. }
  26. }
  27. Cygnet::Color DefaultWorldGen::backgroundColor(Swan::Vec2 pos) {
  28. float y = pos.y;
  29. return Swan::Draw::linearGradient(y, {
  30. { 0, Cygnet::ByteColor{128, 220, 250}},
  31. { 70, Cygnet::ByteColor{107, 87, 5}},
  32. { 100, Cygnet::ByteColor{107, 87, 5}},
  33. { 200, Cygnet::ByteColor{ 20, 20, 23}},
  34. { 300, Cygnet::ByteColor{ 20, 20, 23}},
  35. { 500, Cygnet::ByteColor{ 25, 10, 10}},
  36. {1000, Cygnet::ByteColor{ 65, 10, 10}},
  37. });
  38. }
  39. Swan::Tile::ID DefaultWorldGen::genTile(Swan::TilePos pos) {
  40. int grassLevel = getGrassLevel(perlin_, pos.x);
  41. int stoneLevel = getStoneLevel(perlin_, pos.x);
  42. // Caves
  43. if (pos.y > grassLevel + 7 && perlin_.noise(pos.x / 43.37, pos.y / 16.37) > 0.2)
  44. return tAir_;
  45. // Trees
  46. if (pos.y == grassLevel - 1) {
  47. constexpr int treeProb = 4;
  48. bool spawnTree = Swan::random(pos.x) % treeProb == 0;
  49. if (spawnTree) {
  50. // Avoid trees which are too close
  51. for (int rx = 1; rx <= 5; ++rx) {
  52. if (Swan::random(pos.x + rx) % treeProb == 0) {
  53. spawnTree = false;
  54. break;
  55. }
  56. }
  57. if (spawnTree) {
  58. return tTreeSeeder_;
  59. }
  60. }
  61. }
  62. if (pos.y > stoneLevel)
  63. return tStone_;
  64. else if (pos.y > grassLevel)
  65. return tDirt_;
  66. else if (pos.y == grassLevel)
  67. return tGrass_;
  68. else
  69. return tAir_;
  70. }
  71. void DefaultWorldGen::genChunk(Swan::WorldPlane &plane, Swan::Chunk &chunk) {
  72. for (int cx = 0; cx < Swan::CHUNK_WIDTH; ++cx) {
  73. int tilex = chunk.pos_.x * Swan::CHUNK_WIDTH + cx;
  74. for (int cy = 0; cy < Swan::CHUNK_HEIGHT; ++cy) {
  75. int tiley = chunk.pos_.y * Swan::CHUNK_HEIGHT + cy;
  76. Swan::TilePos pos(tilex, tiley);
  77. Swan::Chunk::RelPos rel(cx, cy);
  78. chunk.setTileData(rel, genTile(pos));
  79. }
  80. }
  81. }
  82. Swan::EntityRef DefaultWorldGen::spawnPlayer(const Swan::Context &ctx) {
  83. int x = 0;
  84. return ctx.plane.spawnEntity<PlayerEntity>(
  85. ctx, Swan::Vec2{ (float)x, (float)getGrassLevel(perlin_, x) - 4 });
  86. }