aboutsummaryrefslogtreecommitdiff
path: root/src/entities/antifaShip.c
diff options
context:
space:
mode:
authornathansmithsmith <nathansmith7@mailfence.com>2023-10-24 19:16:36 -0600
committernathansmithsmith <nathansmith7@mailfence.com>2023-10-24 19:16:36 -0600
commitdf05d6f688422930e8efc4a73df435e497f3776a (patch)
tree13ce312a474abfc2c4169fd5f6fa6449436e467e /src/entities/antifaShip.c
parentb3bf954836efabd77921a92b153b7098c4481aa4 (diff)
Targetting working
Diffstat (limited to 'src/entities/antifaShip.c')
-rw-r--r--src/entities/antifaShip.c54
1 files changed, 39 insertions, 15 deletions
diff --git a/src/entities/antifaShip.c b/src/entities/antifaShip.c
index d3dd3b3..7587358 100644
--- a/src/entities/antifaShip.c
+++ b/src/entities/antifaShip.c
@@ -76,11 +76,36 @@ void getClosestShipToAntifaShip(Game * game, Entity * entity) {
}
}
}
+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));
+
+ return Vector3Distance(directionAiming, directionToTarget) <= ANTIFA_START_AUTO_TARGET_MAX;
+}
void updateAntifaShipTarget(Game * game, Entity * entity) {
+ float t = GetFrameTime();
AntifaShip * data = (AntifaShip*)entity->data;
- data->gunTarget = Vector3RotateByQuaternion((Vector3){0.0, 0.0, 1.0}, entity->rotation);
+ if (data->doAutoTarget) {
+ Entity * targetEntity = getEntityFromWorld(game->world, data->targetedEntityId);
+
+ // Lost target.
+ if (targetEntity == NULL) {
+ data->doAutoTarget = false;
+ return;
+ } else if (targetEntity->fingerprint != data->targetedEntityFingerprint) {
+ data->doAutoTarget = false;
+ return;
+ }
+
+ Vector3 directionToTarget = Vector3Normalize(Vector3Subtract(targetEntity->position, entity->position));
+ data->gunTarget = Vector3Lerp(data->gunTarget, directionToTarget, 0.1);
+ data->gunTarget = directionToTarget;
+ printf("%d %d\n", isAntifaShipGunInRange(entity, targetEntity), data->targetedEntityId);
+ } else {
+ data->gunTarget = Vector3RotateByQuaternion((Vector3){0.0, 0.0, 1.0}, entity->rotation);
+ }
}
void toggleAntifaShipAutoTarget(Game * game, Entity * entity) {
@@ -98,19 +123,11 @@ void toggleAntifaShipAutoTarget(Game * game, Entity * entity) {
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) {
+ // Lock on target if can.
+ if (isAntifaShipGunInRange(entity, getEntityFromWorld(game->world, data->targetedEntityId)))
+ data->doAutoTarget = true;
+ else
data->targetedEntityId = ENTITY_NONE;
- return;
- }
-
- // We locked up target (:
- data->doAutoTarget = true;
}
// This fucker will fire a bullet!!!
@@ -118,8 +135,6 @@ void fireBulletAntifa(Game * game, Entity * entity) {
double t = GetTime();
AntifaShip * data = (AntifaShip*)entity->data;
- updateAntifaShipTarget(game, entity);
-
// Let it cool down.
if (t - data->timeSinceLastBullet < ANTIFA_BULLET_COOLDOWN)
return;
@@ -177,6 +192,8 @@ void controlAntifaShipKeyboardAndMouse(Game * game, Entity * entity) {
}
// Shoot bullet.
+ if (IsMouseButtonPressed(MOUSE_RIGHT_BUTTON))
+ toggleAntifaShipAutoTarget(game, entity);
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
fireBulletAntifa(game, entity);
@@ -211,6 +228,8 @@ void controlAntifaShipKeyboardAndMouse(Game * game, Entity * entity) {
void updateAntifaShip(Game * game, Entity * entity) {
entityUpdateLastValues(entity);
+ updateAntifaShipTarget(game, entity);
+
switch (game->settings.controlMode) {
case JOYSTICK_CONTROL:
controlAntifaShipJoystick(game, entity);
@@ -233,6 +252,11 @@ void drawAntifaShip(Game * game, Entity * entity) {
// Draw bullet.
AntifaShip * data = (AntifaShip*)entity->data;
+ if (data->doAutoTarget) {
+ DrawLine3D(entity->position, Vector3Add(Vector3Scale(data->gunTarget, 100), entity->position), YELLOW);
+ //printVector3(data->gunTarget);
+ }
+
if (GetTime() - data->timeSinceLastBullet <= ANTIFA_DRAW_BULLET_FOR)
DrawRay(data->lastBulletShot.ray, BLUE);
}