diff --git a/ExampleProjects/Astroids/src/Components/Components.h b/ExampleProjects/Astroids/src/Components/Components.h index 2fa4ad8..7ac7eef 100644 --- a/ExampleProjects/Astroids/src/Components/Components.h +++ b/ExampleProjects/Astroids/src/Components/Components.h @@ -13,15 +13,18 @@ struct Component_Movement { float maxSpeed = 1.0f; float acceleration = 1.0f; - float braking = 0.7f; + float brakingSpeed = 0.7f; float maxRotationSpeed = 1.0f; float currRotation = 0.0f; float currSpeed = 0.0f; - Duin::Vector2 inputDir; Duin::Vector2 prevVelocity; Duin::Vector2 currVelocity; + Duin::Vector2 inputVel; + float targetSpeed = 0.0f; + float targetRotation = 0.0f; + Component_Movement() = default; Component_Movement(float maxSpeed, float acceleration) : maxSpeed(maxSpeed), acceleration(acceleration) diff --git a/ExampleProjects/Astroids/src/Components/Handlers.h b/ExampleProjects/Astroids/src/Components/Handlers.h index 88065e1..77168c1 100644 --- a/ExampleProjects/Astroids/src/Components/Handlers.h +++ b/ExampleProjects/Astroids/src/Components/Handlers.h @@ -18,10 +18,28 @@ struct Handler_PlayerInput auto view = registry->View(); for (auto [entity, c_pinput, c_movement] : view.each()) { - float speed = (c_movement.maxSpeed * e.IsKeyDown(Duin::KEY_W)) - (c_movement.braking * e.IsKeyDown(Duin::KEY_S)); - c_movement.currRotation += c_movement.maxRotationSpeed * (e.IsKeyDown(Duin::KEY_A) * -1) * e.IsKeyDown(Duin::KEY_D); - c_movement.inputDir = Duin::Vector2::AngleToVector2(Duin::Maths::DegreesToRadians(c_movement.currRotation)); - c_movement.inputDir = (c_movement.inputDir * speed).Normalized(); + c_movement.targetSpeed = 0; + if (e.IsKeyDown(Duin::KEY_W)) + { + c_movement.targetSpeed += c_movement.maxSpeed; + } + if (e.IsKeyDown(Duin::KEY_S)) + { + c_movement.targetSpeed -= c_movement.brakingSpeed; + } + + c_movement.targetRotation = c_movement.currRotation; + if (e.IsKeyDown(Duin::KEY_A)) + { + c_movement.targetRotation -= c_movement.maxRotationSpeed; + } + if (e.IsKeyDown(Duin::KEY_D)) + { + c_movement.targetRotation += c_movement.maxRotationSpeed; + } + + c_movement.inputVel = Duin::Vector2::AngleToVector2(Duin::Maths::DegreesToRadians(c_movement.targetRotation - 90.0f)) + * c_movement.targetSpeed; } } }; @@ -41,12 +59,9 @@ struct Handler_PlayerMovement for (auto [entity, c_pinput, c_movement] : view.each()) { c_movement.prevVelocity = c_movement.currVelocity; - c_movement.currVelocity += c_movement.inputDir * c_movement.acceleration; + c_movement.currVelocity += c_movement.inputVel * c_movement.acceleration; c_movement.currVelocity.LimitLength(0.0f, c_movement.maxSpeed); - if (c_movement.inputDir.Magnitude() > 0) - { - c_movement.currRotation = Duin::Maths::RadiansToDegrees(c_movement.currVelocity.ToAngle()) + 90.0f; - } + c_movement.currRotation = c_movement.targetRotation; } } }; diff --git a/ExampleProjects/Astroids/src/Player.cpp b/ExampleProjects/Astroids/src/Player.cpp index 6bd5bf7..a9712bb 100644 --- a/ExampleProjects/Astroids/src/Player.cpp +++ b/ExampleProjects/Astroids/src/Player.cpp @@ -14,3 +14,7 @@ Player::Player(Duin::Registry* registry) Player::~Player() { } + +void Player::PhysicsUpdate(double pDelta) +{ +} diff --git a/ExampleProjects/Astroids/src/Player.h b/ExampleProjects/Astroids/src/Player.h index fffdec9..333ace0 100644 --- a/ExampleProjects/Astroids/src/Player.h +++ b/ExampleProjects/Astroids/src/Player.h @@ -7,8 +7,10 @@ class Player : public Duin::Node { public: + std::shared_ptr entity; + Player(Duin::Registry* registry); ~Player(); - std::shared_ptr entity; + void PhysicsUpdate(double pDelta) override; }; \ No newline at end of file