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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 <cygnet/util.h>
  10. #include "common.h"
  11. #include "Item.h"
  12. #include "Tile.h"
  13. #include "WorldPlane.h"
  14. #include "WorldGen.h"
  15. #include "Entity.h"
  16. #include "Collection.h"
  17. #include "Mod.h"
  18. #include "EventEmitter.h"
  19. namespace Swan {
  20. class Game;
  21. class World {
  22. public:
  23. static constexpr Tile::ID INVALID_TILE_ID = 0;
  24. static constexpr char INVALID_TILE_NAME[] = "@::invalid";
  25. static constexpr Tile::ID AIR_TILE_ID = 1;
  26. static constexpr char AIR_TILE_NAME[] = "@::air";
  27. World(Game *game, unsigned long randSeed, std::vector<std::string> modPaths);
  28. void setWorldGen(std::string gen);
  29. void spawnPlayer();
  30. void setCurrentPlane(WorldPlane &plane);
  31. WorldPlane &addPlane(const std::string &gen);
  32. WorldPlane &addPlane() { return addPlane(defaultWorldGen_); }
  33. Tile &getTileByID(Tile::ID id) { return tiles_[id]; }
  34. Tile::ID getTileID(const std::string &name);
  35. Tile &getTile(const std::string &name);
  36. Item &getItem(const std::string &name);
  37. Cygnet::RenderSprite &getSprite(const std::string &name);
  38. Cygnet::Color backgroundColor();
  39. void draw(Cygnet::Renderer &rnd);
  40. void update(float dt);
  41. void tick(float dt);
  42. // These things can be used by the mods as they get initialized in the ctor.
  43. EventEmitter<const Context &, TilePos, Tile &> evtTileBreak_;
  44. // These things get filled in when the ctor loads mods.
  45. std::vector<Tile> tiles_;
  46. std::unordered_map<std::string, Tile::ID> tilesMap_;
  47. std::unordered_map<std::string, Item> items_;
  48. std::unordered_map<std::string, WorldGen::Factory> worldGenFactories_;
  49. std::unordered_map<std::string, EntityCollection::Factory> entCollFactories_;
  50. // These things get initialized in the ctor.
  51. // the above members must be initialized before these.
  52. Game *game_;
  53. std::mt19937 random_;
  54. std::vector<ModWrapper> mods_;
  55. Cygnet::ResourceManager resources_;
  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. }