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

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