From b3bf954836efabd77921a92b153b7098c4481aa4 Mon Sep 17 00:00:00 2001 From: nathansmithsmith Date: Fri, 13 Oct 2023 21:53:02 -0600 Subject: Untested target locking code (: --- src/entities/antifaShip.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) (limited to 'src/entities/antifaShip.c') diff --git a/src/entities/antifaShip.c b/src/entities/antifaShip.c index e1ab730..d3dd3b3 100644 --- a/src/entities/antifaShip.c +++ b/src/entities/antifaShip.c @@ -32,6 +32,7 @@ void initAntifaShip(Entity * entity, Game * game) { data->shouldInitMousePosition = true; data->timeSinceLastBullet = GetTime(); data->doAutoTarget = false; + data->targetedEntityId = ENTITY_NONE; } void closeAntifaShip(Entity * entity) { @@ -42,12 +43,76 @@ void closeAntifaShip(Entity * entity) { entityFreeCollisionModel(entity->transformedCollisionModel); } +void 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; + + // Start out with entity 1 as closest. + Entity * currentEntity = &game->world.entities[1]; + data->targetedEntityId = currentEntity->id; + data->targetedEntityFingerprint = currentEntity->fingerprint; + float closestDistance = Vector3Distance(currentEntity->position, entity->position); + 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) { + + // Get entity and distance. + currentEntity = &game->world.entities[i]; + distance = Vector3Distance(currentEntity->position, entity->position); + + // Is closest. + if (distance < closestDistance) { + closestDistance = distance; + data->targetedEntityId = currentEntity->id; + data->targetedEntityFingerprint = currentEntity->fingerprint; + } + } +} + void updateAntifaShipTarget(Game * game, Entity * entity) { AntifaShip * data = (AntifaShip*)entity->data; data->gunTarget = Vector3RotateByQuaternion((Vector3){0.0, 0.0, 1.0}, entity->rotation); } +void toggleAntifaShipAutoTarget(Game * game, Entity * entity) { + AntifaShip * data = (AntifaShip*)entity->data; + + // Turn off auto target. + if (data->doAutoTarget) { + data->doAutoTarget = false; + return; + } + + getClosestShipToAntifaShip(game, entity); + + // No closest ): + 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) { + data->targetedEntityId = ENTITY_NONE; + return; + } + + // We locked up target (: + data->doAutoTarget = true; +} + // This fucker will fire a bullet!!! void fireBulletAntifa(Game * game, Entity * entity) { double t = GetTime(); -- cgit v1.2.3