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 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #pragma once
  2. #include <memory>
  3. #include <vector>
  4. #include <string>
  5. #include <random>
  6. #include <SDL.h>
  7. #include <cygnet/Renderer.h>
  8. #include <cygnet/ResourceManager.h>
  9. #include "common.h"
  10. #include "Item.h"
  11. #include "Tile.h"
  12. #include "WorldPlane.h"
  13. #include "WorldGen.h"
  14. #include "Entity.h"
  15. #include "Collection.h"
  16. #include "Mod.h"
  17. #include "EventEmitter.h"
  18. namespace Swan {
  19. class Game;
  20. class World {
  21. public:
  22. static constexpr Tile::ID INVALID_TILE_ID = 0;
  23. static constexpr char INVALID_TILE_NAME[] = "@::invalid";
  24. static constexpr Tile::ID AIR_TILE_ID = 1;
  25. static constexpr char AIR_TILE_NAME[] = "@::air";
  26. World(Game *game, unsigned long randSeed, std::vector<std::string> modPaths);
  27. void setWorldGen(std::string gen);
  28. void spawnPlayer();
  29. void setCurrentPlane(WorldPlane &plane);
  30. WorldPlane &addPlane(const std::string &gen);
  31. WorldPlane &addPlane() { return addPlane(defaultWorldGen_); }
  32. Tile &getTileByID(Tile::ID id) { return tiles_[id]; }
  33. Tile::ID getTileID(const std::string &name);
  34. Tile &getTile(const std::string &name);
  35. Item &getItem(const std::string &name);
  36. Cygnet::RenderSprite &getSprite(const std::string &name);
  37. SDL_Color backgroundColor();
  38. void draw(Cygnet::Renderer &rnd);
  39. void update(float dt);
  40. void tick(float dt);
  41. // Event emitters
  42. EventEmitter<const Context &, TilePos, Tile &>
  43. evtTileBreak_;
  44. // World owns all mods
  45. Game *game_; // TODO: reference, not pointer
  46. std::mt19937 random_;
  47. std::vector<ModWrapper> mods_;
  48. Cygnet::ResourceManager resources_;
  49. // World owns tiles and items, the mod just has Builder objects
  50. std::vector<Tile> tiles_;
  51. std::unordered_map<std::string, Tile::ID> tilesMap_;
  52. std::unordered_map<std::string, Item> items_;
  53. // Mods give us factories to create new world gens and new entity collections
  54. std::unordered_map<std::string, WorldGen::Factory> worldGenFactories_;
  55. std::unordered_map<std::string, EntityCollection::Factory> entCollFactories_;
  56. BodyTrait::Body *player_;
  57. private:
  58. class ChunkRenderer {
  59. public:
  60. void tick(WorldPlane &plane, ChunkPos abspos);
  61. };
  62. std::vector<ModWrapper> loadMods(std::vector<std::string> paths);
  63. Cygnet::ResourceManager buildResources();
  64. ChunkRenderer chunkRenderer_;
  65. WorldPlane::ID currentPlane_;
  66. std::vector<std::unique_ptr<WorldPlane>> planes_;
  67. std::string defaultWorldGen_;
  68. };
  69. }