diff options
author | nathansmithsmith <nathansmith7@mailfence.com> | 2023-10-27 15:00:19 -0600 |
---|---|---|
committer | nathansmithsmith <nathansmith7@mailfence.com> | 2023-10-27 15:00:19 -0600 |
commit | 4a5bdb90ffdbd9974f86df14893e7287f2faa933 (patch) | |
tree | ecebe433777c400de51fbd7cb424cf0a37fad10f /src/entities/antifaShip.c | |
parent | df05d6f688422930e8efc4a73df435e497f3776a (diff) |
Better targeting
Diffstat (limited to 'src/entities/antifaShip.c')
-rw-r--r-- | src/entities/antifaShip.c | 38 |
1 files changed, 20 insertions, 18 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!!! |