aboutsummaryrefslogtreecommitdiff
path: root/src/entity.c
diff options
context:
space:
mode:
authornathansmithsmith <thenathansmithsmith@gmail.com>2023-07-07 00:57:19 -0600
committernathansmithsmith <thenathansmithsmith@gmail.com>2023-07-07 00:57:19 -0600
commit028cf5d33d99274deea9567159a4eb07c13ef85c (patch)
treeb2d9f0ae8fb640fdbe1a41114c7c8314f9223103 /src/entity.c
parent416a5cbab21c480ae9e85b07fd9424452cbcb611 (diff)
This fucker is flying
Diffstat (limited to 'src/entity.c')
-rw-r--r--src/entity.c87
1 files changed, 87 insertions, 0 deletions
diff --git a/src/entity.c b/src/entity.c
new file mode 100644
index 0000000..0b6be9d
--- /dev/null
+++ b/src/entity.c
@@ -0,0 +1,87 @@
+#include "entity.h"
+#include "entities/antifaShip.h"
+
+// This fucker is used for creating entities.
+const EntityTypeInfo entityTypeInfo[ENTITY_TYPE_COUNT] = {
+ (EntityTypeInfo){initAntifaShip, closeAntifaShip, updateAntifaShip, drawAntifaShip}
+};
+
+Entity createEntity(EntityType type) {
+ EntityTypeInfo info = entityTypeInfo[type];
+
+ // Set defaults.
+ Entity entity = (Entity){
+ .type = type,
+ .position = Vector3Zero(),
+ .angularVelocity = 0,
+ .rotationAxis = Vector3Zero(),
+ .velocity = Vector3Zero(),
+ .rotation = QuaternionIdentity(),
+ .updateCb = info.updateCb,
+ .drawCb = info.drawCb,
+ .data = NULL
+ };
+
+ // Init.
+ info.initCb(&entity);
+
+ return entity;
+}
+
+void closeEntity(Entity * entity) {
+ entityTypeInfo[entity->type].closeCb(entity);
+}
+
+// Basic wireframe drawing.
+void entityDraw(Entity * entity) {
+ entity->model.transform = QuaternionToMatrix(entity->rotation);
+
+ DrawModelWires(
+ entity->model,
+ entity->position,
+ 1,
+ GREEN
+ );
+}
+
+void entityUpdatePosition(Entity * entity) {
+ float t = GetFrameTime();
+
+ Vector3 velocity = (Vector3){
+ entity->velocity.x * t,
+ entity->velocity.y * t,
+ entity->velocity.z * t
+ };
+
+ entity->position = Vector3Add(entity->position, velocity);
+}
+
+void entityUpdateRotation(Entity * entity) {
+ float t = GetFrameTime();
+
+ Quaternion angularRotation = QuaternionFromAxisAngle(
+ entity->rotationAxis,
+ entity->angularVelocity * t
+ );
+
+ entity->rotation = QuaternionMultiply(entity->rotation, angularRotation);
+}
+
+void entityJoystickControl(Entity * entity, Vector3 stick, float speed) {
+ // Set angular velocity.
+ Vector3 angularVelocity = Vector3Scale(stick, PI);
+ entity->angularVelocity = Vector3Length(angularVelocity);
+ entity->rotationAxis = stick;
+ entityUpdateRotation(entity);
+
+ // Set position.
+ Matrix m = QuaternionToMatrix(QuaternionInvert(entity->rotation));
+
+ entity->velocity = (Vector3){
+ m.m2 * speed,
+ m.m6 * speed,
+ m.m10 * speed,
+ };
+
+ entityUpdatePosition(entity);
+}