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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #pragma once
  2. #include <memory>
  3. #include <vector>
  4. #include <string>
  5. #include <random>
  6. #include <cygnet/Renderer.h>
  7. #include <cygnet/ResourceManager.h>
  8. #include <cygnet/util.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. Cygnet::Color backgroundColor();
  38. void draw(Cygnet::Renderer &rnd);
  39. void update(float dt);
  40. void tick(float dt);
  41. // These things can be used by the mods as they get initialized in the ctor.
  42. EventEmitter<const Context &, TilePos, Tile &> evtTileBreak_;
  43. // These things get filled in when the ctor loads mods.
  44. std::vector<Tile> tiles_;
  45. std::unordered_map<std::string, Tile::ID> tilesMap_;
  46. std::unordered_map<std::string, Item> items_;
  47. std::unordered_map<std::string, WorldGen::Factory> worldGenFactories_;
  48. std::unordered_map<std::string, EntityCollection::Factory> entCollFactories_;
  49. // These things get initialized in the ctor.
  50. // the above members must be initialized before these.
  51. Game *game_;
  52. std::mt19937 random_;
  53. std::vector<ModWrapper> mods_;
  54. Cygnet::ResourceManager resources_;
  55. BodyTrait::Body *player_;
  56. private:
  57. class ChunkRenderer {
  58. public:
  59. void tick(WorldPlane &plane, ChunkPos abspos);
  60. };
  61. std::vector<ModWrapper> loadMods(std::vector<std::string> paths);
  62. Cygnet::ResourceManager buildResources();
  63. ChunkRenderer chunkRenderer_;
  64. WorldPlane::ID currentPlane_;
  65. std::vector<std::unique_ptr<WorldPlane>> planes_;
  66. std::string defaultWorldGen_;
  67. };
  68. }