A 2D tile-based sandbox game.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

WGDefault.cc 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include "WGDefault.h"
  2. static int getHeightAt(const siv::PerlinNoise &perlin, int tilex) {
  3. return (int)(perlin.noise(tilex / 50.0, 0) * 13);
  4. }
  5. static int getDepthAt(const siv::PerlinNoise &perlin, int tilex) {
  6. return (int)(perlin.noise(tilex / 50.0, 10) * 10) + 10;
  7. }
  8. void WGDefault::genChunk(Swan::WorldPlane &plane, Swan::Chunk &chunk) {
  9. for (int cx = 0; cx < Swan::CHUNK_WIDTH; ++cx) {
  10. int tilex = chunk.pos_.x * Swan::CHUNK_WIDTH + cx;
  11. int height = getHeightAt(perlin_, tilex);
  12. int depth = getDepthAt(perlin_, tilex);
  13. if (depth <= height) depth = height + 1;
  14. for (int cy = 0; cy < Swan::CHUNK_HEIGHT; ++cy) {
  15. int tiley = chunk.pos_.y * Swan::CHUNK_HEIGHT + cy;
  16. Swan::TilePos tpos = Swan::TilePos(tilex, tiley);
  17. if (tpos.y == height)
  18. chunk.setTileID(Swan::TilePos(cx, cy), tGrass_);
  19. else if (tpos.y > height && tpos.y <= depth)
  20. chunk.setTileID(Swan::TilePos(cx, cy), tDirt_);
  21. else if (tpos.y > depth)
  22. chunk.setTileID(Swan::TilePos(cx, cy), tStone_);
  23. else
  24. chunk.setTileID(Swan::TilePos(cx, cy), tAir_);
  25. }
  26. }
  27. }
  28. Swan::BodyTrait::HasBody *WGDefault::spawnPlayer(Swan::WorldPlane &plane) {
  29. int x = 0;
  30. return dynamic_cast<Swan::BodyTrait::HasBody *>(
  31. plane.spawnEntity("core::player", Swan::SRFFloatArray{ (float)x, (float)getHeightAt(perlin_, x) - 4 }));
  32. }