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

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. if (pos.y > stone_level)
  12. return tStone_;
  13. else if (pos.y > grass_level)
  14. return tDirt_;
  15. else if (pos.y == grass_level)
  16. return tGrass_;
  17. else
  18. return tAir_;
  19. }
  20. void WGDefault::genChunk(Swan::WorldPlane &plane, Swan::Chunk &chunk) {
  21. for (int cx = 0; cx < Swan::CHUNK_WIDTH; ++cx) {
  22. int tilex = chunk.pos_.x * Swan::CHUNK_WIDTH + cx;
  23. for (int cy = 0; cy < Swan::CHUNK_HEIGHT; ++cy) {
  24. int tiley = chunk.pos_.y * Swan::CHUNK_HEIGHT + cy;
  25. Swan::TilePos pos(tilex, tiley);
  26. Swan::Chunk::RelPos rel(cx, cy);
  27. chunk.setTileID(rel, genTile(pos));
  28. }
  29. }
  30. }
  31. Swan::BodyTrait::HasBody *WGDefault::spawnPlayer(Swan::WorldPlane &plane) {
  32. int x = 0;
  33. return dynamic_cast<Swan::BodyTrait::HasBody *>(
  34. plane.spawnEntity("core::player", Swan::SRFFloatArray{
  35. (float)x, (float)grassLevel(perlin_, x) - 4 }));
  36. }