aboutsummaryrefslogtreecommitdiff
path: root/src/entities
diff options
context:
space:
mode:
authornathansmithsmith <nathansmith7@mailfence.com>2023-10-27 15:00:19 -0600
committernathansmithsmith <nathansmith7@mailfence.com>2023-10-27 15:00:19 -0600
commit4a5bdb90ffdbd9974f86df14893e7287f2faa933 (patch)
treeecebe433777c400de51fbd7cb424cf0a37fad10f /src/entities
parentdf05d6f688422930e8efc4a73df435e497f3776a (diff)
Better targeting
Diffstat (limited to 'src/entities')
-rw-r--r--src/entities/antifaShip.c38
-rw-r--r--src/entities/antifaShip.h6
2 files changed, 23 insertions, 21 deletions
diff --git a/src/entities/antifaShip.c b/src/entities/antifaShip.c
index 7587358..c89c820 100644
--- a/src/entities/antifaShip.c
+++ b/src/entities/antifaShip.c
@@ -43,21 +43,17 @@ void closeAntifaShip(Entity * entity) {
entityFreeCollisionModel(entity->transformedCollisionModel);
}
-void getClosestShipToAntifaShip(Game * game, Entity * entity) {
+// Returns closest entity to ship or none.
+EntityId getClosestShipToAntifaShip(Game * game, Entity * entity) {
int i;
- AntifaShip * data = (AntifaShip*)entity->data;
-
- // Reset closest indeed (:
- data->targetedEntityId = ENTITY_NONE;
// Needs at least two entities to work correctly.
if (game->world.entitiesCount < 2)
- return;
+ return ENTITY_NONE;
// Start out with entity 1 as closest.
Entity * currentEntity = &game->world.entities[1];
- data->targetedEntityId = currentEntity->id;
- data->targetedEntityFingerprint = currentEntity->fingerprint;
+ EntityId closestId = currentEntity->id;
float closestDistance = Vector3Distance(currentEntity->position, entity->position);
float distance;
@@ -71,11 +67,13 @@ void getClosestShipToAntifaShip(Game * game, Entity * entity) {
// Is closest.
if (distance < closestDistance) {
closestDistance = distance;
- data->targetedEntityId = currentEntity->id;
- data->targetedEntityFingerprint = currentEntity->fingerprint;
+ closestId = currentEntity->id;
}
}
+
+ return closestId;
}
+
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));
@@ -97,11 +95,13 @@ void updateAntifaShipTarget(Game * game, Entity * entity) {
} else if (targetEntity->fingerprint != data->targetedEntityFingerprint) {
data->doAutoTarget = false;
return;
+ } else if (!isAntifaShipGunInRange(entity, targetEntity)) {
+ data->doAutoTarget = false;
+ return;
}
Vector3 directionToTarget = Vector3Normalize(Vector3Subtract(targetEntity->position, entity->position));
- data->gunTarget = Vector3Lerp(data->gunTarget, directionToTarget, 0.1);
- data->gunTarget = directionToTarget;
+ data->gunTarget = Vector3Lerp(data->gunTarget, directionToTarget, ANTIFA_AUTO_TARGET_SPEED * t);
printf("%d %d\n", isAntifaShipGunInRange(entity, targetEntity), data->targetedEntityId);
} else {
data->gunTarget = Vector3RotateByQuaternion((Vector3){0.0, 0.0, 1.0}, entity->rotation);
@@ -117,17 +117,19 @@ void toggleAntifaShipAutoTarget(Game * game, Entity * entity) {
return;
}
- getClosestShipToAntifaShip(game, entity);
+ // Get closest entity to target on.
+ EntityId closestId = getClosestShipToAntifaShip(game, entity);
- // No closest ):
- if (data->targetedEntityId == ENTITY_NONE)
+ if (closestId == ENTITY_NONE)
return;
+ data->targetedEntityId = closestId;
+ Entity * closestEntity = getEntityFromWorld(game->world, closestId);
+ data->targetedEntityFingerprint = closestEntity->fingerprint;
+
// Lock on target if can.
- if (isAntifaShipGunInRange(entity, getEntityFromWorld(game->world, data->targetedEntityId)))
+ if (isAntifaShipGunInRange(entity, closestEntity))
data->doAutoTarget = true;
- else
- data->targetedEntityId = ENTITY_NONE;
}
// This fucker will fire a bullet!!!
diff --git a/src/entities/antifaShip.h b/src/entities/antifaShip.h
index 98c85cc..89000c7 100644
--- a/src/entities/antifaShip.h
+++ b/src/entities/antifaShip.h
@@ -10,8 +10,8 @@
#define ANTIFA_DRAW_BULLET_FOR 0.05
// Auto target shit.
-#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
+#define ANTIFA_START_AUTO_TARGET_MAX 0.5 // How far off auto target can be entail it drops.
+#define ANTIFA_AUTO_TARGET_SPEED 100.0
typedef struct AntifaShip {
Vector2 lastMouse;
@@ -21,8 +21,8 @@ typedef struct AntifaShip {
double timeSinceLastBullet;
Bullet lastBulletShot;
Vector3 gunTarget;
- bool doAutoTarget;
+ bool doAutoTarget;
EntityId targetedEntityId;
EntityFingerprint targetedEntityFingerprint;
} AntifaShip;