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

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