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

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