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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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> invalidTile = Tile::createInvalid(resources_);
  17. tilesMap_[invalidTile->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(invalidTile));
  21. // We're also going to need an air tile at location 1
  22. tiles_.push_back(Tile::createAir(resources_));
  23. tilesMap_["@::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. tilesMap_[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. worldgenFactories_.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. entCollFactories_.push_back(fact);
  59. }
  60. mods_.push_back(std::move(mod));
  61. }
  62. void World::setWorldGen(std::string gen) {
  63. defaultWorldGen_ = std::move(gen);
  64. }
  65. void World::spawnPlayer() {
  66. player_ = &((dynamic_cast<BodyTrait *>(
  67. planes_[currentPlane_]->spawnPlayer().get()))->get(BodyTrait::Tag{}));
  68. }
  69. void World::setCurrentPlane(WorldPlane &plane) {
  70. currentPlane_ = plane.id_;
  71. }
  72. WorldPlane &World::addPlane(const std::string &gen) {
  73. WorldPlane::ID id = planes_.size();
  74. auto it = worldgenFactories_.find(gen);
  75. if (it == worldgenFactories_.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(entCollFactories_.size());
  81. for (auto &fact: entCollFactories_) {
  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_.push_back(std::make_unique<WorldPlane>(
  87. id, this, std::move(g), std::move(colls)));
  88. return *planes_[id];
  89. }
  90. Item &World::getItem(const std::string &name) {
  91. auto iter = items_.find(name);
  92. if (iter == items_.end()) {
  93. warn << "Tried to get non-existant item " << name << "!";
  94. return *game_->invalidItem_;
  95. }
  96. return *iter->second;
  97. }
  98. Tile::ID World::getTileID(const std::string &name) {
  99. auto iter = tilesMap_.find(name);
  100. if (iter == tilesMap_.end()) {
  101. warn << "Tried to get non-existant item " << name << "!";
  102. return Tile::INVALID_ID;
  103. }
  104. return iter->second;
  105. }
  106. Tile &World::getTile(const std::string &name) {
  107. Tile::ID id = getTileID(name);
  108. return getTileByID(id);
  109. }
  110. SDL_Color World::backgroundColor() {
  111. return planes_[currentPlane_]->backgroundColor();
  112. }
  113. void World::draw(Win &win) {
  114. ZoneScopedN("World draw");
  115. win.cam_ = player_->pos - (win.getSize() / 2) + (player_->size / 2);
  116. planes_[currentPlane_]->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. chunkRenderer_.tick(
  128. *planes_[currentPlane_],
  129. ChunkPos((int)player_->pos.x / CHUNK_WIDTH, (int)player_->pos.y / CHUNK_HEIGHT));
  130. }
  131. }