From 54cb40aa0b220dd7dbfc1cabd4c7d9328ac50256 Mon Sep 17 00:00:00 2001 From: TrueBabyChaise Date: Fri, 21 May 2021 14:17:37 +0200 Subject: [PATCH] Modification on Controllable and Keyboard Add float variable instead of bool for the axes movement, and use of a map for classic keys Co-Authored-By: Benjamin HENRY <44569175+EternalRat@users.noreply.github.com> --- .../Controllable/ControllableComponent.hpp | 12 ++++------ .../Controllable/ControllableSystem.cpp | 15 +++--------- sources/System/Keyboard/KeyboardSystem.cpp | 23 +++++++++++-------- sources/System/Keyboard/KeyboardSystem.hpp | 1 + 4 files changed, 22 insertions(+), 29 deletions(-) diff --git a/sources/Component/Controllable/ControllableComponent.hpp b/sources/Component/Controllable/ControllableComponent.hpp index 1a784db6..fdab2cd8 100644 --- a/sources/Component/Controllable/ControllableComponent.hpp +++ b/sources/Component/Controllable/ControllableComponent.hpp @@ -13,13 +13,11 @@ namespace BBM class ControllableComponent : public WAL::Component { private: - bool _up; - bool _down; - bool _left; - bool _right; - bool _jump; - bool _bomb; - bool _pause; + float _moveX = 0; + float _moveZ = 0; + bool _jump = false; + bool _bomb = false; + bool _pause = false; public: //! @inherit diff --git a/sources/System/Controllable/ControllableSystem.cpp b/sources/System/Controllable/ControllableSystem.cpp index 4658e61d..78044610 100644 --- a/sources/System/Controllable/ControllableSystem.cpp +++ b/sources/System/Controllable/ControllableSystem.cpp @@ -17,18 +17,9 @@ namespace BBM void ControllableSystem::onFixedUpdate(WAL::Entity &entity) { - auto &controllable= entity.getComponent(); - auto &movable= entity.getComponent(); + auto &controllable = entity.getComponent(); + auto &movable = entity.getComponent(); - if (controllable._left) - movable.addForce(WAL::Vector3f(-1, 0, 0)); - if (controllable._right) - movable.addForce(WAL::Vector3f(1, 0, 0)); - if (controllable._down) - movable.addForce(WAL::Vector3f(0, 0, -1)); - if (controllable._up) - movable.addForce(WAL::Vector3f(0, 0, 1)); - if (controllable._jump) - movable.addForce(WAL::Vector3f(0, 1, 0)); + movable.addForce(WAL::Vector3f(controllable._moveX, 0, controllable._moveZ)); } } \ No newline at end of file diff --git a/sources/System/Keyboard/KeyboardSystem.cpp b/sources/System/Keyboard/KeyboardSystem.cpp index 3e4bd4aa..0c8216ba 100644 --- a/sources/System/Keyboard/KeyboardSystem.cpp +++ b/sources/System/Keyboard/KeyboardSystem.cpp @@ -19,20 +19,23 @@ namespace BBM { auto &keyboard = entity.getComponent(); auto &controllable= entity.getComponent(); + static const std::map keyPressedMap = { + {keyboard.keyJump, controllable._jump}, + {keyboard.keyBomb, controllable._bomb}, + {keyboard.keyPause, controllable._pause} + }; + for (auto key : keyPressedMap) + key.second = RAY::IsKeyPressed(key.first); + controllable._moveX = 0; + controllable._moveZ = 0; if (RAY::IsKeyPressed(keyboard.keyRight)) - controllable._right = true; + controllable._moveX += 1; if (RAY::IsKeyPressed(keyboard.keyLeft)) - controllable._left = true; + controllable._moveX -= 1; if (RAY::IsKeyPressed(keyboard.keyUp)) - controllable._up = true; + controllable._moveX += 1; if (RAY::IsKeyPressed(keyboard.keyDown)) - controllable._down = true; - if (RAY::IsKeyPressed(keyboard.keyBomb)) - controllable._bomb = true; - if (RAY::IsKeyPressed(keyboard.keyJump)) - controllable._jump = true; - if (RAY::IsKeyPressed(keyboard.keyPause)) - controllable._pause = true; + controllable._moveX -= 1; } } \ No newline at end of file diff --git a/sources/System/Keyboard/KeyboardSystem.hpp b/sources/System/Keyboard/KeyboardSystem.hpp index 3127c2a6..5e0e6387 100644 --- a/sources/System/Keyboard/KeyboardSystem.hpp +++ b/sources/System/Keyboard/KeyboardSystem.hpp @@ -6,6 +6,7 @@ #pragma once #include "lib/wal/sources/System/System.hpp" +#include namespace BBM {