From e24ad0fa50a0812c093c1330d4157ca3b717065f Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Mon, 31 May 2021 11:42:39 +0200 Subject: [PATCH] Cleaning up movements --- .../Controllable/ControllableComponent.hpp | 7 +++---- sources/Component/Renderer/CameraComponent.cpp | 5 +++-- sources/Component/Renderer/CameraComponent.hpp | 6 +++++- sources/Models/Vector2.hpp | 7 +++++++ sources/Models/Vector3.hpp | 8 ++++++++ sources/Runner/Runner.cpp | 2 +- sources/System/Controllable/ControllableSystem.cpp | 5 ++++- sources/System/Controllable/ControllableSystem.hpp | 3 +++ sources/System/Gamepad/GamepadSystem.cpp | 13 ++++++------- sources/System/Keyboard/KeyboardSystem.cpp | 11 +++++------ sources/System/Renderer/RenderScreenSystem.cpp | 2 ++ 11 files changed, 47 insertions(+), 22 deletions(-) diff --git a/sources/Component/Controllable/ControllableComponent.hpp b/sources/Component/Controllable/ControllableComponent.hpp index dffb1932..8376d43f 100644 --- a/sources/Component/Controllable/ControllableComponent.hpp +++ b/sources/Component/Controllable/ControllableComponent.hpp @@ -5,6 +5,7 @@ #pragma once +#include #include "Component/Component.hpp" #include "Entity/Entity.hpp" @@ -13,10 +14,8 @@ namespace BBM class ControllableComponent : public WAL::Component { public: - //! @brief input value for X axe - float moveX = 0; - //! @brief input value for Z axe - float moveZ = 0; + //! @brief The X and Z abscis of the movement. + Vector2f move; //! @brief input value for jump bool jump = false; //! @brief input value for bomb diff --git a/sources/Component/Renderer/CameraComponent.cpp b/sources/Component/Renderer/CameraComponent.cpp index 3f6c1239..5934d527 100644 --- a/sources/Component/Renderer/CameraComponent.cpp +++ b/sources/Component/Renderer/CameraComponent.cpp @@ -6,8 +6,9 @@ namespace BBM { - CameraComponent::CameraComponent(WAL::Entity &entity) - : Component(entity) + CameraComponent::CameraComponent(WAL::Entity &entity, Vector3f target) + : Component(entity), + target(target) {} WAL::Component *BBM::CameraComponent::clone(WAL::Entity &entity) const diff --git a/sources/Component/Renderer/CameraComponent.hpp b/sources/Component/Renderer/CameraComponent.hpp index 1aa5cc38..3f8e7fc5 100644 --- a/sources/Component/Renderer/CameraComponent.hpp +++ b/sources/Component/Renderer/CameraComponent.hpp @@ -5,6 +5,7 @@ #pragma once #include +#include namespace BBM { @@ -13,11 +14,14 @@ namespace BBM class CameraComponent : public WAL::Component { public: + //! @brief The camera's target, the cam will look at this position. + Vector3f target; + //! @inherit Component *clone(WAL::Entity &entity) const override; //! @brief Ctor - explicit CameraComponent(WAL::Entity &); + explicit CameraComponent(WAL::Entity &, Vector3f target = Vector3f()); //! @brief A camera component is copy constructable. CameraComponent(const CameraComponent &) = default; //! @brief Default destructor. diff --git a/sources/Models/Vector2.hpp b/sources/Models/Vector2.hpp index 07904949..629a11b8 100644 --- a/sources/Models/Vector2.hpp +++ b/sources/Models/Vector2.hpp @@ -129,6 +129,11 @@ namespace BBM { double mag = this->magnitude(); + if (mag == 0) { + this->x = 0; + this->y = 0; + return *this; + } this->x /= mag; this->y /= mag; return *this; @@ -138,6 +143,8 @@ namespace BBM { T mag = this->magnitude(); + if (mag == 0) + return Vector2(); return Vector2(this->x / mag, this->y / mag); } diff --git a/sources/Models/Vector3.hpp b/sources/Models/Vector3.hpp index 5579b382..def6d322 100644 --- a/sources/Models/Vector3.hpp +++ b/sources/Models/Vector3.hpp @@ -136,6 +136,12 @@ namespace BBM { double mag = this->magnitude(); + if (mag == 0) { + this->x = 0; + this->y = 0; + this->z = 0; + return *this; + } this->x /= mag; this->y /= mag; this->z /= mag; @@ -146,6 +152,8 @@ namespace BBM { T mag = this->magnitude(); + if (mag == 0) + return Vector3(); return Vector3(this->x / mag, this->y / mag, this->z / mag); } diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 7e0dce41..f6d9439e 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -72,7 +72,7 @@ namespace BBM .addComponent() .addComponent(); scene->addEntity("camera") - .addComponent(10, 10, 10) + .addComponent(0, 20, -5) .addComponent(); return scene; } diff --git a/sources/System/Controllable/ControllableSystem.cpp b/sources/System/Controllable/ControllableSystem.cpp index 3c6dc01c..0a9c789c 100644 --- a/sources/System/Controllable/ControllableSystem.cpp +++ b/sources/System/Controllable/ControllableSystem.cpp @@ -10,6 +10,8 @@ namespace BBM { + float ControllableSystem::speed = .25f; + ControllableSystem::ControllableSystem() : WAL::System({ typeid(ControllableComponent), @@ -21,7 +23,8 @@ namespace BBM { auto &controllable = entity.getComponent(); auto &movable = entity.getComponent(); + Vector2f move = controllable.move.normalized() * ControllableSystem::speed; - movable.addForce(Vector3f(controllable.moveX, controllable.jump, controllable.moveZ)); + movable.addForce(Vector3f(move.x, controllable.jump, move.y)); } } \ No newline at end of file diff --git a/sources/System/Controllable/ControllableSystem.hpp b/sources/System/Controllable/ControllableSystem.hpp index 576cb98a..fac4db08 100644 --- a/sources/System/Controllable/ControllableSystem.hpp +++ b/sources/System/Controllable/ControllableSystem.hpp @@ -13,6 +13,9 @@ namespace BBM class ControllableSystem : public WAL::System { public: + //! @brief The speed applied to every controllable entities. + static float speed; + //! @inherit void onFixedUpdate(WAL::Entity &entity) override; diff --git a/sources/System/Gamepad/GamepadSystem.cpp b/sources/System/Gamepad/GamepadSystem.cpp index 2a12a11b..03851be2 100644 --- a/sources/System/Gamepad/GamepadSystem.cpp +++ b/sources/System/Gamepad/GamepadSystem.cpp @@ -27,7 +27,7 @@ namespace BBM auto &controllable = entity.getComponent(); Gamepad gamepad(gamepadComponent.getID()); - const std::map keyPressedMap = { + const std::map keyPressedMap = { {gamepadComponent.keyJump, controllable.jump}, {gamepadComponent.keyBomb, controllable.bomb}, {gamepadComponent.keyPause, controllable.pause} @@ -35,11 +35,10 @@ namespace BBM for (auto key : keyPressedMap) key.second = gamepad.isPressed(key.first); - controllable.moveX = 0; - controllable.moveZ = 0; - controllable.moveX += gamepad.isPressed(gamepadComponent.keyRight); - controllable.moveX -= gamepad.isPressed(gamepadComponent.keyLeft); - controllable.moveX += gamepad.isPressed(gamepadComponent.keyUp); - controllable.moveX -= gamepad.isPressed(gamepadComponent.keyDown); + controllable.move = Vector2f(); + controllable.move.x += gamepad.isPressed(gamepadComponent.keyRight); + controllable.move.x -= gamepad.isPressed(gamepadComponent.keyLeft); + controllable.move.y += gamepad.isPressed(gamepadComponent.keyUp); + controllable.move.y -= gamepad.isPressed(gamepadComponent.keyDown); } } \ No newline at end of file diff --git a/sources/System/Keyboard/KeyboardSystem.cpp b/sources/System/Keyboard/KeyboardSystem.cpp index 686cd8bf..13ad9804 100644 --- a/sources/System/Keyboard/KeyboardSystem.cpp +++ b/sources/System/Keyboard/KeyboardSystem.cpp @@ -34,15 +34,14 @@ namespace BBM for (auto key : keyPressedMap) key.second = Keyboard::isDown(key.first); - controllable.moveX = 0; - controllable.moveZ = 0; + controllable.move = Vector2f(); if (Keyboard::isDown(keyboard.keyRight)) - controllable.moveX += 1; + controllable.move.x += 1; if (Keyboard::isDown(keyboard.keyLeft)) - controllable.moveX -= 1; + controllable.move.x -= 1; if (Keyboard::isDown(keyboard.keyUp)) - controllable.moveZ += 1; + controllable.move.y += 1; if (Keyboard::isDown(keyboard.keyDown)) - controllable.moveZ -= 1; + controllable.move.y -= 1; } } \ No newline at end of file diff --git a/sources/System/Renderer/RenderScreenSystem.cpp b/sources/System/Renderer/RenderScreenSystem.cpp index 5523ee48..8eba847a 100644 --- a/sources/System/Renderer/RenderScreenSystem.cpp +++ b/sources/System/Renderer/RenderScreenSystem.cpp @@ -27,6 +27,8 @@ namespace BBM void RenderScreenSystem::onUpdate(WAL::Entity &entity, std::chrono::nanoseconds dtime) { const auto &pos = entity.getComponent(); + const auto &cam = entity.getComponent(); _camera.setPosition(pos.position); + _camera.setTarget(cam.target); } } \ No newline at end of file