Fixed input
This commit is contained in:
		| @@ -32,6 +32,7 @@ void Game::update() { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   timeManager.update(); |   timeManager.update(); | ||||||
|  |   inputManager.update(); | ||||||
|   if(currentScene) currentScene->update(); |   if(currentScene) currentScene->update(); | ||||||
|   renderHost.update(shared_from_this()); |   renderHost.update(shared_from_this()); | ||||||
| } | } | ||||||
|   | |||||||
| @@ -5,17 +5,17 @@ | |||||||
|  |  | ||||||
| #pragma once | #pragma once | ||||||
| #include "util/Math.hpp" | #include "util/Math.hpp" | ||||||
|  | #include "input/InputBinds.hpp" | ||||||
|  |  | ||||||
| namespace Dawn { | namespace Dawn { | ||||||
|   class DawnGame; |   class DawnGame; | ||||||
|   typedef int_fast16_t inputbind_t; |  | ||||||
|  |  | ||||||
|   template<typename T> |   template<typename T> | ||||||
|   class IInputManager { |   class IInputManager { | ||||||
|     protected: |     protected: | ||||||
|       std::unordered_map<inputbind_t, std::vector<T>> binds; |       std::unordered_map<enum InputBind, std::vector<T>> binds; | ||||||
|       std::unordered_map<inputbind_t, float_t> valuesLeft; |       std::unordered_map<enum InputBind, float_t> valuesLeft; | ||||||
|       std::unordered_map<inputbind_t, float_t> valuesRight; |       std::unordered_map<enum InputBind, float_t> valuesRight; | ||||||
|       bool_t currentIsLeft = true; |       bool_t currentIsLeft = true; | ||||||
|  |  | ||||||
|       /** |       /** | ||||||
| @@ -34,7 +34,7 @@ namespace Dawn { | |||||||
|        * @param bind Bind to bind the axis to. |        * @param bind Bind to bind the axis to. | ||||||
|        * @param axis Axis to use for this bind. |        * @param axis Axis to use for this bind. | ||||||
|        */ |        */ | ||||||
|       void bind(const inputbind_t bind, const T axis) { |       void bind(const enum InputBind bind, const T axis) { | ||||||
|         this->binds[bind].push_back(axis); |         this->binds[bind].push_back(axis); | ||||||
|       } |       } | ||||||
|  |  | ||||||
| @@ -43,7 +43,7 @@ namespace Dawn { | |||||||
|        *  |        *  | ||||||
|        * @param bind Bind to remove all binds from. |        * @param bind Bind to remove all binds from. | ||||||
|        */ |        */ | ||||||
|       void unbind(const inputbind_t bind) { |       void unbind(const enum InputBind bind) { | ||||||
|         this->binds[bind].clear(); |         this->binds[bind].clear(); | ||||||
|       } |       } | ||||||
|  |  | ||||||
| @@ -61,7 +61,7 @@ namespace Dawn { | |||||||
|        * @param bind Bind to get the value of. |        * @param bind Bind to get the value of. | ||||||
|        * @return The current input state (between 0 and 1). |        * @return The current input state (between 0 and 1). | ||||||
|        */ |        */ | ||||||
|       float_t getValue(const inputbind_t bind) { |       float_t getValue(const enum InputBind bind) { | ||||||
|         if(this->currentIsLeft) { |         if(this->currentIsLeft) { | ||||||
|           auto exist = this->valuesLeft.find(bind); |           auto exist = this->valuesLeft.find(bind); | ||||||
|           return exist == this->valuesLeft.end() ? 0.0f : exist->second; |           return exist == this->valuesLeft.end() ? 0.0f : exist->second; | ||||||
| @@ -77,7 +77,7 @@ namespace Dawn { | |||||||
|        * @param bind Bind to get the value of. |        * @param bind Bind to get the value of. | ||||||
|        * @return The value of the bind, last frame. |        * @return The value of the bind, last frame. | ||||||
|        */ |        */ | ||||||
|       float_t getValueLastUpdate(const inputbind_t bind) { |       float_t getValueLastUpdate(const enum InputBind bind) { | ||||||
|         if(this->currentIsLeft) { |         if(this->currentIsLeft) { | ||||||
|           auto exist = this->valuesRight.find(bind); |           auto exist = this->valuesRight.find(bind); | ||||||
|           return exist == this->valuesRight.end() ? 0.0f : exist->second; |           return exist == this->valuesRight.end() ? 0.0f : exist->second; | ||||||
| @@ -99,15 +99,15 @@ namespace Dawn { | |||||||
|        * @param positive Bind to use for the positive axis. |        * @param positive Bind to use for the positive axis. | ||||||
|        * @return A value between -1 and 1. |        * @return A value between -1 and 1. | ||||||
|        */ |        */ | ||||||
|       float_t getAxis(const inputbind_t negative, const inputbind_t positive) { |       float_t getAxis(const enum InputBind negative, const enum InputBind positive) { | ||||||
|         return -getValue(negative) + getValue(positive); |         return -getValue(negative) + getValue(positive); | ||||||
|       } |       } | ||||||
|  |  | ||||||
|       glm::vec2 getAxis2D( |       glm::vec2 getAxis2D( | ||||||
|         const inputbind_t negativeX, |         const enum InputBind negativeX, | ||||||
|         const inputbind_t positiveX, |         const enum InputBind positiveX, | ||||||
|         const inputbind_t negativeY, |         const enum InputBind negativeY, | ||||||
|         const inputbind_t positiveY |         const enum InputBind positiveY | ||||||
|       ) { |       ) { | ||||||
|         return glm::vec2( |         return glm::vec2( | ||||||
|           getAxis(negativeX, positiveX), |           getAxis(negativeX, positiveX), | ||||||
| @@ -122,7 +122,7 @@ namespace Dawn { | |||||||
|        * @param y Y Axis bind. |        * @param y Y Axis bind. | ||||||
|        * @return 2D vector of the two given input binds. |        * @return 2D vector of the two given input binds. | ||||||
|        */ |        */ | ||||||
|       glm::vec2 getAxis2D(const inputbind_t x, const inputbind_t y) { |       glm::vec2 getAxis2D(const enum InputBind x, const enum InputBind y) { | ||||||
|         return glm::vec2(getValue(x), getValue(y)); |         return glm::vec2(getValue(x), getValue(y)); | ||||||
|       } |       } | ||||||
|  |  | ||||||
| @@ -133,7 +133,7 @@ namespace Dawn { | |||||||
|        * @param bind Bind to check if pressed. |        * @param bind Bind to check if pressed. | ||||||
|        * @return True if value is non-zero, or false for zero. |        * @return True if value is non-zero, or false for zero. | ||||||
|        */ |        */ | ||||||
|       bool_t isDown(const inputbind_t bind) { |       bool_t isDown(const enum InputBind bind) { | ||||||
|         return this->getValue(bind) != 0.0f; |         return this->getValue(bind) != 0.0f; | ||||||
|       } |       } | ||||||
|  |  | ||||||
| @@ -144,7 +144,7 @@ namespace Dawn { | |||||||
|        * @param bind Bind to check if pressed. |        * @param bind Bind to check if pressed. | ||||||
|        * @return True if down this frame and not down last frame. |        * @return True if down this frame and not down last frame. | ||||||
|        */ |        */ | ||||||
|       bool_t isPressed(const inputbind_t bind) { |       bool_t isPressed(const enum InputBind bind) { | ||||||
|         return this->getValue(bind) != 0 && this->getValueLastUpdate(bind) == 0; |         return this->getValue(bind) != 0 && this->getValueLastUpdate(bind) == 0; | ||||||
|       } |       } | ||||||
|  |  | ||||||
| @@ -155,7 +155,7 @@ namespace Dawn { | |||||||
|        * @param bind Bind to check if released. |        * @param bind Bind to check if released. | ||||||
|        * @return True if up this frame, and down last frame. |        * @return True if up this frame, and down last frame. | ||||||
|        */ |        */ | ||||||
|       bool_t wasReleased(const inputbind_t bind) { |       bool_t wasReleased(const enum InputBind bind) { | ||||||
|         return this->getValue(bind) == 0 && this->getValueLastUpdate(bind) != 0; |         return this->getValue(bind) == 0 && this->getValueLastUpdate(bind) != 0; | ||||||
|       } |       } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -123,6 +123,32 @@ glm::quat SceneItemTransform::getLocalRotation() { | |||||||
|   return this->localRotation; |   return this->localRotation; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | void SceneItemTransform::setParent(std::shared_ptr<SceneItem> parent) { | ||||||
|  |   if(this->parent.lock() == parent) return; | ||||||
|  |  | ||||||
|  |   auto self = dynamic_cast<SceneItem*>(this); | ||||||
|  |   assertNotNull(self, "Cannot set parent of null item?"); | ||||||
|  |  | ||||||
|  |   if(auto oldParent = this->parent.lock()) { | ||||||
|  |     auto &children = oldParent->children; | ||||||
|  |     children.erase( | ||||||
|  |       std::remove_if( | ||||||
|  |         children.begin(), | ||||||
|  |         children.end(), | ||||||
|  |         [self](std::weak_ptr<SceneItem> &item) { | ||||||
|  |           return item.lock() == self->shared_from_this(); | ||||||
|  |         } | ||||||
|  |       ), | ||||||
|  |       children.end() | ||||||
|  |     ); | ||||||
|  |   } | ||||||
|  |   this->parent = parent; | ||||||
|  |   if(auto newParent = this->parent.lock()) { | ||||||
|  |     newParent->children.push_back(self->shared_from_this()); | ||||||
|  |   } | ||||||
|  |   this->updateWorldTransformFromParent(); | ||||||
|  | } | ||||||
|  |  | ||||||
| void SceneItemTransform::setLocalTransform(const glm::mat4 transform) { | void SceneItemTransform::setLocalTransform(const glm::mat4 transform) { | ||||||
|   this->transformLocal = transform; |   this->transformLocal = transform; | ||||||
|   this->updateLocalValuesFromLocalTransform(); |   this->updateLocalValuesFromLocalTransform(); | ||||||
|   | |||||||
| @@ -107,6 +107,13 @@ namespace Dawn { | |||||||
|        */ |        */ | ||||||
|       glm::quat getLocalRotation(); |       glm::quat getLocalRotation(); | ||||||
|        |        | ||||||
|  |       /** | ||||||
|  |        * Sets the parent of this item. | ||||||
|  |        *  | ||||||
|  |        * @param parent Parent of this item. | ||||||
|  |        */ | ||||||
|  |       void setParent(std::shared_ptr<SceneItem> parent); | ||||||
|  |  | ||||||
|       /** |       /** | ||||||
|        * Sets the transform of this item within local space (relative to parent) |        * Sets the transform of this item within local space (relative to parent) | ||||||
|        *  |        *  | ||||||
|   | |||||||
| @@ -44,6 +44,25 @@ void InputManager::init(const std::shared_ptr<Game> game) { | |||||||
|       action == GLFW_PRESS ? 1.0f : 0.0f |       action == GLFW_PRESS ? 1.0f : 0.0f | ||||||
|     ); |     ); | ||||||
|   }); |   }); | ||||||
|  |  | ||||||
|  |   glfwSetKeyCallback(window, []( | ||||||
|  |     GLFWwindow* window, | ||||||
|  |     int32_t key, | ||||||
|  |     int32_t scancode, | ||||||
|  |     int32_t action, | ||||||
|  |     int32_t mods | ||||||
|  |   ) { | ||||||
|  |     auto game = (Game*)glfwGetWindowUserPointer(window); | ||||||
|  |     game->inputManager.rawInputValues[key] = ( | ||||||
|  |       action == GLFW_PRESS ? 1.0f : 0.0f | ||||||
|  |     ); | ||||||
|  |   }); | ||||||
|  |  | ||||||
|  |   // Set default values | ||||||
|  |   this->bind(InputBind::Up, GLFW_KEY_W); | ||||||
|  |   this->bind(InputBind::Up, GLFW_KEY_UP); | ||||||
|  |   this->bind(InputBind::Down, GLFW_KEY_S); | ||||||
|  |   this->bind(InputBind::Down, GLFW_KEY_DOWN); | ||||||
| } | } | ||||||
|  |  | ||||||
| float_t InputManager::getInputValue(int32_t axis) { | float_t InputManager::getInputValue(int32_t axis) { | ||||||
|   | |||||||
| @@ -6,4 +6,5 @@ | |||||||
| target_sources(${DAWN_TARGET_NAME} | target_sources(${DAWN_TARGET_NAME} | ||||||
|   PRIVATE |   PRIVATE | ||||||
|     Entity.cpp |     Entity.cpp | ||||||
|  |     Player.cpp | ||||||
| ) | ) | ||||||
| @@ -10,7 +10,7 @@ using namespace Dawn; | |||||||
|  |  | ||||||
| void Entity::onInit() { | void Entity::onInit() { | ||||||
|   auto world = this->world.lock(); |   auto world = this->world.lock(); | ||||||
|   assertNotNull(world, "World is no longer active."); |   assertNotNull(world, "World is no longer active?  "); | ||||||
| } | } | ||||||
|  |  | ||||||
| void Entity::onDispose() { | void Entity::onDispose() { | ||||||
|   | |||||||
							
								
								
									
										23
									
								
								src/dawnrpg/component/entity/Player.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								src/dawnrpg/component/entity/Player.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,23 @@ | |||||||
|  | // Copyright (c) 2024 Dominic Masters | ||||||
|  | //  | ||||||
|  | // This software is released under the MIT License. | ||||||
|  | // https://opensource.org/licenses/MIT | ||||||
|  |  | ||||||
|  | #include "Player.hpp" | ||||||
|  | #include "scene/Scene.hpp" | ||||||
|  | #include "input/InputBinds.hpp" | ||||||
|  |  | ||||||
|  | using namespace Dawn; | ||||||
|  |  | ||||||
|  | void Player::onInit() { | ||||||
|  |   listenerUpdate = getScene()->onUnpausedUpdate.listen([this](float delta) { | ||||||
|  |     InputManager &im = getGame()->inputManager; | ||||||
|  |     if(im.isDown(InputBind::Up)) { | ||||||
|  |        | ||||||
|  |     } | ||||||
|  |   }); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void Player::onDispose() { | ||||||
|  |   listenerUpdate(); | ||||||
|  | } | ||||||
							
								
								
									
										18
									
								
								src/dawnrpg/component/entity/Player.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								src/dawnrpg/component/entity/Player.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,18 @@ | |||||||
|  | // Copyright (c) 2024 Dominic Masters | ||||||
|  | //  | ||||||
|  | // This software is released under the MIT License. | ||||||
|  | // https://opensource.org/licenses/MIT | ||||||
|  |  | ||||||
|  | #pragma once | ||||||
|  | #include "scene/SceneComponent.hpp" | ||||||
|  |  | ||||||
|  | namespace Dawn { | ||||||
|  |   class Player : public SceneComponent { | ||||||
|  |     protected: | ||||||
|  |       std::function<void()> listenerUpdate; | ||||||
|  |  | ||||||
|  |     public: | ||||||
|  |       void onInit() override; | ||||||
|  |       void onDispose() override; | ||||||
|  |   }; | ||||||
|  | } | ||||||
							
								
								
									
										13
									
								
								src/dawnrpg/input/InputBinds.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								src/dawnrpg/input/InputBinds.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,13 @@ | |||||||
|  | // Copyright (c) 2024 Dominic Masters | ||||||
|  | //  | ||||||
|  | // This software is released under the MIT License. | ||||||
|  | // https://opensource.org/licenses/MIT | ||||||
|  |  | ||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | namespace Dawn { | ||||||
|  |   enum class InputBind : uint8_t { | ||||||
|  |     Up, | ||||||
|  |     Down | ||||||
|  |   }; | ||||||
|  | } | ||||||
| @@ -4,6 +4,7 @@ | |||||||
| // https://opensource.org/licenses/MIT | // https://opensource.org/licenses/MIT | ||||||
|  |  | ||||||
| #include "PlayerPrefab.hpp" | #include "PlayerPrefab.hpp" | ||||||
|  | #include "display/mesh/QuadMesh.hpp" | ||||||
|  |  | ||||||
| using namespace Dawn; | using namespace Dawn; | ||||||
|  |  | ||||||
| @@ -12,9 +13,27 @@ struct PlayerPrefab Dawn::createPlayerPrefab( | |||||||
| ) { | ) { | ||||||
|   struct PlayerPrefab player; |   struct PlayerPrefab player; | ||||||
|   player.item = world->getScene()->createSceneItem(); |   player.item = world->getScene()->createSceneItem(); | ||||||
|  |   player.item->setParent(world->getItem()); | ||||||
|  |  | ||||||
|   player.entity = player.item->addComponent<Entity>(); |   player.entity = player.item->addComponent<Entity>(); | ||||||
|   player.entity->world = world; |   player.entity->world = world; | ||||||
|  |  | ||||||
|  |   player.player = player.item->addComponent<Player>(); | ||||||
|  |  | ||||||
|  |   player.mesh = std::make_shared<Mesh>(); | ||||||
|  |   player.mesh->createBuffers(QUAD_VERTICE_COUNT, QUAD_INDICE_COUNT); | ||||||
|  |   QuadMesh::buffer( | ||||||
|  |     player.mesh, | ||||||
|  |     glm::vec4(0, 0, 1, 1), | ||||||
|  |     glm::vec4(0, 0, 1, 1), | ||||||
|  |     0, 0 | ||||||
|  |   ); | ||||||
|  |  | ||||||
|  |   player.meshRenderer = player.item->addComponent<MeshRenderer>(); | ||||||
|  |   player.meshRenderer->mesh = player.mesh; | ||||||
|  |  | ||||||
|  |   player.material = player.item->addComponent<SimpleTexturedMaterial>(); | ||||||
|  |   player.material->setColor(COLOR_MAGENTA); | ||||||
|  |  | ||||||
|   return player; |   return player; | ||||||
| } | } | ||||||
| @@ -6,11 +6,18 @@ | |||||||
| #pragma once | #pragma once | ||||||
| #include "prefab/Prefab.hpp" | #include "prefab/Prefab.hpp" | ||||||
| #include "component/world/World.hpp" | #include "component/world/World.hpp" | ||||||
|  | #include "component/display/MeshRenderer.hpp" | ||||||
|  | #include "component/display/material/SimpleTexturedMaterial.hpp" | ||||||
|  | #include "component/entity/Player.hpp" | ||||||
|  |  | ||||||
| namespace Dawn { | namespace Dawn { | ||||||
|   struct PlayerPrefab : public Prefab { |   struct PlayerPrefab : public Prefab { | ||||||
|     public: |     public: | ||||||
|       std::shared_ptr<Entity> entity; |       std::shared_ptr<Entity> entity; | ||||||
|  |       std::shared_ptr<Player> player; | ||||||
|  |       std::shared_ptr<Mesh> mesh; | ||||||
|  |       std::shared_ptr<MeshRenderer> meshRenderer; | ||||||
|  |       std::shared_ptr<SimpleTexturedMaterial> material; | ||||||
|   }; |   }; | ||||||
|  |  | ||||||
|   struct PlayerPrefab createPlayerPrefab(const std::shared_ptr<World> world); |   struct PlayerPrefab createPlayerPrefab(const std::shared_ptr<World> world); | ||||||
|   | |||||||
| @@ -4,6 +4,7 @@ | |||||||
| // https://opensource.org/licenses/MIT | // https://opensource.org/licenses/MIT | ||||||
|  |  | ||||||
| #include "WorldPrefab.hpp" | #include "WorldPrefab.hpp" | ||||||
|  | #include "display/mesh/QuadMesh.hpp"   | ||||||
|  |  | ||||||
| using namespace Dawn; | using namespace Dawn; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -7,7 +7,7 @@ | |||||||
| #include "scenes/SceneList.hpp" | #include "scenes/SceneList.hpp" | ||||||
| #include "prefab/PlayerPrefab.hpp" | #include "prefab/PlayerPrefab.hpp" | ||||||
| #include "prefab/WorldPrefab.hpp" | #include "prefab/WorldPrefab.hpp" | ||||||
| // #include "component/display/Camera.hpp" | #include "component/display/Camera.hpp" | ||||||
| // #include "prefab/SimpleSpinningCube.hpp" | // #include "prefab/SimpleSpinningCube.hpp" | ||||||
| // #include "component/display/material/SimpleTexturedMaterial.hpp" | // #include "component/display/material/SimpleTexturedMaterial.hpp" | ||||||
| // #include "display/mesh/CubeMesh.hpp" | // #include "display/mesh/CubeMesh.hpp" | ||||||
| @@ -24,10 +24,10 @@ | |||||||
| using namespace Dawn; | using namespace Dawn; | ||||||
|  |  | ||||||
| void Dawn::worldScene(Scene &s) { | void Dawn::worldScene(Scene &s) { | ||||||
|   // auto cameraItem = s.createSceneItem(); |   auto cameraItem = s.createSceneItem(); | ||||||
|   // auto camera = cameraItem->addComponent<Camera>(); |   auto camera = cameraItem->addComponent<Camera>(); | ||||||
|   // cameraItem->lookAt({ 3, 3, 3 }, { 0, 0, 0 }, { 0, 1, 0 }); |   cameraItem->lookAt({ 3, 3, 3 }, { 0, 0, 0 }, { 0, 1, 0 }); | ||||||
|   // camera->clipFar = 99999.99f; |   camera->clipFar = 99999.99f; | ||||||
|  |  | ||||||
|   // auto cube = s.createSceneItem(); |   // auto cube = s.createSceneItem(); | ||||||
|   // auto cubeMesh = std::make_shared<Mesh>(); |   // auto cubeMesh = std::make_shared<Mesh>(); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user