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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include "World.h"
  2. #include <algorithm>
  3. #include "log.h"
  4. #include "Game.h"
  5. #include "Win.h"
  6. #include "Clock.h"
  7. namespace Swan {
  8. static void chunkLine(int l, WorldPlane &plane, ChunkPos &abspos, const Vec2i &dir) {
  9. for (int i = 0; i < l; ++i) {
  10. plane.slowGetChunk(abspos).keepActive();
  11. abspos += dir;
  12. }
  13. }
  14. World::World(Game *game, unsigned long rand_seed):
  15. game_(game), random_(rand_seed), resources_(game->win_) {
  16. std::unique_ptr<Tile> invalid_tile = Tile::createInvalid(resources_);
  17. tiles_map_[invalid_tile->name_] = 0;
  18. // tiles_ is empty, so pushing back now will ensure invalid_tile
  19. // ends up at location 0
  20. tiles_.push_back(std::move(invalid_tile));
  21. // We're also going to need an air tile at location 1
  22. tiles_.push_back(Tile::createAir(resources_));
  23. tiles_map_["@::air"] = 1;
  24. }
  25. void World::ChunkRenderer::tick(WorldPlane &plane, ChunkPos abspos) {
  26. ZoneScopedN("World::ChunkRenderer tick");
  27. int l = 0;
  28. RTClock clock;
  29. for (int i = 0; i < 4; ++i) {
  30. chunkLine(l, plane, abspos, Vec2i(0, -1));
  31. chunkLine(l, plane, abspos, Vec2i(1, 0));
  32. l += 1;
  33. chunkLine(l, plane, abspos, Vec2i(0, 1));
  34. chunkLine(l, plane, abspos, Vec2i(-1, 0));
  35. l += 1;
  36. }
  37. }
  38. void World::addMod(ModWrapper &&mod) {
  39. info << "World: adding mod " << mod.mod_->name_;
  40. for (auto i: mod.buildImages(game_->win_.renderer_)) {
  41. resources_.addImage(std::move(i));
  42. }
  43. for (auto t: mod.buildTiles(resources_)) {
  44. Tile::ID id = tiles_.size();
  45. tiles_map_[t->name_] = id;
  46. tiles_.push_back(std::move(t));
  47. }
  48. for (auto i: mod.buildItems(resources_)) {
  49. items_[i->name_] = std::move(i);
  50. }
  51. for (auto fact: mod.getWorldGens()) {
  52. worldgen_factories_.emplace(
  53. std::piecewise_construct,
  54. std::forward_as_tuple(fact.name),
  55. std::forward_as_tuple(fact));
  56. }
  57. for (auto fact: mod.getEntities()) {
  58. ent_coll_factories_.push_back(fact);
  59. }
  60. mods_.push_back(std::move(mod));
  61. }
  62. void World::setWorldGen(std::string gen) {
  63. default_world_gen_ = std::move(gen);
  64. }
  65. void World::spawnPlayer() {
  66. player_ = dynamic_cast<BodyTrait::HasBody *>(
  67. planes_[current_plane_].spawnPlayer().get());
  68. }
  69. void World::setCurrentPlane(WorldPlane &plane) {
  70. current_plane_ = plane.id_;
  71. }
  72. WorldPlane &World::addPlane(const std::string &gen) {
  73. WorldPlane::ID id = planes_.size();
  74. auto it = worldgen_factories_.find(gen);
  75. if (it == worldgen_factories_.end()) {
  76. panic << "Tried to add plane with non-existant world gen " << gen << "!";
  77. abort();
  78. }
  79. std::vector<std::unique_ptr<EntityCollection>> colls;
  80. colls.reserve(ent_coll_factories_.size());
  81. for (auto &fact: ent_coll_factories_) {
  82. colls.emplace_back(fact.create(fact.name));
  83. }
  84. WorldGen::Factory &factory = it->second;
  85. std::unique_ptr<WorldGen> g = factory.create(*this);
  86. planes_.emplace_back(id, this, std::move(g), std::move(colls));
  87. return planes_[id];
  88. }
  89. Item &World::getItem(const std::string &name) {
  90. auto iter = items_.find(name);
  91. if (iter == items_.end()) {
  92. warn << "Tried to get non-existant item " << name << "!";
  93. return *game_->invalid_item_;
  94. }
  95. return *iter->second;
  96. }
  97. Tile::ID World::getTileID(const std::string &name) {
  98. auto iter = tiles_map_.find(name);
  99. if (iter == tiles_map_.end()) {
  100. warn << "Tried to get non-existant item " << name << "!";
  101. return Tile::INVALID_ID;
  102. }
  103. return iter->second;
  104. }
  105. Tile &World::getTile(const std::string &name) {
  106. Tile::ID id = getTileID(name);
  107. return getTileByID(id);
  108. }
  109. SDL_Color World::backgroundColor() {
  110. return planes_[current_plane_].backgroundColor();
  111. }
  112. void World::draw(Win &win) {
  113. ZoneScopedN("World draw");
  114. auto bounds = player_->getBody().getBounds();
  115. win.cam_ = bounds.pos - (win.getSize() / 2) + (bounds.size / 2);
  116. planes_[current_plane_].draw(win);
  117. }
  118. void World::update(float dt) {
  119. ZoneScopedN("World update");
  120. for (auto &plane: planes_)
  121. plane.update(dt);
  122. }
  123. void World::tick(float dt) {
  124. ZoneScopedN("World tick");
  125. for (auto &plane: planes_)
  126. plane.tick(dt);
  127. auto bounds = player_->getBody().getBounds();
  128. chunk_renderer_.tick(
  129. planes_[current_plane_],
  130. ChunkPos((int)bounds.pos.x / CHUNK_WIDTH, (int)bounds.pos.y / CHUNK_HEIGHT));
  131. }
  132. }