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 "Collection.h"
  14. #include "Resource.h"
  15. #include "Mod.h"
  16. #include "EventEmitter.h"
  17. namespace Swan {
  18. class Game;
  19. class World {
  20. public:
  21. World(Game *game, unsigned long rand_seed);
  22. void addMod(ModWrapper &&mod);
  23. void setWorldGen(std::string gen);
  24. void spawnPlayer();
  25. void setCurrentPlane(WorldPlane &plane);
  26. WorldPlane &addPlane(const std::string &gen);
  27. WorldPlane &addPlane() { return addPlane(default_world_gen_); }
  28. Tile &getTileByID(Tile::ID id) { return *tiles_[id]; }
  29. Tile::ID getTileID(const std::string &name);
  30. Tile &getTile(const std::string &name);
  31. Item &getItem(const std::string &name);
  32. SDL_Color backgroundColor();
  33. void draw(Win &win);
  34. void update(float dt);
  35. void tick(float dt);
  36. // Event emitters
  37. EventEmitter<const Context &, TilePos, Tile &>
  38. evt_tile_break_;
  39. // World owns all mods
  40. std::vector<ModWrapper> mods_;
  41. // World owns tiles and items, the mod just has Builder objects
  42. std::vector<std::unique_ptr<Tile>> tiles_;
  43. std::unordered_map<std::string, Tile::ID> tiles_map_;
  44. std::unordered_map<std::string, std::unique_ptr<Item>> items_;
  45. // Mods give us factories to create new world gens and new entity collections
  46. std::unordered_map<std::string, WorldGen::Factory> worldgen_factories_;
  47. std::vector<EntityCollection::Factory> ent_coll_factories_;
  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. }