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.

World.h 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #pragma once
  2. #include <memory>
  3. #include <vector>
  4. #include <string>
  5. #include <random>
  6. #include "common.h"
  7. #include "Item.h"
  8. #include "Tile.h"
  9. #include "WorldPlane.h"
  10. #include "WorldGen.h"
  11. #include "Entity.h"
  12. #include "Resource.h"
  13. #include "Mod.h"
  14. namespace Swan {
  15. class Game;
  16. class World {
  17. public:
  18. World(Game *game, unsigned long rand_seed);
  19. void addMod(std::unique_ptr<Mod> mod);
  20. void setWorldGen(const std::string &gen);
  21. void spawnPlayer();
  22. void setCurrentPlane(WorldPlane &plane);
  23. WorldPlane &addPlane(const std::string &gen);
  24. WorldPlane &addPlane() { return addPlane(default_world_gen_); }
  25. Tile &getTileByID(Tile::ID id);
  26. Tile::ID getTileID(const std::string &name);
  27. Tile &getTile(const std::string &name);
  28. Item &getItem(const std::string &name);
  29. void draw(Win &win);
  30. void update(float dt);
  31. void tick(float dt);
  32. std::vector<std::unique_ptr<Mod>> mods_;
  33. // World owns tiles and items, the mod just has Builder objects
  34. std::vector<std::unique_ptr<Tile>> tiles_;
  35. std::unordered_map<std::string, Tile::ID> tiles_map_;
  36. std::unordered_map<std::string, std::unique_ptr<Item>> items_;
  37. // The mods themselves retain ownership of world gens and entities,
  38. // the world just has non-owning pointers to them
  39. std::unordered_map<std::string, WorldGen::Factory *> worldgens_;
  40. std::unordered_map<std::string, Entity::Factory *> ents_;
  41. BodyTrait::HasBody *player_;
  42. Game *game_;
  43. std::mt19937 random_;
  44. ResourceManager resources_;
  45. private:
  46. class ChunkRenderer {
  47. public:
  48. void tick(WorldPlane &plane, ChunkPos abspos);
  49. };
  50. ChunkRenderer chunk_renderer_;
  51. WorldPlane::ID current_plane_;
  52. std::vector<WorldPlane> planes_;
  53. std::string default_world_gen_;
  54. };
  55. }