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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 "Resource.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. SDL_Color backgroundColor();
  38. void draw(Win &win);
  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. //ResourceManager resources_;
  49. Cygnet::ResourceManager resources_;
  50. // World owns tiles and items, the mod just has Builder objects
  51. std::vector<Tile> tiles_;
  52. std::unordered_map<std::string, Tile::ID> tilesMap_;
  53. std::unordered_map<std::string, Item> items_;
  54. // Mods give us factories to create new world gens and new entity collections
  55. std::unordered_map<std::string, WorldGen::Factory> worldGenFactories_;
  56. std::unordered_map<std::string, EntityCollection::Factory> entCollFactories_;
  57. BodyTrait::Body *player_;
  58. private:
  59. class ChunkRenderer {
  60. public:
  61. void tick(WorldPlane &plane, ChunkPos abspos);
  62. };
  63. std::vector<ModWrapper> loadMods(std::vector<std::string> paths);
  64. Cygnet::ResourceManager buildResources();
  65. ChunkRenderer chunkRenderer_;
  66. WorldPlane::ID currentPlane_;
  67. std::vector<std::unique_ptr<WorldPlane>> planes_;
  68. std::string defaultWorldGen_;
  69. };
  70. }