quadrants/source/entity.cpp

49 lines
1.5 KiB
C++
Raw Permalink Normal View History

2023-02-27 14:02:30 +01:00
#include "core.hpp"
struct QuadrantCoords {
long x;
long y;
};
struct Entity {
float orientation;
Vector2 speed;
Vector2 localPosition;
QuadrantCoords quadrantPosition;
};
struct SpaceshipEntity {
Entity entity;
float delayedOrientation;
Texture fusilageTexture;
Texture enginesTexture;
};
Vector2 const AddEntityForce(Entity& entity, float const direction, float const magnitude) {
constexpr float MAGNITUDE_MAX = 0.1f;
Vector2 const appliedForce = LengthDirection(Vector2{magnitude, magnitude}, direction);
entity.speed = (entity.speed + appliedForce);
return appliedForce;
}
void RemoveEntityForce(Entity& entity, float const magnitude) {
constexpr float MIN_DRIFT = 0.001f;
// As LengthDirection deals in vector math, the magnitude will have to be converted from a one-
// dimensional value into a two-dimensional one. Additionally, this magnitude vector needs its
// x and y flipping according to the relative x and y values of the subject Entity.
Vector2 const relativeMagnitude = {
(magnitude * (SIGN(entity.speed.x) * -1)),
(magnitude * (SIGN(entity.speed.y) * -1))
};
// Only deduct the maximum possible deductable speed before the value would inverse on itself.
entity.speed = (entity.speed + Vector2{
(entity.speed.x < 0.0f) ? MAX(relativeMagnitude.x, entity.speed.x) :
MIN(relativeMagnitude.x, entity.speed.x),
(entity.speed.y < 0.0f) ? MAX(relativeMagnitude.y, entity.speed.y) :
MIN(relativeMagnitude.y, entity.speed.y)
});
}