Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- /*!
- \file InputHandler.h
- */
- #include <fstream>
- #include <vector>
- #include <string>
- #include <sstream>
- #include <map>
- #include "GameObject.h"
- #include "TransformComponent.h"
- #include "SceneHandlerComponent.h"
- #include "FirstPersonCameraComponent.h"
- #include "ModelComponent.h"
- #include "json/json.h"
- #define TRANSLATE_ADJUSTMENT_VALUE 0.01f;
- /*!
- \class InputCommand
- \brief Input interface which handles execution of inputs.
- */
- class InputCommand
- {
- public:
- virtual ~InputCommand() {}
- virtual void execute(GameObject& object ) = 0;
- };
- /*!
- \class RotateLeftCommand
- \brief Handles Roations to the left.
- */
- class RotateLeftCommand : public InputCommand
- {
- public:
- void execute(GameObject& obj) override
- {
- if (obj.getComponent<TransformComponent>())
- {
- obj.getComponent<TransformComponent>()->OnMessage("rotateLeft");
- obj.getComponent<FirstPersonCameraComponent>()->OnMessage("rotateLeft");
- obj.getComponent<ThirdPersonCameraComponent>()->OnMessage("rotateLeft");
- }
- }
- };
- /*!
- \class RotateRightCommand
- \brief Handles Roations to the Right.
- */
- class RotateRightCommand : public InputCommand
- {
- public:
- void execute(GameObject& obj) override
- {
- if (obj.getComponent<TransformComponent>())
- {
- obj.getComponent<TransformComponent>()->OnMessage("rotateRight");
- obj.getComponent<FirstPersonCameraComponent>()->OnMessage("rotateRight");
- obj.getComponent<ThirdPersonCameraComponent>()->OnMessage("rotateRight");
- }
- }
- };
- /*!
- \class RotateUpCommand
- \brief Handles Roations upwards.
- */
- class RotateUpCommand : public InputCommand
- {
- public:
- void execute(GameObject& obj) override
- {
- if (obj.getComponent<TransformComponent>())
- {
- obj.getComponent<TransformComponent>()->OnMessage("rotateUp");
- obj.getComponent<FirstPersonCameraComponent>()->OnMessage("rotateUp");
- obj.getComponent<ThirdPersonCameraComponent>()->OnMessage("spinLeft");
- }
- }
- };
- /*!
- \class RotateDownCommand
- \brief Handles Roations downwards.
- */
- class RotateDownCommand : public InputCommand
- {
- public:
- void execute(GameObject& obj) override
- {
- if (obj.getComponent<TransformComponent>())
- {
- obj.getComponent<TransformComponent>()->OnMessage("rotateDown");
- obj.getComponent<FirstPersonCameraComponent>()->OnMessage("rotateDown");
- obj.getComponent<ThirdPersonCameraComponent>()->OnMessage("spinRight");
- }
- }
- };
- /*!
- \class TranslateLeftCommand
- \brief Handles translation to the left.
- */
- class TranslateLeftCommand : public InputCommand
- {
- public:
- void execute(GameObject& obj) override
- {
- if (obj.getComponent<TransformComponent>())
- {
- obj.getComponent<TransformComponent>()->OnMessage("moveLeft");
- obj.getComponent<FirstPersonCameraComponent>()->OnMessage("moveLeft");
- obj.getComponent<ThirdPersonCameraComponent>()->OnMessage("moveLeft");
- }
- }
- };
- /*!
- \class TranslateRightCommand
- \brief Handles translation to the right.
- */
- class TranslateRightCommand : public InputCommand
- {
- public:
- void execute(GameObject& obj) override
- {
- if (obj.getComponent<TransformComponent>())
- {
- obj.getComponent<TransformComponent>()->OnMessage("moveRight");
- obj.getComponent<FirstPersonCameraComponent>()->OnMessage("moveRight");
- obj.getComponent<ThirdPersonCameraComponent>()->OnMessage("moveRight");
- }
- }
- };
- /*!
- \class TranslateForwardsCommand
- \brief Handles translation forwards.
- */
- class TranslateForwardsCommand : public InputCommand
- {
- public:
- void execute(GameObject& obj) override
- {
- if (obj.getComponent<TransformComponent>())
- {
- obj.getComponent<TransformComponent>()->OnMessage("moveForwards");
- obj.getComponent<FirstPersonCameraComponent>()->OnMessage("moveForwards");
- obj.getComponent<ThirdPersonCameraComponent>()->OnMessage("moveForwards");
- }
- }
- };
- /*!
- \class TranslateBackwardsCommand
- \brief Handles translation backwards.
- */
- class TranslateBackwardsCommand : public InputCommand
- {
- public:
- void execute(GameObject& obj) override
- {
- if (obj.getComponent<TransformComponent>())
- {
- obj.getComponent<TransformComponent>()->OnMessage("moveBackwards");
- obj.getComponent<FirstPersonCameraComponent>()->OnMessage("moveBackwards");
- obj.getComponent<ThirdPersonCameraComponent>()->OnMessage("moveBackwards");
- }
- }
- };
- /*!
- \class FirstPersonCamCommand
- \brief Sets and Handles first person camera and its inputs.
- */
- class FirstPersonCamCommand : public InputCommand
- {
- public:
- void execute(GameObject& obj) override
- {
- if (obj.getComponent<FirstPersonCameraComponent>())
- {
- //obj.getComponent<TransformComponent>()->OnMessage("swapCam");
- obj.getComponent<FirstPersonCameraComponent>()->OnMessage("swapCam");
- //obj.getComponent<ThirdPersonCameraComponent>()->OnMessage("swapCam");
- }
- }
- };
- ///*!
- //\class FirstPersonCamCommand
- //\brief Sets and Handles first person camera and its inputs.
- //*/
- //class ThirdPersonCamCommand : public InputCommand
- //{
- //public:
- // void execute(GameObject& obj) override
- // {
- // if (obj.getComponent<ThirdPersonCameraComponent>())
- // {
- // //obj.getComponent<TransformComponent>()->OnMessage("swapCam");
- // obj.getComponent<ThirdPersonCameraComponent>()->OnMessage("swapCam");
- // //obj.getComponent<FirstPersonCameraComponent>()->OnMessage("swapCam");
- //
- // }
- //
- // }
- //};
- /*!
- \class InputHandler
- \brief communicates with game objects to execute tasks.
- */
- struct InputHandler
- {
- //int w, s, a, d;
- //bool setupInputs(std::string keyconfig) //! Loads player from file, choose from different players based on model.
- //{
- // std::fstream jsonData;
- // Json::Value root;
- // Json::Reader reader;
- // jsonData.open(keyconfig.c_str());
- // // check for errors
- // if (!reader.parse(jsonData, root))
- // {
- // std::cout << "Failed to parse data from: "
- // << keyconfig
- // << reader.getFormattedErrorMessages();
- // return false;
- // }
- // const Json::Value keyboardInputs = root["KeyboardInputs"];
- // //----> the values pos or scale in json <------//
- // float w, a, s, d;
- // // get the position node
- // const Json::Value wKey = keyboardInputs["MoveForward"].asInt();
- // w = wKey.asInt(); // get float
- // // get the position node
- // const Json::Value sKey = keyboardInputs["MoveBackward"].asInt();
- // s = sKey.asInt(); // get float
- // // get the position node
- // const Json::Value aKey = keyboardInputs["MoveLeft"].asInt();
- // a = aKey.asInt(); // get float
- // // get the position node
- // const Json::Value dKey = keyboardInputs["MoveRight"].asInt();
- // d = dKey.asInt(); // get float
- // return true;
- //}
- //
- GameObject* m_playerObject; //!< game object pointer.
- std::map<int, InputCommand*> m_controlMapping; //!< map of controls.
- InputHandler(GameObject* obj) : m_playerObject(obj)
- {
- //setupInputs("assets/Levels/keyconfig.json");
- m_controlMapping[(int)'W'] = new TranslateForwardsCommand;
- m_controlMapping[(int)'S'] = new TranslateBackwardsCommand;
- m_controlMapping[(int)'A'] = new TranslateLeftCommand;
- m_controlMapping[(int)'D'] = new TranslateRightCommand;
- m_controlMapping[(int)'Q'] = new RotateLeftCommand;
- m_controlMapping[(int)'E'] = new RotateRightCommand;
- m_controlMapping[(int)'T'] = new RotateUpCommand;
- m_controlMapping[(int)'R'] = new RotateDownCommand;
- m_controlMapping[(int)'C'] = new FirstPersonCamCommand;
- //m_controlMapping[(int)'V'] = new ThirdPersonCamCommand;
- }
- void handleInputs(const std::vector<bool>& keyBuffer)
- {
- for(const auto& mapEntry : m_controlMapping)
- {
- if(keyBuffer[mapEntry.first])
- {
- mapEntry.second->execute(*m_playerObject);
- }
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement