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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. Cygnet::Color DefaultWorldGen::backgroundColor(Swan::Vec2 pos) {
  27. float y = pos.y;
  28. return Swan::Draw::linearGradient(y, {
  29. { 0, Cygnet::ByteColor{128, 220, 250}},
  30. { 70, Cygnet::ByteColor{107, 87, 5}},
  31. { 100, Cygnet::ByteColor{107, 87, 5}},
  32. { 200, Cygnet::ByteColor{ 20, 20, 23}},
  33. { 300, Cygnet::ByteColor{ 20, 20, 23}},
  34. { 500, Cygnet::ByteColor{ 25, 10, 10}},
  35. {1000, Cygnet::ByteColor{ 65, 10, 10}},
  36. });
  37. }
  38. Swan::Tile::ID DefaultWorldGen::genTile(Swan::TilePos pos) {
  39. int grassLevel = getGrassLevel(perlin_, pos.x);
  40. int stoneLevel = getStoneLevel(perlin_, pos.x);
  41. // Caves
  42. if (pos.y > grassLevel + 7 && perlin_.noise(pos.x / 43.37, pos.y / 16.37) > 0.2)
  43. return tAir_;
  44. if (pos.y > stoneLevel)
  45. return tStone_;
  46. else if (pos.y > grassLevel)
  47. return tDirt_;
  48. else if (pos.y == grassLevel)
  49. return tGrass_;
  50. else
  51. return tAir_;
  52. }
  53. void DefaultWorldGen::genChunk(Swan::WorldPlane &plane, Swan::Chunk &chunk) {
  54. for (int cx = 0; cx < Swan::CHUNK_WIDTH; ++cx) {
  55. int tilex = chunk.pos_.x * Swan::CHUNK_WIDTH + cx;
  56. for (int cy = 0; cy < Swan::CHUNK_HEIGHT; ++cy) {
  57. int tiley = chunk.pos_.y * Swan::CHUNK_HEIGHT + cy;
  58. Swan::TilePos pos(tilex, tiley);
  59. Swan::Chunk::RelPos rel(cx, cy);
  60. chunk.setTileData(rel, genTile(pos));
  61. }
  62. }
  63. }
  64. Swan::EntityRef DefaultWorldGen::spawnPlayer(const Swan::Context &ctx) {
  65. int x = 0;
  66. return ctx.plane.spawnEntity<PlayerEntity>(
  67. ctx, Swan::Vec2{ (float)x, (float)getGrassLevel(perlin_, x) - 4 });
  68. }