diff options
author | nathansmithsmith <nathansmith7@mailfence.com> | 2023-07-31 19:36:10 -0600 |
---|---|---|
committer | nathansmithsmith <nathansmith7@mailfence.com> | 2023-07-31 19:36:10 -0600 |
commit | 0a720a2259aa7b10475854964ebf74900456d229 (patch) | |
tree | fd34b35e49f1d27c1da873b7fa68d6b24104d04d /src | |
parent | fc6e0037a2f0769fdbd4c18bd96f49d55f630757 (diff) |
Sergente stuff working
Diffstat (limited to 'src')
-rw-r--r-- | src/entities/caporale.c | 3 | ||||
-rw-r--r-- | src/entities/sergente.c | 101 | ||||
-rw-r--r-- | src/entities/sergente.h | 15 | ||||
-rw-r--r-- | src/entities/soldato.c | 6 | ||||
-rw-r--r-- | src/entity.c | 17 | ||||
-rw-r--r-- | src/entity.h | 1 | ||||
-rw-r--r-- | src/game.c | 2 | ||||
-rw-r--r-- | src/gameCommon.h | 1 | ||||
-rw-r--r-- | src/playerCamera.c | 6 |
9 files changed, 138 insertions, 14 deletions
diff --git a/src/entities/caporale.c b/src/entities/caporale.c index c8b4345..bfdc610 100644 --- a/src/entities/caporale.c +++ b/src/entities/caporale.c @@ -22,7 +22,8 @@ void initCaporale(Entity * entity, Game * game) { .controller.bangbang.speed = 80, .controller.bangbang.stopAt = 0.0, .controlType = ENTITY_FLY_TO_POINT_BANG_BANG, - .rotationSpeed = 0.5 + .rotationSpeed = 0.5, + .applyRotation = false }; } diff --git a/src/entities/sergente.c b/src/entities/sergente.c index 2614ec8..e9b0b71 100644 --- a/src/entities/sergente.c +++ b/src/entities/sergente.c @@ -4,17 +4,114 @@ void initSergente(Entity * entity, Game * game) { entity->model = &game->assets.models[SERGENTE_ASSET]; + entity->collisionModel = entityCreateCollisionModel(*entity->model); + entity->transformedCollisionModel = entityCreateCollisionModel(*entity->model); setEntityRadius(entity); - entity->velocity.angularVelocity = (AxisAngle){(Vector3){1.0, 1.0, 1.0}, 1.0}; + + // Allocate data. + entity->data = KF_MALLOC(sizeof(Sergente)); + + if (entity->data == NULL) { + ALLOCATION_ERROR; + return; + } + + Sergente * data = (Sergente*)entity->data; + + data->flyToPoint = (EntityFlyToPointInfo){ + .controller.bangbang.speed = 85.0, + .controller.bangbang.stopAt = 0.0, + .controlType = ENTITY_FLY_TO_POINT_BANG_BANG, + .rotationSpeed = 0.0 + }; + + createSergenteTarget(game, entity); } void closeSergente(Entity * entity) { + if (entity->data != NULL) + KF_FREE(entity->data); + + entityFreeCollisionModel(entity->collisionModel); + entityFreeCollisionModel(entity->transformedCollisionModel); +} + +void updateRotationSergente(Game * game, Entity * entity) { + float t = GetFrameTime(); + Entity * player = getEntityFromWorld(game->world, 0); + + // Get rotation. + Matrix matrix = MatrixLookAt(player->position, entity->position, (Vector3){0.0, 1.0, 0.0}); + Quaternion rotation = QuaternionInvert(QuaternionFromMatrix(matrix)); + + // Update current rotation. + entity->rotation = QuaternionSlerp(entity->rotation, rotation, t * SERGENTE_ROTATION_SPEED); } void updateSergente(Game * game, Entity * entity) { - entityUpdateRotation(entity); + entityUpdateLastValues(entity); + + Sergente * data = (Sergente*)entity->data; + + entityFlyToPoint(entity, data->target, &data->flyToPoint); + + // Create next point. + if (Vector3Distance(entity->position, data->target) <= SERGENTE_NEXT_POINT_THRESHOLD) + createSergenteTarget(game, entity); + + updateRotationSergente(game, entity); + entityCheckTransformedCollisionModel(entity); } void drawSergente(Game * game, Entity * entity) { entityDraw(entity); + + // Test if facing player always. + DrawLine3D( + entity->position, + Vector3Add( + entity->position, + Vector3Scale(Vector3RotateByQuaternion((Vector3){0.0, 0.0, 1.0}, entity->rotation), 500.0) + ), + BLUE + ); + + // The fucking debug line. + //DrawLine3D(entity->position, ((Sergente*)entity->data)->target, BLUE); +} + +void comeBackToPlayerSergente(Game * game, Entity * entity) { + Entity * player = getEntityFromWorld(game->world, 0); + Sergente * data = (Sergente*)entity->data; + + data->target = Vector3Subtract(player->position, entity->position); + data->target = Vector3Scale(data->target, SERGENTE_COME_BACK_PERCENT); + data->target = Vector3Add(entity->position, data->target); +} + +void createSergenteTarget(Game * game, Entity * entity) { + Sergente * data = (Sergente*)entity->data; + Entity * player = getEntityFromWorld(game->world, 0); + + // To far away. + if (Vector3Distance(entity->position, player->position) >= SERGENTE_COME_BACK_DIS) { + comeBackToPlayerSergente(game, entity); + return; + } + + SetRandomSeed(time(NULL)); + + // Set target direction. + data->target = (Vector3){ + GetRandomValue(-UCHAR_MAX, UCHAR_MAX), + GetRandomValue(-UCHAR_MAX, UCHAR_MAX), + GetRandomValue(-UCHAR_MAX, UCHAR_MAX) + }; + + data->target = Vector3Normalize(data->target); + + // Scale target and transform. + float dis = GetRandomValue(SERGENTE_TARGET_DIS_MIN, SERGENTE_TARGET_DIS_MAX); + data->target = Vector3Scale(data->target, dis); + data->target = Vector3Add(entity->position, data->target); } diff --git a/src/entities/sergente.h b/src/entities/sergente.h index 15d4c0d..9ef4e0f 100644 --- a/src/entities/sergente.h +++ b/src/entities/sergente.h @@ -4,9 +4,24 @@ #ifndef SERGENTE_H #define SERGENTE_H +#define SERGENTE_TARGET_DIS_MIN 1 +#define SERGENTE_TARGET_DIS_MAX 100 +#define SERGENTE_NEXT_POINT_THRESHOLD 0.1 +#define SERGENTE_COME_BACK_DIS 200.0 +#define SERGENTE_COME_BACK_PERCENT 0.5 +#define SERGENTE_ROTATION_SPEED 40.0 + +typedef struct Sergente { + EntityFlyToPointInfo flyToPoint; + Vector3 target; +} Sergente; + void initSergente(Entity * entity, Game * game); void closeSergente(Entity * entity); void updateSergente(Game * game, Entity * entity); void drawSergente(Game * game, Entity * entity); +// Sets target to random. +void createSergenteTarget(Game * game, Entity * entity); + #endif diff --git a/src/entities/soldato.c b/src/entities/soldato.c index f4ef912..46a0e23 100644 --- a/src/entities/soldato.c +++ b/src/entities/soldato.c @@ -37,13 +37,15 @@ void initSoldato(Entity * entity, Game * game) { .controller.bangbang.speed = 50, .controller.bangbang.stopAt = 0.0, .controlType = ENTITY_FLY_TO_POINT_BANG_BANG, - .rotationSpeed = 50.0 + .rotationSpeed = 50.0, + .applyRotation = true }; data->flyToPointFollowing = (EntityFlyToPointInfo){ .controller.speedPID = createPID(followingPID), .controlType = ENTITY_FLY_TO_POINT_PID, - .rotationSpeed = 50.0 + .rotationSpeed = 50.0, + .applyRotation = true }; } diff --git a/src/entity.c b/src/entity.c index fa610d0..4136463 100644 --- a/src/entity.c +++ b/src/entity.c @@ -460,10 +460,12 @@ void entityFlyToPoint(Entity * entity, Vector3 point, EntityFlyToPointInfo * inf Quaternion rotation = QuaternionInvert(QuaternionFromMatrix(matrix)); // Rotate this fucker. - if (info->rotationSpeed == 0.0) - entity->rotation = rotation; - else - entity->rotation = QuaternionSlerp(entity->rotation, rotation, t * info->rotationSpeed); + if (info->applyRotation) { + if (info->rotationSpeed == 0.0) + entity->rotation = rotation; + else + entity->rotation = QuaternionSlerp(entity->rotation, rotation, t * info->rotationSpeed); + } // Velocity control. float speed = 0.0; @@ -485,7 +487,12 @@ void entityFlyToPoint(Entity * entity, Vector3 point, EntityFlyToPointInfo * inf break; } - Matrix m = QuaternionToMatrix(QuaternionInvert(entity->rotation)); + Matrix m; + + if (info->applyRotation) + m = QuaternionToMatrix(QuaternionInvert(entity->rotation)); + else + m = matrix; // Accelerate. if (entity->useAcceleration) diff --git a/src/entity.h b/src/entity.h index 33e1925..37b80a5 100644 --- a/src/entity.h +++ b/src/entity.h @@ -174,6 +174,7 @@ typedef struct EntityFlyToPointInfo { uint8_t controlType; float rotationSpeed; // 0.0 to not use. + bool applyRotation; } EntityFlyToPointInfo; void entityFlyToPoint(Entity * entity, Vector3 point, EntityFlyToPointInfo * info); @@ -35,7 +35,7 @@ void initGame(Game * game) { WorldEntry entries[2] = { (WorldEntry){ENTITY_ANTIFA, (Vector3){0.0, 0.0, 0.0}, QuaternionIdentity()}, - (WorldEntry){ENTITY_CAPORALE, (Vector3){0.0, 0.0, 10.0}, QuaternionIdentity()} + (WorldEntry){ENTITY_SERGENTE, (Vector3){0.0, 0.0, 10.0}, QuaternionIdentity()} }; addEntriesToWorld( diff --git a/src/gameCommon.h b/src/gameCommon.h index 089d891..0f2a049 100644 --- a/src/gameCommon.h +++ b/src/gameCommon.h @@ -6,6 +6,7 @@ #include <time.h> #include <ctype.h> #include <stdbool.h> +#include <limits.h> #include <raylib.h> #include <raymath.h> diff --git a/src/playerCamera.c b/src/playerCamera.c index 6b165f3..f759166 100644 --- a/src/playerCamera.c +++ b/src/playerCamera.c @@ -23,7 +23,7 @@ void updatePlayerCamera(Camera3D * camera, Game * game) { // Up. camera->up = Vector3RotateByQuaternion((Vector3){0.0, 1.0, 0.0}, player->rotation); - //camera->target = player->position; - //camera->position = (Vector3){10.0, 10.0, 10.0}; - //camera->up = (Vector3){0.0, 1.0, 0.0}; + camera->target = player->position; + camera->position = (Vector3){10.0, 10.0, 10.0}; + camera->up = (Vector3){0.0, 1.0, 0.0}; } |