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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. #include "World.h"
  2. #include <algorithm>
  3. #include <tuple>
  4. #include "log.h"
  5. #include "Game.h"
  6. #include "Clock.h"
  7. #include "assets.h"
  8. namespace Swan {
  9. static void chunkLine(int l, WorldPlane &plane, ChunkPos &abspos, const Vec2i &dir) {
  10. for (int i = 0; i < l; ++i) {
  11. plane.slowGetChunk(abspos).keepActive();
  12. abspos += dir;
  13. }
  14. }
  15. std::vector<ModWrapper> World::loadMods(std::vector<std::string> paths) {
  16. std::vector<ModWrapper> mods;
  17. mods.reserve(paths.size());
  18. for (auto &path: paths) {
  19. OS::Dynlib dl(path + "/mod");
  20. auto create = dl.get<Mod *(*)(World &)>("mod_create");
  21. if (create == NULL) {
  22. warn << path << ": No 'mod_create' function!";
  23. continue;
  24. }
  25. std::unique_ptr<Mod> mod(create(*this));
  26. mods.push_back(ModWrapper(std::move(mod), std::move(path), std::move(dl)));
  27. }
  28. return mods;
  29. }
  30. Cygnet::ResourceManager World::buildResources() {
  31. Cygnet::ResourceBuilder builder(game_->renderer_);
  32. auto fillTileImage = [&](unsigned char *data, int r, int g, int b, int a) {
  33. for (size_t i = 0; i < TILE_SIZE * TILE_SIZE; ++i) {
  34. data[i * 4 + 0] = r;
  35. data[i * 4 + 1] = g;
  36. data[i * 4 + 2] = b;
  37. data[i * 4 + 2] = a;
  38. }
  39. };
  40. struct ImageAsset fallbackImage = {
  41. .width = 32,
  42. .frameHeight = 32,
  43. .frameCount = 1,
  44. .data = std::make_unique<unsigned char[]>(TILE_SIZE * TILE_SIZE * 4),
  45. };
  46. fillTileImage(fallbackImage.data.get(),
  47. PLACEHOLDER_RED, PLACEHOLDER_GREEN, PLACEHOLDER_BLUE, 255);
  48. auto airImage = std::make_unique<unsigned char[]>(TILE_SIZE * TILE_SIZE * 4);
  49. fillTileImage(airImage.get(),
  50. PLACEHOLDER_RED, PLACEHOLDER_GREEN, PLACEHOLDER_BLUE, 255);
  51. // Let tile ID 0 be the invalid tile
  52. builder.addTile(INVALID_TILE_ID, fallbackImage.data.get());
  53. tilesMap_[INVALID_TILE_NAME] = INVALID_TILE_ID;
  54. tiles_.push_back(Tile(INVALID_TILE_ID, INVALID_TILE_NAME, {
  55. .name = "", .image = "", // Not used in this case
  56. .isSolid = false,
  57. }));
  58. items_.emplace(INVALID_TILE_NAME, Item(INVALID_TILE_ID, INVALID_TILE_NAME, {
  59. .name = "", .image = "", // Not used in this case
  60. }));
  61. // ...And tile ID 1 be the air tile
  62. builder.addTile(AIR_TILE_ID, std::move(airImage));
  63. tilesMap_[AIR_TILE_NAME] = AIR_TILE_ID;
  64. tiles_.push_back(Tile(AIR_TILE_ID, AIR_TILE_NAME, {
  65. .name = "", .image = "", // Not used in this case
  66. .isSolid = false,
  67. }));
  68. items_.emplace(AIR_TILE_NAME, Item(AIR_TILE_ID, AIR_TILE_NAME, {
  69. .name = "", .image = "", // Not used in this case
  70. }));
  71. // Assets are namespaced on the mod, so if something references, say,
  72. // "core::stone", we need to know which directory the "core" mod is in
  73. std::unordered_map<std::string, std::string> modPaths;
  74. for (auto &mod: mods_) {
  75. modPaths[mod.name()] = mod.path_;
  76. }
  77. auto loadTileImage = [&](std::string path) -> Result<ImageAsset> {
  78. // Don't force all tiles/items to have an associated image.
  79. // It could be that some tiles/items exist for a purpose which implies
  80. // it should never actually be visible.
  81. if (path == INVALID_TILE_NAME) {
  82. ImageAsset asset{
  83. .width = 32,
  84. .frameHeight = 32,
  85. .frameCount = 1,
  86. .data = std::make_unique<unsigned char[]>(TILE_SIZE * TILE_SIZE * 4),
  87. };
  88. memcpy(asset.data.get(), fallbackImage.data.get(), TILE_SIZE * TILE_SIZE * 4);
  89. return {Ok, std::move(asset)};
  90. }
  91. auto image = loadImageAsset(modPaths, path);
  92. if (!image) {
  93. warn << '\'' << path << "': " << image.err();
  94. return {Err, '\'' + path + "': " + image.err()};
  95. } else if (image->width != TILE_SIZE) {
  96. warn << '\'' << path << "': Width must be " << TILE_SIZE << " pixels";
  97. return {Err, '\'' + path + "': Width must be " + std::to_string(TILE_SIZE) + " pixels"};
  98. } else {
  99. return image;
  100. }
  101. };
  102. // Need to fill in every tile before we do items,
  103. // because all items will end up after all tiles in the tile atlas.
  104. // In the rendering system, there's no real difference between a tile
  105. // and an item.
  106. for (auto &mod: mods_) {
  107. for (auto &tileBuilder: mod.tiles()) {
  108. auto image = loadTileImage(tileBuilder.image);
  109. std::string tileName = mod.name() + "::" + tileBuilder.name;
  110. Tile::ID tileId = tiles_.size();
  111. if (image) {
  112. builder.addTile(tileId, std::move(image->data));
  113. } else {
  114. warn << image.err();
  115. builder.addTile(tileId, fallbackImage.data.get());
  116. }
  117. tilesMap_[tileName] = tileId;
  118. tiles_.push_back(Tile(tileId, tileName, tileBuilder));
  119. // All tiles should have an item.
  120. // Some items will be overwritten later my mod_->items,
  121. // but if not, this is their default item.
  122. items_.emplace(tileName, Item(tileId, tileName, {
  123. .name = "", .image = "", // Not used in this case
  124. }));
  125. }
  126. }
  127. // Put all items after all the tiles
  128. Tile::ID nextItemId = tiles_.size();
  129. // Load all items which aren't just tiles in disguise.
  130. for (auto &mod: mods_) {
  131. for (auto &itemBuilder: mod.items()) {
  132. auto image = loadTileImage(itemBuilder.image);
  133. std::string itemName = mod.name() + "::" + itemBuilder.name;
  134. Tile::ID itemId = nextItemId++;
  135. if (image) {
  136. builder.addTile(itemId, std::move(image->data));
  137. } else {
  138. warn << image.err();
  139. builder.addTile(itemId, fallbackImage.data.get());
  140. }
  141. items_.emplace(itemName, Item(itemId, itemName, itemBuilder));
  142. }
  143. }
  144. // Load sprites
  145. for (auto &mod: mods_) {
  146. for (auto spritePath: mod.sprites()) {
  147. std::string path = mod.name() + "::" + spritePath;
  148. auto image = loadImageAsset(modPaths, path);
  149. if (image) {
  150. builder.addSprite(
  151. path, image->data.get(), image->width,
  152. image->frameHeight * image->frameCount,
  153. image->frameHeight);
  154. } else {
  155. warn << '\'' << path << "': " << image.err();
  156. builder.addSprite(
  157. path, fallbackImage.data.get(), fallbackImage.width,
  158. fallbackImage.frameHeight * fallbackImage.frameCount,
  159. fallbackImage.frameHeight);
  160. }
  161. }
  162. }
  163. // Load world gens and entities
  164. for (auto &mod: mods_) {
  165. for (auto &worldGenFactory: mod.worldGens()) {
  166. std::string name = mod.name() + "::" + worldGenFactory.name;
  167. worldGenFactories_.emplace(name, worldGenFactory);
  168. }
  169. for (auto &entCollFactory: mod.entities()) {
  170. std::string name = mod.name() + "::" + entCollFactory.name;
  171. entCollFactories_.emplace(name, entCollFactory);
  172. }
  173. }
  174. return Cygnet::ResourceManager(std::move(builder));
  175. }
  176. World::World(Game *game, unsigned long randSeed, std::vector<std::string> modPaths):
  177. game_(game), random_(randSeed), mods_(loadMods(std::move(modPaths))),
  178. resources_(buildResources()) {}
  179. void World::ChunkRenderer::tick(WorldPlane &plane, ChunkPos abspos) {
  180. ZoneScopedN("World::ChunkRenderer tick");
  181. int l = 0;
  182. RTClock clock;
  183. for (int i = 0; i < 4; ++i) {
  184. chunkLine(l, plane, abspos, Vec2i(0, -1));
  185. chunkLine(l, plane, abspos, Vec2i(1, 0));
  186. l += 1;
  187. chunkLine(l, plane, abspos, Vec2i(0, 1));
  188. chunkLine(l, plane, abspos, Vec2i(-1, 0));
  189. l += 1;
  190. }
  191. }
  192. void World::setWorldGen(std::string gen) {
  193. defaultWorldGen_ = std::move(gen);
  194. }
  195. void World::spawnPlayer() {
  196. player_ = &((dynamic_cast<BodyTrait *>(
  197. planes_[currentPlane_]->spawnPlayer().get()))->get(BodyTrait::Tag{}));
  198. }
  199. void World::setCurrentPlane(WorldPlane &plane) {
  200. currentPlane_ = plane.id_;
  201. }
  202. WorldPlane &World::addPlane(const std::string &gen) {
  203. WorldPlane::ID id = planes_.size();
  204. auto it = worldGenFactories_.find(gen);
  205. if (it == worldGenFactories_.end()) {
  206. panic << "Tried to add plane with non-existent world gen " << gen << "!";
  207. abort();
  208. }
  209. std::vector<std::unique_ptr<EntityCollection>> colls;
  210. colls.reserve(entCollFactories_.size());
  211. for (auto &fact: entCollFactories_) {
  212. colls.emplace_back(fact.second.create(fact.second.name));
  213. }
  214. WorldGen::Factory &factory = it->second;
  215. std::unique_ptr<WorldGen> g = factory.create(*this);
  216. planes_.push_back(std::make_unique<WorldPlane>(
  217. id, this, std::move(g), std::move(colls)));
  218. return *planes_[id];
  219. }
  220. Tile::ID World::getTileID(const std::string &name) {
  221. auto iter = tilesMap_.find(name);
  222. if (iter == tilesMap_.end()) {
  223. warn << "Tried to get non-existent item " << name << "!";
  224. return INVALID_TILE_ID;
  225. }
  226. return iter->second;
  227. }
  228. Tile &World::getTile(const std::string &name) {
  229. Tile::ID id = getTileID(name);
  230. return getTileByID(id);
  231. }
  232. Item &World::getItem(const std::string &name) {
  233. auto iter = items_.find(name);
  234. if (iter == items_.end()) {
  235. warn << "Tried to get non-existent item " << name << "!";
  236. return items_.at(INVALID_TILE_NAME);
  237. }
  238. return iter->second;
  239. }
  240. Cygnet::RenderSprite &World::getSprite(const std::string &name) {
  241. auto iter = resources_.sprites_.find(name);
  242. if (iter == resources_.sprites_.end()) {
  243. warn << "Tried to get non-existent sprite " << name << "!";
  244. return resources_.sprites_.at(INVALID_TILE_NAME);
  245. }
  246. return iter->second;
  247. }
  248. SDL_Color World::backgroundColor() {
  249. return planes_[currentPlane_]->backgroundColor();
  250. }
  251. void World::draw(Cygnet::Renderer &rnd) {
  252. ZoneScopedN("World draw");
  253. planes_[currentPlane_]->draw(rnd);
  254. }
  255. void World::update(float dt) {
  256. ZoneScopedN("World update");
  257. for (auto &plane: planes_)
  258. plane->update(dt);
  259. game_->cam_.pos = player_->pos + Vec2{0.5, 0.5};
  260. }
  261. void World::tick(float dt) {
  262. ZoneScopedN("World tick");
  263. for (auto &plane: planes_)
  264. plane->tick(dt);
  265. chunkRenderer_.tick(
  266. *planes_[currentPlane_],
  267. ChunkPos((int)player_->pos.x / CHUNK_WIDTH, (int)player_->pos.y / CHUNK_HEIGHT));
  268. }
  269. }