Skip to content

Commit

Permalink
Astroids - fixed player movement
Browse files Browse the repository at this point in the history
  • Loading branch information
Y0L042 committed Jun 9, 2024
1 parent 8b28ccf commit 7b9ec68
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
7 changes: 5 additions & 2 deletions ExampleProjects/Astroids/src/Components/Components.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
33 changes: 24 additions & 9 deletions ExampleProjects/Astroids/src/Components/Handlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,28 @@ struct Handler_PlayerInput
auto view = registry->View<Component_PlayerInput, Component_Movement>();
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;
}
}
};
Expand All @@ -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;
}
}
};
Expand Down
4 changes: 4 additions & 0 deletions ExampleProjects/Astroids/src/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ Player::Player(Duin::Registry* registry)
Player::~Player()
{
}

void Player::PhysicsUpdate(double pDelta)
{
}
4 changes: 3 additions & 1 deletion ExampleProjects/Astroids/src/Player.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
class Player : public Duin::Node
{
public:
std::shared_ptr<Duin::Entity> entity;

Player(Duin::Registry* registry);
~Player();

std::shared_ptr<Duin::Entity> entity;
void PhysicsUpdate(double pDelta) override;
};

0 comments on commit 7b9ec68

Please sign in to comment.