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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. int l = 0;
  27. RTClock clock;
  28. for (int i = 0; i < 4; ++i) {
  29. chunkLine(l, plane, abspos, Vec2i(0, -1));
  30. chunkLine(l, plane, abspos, Vec2i(1, 0));
  31. l += 1;
  32. chunkLine(l, plane, abspos, Vec2i(0, 1));
  33. chunkLine(l, plane, abspos, Vec2i(-1, 0));
  34. l += 1;
  35. }
  36. }
  37. void World::addMod(ModWrapper &&mod) {
  38. info << "World: adding mod " << mod.mod_->name_;
  39. for (auto i: mod.buildImages(game_->win_.renderer_)) {
  40. resources_.addImage(std::move(i));
  41. }
  42. for (auto t: mod.buildTiles(resources_)) {
  43. Tile::ID id = tiles_.size();
  44. tiles_map_[t->name_] = id;
  45. tiles_.push_back(std::move(t));
  46. }
  47. for (auto i: mod.buildItems(resources_)) {
  48. items_[i->name_] = std::move(i);
  49. }
  50. for (auto fact: mod.getWorldGens()) {
  51. worldgen_factories_.emplace(
  52. std::piecewise_construct,
  53. std::forward_as_tuple(fact.name),
  54. std::forward_as_tuple(fact));
  55. }
  56. for (auto fact: mod.getEntities()) {
  57. ent_coll_factories_.push_back(fact);
  58. }
  59. mods_.push_back(std::move(mod));
  60. }
  61. void World::setWorldGen(std::string gen) {
  62. default_world_gen_ = std::move(gen);
  63. }
  64. void World::spawnPlayer() {
  65. player_ = dynamic_cast<BodyTrait::HasBody *>(
  66. planes_[current_plane_].spawnPlayer().get());
  67. }
  68. void World::setCurrentPlane(WorldPlane &plane) {
  69. current_plane_ = plane.id_;
  70. }
  71. WorldPlane &World::addPlane(const std::string &gen) {
  72. WorldPlane::ID id = planes_.size();
  73. auto it = worldgen_factories_.find(gen);
  74. if (it == worldgen_factories_.end()) {
  75. panic << "Tried to add plane with non-existant world gen " << gen << "!";
  76. abort();
  77. }
  78. std::vector<std::unique_ptr<EntityCollection>> colls;
  79. colls.reserve(ent_coll_factories_.size());
  80. for (auto &fact: ent_coll_factories_) {
  81. colls.emplace_back(fact.create(fact.name));
  82. }
  83. WorldGen::Factory &factory = it->second;
  84. std::unique_ptr<WorldGen> g = factory.create(*this);
  85. planes_.emplace_back(id, this, std::move(g), std::move(colls));
  86. return planes_[id];
  87. }
  88. Item &World::getItem(const std::string &name) {
  89. auto iter = items_.find(name);
  90. if (iter == items_.end()) {
  91. warn << "Tried to get non-existant item " << name << "!";
  92. return *game_->invalid_item_;
  93. }
  94. return *iter->second;
  95. }
  96. Tile::ID World::getTileID(const std::string &name) {
  97. auto iter = tiles_map_.find(name);
  98. if (iter == tiles_map_.end()) {
  99. warn << "Tried to get non-existant item " << name << "!";
  100. return Tile::INVALID_ID;
  101. }
  102. return iter->second;
  103. }
  104. Tile &World::getTile(const std::string &name) {
  105. Tile::ID id = getTileID(name);
  106. return getTileByID(id);
  107. }
  108. SDL_Color World::backgroundColor() {
  109. return planes_[current_plane_].backgroundColor();
  110. }
  111. void World::draw(Win &win) {
  112. auto bounds = player_->getBody().getBounds();
  113. win.cam_ = bounds.pos - (win.getSize() / 2) + (bounds.size / 2);
  114. planes_[current_plane_].draw(win);
  115. }
  116. void World::update(float dt) {
  117. for (auto &plane: planes_)
  118. plane.update(dt);
  119. }
  120. void World::tick(float dt) {
  121. for (auto &plane: planes_)
  122. plane.tick(dt);
  123. auto bounds = player_->getBody().getBounds();
  124. chunk_renderer_.tick(
  125. planes_[current_plane_],
  126. ChunkPos((int)bounds.pos.x / CHUNK_WIDTH, (int)bounds.pos.y / CHUNK_HEIGHT));
  127. }
  128. }