diff options
author | nathansmithsmith <nathansmith7@mailfence.com> | 2023-11-04 20:25:08 -0600 |
---|---|---|
committer | nathansmithsmith <nathansmith7@mailfence.com> | 2023-11-04 20:25:08 -0600 |
commit | 5627fd8128957710c6f16330b2bc1bc3251c5355 (patch) | |
tree | 63a175c8e8f0e1cbee756dd89b841477feaa7159 /src/entities/antifaShip.c | |
parent | 89162a2e733fdca7733a0ce57c610461baf2dfaa (diff) |
Getting it ready for real level making
Diffstat (limited to 'src/entities/antifaShip.c')
-rw-r--r-- | src/entities/antifaShip.c | 38 |
1 files changed, 18 insertions, 20 deletions
diff --git a/src/entities/antifaShip.c b/src/entities/antifaShip.c index 60f9957..5383537 100644 --- a/src/entities/antifaShip.c +++ b/src/entities/antifaShip.c @@ -46,8 +46,14 @@ void closeAntifaShip(Entity * entity) { entityFreeCollisionModel(entity->transformedCollisionModel); } -// Returns closest entity to ship or none. -EntityId getClosestShipToAntifaShip(Game * game, Entity * entity) { +float getAntifaShipAimDistance(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); +} + +// Returns closest entity to ship and in range or none. +EntityId getShipInRangeAntifaShip(Game * game, Entity * entity) { int i; // Needs at least two entities to work correctly. @@ -56,19 +62,19 @@ EntityId getClosestShipToAntifaShip(Game * game, Entity * entity) { // Start out with entity 1 as closest. Entity * currentEntity = &game->world.entities[1]; - EntityId closestId = currentEntity->id; - float closestDistance = Vector3Distance(currentEntity->position, entity->position); + EntityId closestId = ENTITY_NONE; + float closestDistance = (float)INT_MAX; float distance; - // This entity will only ever be at id 0 so skip it. Also skip the one after that (: - for (i = 2; i < game->world.entitiesCount; ++i) { + // This entity will only ever be at id 0 so skip it. + for (i = 1; i < game->world.entitiesCount; ++i) { // Get entity and distance. currentEntity = &game->world.entities[i]; distance = Vector3Distance(currentEntity->position, entity->position); - // Is closest. - if (distance < closestDistance) { + // Is closest and in range. + if (distance < closestDistance && getAntifaShipAimDistance(entity, currentEntity) <= ANTIFA_START_AUTO_TARGET_MAX) { closestDistance = distance; closestId = currentEntity->id; } @@ -77,12 +83,6 @@ EntityId getClosestShipToAntifaShip(Game * game, Entity * entity) { return closestId; } -float getAntifaShipAimDistance(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); -} - void updateAntifaShipTarget(Game * game, Entity * entity) { float t = GetFrameTime(); AntifaShip * data = (AntifaShip*)entity->data; @@ -110,8 +110,9 @@ void updateAntifaShipTarget(Game * game, Entity * entity) { Vector3 directionToTarget = Vector3Normalize(Vector3Subtract(targetEntity->position, entity->position)); // Update target. - float speed = powf(ANTIFA_TARGETING_SPEED / aimDistance, 2.0); + float speed = ANTIFA_TARGETING_SPEED / aimDistance; data->gunTarget = Vector3Lerp(data->gunTarget, directionToTarget, speed * t); + printf("%f\n", speed); // Is on target. data->isOnTarget = traceRayToEntity(*targetEntity, (Ray){entity->position, data->gunTarget}).hit; @@ -130,8 +131,7 @@ void toggleAntifaShipAutoTarget(Game * game, Entity * entity) { return; } - // Get closest entity to target on. - EntityId closestId = getClosestShipToAntifaShip(game, entity); + EntityId closestId = getShipInRangeAntifaShip(game, entity); if (closestId == ENTITY_NONE) return; @@ -140,9 +140,7 @@ void toggleAntifaShipAutoTarget(Game * game, Entity * entity) { Entity * closestEntity = getEntityFromWorld(game->world, closestId); data->targetedEntityFingerprint = closestEntity->fingerprint; - // Lock on target if can. - if (getAntifaShipAimDistance(entity, closestEntity) <= ANTIFA_START_AUTO_TARGET_MAX) - data->doAutoTarget = true; + data->doAutoTarget = true; } // This fucker will fire a bullet!!! |