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.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. void despawnEntity(Entity &ent);
  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. Tile::ID getTileID(TilePos pos);
  37. Tile &getTile(TilePos pos);
  38. Iter<Entity *> getEntsInArea(Vec2 center, float radius);
  39. template<typename T>
  40. Iter<T *>getEntsOfType() {
  41. return Iter<T *>([] { return std::nullopt; });
  42. /* TODO
  43. return mapFilter(entities_.begin(), entities_.end(), [](std::unique_ptr<Entity> &ent) -> std::optional<T *> {
  44. if (T *e = dynamic_cast<T *>(ent.get()); e != nullptr)
  45. return e;
  46. return std::nullopt;
  47. });
  48. */
  49. }
  50. EntityRef spawnPlayer();
  51. void breakTile(TilePos pos);
  52. SDL_Color backgroundColor();
  53. void draw(Win &win);
  54. void update(float dt);
  55. void tick(float dt);
  56. void debugBox(TilePos pos);
  57. ID id_;
  58. World *world_;
  59. std::unique_ptr<WorldGen> gen_;
  60. private:
  61. std::map<std::pair<int, int>, Chunk> chunks_;
  62. std::vector<Chunk *> active_chunks_;
  63. std::vector<std::pair<ChunkPos, Chunk *>> tick_chunks_;
  64. std::vector<std::unique_ptr<EntityCollection>> ent_colls_;
  65. std::unordered_map<std::type_index, EntityCollection *> ent_colls_by_type_;
  66. std::unordered_map<std::string, EntityCollection *> ent_colls_by_name_;
  67. std::deque<Chunk *> chunk_init_list_;
  68. std::vector<TilePos> debug_boxes_;
  69. };
  70. /*
  71. * WorldPlane
  72. */
  73. template<typename Ent, typename... Args>
  74. inline EntityRef WorldPlane::spawnEntity(Args&&... args) {
  75. return ent_colls_by_type_.at(typeid(Ent))->spawn<Ent, Args...>(std::forward<Args>(args)...);
  76. }
  77. }