From f3f5fedbf591c10fa675a32103bab9480b42abe8 Mon Sep 17 00:00:00 2001
From: nathansmithsmith <thenathansmithsmith@gmail.com>
Date: Tue, 18 Jul 2023 05:54:30 -0600
Subject: Bullet system added

---
 src/entity.c | 40 ++++++++++++++++++++++++++++++----------
 1 file changed, 30 insertions(+), 10 deletions(-)

(limited to 'src/entity.c')

diff --git a/src/entity.c b/src/entity.c
index e918617..285744d 100644
--- a/src/entity.c
+++ b/src/entity.c
@@ -52,6 +52,7 @@ Entity createEntity(EntityType type, Game * game) {
 	// Set defaults.
 	Entity entity = (Entity){
 		.type = type,
+		.model = NULL,
 		.position = Vector3Zero(),
 		.rotation = QuaternionIdentity(),
 		.velocity = entityVelocityIdentity(),
@@ -59,6 +60,7 @@ Entity createEntity(EntityType type, Game * game) {
 		.useAcceleration = false,
 		.updateCb = info.updateCb,
 		.drawCb = info.drawCb,
+		.health = 1.0,
 		.data = NULL
 	};
 
@@ -152,7 +154,7 @@ void entityJoystickControl(Entity * entity, Vector3 stick, float speed) {
 	entityUpdatePosition(entity);
 }
 
-void entityFlightToPoint(Entity * entity, Vector3 point, PID * speedPID, float rotationSpeed) {
+void entityFlyToPoint(Entity * entity, Vector3 point, EntityFlyToPointInfo * info) {
 	float t = GetFrameTime();
 
 	// Get distance and direction.
@@ -164,19 +166,37 @@ void entityFlightToPoint(Entity * entity, Vector3 point, PID * speedPID, float r
 	Quaternion rotation = QuaternionInvert(QuaternionFromMatrix(matrix));
 
 	// Rotate this fucker.
-	if (rotationSpeed == 0.0)
+	if (info->rotationSpeed == 0.0)
 		entity->rotation = rotation;
 	else
-		entity->rotation = QuaternionSlerp(entity->rotation, rotation, t * rotationSpeed);
+		entity->rotation = QuaternionSlerp(entity->rotation, rotation, t * info->rotationSpeed);
 
 	// Velocity control.
-	float s = runPID(0.0, Vector3Length(dis), speedPID);
+	float speed = 0.0;
+
+	float distance = Vector3Length(dis);
+
+	switch (info->controlType) {
+		case ENTITY_FLY_TO_POINT_PID:
+			speed = runPID(0.0, -distance, &info->controller.speedPID);
+			break;
+		case ENTITY_FLY_TO_POINT_BANG_BANG:
+			speed = info->controller.bangbang.speed;
+
+			if (distance <= info->controller.bangbang.stopAt)
+				speed = 0.0;
+
+			break;
+		default: // Something is fucked up.
+			break;
+	}
+
 	Matrix m = QuaternionToMatrix(QuaternionInvert(entity->rotation));
 
 	// Accelerate.
 	if (entity->useAcceleration)
-		s = accelerateValue(
-			s,
+		speed = accelerateValue(
+			speed,
 			entity->lastVelocity.speed,
 			entity->acceleration.speedUp * t,
 			entity->acceleration.speedDown * t
@@ -184,13 +204,13 @@ void entityFlightToPoint(Entity * entity, Vector3 point, PID * speedPID, float r
 
 	// Velocity.
 	entity->velocity.velocity = (Vector3){
-		m.m2 * s,
-		m.m6 * s,
-		m.m10 * s
+		m.m2 * speed,
+		m.m6 * speed,
+		m.m10 * speed
 	};
 
 	entityUpdatePosition(entity);
 
-	entity->velocity.speed = s;
+	entity->velocity.speed = speed;
 	entity->lastVelocity = entity->velocity;
 }
-- 
cgit v1.2.3