#pragma once #include #include #include #include #include #include #include #include "common.h" #include "traits/BodyTrait.h" #include "util.h" #include "Chunk.h" #include "Tile.h" #include "WorldGen.h" #include "Entity.h" #include "Collection.h" namespace Swan { class World; class Game; class WorldPlane: NonCopyable { public: using ID = uint16_t; WorldPlane( ID id, World *world, std::unique_ptr gen, std::vector> &&colls); EntityRef spawnEntity(const std::string &name, const Entity::PackObject ¶ms); template EntityRef spawnEntity(Args&&... args); Context getContext(); bool hasChunk(ChunkPos pos); Chunk &getChunk(ChunkPos pos); Chunk &slowGetChunk(ChunkPos pos); void setTileID(TilePos pos, Tile::ID id); void setTile(TilePos pos, const std::string &name); template EntityCollection &getCollectionOf(); EntityCollection &getCollectionOf(std::string name); EntityCollection &getCollectionOf(std::type_index type); Tile::ID getTileID(TilePos pos); Tile &getTile(TilePos pos); Iter getEntsInArea(Vec2 center, float radius); template ItergetEntsOfType() { return Iter([] { return std::nullopt; }); /* TODO return mapFilter(entities_.begin(), entities_.end(), [](std::unique_ptr &ent) -> std::optional { if (T *e = dynamic_cast(ent.get()); e != nullptr) return e; return std::nullopt; }); */ } EntityRef spawnPlayer(); void breakTile(TilePos pos); SDL_Color backgroundColor(); void draw(Win &win); void update(float dt); void tick(float dt); void debugBox(TilePos pos); ID id_; World *world_; std::unique_ptr gen_; private: void addLight(TilePos pos, uint8_t level); void removeLight(TilePos pos, uint8_t level); std::map, Chunk> chunks_; std::vector active_chunks_; std::vector> tick_chunks_; std::vector> ent_colls_; std::unordered_map ent_colls_by_type_; std::unordered_map ent_colls_by_name_; std::deque chunk_init_list_; std::vector debug_boxes_; }; /* * WorldPlane */ template inline EntityRef WorldPlane::spawnEntity(Args&&... args) { return getCollectionOf(typeid(Ent)).spawn(std::forward(args)...); } template inline EntityCollection &WorldPlane::getCollectionOf() { return *ent_colls_by_type_.at(typeid(Ent)); } inline EntityCollection &WorldPlane::getCollectionOf(std::string name) { return *ent_colls_by_name_.at(name); } inline EntityCollection &WorldPlane::getCollectionOf(std::type_index type) { return *ent_colls_by_type_.at(type); } }