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.2KB

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