LevelLoader.h 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // ----------------------------------------------------------------
  2. // From Game Programming in C++ by Sanjay Madhav
  3. // Copyright (C) 2017 Sanjay Madhav. All rights reserved.
  4. //
  5. // Released under the BSD License
  6. // See LICENSE in root directory for full details.
  7. // ----------------------------------------------------------------
  8. #pragma once
  9. #include <string>
  10. #include <rapidjson/document.h>
  11. #include <functional>
  12. #include <unordered_map>
  13. #include "Math.h"
  14. using ActorFunc = std::function<class Actor*(class Game*, const rapidjson::Value&)>;
  15. using ComponentFunc = std::function<
  16. class Component*(class Actor*, const rapidjson::Value&)
  17. >;
  18. class LevelLoader
  19. {
  20. public:
  21. // Load the level -- returns true if successful
  22. static bool LoadLevel(class Game* game, const std::string& fileName);
  23. // Loads a JSON file into a RapidJSON document
  24. static bool LoadJSON(const std::string& fileName, rapidjson::Document& outDoc);
  25. // Save the level
  26. static void SaveLevel(class Game* game, const std::string& fileName);
  27. protected:
  28. // Helper to load global properties
  29. static void LoadGlobalProperties(class Game* game, const rapidjson::Value& inObject);
  30. // Helper to load in actors
  31. static void LoadActors(class Game* game, const rapidjson::Value& inArray);
  32. // Helper to load in components
  33. static void LoadComponents(class Actor* actor, const rapidjson::Value& inArray);
  34. // Maps for data
  35. static std::unordered_map<std::string, ActorFunc> sActorFactoryMap;
  36. static std::unordered_map<std::string, std::pair<int, ComponentFunc>> sComponentFactoryMap;
  37. // Helper to save global properties
  38. static void SaveGlobalProperties(rapidjson::Document::AllocatorType& alloc,
  39. class Game* game, rapidjson::Value& inObject);
  40. // Helper to save actors
  41. static void SaveActors(rapidjson::Document::AllocatorType& alloc,
  42. class Game* game, rapidjson::Value& inArray);
  43. // Helper to save components
  44. static void SaveComponents(rapidjson::Document::AllocatorType& alloc,
  45. const class Actor* actor, rapidjson::Value& inArray);
  46. };
  47. class JsonHelper
  48. {
  49. public:
  50. // Helpers - Return true if successful, and also sets out parameter to parsed value
  51. // For each function, the first parameter is the containing JSON object, the second is the
  52. // name of the property in the containing object, and the third is the value you acquire.
  53. // Furthermore, if the property is not found, each function is guaranteed not to modify the
  54. // return value.
  55. static bool GetInt(const rapidjson::Value& inObject, const char* inProperty, int& outInt);
  56. static bool GetFloat(const rapidjson::Value& inObject, const char* inProperty, float& outFloat);
  57. static bool GetString(const rapidjson::Value& inObject, const char* inProperty, std::string& outStr);
  58. static bool GetBool(const rapidjson::Value& inObject, const char* inProperty, bool& outBool);
  59. static bool GetVector3(const rapidjson::Value& inObject, const char* inProperty, Vector3& outVector);
  60. static bool GetQuaternion(const rapidjson::Value& inObject, const char* inProperty, Quaternion& outQuat);
  61. // Setter functions
  62. static void AddInt(rapidjson::Document::AllocatorType& alloc,
  63. rapidjson::Value& inObject, const char* name, int value);
  64. static void AddFloat(rapidjson::Document::AllocatorType& alloc,
  65. rapidjson::Value& inObject, const char* name, float value);
  66. static void AddString(rapidjson::Document::AllocatorType& alloc,
  67. rapidjson::Value& inObject, const char* name, const std::string& value);
  68. static void AddBool(rapidjson::Document::AllocatorType& alloc,
  69. rapidjson::Value& inObject, const char* name, bool value);
  70. static void AddVector3(rapidjson::Document::AllocatorType& alloc,
  71. rapidjson::Value& inObject, const char* name, const Vector3& value);
  72. static void AddQuaternion(rapidjson::Document::AllocatorType& alloc,
  73. rapidjson::Value& inObject, const char* name, const Quaternion& value);
  74. };