diff options
author | nathansmithsmith <nathansmith7@mailfence.com> | 2023-07-30 23:51:37 -0600 |
---|---|---|
committer | nathansmithsmith <nathansmith7@mailfence.com> | 2023-07-30 23:51:37 -0600 |
commit | fc6e0037a2f0769fdbd4c18bd96f49d55f630757 (patch) | |
tree | 79f6561d192c3e6e5887422c6b31beac35eab046 /src | |
parent | f6dc479873edc98704dcf1ffb116ba5da03805b2 (diff) |
Started caporale circling thingy
Diffstat (limited to 'src')
-rw-r--r-- | src/bullets.c | 13 | ||||
-rw-r--r-- | src/bullets.h | 3 | ||||
-rw-r--r-- | src/entities/antifaShip.c | 2 | ||||
-rw-r--r-- | src/entities/caporale.c | 34 | ||||
-rw-r--r-- | src/entities/caporale.h | 4 | ||||
-rw-r--r-- | src/entities/soldato.c | 42 | ||||
-rw-r--r-- | src/entities/soldato.h | 5 | ||||
-rw-r--r-- | src/entity.c | 29 | ||||
-rw-r--r-- | src/entity.h | 3 | ||||
-rw-r--r-- | src/game.c | 18 | ||||
-rw-r--r-- | src/world.c | 6 |
11 files changed, 145 insertions, 14 deletions
diff --git a/src/bullets.c b/src/bullets.c index 1619111..ec5908f 100644 --- a/src/bullets.c +++ b/src/bullets.c @@ -43,3 +43,16 @@ BulletHitInfo shootBullet(World * world, Bullet bullet) { .hitId = ENTITY_NONE, }; } + +BulletHitInfo shootBulletAtEntity(Entity * entity, Bullet bullet) { + RayCollision collision = traceRayToEntity(*entity, bullet.ray); + + if (collision.hit) + return handleBulletHit(entity, bullet); + + return (BulletHitInfo){ + .hit = false, + .killed = false, + .hitId = ENTITY_NONE, + }; +} diff --git a/src/bullets.h b/src/bullets.h index eb6c15c..fea7961 100644 --- a/src/bullets.h +++ b/src/bullets.h @@ -26,4 +26,7 @@ Bullet createBulletFromEntity(Entity entity, float damage); // Shoot this fucker. BulletHitInfo shootBullet(World * world, Bullet bullet); +// Shott this fucker but only at one entity. +BulletHitInfo shootBulletAtEntity(Entity * entity, Bullet bullet); + #endif diff --git a/src/entities/antifaShip.c b/src/entities/antifaShip.c index 94fc8c8..6c017c8 100644 --- a/src/entities/antifaShip.c +++ b/src/entities/antifaShip.c @@ -120,6 +120,8 @@ void updateAntifaShip(Game * game, Entity * entity) { break; } + //printf("%f\n", entity->health); + entityCheckTransformedCollisionModel(entity); } diff --git a/src/entities/caporale.c b/src/entities/caporale.c index bd6fcd5..c8b4345 100644 --- a/src/entities/caporale.c +++ b/src/entities/caporale.c @@ -4,15 +4,45 @@ void initCaporale(Entity * entity, Game * game) { entity->model = &game->assets.models[CAPORATE_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(Caporale)); + + if (entity->data == NULL) { + ALLOCATION_ERROR; + return; + } + + Caporale * data = (Caporale*)entity->data; + + data->flyToPlayer = (EntityFlyToPointInfo){ + .controller.bangbang.speed = 80, + .controller.bangbang.stopAt = 0.0, + .controlType = ENTITY_FLY_TO_POINT_BANG_BANG, + .rotationSpeed = 0.5 + }; } void closeCaporale(Entity * entity) { + if (entity->data != NULL) + KF_FREE(entity->data); + + entityFreeCollisionModel(entity->collisionModel); + entityFreeCollisionModel(entity->transformedCollisionModel); } void updateCaporale(Game * game, Entity * entity) { - entityUpdateRotation(entity); + entityUpdateLastValues(entity); + + Caporale * data = (Caporale*)entity->data; + Entity * player = getEntityFromWorld(game->world, 0); + + entityFlyToPoint(entity, player->position, &data->flyToPlayer); + + entityCheckTransformedCollisionModel(entity); } void drawCaporale(Game * game, Entity * entity) { diff --git a/src/entities/caporale.h b/src/entities/caporale.h index 13f86d1..e855b30 100644 --- a/src/entities/caporale.h +++ b/src/entities/caporale.h @@ -4,6 +4,10 @@ #ifndef CAPORALE_H #define CAPORALE_H +typedef struct Caporale { + EntityFlyToPointInfo flyToPlayer; +} Caporale; + void initCaporale(Entity * entity, Game * game); void closeCaporale(Entity * entity); void updateCaporale(Game * game, Entity * entity); diff --git a/src/entities/soldato.c b/src/entities/soldato.c index 69dd99a..f4ef912 100644 --- a/src/entities/soldato.c +++ b/src/entities/soldato.c @@ -20,6 +20,8 @@ void initSoldato(Entity * entity, Game * game) { Soldato * data = (Soldato*)entity->data; + data->timeSinceLastShot = GetTime(); + PIDConfig followingPID = (PIDConfig){ .kP = 1.0, .kI = 0.0, @@ -100,6 +102,36 @@ void handleFollower(Game * game, Entity * entity) { } } +void updateSoldatoGuns(Game * game, Entity * entity) { + double t = GetTime(); + Entity * player = getEntityFromWorld(game->world, 0); + Soldato * data = (Soldato*)entity->data; + + // Needs more time. + if (t - data->timeSinceLastShot < SOLDATO_COOLDOWN) + return; + + Bullet bullet = createBulletFromEntity(*entity, SOLDATO_BULLET_DAMAGE); + shootBulletAtEntity(player, bullet); + data->timeSinceLastShot = t; + + /* + // See if ray hits player radius. + Ray ray = (Ray){ + entity->position, + Vector3RotateByQuaternion((Vector3){0.0, 0.0, 1.0}, entity->rotation) + }; + + RayCollision collision = traceRayToEntityRadius(*player, ray, SOLDATO_AIM_RADIOUS); + + // Shoots bullet is ray hits. + if (collision.hit) { + shootBulletAtEntity(player, createBulletFromEntity(*entity, SOLDATO_BULLET_DAMAGE)); + data->timeSinceLastShot = t; + } + */ +} + void updateSoldato(Game * game, Entity * entity) { entityUpdateLastValues(entity); @@ -108,22 +140,21 @@ void updateSoldato(Game * game, Entity * entity) { // Fly to player if no leader. if (entity->follow.leaderId == ENTITY_NONE) { - entityFlyToPoint( - entity, - player->position, - &data->flyToPointLeading - ); + entityFlyToPoint(entity, player->position, &data->flyToPointLeading); } else { soldatoFollowLeader(game, entity); handleFollower(game, entity); } + updateSoldatoGuns(game, entity); + entityCheckTransformedCollisionModel(entity); } void drawSoldato(Game * game, Entity * entity) { entityDraw(entity); + /* Entity * leader; // Debug line. @@ -135,6 +166,7 @@ void drawSoldato(Game * game, Entity * entity) { DrawLine3D(entity->position, leader->position, BLUE); } + */ } void setSoldatoLeader(Entity * entity1, Entity * entity2) { diff --git a/src/entities/soldato.h b/src/entities/soldato.h index eb336db..f4228df 100644 --- a/src/entities/soldato.h +++ b/src/entities/soldato.h @@ -5,9 +5,14 @@ #ifndef SOLDATO_H #define SOLDATO_H +#define SOLDATO_COOLDOWN 1.0 +#define SOLDATO_AIM_RADIOUS 1.5 +#define SOLDATO_BULLET_DAMAGE 0.01 + typedef struct Soldato { EntityFlyToPointInfo flyToPointLeading; EntityFlyToPointInfo flyToPointFollowing; + double timeSinceLastShot; } Soldato; void initSoldato(Entity * entity, Game * game); diff --git a/src/entity.c b/src/entity.c index f4c3747..fa610d0 100644 --- a/src/entity.c +++ b/src/entity.c @@ -333,6 +333,35 @@ bool checkEntityCollision(Entity * entity1, Entity * entity2) { return false; } +RayCollision traceRayToEntity(Entity entity, Ray ray) { + int i; + RayCollision collision; + + Ray transformedRay = (Ray){ + .direction = ray.direction, + .position = Vector3Subtract(ray.position, entity.position) + }; + + // Check every mesh. + for (i = 0; i < entity.model->meshCount; ++i) { + collision = GetRayCollisionMesh( + transformedRay, + entity.model->meshes[i], + entity.model->transform + ); + + // Hit. + if (collision.hit) + return collision; + } + + return (RayCollision){.hit = false}; +} + +RayCollision traceRayToEntityRadius(Entity entity, Ray ray, float scale) { + return GetRayCollisionSphere(ray, entity.position, entity.radius * scale); +} + // Basic wireframe drawing. void entityDraw(Entity * entity) { entity->model->transform = QuaternionToMatrix(entity->rotation); diff --git a/src/entity.h b/src/entity.h index 5faa413..33e1925 100644 --- a/src/entity.h +++ b/src/entity.h @@ -145,6 +145,9 @@ void closeEntity(Entity * entity); void setEntityRadius(Entity * entity); // Uses model to set radius; bool checkEntityCollision(Entity * entity1, Entity * entity2); +RayCollision traceRayToEntity(Entity entity, Ray ray); +RayCollision traceRayToEntityRadius(Entity entity, Ray ray, float scale); + // Helper functions for updating, drawing... void entityDraw(Entity * entity); void entityUpdatePosition(Entity * entity); @@ -24,13 +24,19 @@ void initGame(Game * game) { initWorld(&game->world); // Debug. - WorldEntry entries[31] = { - (WorldEntry){ENTITY_ANTIFA, (Vector3){0.0, 0.0, -100.0}, QuaternionIdentity()}, - }; + //WorldEntry entries[31] = { + // (WorldEntry){ENTITY_ANTIFA, (Vector3){0.0, 0.0, -100.0}, QuaternionIdentity()}, + // (WorldEntry){ENTITY_CAPORALE, (Vector3){0.0, 0.0, 6.0}, QuaternionIdentity()}, + //}; - for (int i = 1; i < 31; ++i) { - entries[i] = (WorldEntry){ENTITY_SOLDATO, (Vector3){0.0, 0.0, i*10}, QuaternionIdentity()}; - } + //for (int i = 2; i < 31; ++i) { + // entries[i] = (WorldEntry){ENTITY_SOLDATO, (Vector3){0.0, 0.0, i*10}, QuaternionIdentity()}; + //} + + 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()} + }; addEntriesToWorld( &game->world, diff --git a/src/world.c b/src/world.c index 6d51a09..ebfc7ed 100644 --- a/src/world.c +++ b/src/world.c @@ -297,8 +297,12 @@ void updateWorld(World * world, Game * game) { } // "bring out your dead!" - for (i = 0; i < killCount; ++i) + for (i = 0; i < killCount; ++i) { + if (kills[i] == 0) // Hack to keep player alive while debugging. + continue; + removeEntityFromWorld(world, kills[i]); + } } void drawWorld(World * world, Game * game) { |