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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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_[gen->name_] = gen;
  49. }
  50. for (auto *ent: mod->getEntities()) {
  51. ents_[ent->name_] = ent;
  52. }
  53. mods_.push_back(std::move(mod));
  54. }
  55. void World::setWorldGen(const std::string &gen) {
  56. default_world_gen_ = gen;
  57. }
  58. void World::spawnPlayer() {
  59. player_ = planes_[current_plane_].spawnPlayer();
  60. }
  61. void World::setCurrentPlane(WorldPlane &plane) {
  62. current_plane_ = plane.id_;
  63. }
  64. WorldPlane &World::addPlane(const std::string &gen) {
  65. WorldPlane::ID id = planes_.size();
  66. auto it = worldgens_.find(gen);
  67. if (it == end(worldgens_)) {
  68. panic << "Tried to add plane with non-existant world gen " << gen << "!";
  69. abort();
  70. }
  71. WorldGen::Factory *factory = it->second;
  72. WorldGen *g = factory->create(*this);
  73. planes_.emplace_back(id, this, std::shared_ptr<WorldGen>(g));
  74. return planes_[id];
  75. }
  76. Item &World::getItem(const std::string &name) {
  77. auto iter = items_.find(name);
  78. if (iter == items_.end()) {
  79. warn << "Tried to get non-existant item " << name << "!";
  80. return *game_->invalid_item_;
  81. }
  82. return *iter->second;
  83. }
  84. Tile::ID World::getTileID(const std::string &name) {
  85. auto iter = tiles_map_.find(name);
  86. if (iter == tiles_map_.end()) {
  87. warn << "Tried to get non-existant item " << name << "!";
  88. return Tile::INVALID_ID;
  89. }
  90. return iter->second;
  91. }
  92. Tile &World::getTileByID(Tile::ID id) {
  93. return *tiles_[id];
  94. }
  95. Tile &World::getTile(const std::string &name) {
  96. Tile::ID id = getTileID(name);
  97. return getTileByID(id);
  98. }
  99. SDL_Color World::backgroundColor() {
  100. return planes_[current_plane_].backgroundColor();
  101. }
  102. void World::draw(Win &win) {
  103. auto bounds = player_->getBody().getBounds();
  104. win.cam_ = bounds.pos - (win.getSize() / 2) + (bounds.size / 2);
  105. planes_[current_plane_].draw(win);
  106. }
  107. void World::update(float dt) {
  108. for (auto &plane: planes_)
  109. plane.update(dt);
  110. }
  111. void World::tick(float dt) {
  112. for (auto &plane: planes_)
  113. plane.tick(dt);
  114. auto bounds = player_->getBody().getBounds();
  115. chunk_renderer_.tick(
  116. planes_[current_plane_],
  117. ChunkPos((int)bounds.pos.x / CHUNK_WIDTH, (int)bounds.pos.y / CHUNK_HEIGHT));
  118. }
  119. }