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.

tree.cc 841B

12345678910111213141516171819202122232425262728293031
  1. #include "tree.h"
  2. #include <algorithm>
  3. void spawnTree(const Swan::Context &ctx, Swan::TilePos pos) {
  4. Swan::Tile::ID logID = ctx.world.getTileID("core::tree-trunk");
  5. Swan::Tile::ID leavesID = ctx.world.getTileID("core::tree-leaves");
  6. int height = 4 + Swan::random(pos.x) % 3;
  7. for (int y = pos.y; y >= pos.y - height; --y) {
  8. ctx.plane.setTileID({pos.x, y}, logID);
  9. }
  10. int radius = 2 + Swan::random(pos.x) % 2;
  11. int radius2 = radius * radius;
  12. Swan::TilePos top = pos - Swan::Vec2i{0, height};
  13. for (int ry = -radius; ry <= radius + 2; ++ry) {
  14. for (int rx = -radius; rx <= radius; ++rx) {
  15. if (rx == 0 && ry <= -radius / 2) {
  16. continue;
  17. }
  18. int d2 = std::max(
  19. ry * ry + rx * rx,
  20. (ry + 1) * (ry + 1) + rx * rx);
  21. if (d2 <= radius2) {
  22. ctx.plane.setTileID(top - Swan::Vec2i{rx, ry}, leavesID);
  23. }
  24. }
  25. }
  26. }