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 2.1KB

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