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.

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