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.

World.cc 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include "World.h"
  2. #include "log.h"
  3. #include "Game.h"
  4. #include "Win.h"
  5. namespace Swan {
  6. static bool chunkLine(int l, WorldPlane &plane, ChunkPos &abspos, const Vec2i &dir) {
  7. for (int i = 0; i < l; ++i) {
  8. plane.getChunk(abspos);
  9. // TODO: Fix this with a non-SFML clock implementation
  10. // Don't blow our frame budget on generating chunks,
  11. // but generate as many as possible within the budget
  12. //if (clock.getElapsedTime().asSeconds() > 1.0 / 100)
  13. // return true;
  14. abspos += dir;
  15. }
  16. return false;
  17. }
  18. World::World(Game *game, unsigned long rand_seed):
  19. game_(game), random_(rand_seed), resources_(game->win_) {}
  20. void World::ChunkRenderer::tick(WorldPlane &plane, ChunkPos abspos) {
  21. int l = 0;
  22. for (int i = 0; i < 4; ++i) {
  23. if (chunkLine(l, plane, abspos, Vec2i(0, -1))) break;
  24. if (chunkLine(l, plane, abspos, Vec2i(1, 0))) break;
  25. l += 1;
  26. if (chunkLine(l, plane, abspos, Vec2i(0, 1))) break;
  27. if (chunkLine(l, plane, abspos, Vec2i(-1, 0))) break;
  28. l += 1;
  29. }
  30. }
  31. void World::addMod(std::unique_ptr<Mod> mod) {
  32. info << "World: adding mod " << mod->name_;
  33. for (auto t: mod->buildTiles(resources_)) {
  34. Tile::ID id = tiles_.size();
  35. tiles_map_[t->name_] = id;
  36. tiles_.push_back(std::move(t));
  37. }
  38. for (auto i: mod->buildItems(resources_)) {
  39. items_[i->name_] = std::move(i);
  40. }
  41. for (auto *gen: mod->getWorldGens()) {
  42. worldgens_[gen->name_] = gen;
  43. }
  44. for (auto *ent: mod->getEntities()) {
  45. ents_[ent->name_] = ent;
  46. }
  47. mods_.push_back(std::move(mod));
  48. }
  49. void World::setWorldGen(const std::string &gen) {
  50. default_world_gen_ = gen;
  51. }
  52. void World::spawnPlayer() {
  53. player_ = &planes_[current_plane_].spawnPlayer();
  54. }
  55. void World::setCurrentPlane(WorldPlane &plane) {
  56. current_plane_ = plane.id_;
  57. }
  58. WorldPlane &World::addPlane(const std::string &gen) {
  59. WorldPlane::ID id = planes_.size();
  60. auto it = worldgens_.find(gen);
  61. if (it == end(worldgens_)) {
  62. panic << "Tried to add plane with non-existant world gen " << gen << "!";
  63. abort();
  64. }
  65. WorldGen *g = it->second->create(*this);
  66. planes_.push_back(WorldPlane(id, this, std::shared_ptr<WorldGen>(g)));
  67. return planes_[id];
  68. }
  69. Item &World::getItem(const std::string &name) {
  70. auto iter = items_.find(name);
  71. if (iter == items_.end()) {
  72. warn << "Tried to get non-existant item " << name << "!";
  73. return *game_->invalid_item_;
  74. }
  75. return *iter->second;
  76. }
  77. Tile::ID World::getTileID(const std::string &name) {
  78. auto iter = tiles_map_.find(name);
  79. if (iter == tiles_map_.end()) {
  80. warn << "Tried to get non-existant item " << name << "!";
  81. return Tile::INVALID_ID;
  82. }
  83. return iter->second;
  84. }
  85. Tile &World::getTileByID(Tile::ID id) {
  86. return *tiles_[id];
  87. }
  88. Tile &World::getTile(const std::string &name) {
  89. Tile::ID id = getTileID(name);
  90. return getTileByID(id);
  91. }
  92. void World::draw(Win &win) {
  93. auto bounds = *player_->getBounds();
  94. win.cam_ = bounds.pos - (win.getSize() / 2) + (bounds.size / 2);
  95. planes_[current_plane_].draw(win);
  96. }
  97. void World::update(float dt) {
  98. for (auto &plane: planes_)
  99. plane.update(dt);
  100. }
  101. void World::tick(float dt) {
  102. for (auto &plane: planes_)
  103. plane.tick(dt);
  104. auto bounds = *player_->getBounds();
  105. chunk_renderer_.tick(
  106. planes_[current_plane_],
  107. ChunkPos((int)bounds.pos.x / CHUNK_WIDTH, (int)bounds.pos.y / CHUNK_HEIGHT));
  108. }
  109. }