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.

WorldPlane.h 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #pragma once
  2. #include <vector>
  3. #include <deque>
  4. #include <utility>
  5. #include <memory>
  6. #include <map>
  7. #include <set>
  8. #include <typeindex>
  9. #include <mutex>
  10. #include "common.h"
  11. #include "traits/BodyTrait.h"
  12. #include "util.h"
  13. #include "Chunk.h"
  14. #include "Tile.h"
  15. #include "WorldGen.h"
  16. #include "Entity.h"
  17. #include "Collection.h"
  18. #include "LightServer.h"
  19. namespace Swan {
  20. class World;
  21. class Game;
  22. class WorldPlane final: NonCopyable, public LightCallback {
  23. public:
  24. using ID = uint16_t;
  25. WorldPlane(
  26. ID id, World *world, std::unique_ptr<WorldGen> gen,
  27. std::vector<std::unique_ptr<EntityCollection>> &&colls);
  28. EntityRef spawnEntity(const std::string &name, const Entity::PackObject &params);
  29. template<typename Ent, typename... Args>
  30. EntityRef spawnEntity(Args&&... args);
  31. Context getContext();
  32. bool hasChunk(ChunkPos pos);
  33. Chunk &getChunk(ChunkPos pos);
  34. Chunk &slowGetChunk(ChunkPos pos);
  35. void setTileID(TilePos pos, Tile::ID id);
  36. void setTile(TilePos pos, const std::string &name);
  37. template<typename Ent>
  38. EntityCollection &getCollectionOf();
  39. EntityCollection &getCollectionOf(std::string name);
  40. EntityCollection &getCollectionOf(std::type_index type);
  41. Tile::ID getTileID(TilePos pos);
  42. Tile &getTile(TilePos pos);
  43. Iter<Entity *> getEntsInArea(Vec2 center, float radius);
  44. template<typename T>
  45. Iter<T *>getEntsOfType() {
  46. return Iter<T *>([] { return std::nullopt; });
  47. /* TODO
  48. return mapFilter(entities_.begin(), entities_.end(), [](std::unique_ptr<Entity> &ent) -> std::optional<T *> {
  49. if (T *e = dynamic_cast<T *>(ent.get()); e != nullptr)
  50. return e;
  51. return std::nullopt;
  52. });
  53. */
  54. }
  55. EntityRef spawnPlayer();
  56. void breakTile(TilePos pos);
  57. SDL_Color backgroundColor();
  58. void draw(Win &win);
  59. void update(float dt);
  60. void tick(float dt);
  61. void debugBox(TilePos pos);
  62. void addLight(TilePos pos, float level);
  63. void removeLight(TilePos pos, float level);
  64. // LightingCallback implementation
  65. void onLightChunkUpdated(const LightChunk &chunk, Vec2i pos) final;
  66. ID id_;
  67. World *world_;
  68. std::unique_ptr<WorldGen> gen_;
  69. std::mutex mut_;
  70. private:
  71. std::unique_ptr<LightServer> lighting_;
  72. std::map<std::pair<int, int>, Chunk> chunks_;
  73. std::vector<Chunk *> activeChunks_;
  74. std::vector<std::pair<ChunkPos, Chunk *>> tickChunks_;
  75. std::vector<std::unique_ptr<EntityCollection>> entColls_;
  76. std::unordered_map<std::type_index, EntityCollection *> entCollsByType_;
  77. std::unordered_map<std::string, EntityCollection *> entCollsByName_;
  78. std::deque<Chunk *> chunkInitList_;
  79. std::vector<TilePos> debugBoxes_;
  80. };
  81. /*
  82. * WorldPlane
  83. */
  84. template<typename Ent, typename... Args>
  85. inline EntityRef WorldPlane::spawnEntity(Args&&... args) {
  86. return getCollectionOf(typeid(Ent)).spawn<Ent, Args...>(std::forward<Args>(args)...);
  87. }
  88. template<typename Ent>
  89. inline EntityCollection &WorldPlane::getCollectionOf() {
  90. return *entCollsByType_.at(typeid(Ent));
  91. }
  92. inline EntityCollection &WorldPlane::getCollectionOf(std::string name) {
  93. return *entCollsByName_.at(name);
  94. }
  95. inline EntityCollection &WorldPlane::getCollectionOf(std::type_index type) {
  96. return *entCollsByType_.at(type);
  97. }
  98. }