Advertisement
Guest User

Untitled

a guest
Jan 19th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.63 KB | None | 0 0
  1. #pragma once
  2. /*!
  3. \file InputHandler.h
  4. */
  5. #include <fstream>
  6. #include <vector>
  7. #include <string>
  8. #include <sstream>
  9. #include <map>
  10.  
  11. #include "GameObject.h"
  12. #include "TransformComponent.h"
  13. #include "SceneHandlerComponent.h"
  14. #include "FirstPersonCameraComponent.h"
  15. #include "ModelComponent.h"
  16. #include "json/json.h"
  17.  
  18. #define TRANSLATE_ADJUSTMENT_VALUE 0.01f;
  19. /*!
  20. \class InputCommand
  21. \brief Input interface which handles execution of inputs.
  22. */
  23. class InputCommand
  24. {
  25. public:
  26. virtual ~InputCommand() {}
  27. virtual void execute(GameObject& object ) = 0;
  28.  
  29. };
  30.  
  31.  
  32.  
  33. /*!
  34. \class RotateLeftCommand
  35. \brief Handles Roations to the left.
  36. */
  37. class RotateLeftCommand : public InputCommand
  38. {
  39. public:
  40. void execute(GameObject& obj) override
  41. {
  42. if (obj.getComponent<TransformComponent>())
  43. {
  44. obj.getComponent<TransformComponent>()->OnMessage("rotateLeft");
  45. obj.getComponent<FirstPersonCameraComponent>()->OnMessage("rotateLeft");
  46. obj.getComponent<ThirdPersonCameraComponent>()->OnMessage("rotateLeft");
  47. }
  48. }
  49.  
  50.  
  51. };
  52.  
  53. /*!
  54. \class RotateRightCommand
  55. \brief Handles Roations to the Right.
  56. */
  57. class RotateRightCommand : public InputCommand
  58. {
  59. public:
  60. void execute(GameObject& obj) override
  61. {
  62. if (obj.getComponent<TransformComponent>())
  63. {
  64. obj.getComponent<TransformComponent>()->OnMessage("rotateRight");
  65. obj.getComponent<FirstPersonCameraComponent>()->OnMessage("rotateRight");
  66. obj.getComponent<ThirdPersonCameraComponent>()->OnMessage("rotateRight");
  67. }
  68. }
  69.  
  70.  
  71. };
  72.  
  73. /*!
  74. \class RotateUpCommand
  75. \brief Handles Roations upwards.
  76. */
  77. class RotateUpCommand : public InputCommand
  78. {
  79. public:
  80. void execute(GameObject& obj) override
  81. {
  82. if (obj.getComponent<TransformComponent>())
  83. {
  84. obj.getComponent<TransformComponent>()->OnMessage("rotateUp");
  85. obj.getComponent<FirstPersonCameraComponent>()->OnMessage("rotateUp");
  86. obj.getComponent<ThirdPersonCameraComponent>()->OnMessage("spinLeft");
  87. }
  88. }
  89.  
  90.  
  91. };
  92.  
  93. /*!
  94. \class RotateDownCommand
  95. \brief Handles Roations downwards.
  96. */
  97. class RotateDownCommand : public InputCommand
  98. {
  99. public:
  100. void execute(GameObject& obj) override
  101. {
  102. if (obj.getComponent<TransformComponent>())
  103. {
  104. obj.getComponent<TransformComponent>()->OnMessage("rotateDown");
  105. obj.getComponent<FirstPersonCameraComponent>()->OnMessage("rotateDown");
  106. obj.getComponent<ThirdPersonCameraComponent>()->OnMessage("spinRight");
  107. }
  108. }
  109.  
  110.  
  111. };
  112.  
  113. /*!
  114. \class TranslateLeftCommand
  115. \brief Handles translation to the left.
  116. */
  117. class TranslateLeftCommand : public InputCommand
  118. {
  119. public:
  120. void execute(GameObject& obj) override
  121. {
  122. if (obj.getComponent<TransformComponent>())
  123. {
  124. obj.getComponent<TransformComponent>()->OnMessage("moveLeft");
  125. obj.getComponent<FirstPersonCameraComponent>()->OnMessage("moveLeft");
  126. obj.getComponent<ThirdPersonCameraComponent>()->OnMessage("moveLeft");
  127. }
  128. }
  129.  
  130.  
  131. };
  132.  
  133. /*!
  134. \class TranslateRightCommand
  135. \brief Handles translation to the right.
  136. */
  137. class TranslateRightCommand : public InputCommand
  138. {
  139. public:
  140. void execute(GameObject& obj) override
  141. {
  142. if (obj.getComponent<TransformComponent>())
  143. {
  144. obj.getComponent<TransformComponent>()->OnMessage("moveRight");
  145. obj.getComponent<FirstPersonCameraComponent>()->OnMessage("moveRight");
  146. obj.getComponent<ThirdPersonCameraComponent>()->OnMessage("moveRight");
  147. }
  148. }
  149. };
  150.  
  151. /*!
  152. \class TranslateForwardsCommand
  153. \brief Handles translation forwards.
  154. */
  155. class TranslateForwardsCommand : public InputCommand
  156. {
  157. public:
  158. void execute(GameObject& obj) override
  159. {
  160. if (obj.getComponent<TransformComponent>())
  161. {
  162. obj.getComponent<TransformComponent>()->OnMessage("moveForwards");
  163. obj.getComponent<FirstPersonCameraComponent>()->OnMessage("moveForwards");
  164. obj.getComponent<ThirdPersonCameraComponent>()->OnMessage("moveForwards");
  165. }
  166. }
  167. };
  168.  
  169. /*!
  170. \class TranslateBackwardsCommand
  171. \brief Handles translation backwards.
  172. */
  173. class TranslateBackwardsCommand : public InputCommand
  174. {
  175. public:
  176. void execute(GameObject& obj) override
  177. {
  178. if (obj.getComponent<TransformComponent>())
  179. {
  180. obj.getComponent<TransformComponent>()->OnMessage("moveBackwards");
  181. obj.getComponent<FirstPersonCameraComponent>()->OnMessage("moveBackwards");
  182. obj.getComponent<ThirdPersonCameraComponent>()->OnMessage("moveBackwards");
  183. }
  184. }
  185. };
  186.  
  187.  
  188. /*!
  189. \class FirstPersonCamCommand
  190. \brief Sets and Handles first person camera and its inputs.
  191. */
  192. class FirstPersonCamCommand : public InputCommand
  193. {
  194. public:
  195. void execute(GameObject& obj) override
  196. {
  197. if (obj.getComponent<FirstPersonCameraComponent>())
  198. {
  199. //obj.getComponent<TransformComponent>()->OnMessage("swapCam");
  200. obj.getComponent<FirstPersonCameraComponent>()->OnMessage("swapCam");
  201. //obj.getComponent<ThirdPersonCameraComponent>()->OnMessage("swapCam");
  202.  
  203. }
  204. }
  205. };
  206.  
  207. ///*!
  208. //\class FirstPersonCamCommand
  209. //\brief Sets and Handles first person camera and its inputs.
  210. //*/
  211. //class ThirdPersonCamCommand : public InputCommand
  212. //{
  213. //public:
  214. // void execute(GameObject& obj) override
  215. // {
  216. // if (obj.getComponent<ThirdPersonCameraComponent>())
  217. // {
  218. // //obj.getComponent<TransformComponent>()->OnMessage("swapCam");
  219. // obj.getComponent<ThirdPersonCameraComponent>()->OnMessage("swapCam");
  220. // //obj.getComponent<FirstPersonCameraComponent>()->OnMessage("swapCam");
  221. //
  222. // }
  223. //
  224. // }
  225. //};
  226.  
  227.  
  228. /*!
  229. \class InputHandler
  230. \brief communicates with game objects to execute tasks.
  231. */
  232. struct InputHandler
  233. {
  234.  
  235. //int w, s, a, d;
  236.  
  237. //bool setupInputs(std::string keyconfig) //! Loads player from file, choose from different players based on model.
  238. //{
  239. // std::fstream jsonData;
  240. // Json::Value root;
  241. // Json::Reader reader;
  242. // jsonData.open(keyconfig.c_str());
  243. // // check for errors
  244. // if (!reader.parse(jsonData, root))
  245. // {
  246. // std::cout << "Failed to parse data from: "
  247. // << keyconfig
  248. // << reader.getFormattedErrorMessages();
  249. // return false;
  250. // }
  251. // const Json::Value keyboardInputs = root["KeyboardInputs"];
  252.  
  253. // //----> the values pos or scale in json <------//
  254. // float w, a, s, d;
  255. // // get the position node
  256. // const Json::Value wKey = keyboardInputs["MoveForward"].asInt();
  257. // w = wKey.asInt(); // get float
  258.  
  259. // // get the position node
  260. // const Json::Value sKey = keyboardInputs["MoveBackward"].asInt();
  261. // s = sKey.asInt(); // get float
  262.  
  263. // // get the position node
  264. // const Json::Value aKey = keyboardInputs["MoveLeft"].asInt();
  265. // a = aKey.asInt(); // get float
  266.  
  267. // // get the position node
  268. // const Json::Value dKey = keyboardInputs["MoveRight"].asInt();
  269. // d = dKey.asInt(); // get float
  270.  
  271.  
  272. // return true;
  273. //}
  274. //
  275.  
  276.  
  277. GameObject* m_playerObject; //!< game object pointer.
  278.  
  279. std::map<int, InputCommand*> m_controlMapping; //!< map of controls.
  280.  
  281. InputHandler(GameObject* obj) : m_playerObject(obj)
  282. {
  283. //setupInputs("assets/Levels/keyconfig.json");
  284.  
  285. m_controlMapping[(int)'W'] = new TranslateForwardsCommand;
  286. m_controlMapping[(int)'S'] = new TranslateBackwardsCommand;
  287. m_controlMapping[(int)'A'] = new TranslateLeftCommand;
  288. m_controlMapping[(int)'D'] = new TranslateRightCommand;
  289.  
  290. m_controlMapping[(int)'Q'] = new RotateLeftCommand;
  291. m_controlMapping[(int)'E'] = new RotateRightCommand;
  292. m_controlMapping[(int)'T'] = new RotateUpCommand;
  293. m_controlMapping[(int)'R'] = new RotateDownCommand;
  294.  
  295. m_controlMapping[(int)'C'] = new FirstPersonCamCommand;
  296. //m_controlMapping[(int)'V'] = new ThirdPersonCamCommand;
  297.  
  298. }
  299.  
  300. void handleInputs(const std::vector<bool>& keyBuffer)
  301. {
  302. for(const auto& mapEntry : m_controlMapping)
  303. {
  304. if(keyBuffer[mapEntry.first])
  305. {
  306. mapEntry.second->execute(*m_playerObject);
  307. }
  308. }
  309.  
  310. }
  311. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement