diff options
author | nathansmithsmith <nathansmith7@mailfence.com> | 2023-10-13 21:53:02 -0600 |
---|---|---|
committer | nathansmithsmith <nathansmith7@mailfence.com> | 2023-10-13 21:53:02 -0600 |
commit | b3bf954836efabd77921a92b153b7098c4481aa4 (patch) | |
tree | 58b5d152267cb42f17f8703e986888b0d88d61f9 | |
parent | f80670a77eb2680220aad131b62ed2b9cdfdfa2a (diff) |
Untested target locking code (:
-rw-r--r-- | src/entities/antifaShip.c | 65 | ||||
-rw-r--r-- | src/entities/antifaShip.h | 6 |
2 files changed, 71 insertions, 0 deletions
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(); diff --git a/src/entities/antifaShip.h b/src/entities/antifaShip.h index c272865..54474ad 100644 --- a/src/entities/antifaShip.h +++ b/src/entities/antifaShip.h @@ -9,6 +9,9 @@ #define ANTIFA_BULLET_COOLDOWN 0.5 #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. + typedef struct AntifaShip { Vector2 lastMouse; float forwardSpeed; @@ -18,6 +21,9 @@ typedef struct AntifaShip { Bullet lastBulletShot; Vector3 gunTarget; bool doAutoTarget; + + EntityId targetedEntityId; + EntityFingerprint targetedEntityFingerprint; } AntifaShip; void initAntifaShip(Entity * entity, Game * game); |