From df05d6f688422930e8efc4a73df435e497f3776a Mon Sep 17 00:00:00 2001
From: nathansmithsmith <nathansmith7@mailfence.com>
Date: Tue, 24 Oct 2023 19:16:36 -0600
Subject: Targetting working

---
 src/entities/antifaShip.c | 54 ++++++++++++++++++++++++++++++++++-------------
 src/entities/antifaShip.h |  3 ++-
 2 files changed, 41 insertions(+), 16 deletions(-)

(limited to 'src')

diff --git a/src/entities/antifaShip.c b/src/entities/antifaShip.c
index d3dd3b3..7587358 100644
--- a/src/entities/antifaShip.c
+++ b/src/entities/antifaShip.c
@@ -76,11 +76,36 @@ void getClosestShipToAntifaShip(Game * game, Entity * entity) {
 		}
 	}
 }
+bool isAntifaShipGunInRange(Entity * ship, Entity * targetEntity) {
+	Vector3 directionAiming = Vector3RotateByQuaternion((Vector3){0.0, 0.0, 1.0}, ship->rotation);
+	Vector3 directionToTarget = Vector3Normalize(Vector3Subtract(targetEntity->position, ship->position));
+
+	return Vector3Distance(directionAiming, directionToTarget) <= ANTIFA_START_AUTO_TARGET_MAX;
+}
 
 void updateAntifaShipTarget(Game * game, Entity * entity) {
+	float t = GetFrameTime();
 	AntifaShip * data = (AntifaShip*)entity->data;
 
-	data->gunTarget = Vector3RotateByQuaternion((Vector3){0.0, 0.0, 1.0}, entity->rotation);
+	if (data->doAutoTarget) {
+		Entity * targetEntity = getEntityFromWorld(game->world, data->targetedEntityId);
+
+		// Lost target.
+		if (targetEntity == NULL) {
+			data->doAutoTarget = false;
+			return;
+		} else if (targetEntity->fingerprint != data->targetedEntityFingerprint) {
+			data->doAutoTarget = false;
+			return;
+		}
+
+		Vector3 directionToTarget = Vector3Normalize(Vector3Subtract(targetEntity->position, entity->position));
+		data->gunTarget = Vector3Lerp(data->gunTarget, directionToTarget, 0.1);
+		data->gunTarget = directionToTarget;
+		printf("%d %d\n", isAntifaShipGunInRange(entity, targetEntity), data->targetedEntityId);
+	} else {
+		data->gunTarget = Vector3RotateByQuaternion((Vector3){0.0, 0.0, 1.0}, entity->rotation);
+	}
 }
 
 void toggleAntifaShipAutoTarget(Game * game, Entity * entity) {
@@ -98,19 +123,11 @@ void toggleAntifaShipAutoTarget(Game * game, Entity * entity) {
 	if (data->targetedEntityId == ENTITY_NONE)
 		return;
 
-	// Try to lock on target.
-	Entity * targetEntity = getEntityFromWorld(game->world, data->targetedEntityId);
-	Vector3 directionAiming = Vector3RotateByQuaternion((Vector3){0.0, 0.0, 1.0}, entity->rotation);
-	Vector3 directionToTarget = Vector3Normalize(Vector3Subtract(entity->position, targetEntity->position));
-
-	// Cant lock on target.
-	if (Vector3Distance(directionAiming, directionToTarget) > ANTIFA_START_AUTO_TARGET_MAX) {
+	// Lock on target if can.
+	if (isAntifaShipGunInRange(entity, getEntityFromWorld(game->world, data->targetedEntityId)))
+		data->doAutoTarget = true;
+	else
 		data->targetedEntityId = ENTITY_NONE;
-		return;
-	}
-
-	// We locked up target (:
-	data->doAutoTarget = true;
 }
 
 // This fucker will fire a bullet!!!
@@ -118,8 +135,6 @@ void fireBulletAntifa(Game * game, Entity * entity) {
 	double t = GetTime();
 	AntifaShip * data = (AntifaShip*)entity->data;
 
-	updateAntifaShipTarget(game, entity);
-
 	// Let it cool down.
 	if (t - data->timeSinceLastBullet < ANTIFA_BULLET_COOLDOWN)
 		return;
@@ -177,6 +192,8 @@ void controlAntifaShipKeyboardAndMouse(Game * game, Entity * entity) {
 	}
 
 	// Shoot bullet.
+	if (IsMouseButtonPressed(MOUSE_RIGHT_BUTTON))
+		toggleAntifaShipAutoTarget(game, entity);
 	if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
 		fireBulletAntifa(game, entity);
 
@@ -211,6 +228,8 @@ void controlAntifaShipKeyboardAndMouse(Game * game, Entity * entity) {
 void updateAntifaShip(Game * game, Entity * entity) {
 	entityUpdateLastValues(entity);
 
+	updateAntifaShipTarget(game, entity);
+
 	switch (game->settings.controlMode) {
 		case JOYSTICK_CONTROL:
 			controlAntifaShipJoystick(game, entity);
@@ -233,6 +252,11 @@ void drawAntifaShip(Game * game, Entity * entity) {
 	// Draw bullet.
 	AntifaShip * data = (AntifaShip*)entity->data;
 
+	if (data->doAutoTarget) {
+		DrawLine3D(entity->position, Vector3Add(Vector3Scale(data->gunTarget, 100), entity->position), YELLOW);
+		//printVector3(data->gunTarget);
+	}
+
 	if (GetTime() - data->timeSinceLastBullet <= ANTIFA_DRAW_BULLET_FOR)
 		DrawRay(data->lastBulletShot.ray, BLUE);
 }
diff --git a/src/entities/antifaShip.h b/src/entities/antifaShip.h
index 54474ad..98c85cc 100644
--- a/src/entities/antifaShip.h
+++ b/src/entities/antifaShip.h
@@ -10,7 +10,8 @@
 #define ANTIFA_DRAW_BULLET_FOR 0.05
 
 // Auto target shit.
-#define ANTIFA_START_AUTO_TARGET_MAX 1.0 // How far off auto target can be entail it drops.
+#define ANTIFA_START_AUTO_TARGET_MAX 3.0 // How far off auto target can be entail it drops.
+#define ANTIFA_AUTO_TARGET_SPEED 1.0
 
 typedef struct AntifaShip {
 	Vector2 lastMouse;
-- 
cgit v1.2.3