aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authornathansmithsmith <thenathansmithsmith@gmail.com>2023-07-23 18:53:25 -0600
committernathansmithsmith <thenathansmithsmith@gmail.com>2023-07-23 18:53:25 -0600
commit5a3b3a062862cdeee96b3ec4641ac684b28659f8 (patch)
tree2994bea3543c1bc694f18f2bc26b0459178759fa /src
parenta408b352f13df546b2262ca01cf162b60891cdae (diff)
Soldato following
Diffstat (limited to 'src')
-rw-r--r--src/entities/soldato.c131
-rw-r--r--src/entities/soldato.h8
-rw-r--r--src/entitiesInclude.h9
-rw-r--r--src/entity.c12
-rw-r--r--src/game.c6
-rw-r--r--src/gameCommon.h1
-rw-r--r--src/playerCamera.c6
-rw-r--r--src/world.c43
8 files changed, 168 insertions, 48 deletions
diff --git a/src/entities/soldato.c b/src/entities/soldato.c
index 2cc0e07..ad454d8 100644
--- a/src/entities/soldato.c
+++ b/src/entities/soldato.c
@@ -8,22 +8,7 @@ void initSoldato(Entity * entity, Game * game) {
setEntityRadius(entity);
// Acceleration.
- entity->useAcceleration = true;
- entity->acceleration = (EntityAcceleration){
- .speedUp = 20.0,
- .speedDown = 15.0
- };
-
- // PID configs.
- PIDConfig speedPIDConfig = {
- .kP = 1,
- .kI = 0.0,
- .kD = 0.0,
- .angleMode = false,
- .doClamp = true,
- .min = 0.0,
- .max = 100.0
- };
+ entity->useAcceleration = false;
// Allocate data.
entity->data = KF_MALLOC(sizeof(Soldato));
@@ -35,14 +20,26 @@ void initSoldato(Entity * entity, Game * game) {
Soldato * data = (Soldato*)entity->data;
+ PIDConfig speedPID = (PIDConfig){
+ .kP = 0.5,
+ .kI = 0.0,
+ .kD = 0.0,
+ .angleMode = false,
+ .doClamp = true,
+ .min = 0.0,
+ .max = 20.0
+ };
+
// Create fly to point.
data->flyToPoint = (EntityFlyToPointInfo){
- //.controller.speedPID = createPID(speedPIDConfig),
- .controller.bangbang.speed = 20.0,
- .controller.bangbang.stopAt = 0.0,
+ .controller.bangbang.speed = 50.0,
+ .controller.bangbang.stopAt = entity->radius * 2,
.controlType = ENTITY_FLY_TO_POINT_BANG_BANG,
.rotationSpeed = 0.0
};
+
+ data->leaderId = ENTITY_NONE;
+ data->followerId = ENTITY_NONE;
}
void closeSoldato(Entity * entity) {
@@ -53,21 +50,107 @@ void closeSoldato(Entity * entity) {
entityFreeCollisionModel(entity->transformedCollisionModel);
}
-void updateSoldato(Game * game, Entity * entity) {
- entityUpdateLastValues(entity);
-
- Entity * player = getEntityFromWorld(game->world, 0);
+void soldatoFollowLeader(Game * game, Entity * entity) {
Soldato * data = (Soldato*)entity->data;
+ Entity * leader;
+
+ // Get leader.
+ leader = getEntityFromWorld(game->world, data->leaderId);
+
+ // Bye bye leader.
+ if (leader == NULL) {
+ data->leaderId = ENTITY_NONE;
+ return;
+ }
+ if (leader->fingerprint != data->leaderFingerprint) {
+ data->leaderId = ENTITY_NONE;
+ return;
+ }
+
+ // Fly to leader.
entityFlyToPoint(
entity,
- player->position,
+ leader->position,
&data->flyToPoint
);
+}
+
+void handleFollower(Game * game, Entity * entity) {
+ Soldato * data = (Soldato*)entity->data;
+ Entity * follower;
+
+ if (data->followerId == ENTITY_NONE)
+ return;
+
+ // Get follower.
+ follower = getEntityFromWorld(game->world, data->followerId);
+
+ // Bye bye follower.
+ if (follower == NULL) {
+ data->followerId = ENTITY_NONE;
+ return;
+ }
+
+ if (follower->fingerprint != data->followerFingerprint) {
+ data->followerId = ENTITY_NONE;
+ return;
+ }
+}
+
+void updateSoldato(Game * game, Entity * entity) {
+ entityUpdateLastValues(entity);
+
+ Entity * player = getEntityFromWorld(game->world, 0);
+ Soldato * data = (Soldato*)entity->data;
+
+ // Fly to player if no leader.
+ if (data->leaderId == ENTITY_NONE) {
+ entityFlyToPoint(
+ entity,
+ player->position,
+ &data->flyToPoint
+ );
+ } else {
+ soldatoFollowLeader(game, entity);
+ handleFollower(game, entity);
+ }
entityCheckTransformedCollisionModel(entity);
}
void drawSoldato(Game * game, Entity * entity) {
entityDraw(entity);
+
+ Soldato * data = (Soldato*)entity->data;
+ Entity * leader;
+
+ // Debug line.
+ //if (data->leaderId != ENTITY_NONE) {
+ // leader = getEntityFromWorld(game->world, data->leaderId);
+ // DrawLine3D(entity->position, leader->position, BLUE);
+ //}
+}
+
+void setSoldatoLeader(Entity * entity1, Entity * entity2) {
+ Soldato * data1;
+ Soldato * data2;
+
+ if (entity1->type != ENTITY_SOLDATO || entity2->type != ENTITY_SOLDATO)
+ return;
+
+ data1 = (Soldato*)entity1->data;
+ data2 = (Soldato*)entity2->data;
+
+ // Already has follower.
+ if (data2->followerId != ENTITY_NONE)
+ return;
+
+ if (data1->leaderId == ENTITY_NONE) {
+ data1->leaderId = entity2->id;
+ data1->leaderFingerprint = entity2->fingerprint;
+
+ data2->followerId = entity1->id;
+ data2->followerFingerprint = entity1->fingerprint;
+ }
}
diff --git a/src/entities/soldato.h b/src/entities/soldato.h
index f93206d..460c799 100644
--- a/src/entities/soldato.h
+++ b/src/entities/soldato.h
@@ -7,6 +7,12 @@
typedef struct Soldato {
EntityFlyToPointInfo flyToPoint;
+
+ EntityId leaderId;
+ EntityFingerprint leaderFingerprint;
+
+ EntityId followerId;
+ EntityFingerprint followerFingerprint;
} Soldato;
void initSoldato(Entity * entity, Game * game);
@@ -14,4 +20,6 @@ void closeSoldato(Entity * entity);
void updateSoldato(Game * game, Entity * entity);
void drawSoldato(Game * game, Entity * entity);
+void setSoldatoLeader(Entity * entity1, Entity * entity2);
+
#endif
diff --git a/src/entitiesInclude.h b/src/entitiesInclude.h
new file mode 100644
index 0000000..4c6f5ac
--- /dev/null
+++ b/src/entitiesInclude.h
@@ -0,0 +1,9 @@
+#include "entities/antifaShip.h"
+#include "entities/soldato.h"
+#include "entities/caporale.h"
+#include "entities/sergente.h"
+#include "entities/maresciallo.h"
+#include "entities/generale.h"
+#include "entities/mussolini.h"
+
+// Through the magic of this ugly file you can include all entities at once.
diff --git a/src/entity.c b/src/entity.c
index e5afbcd..0757109 100644
--- a/src/entity.c
+++ b/src/entity.c
@@ -1,13 +1,5 @@
#include "entity.h"
-
-// Entities.
-#include "entities/antifaShip.h"
-#include "entities/soldato.h"
-#include "entities/caporale.h"
-#include "entities/sergente.h"
-#include "entities/maresciallo.h"
-#include "entities/generale.h"
-#include "entities/mussolini.h"
+#include "entitiesInclude.h"
// This fucker is used for creating entities.
const EntityTypeInfo entityTypeInfo[ENTITY_TYPE_COUNT] = {
@@ -281,7 +273,7 @@ void entityCheckTransformedCollisionModel(Entity * entity) {
entity->collisionModelTransformed = false;
}
-// Big mesh helper for checkEntityCollision.
+// Big mesh helper for checkEntityCollision.
bool checkEntityMeshCollision(Entity entity1, Entity entity2, int entity1MeshNum, int entity2MeshNum) {
int triangle1Num;
int triangle2Num;
diff --git a/src/game.c b/src/game.c
index f27449f..a210e83 100644
--- a/src/game.c
+++ b/src/game.c
@@ -24,11 +24,11 @@ void initGame(Game * game) {
initWorld(&game->world);
// Debug.
- WorldEntry entries[101] = {
- (WorldEntry){ENTITY_ANTIFA, Vector3Zero(), QuaternionIdentity()},
+ WorldEntry entries[31] = {
+ (WorldEntry){ENTITY_ANTIFA, (Vector3){0.0, 0.0, -100.0}, QuaternionIdentity()},
};
- for (int i = 1; i < 101; ++i) {
+ for (int i = 1; i < 31; ++i) {
entries[i] = (WorldEntry){ENTITY_SOLDATO, (Vector3){0.0, 0.0, i*10}, QuaternionIdentity()};
}
diff --git a/src/gameCommon.h b/src/gameCommon.h
index d1d78ee..089d891 100644
--- a/src/gameCommon.h
+++ b/src/gameCommon.h
@@ -17,6 +17,7 @@
// Types be like.
typedef struct Game Game;
+typedef struct World World;
typedef struct Entity Entity;
// How far from center you can go.
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};
}
diff --git a/src/world.c b/src/world.c
index 1a3edde..946a283 100644
--- a/src/world.c
+++ b/src/world.c
@@ -1,5 +1,7 @@
#include "world.h"
#include "game.h"
+#include "entity.h"
+#include "entitiesInclude.h"
void initWorld(World * world) {
world->entities = NULL;
@@ -226,12 +228,30 @@ KfError removeEntityFromWorld(World * world, EntityId id) {
}
void handleCollisionInWorld(Entity * entity1, Entity * entity2) {
- if (entity1->id != 0)
- entity1->health = 0.0;
- if (entity2->id != 0)
- entity2->health = 0.0;
-
- puts("cc");
+ //if (entity1->id != 0)
+ // entity1->health = 0.0;
+ //if (entity2->id != 0)
+ // entity2->health = 0.0;
+
+ switch (entity1->type) {
+ case ENTITY_ANTIFA:
+ break;
+ case ENTITY_SOLDATO:
+ setSoldatoLeader(entity1, entity2);
+ break;
+ case ENTITY_CAPORALE:
+ break;
+ case ENTITY_SERGENTE:
+ break;
+ case ENTITY_MARESCIALLO:
+ break;
+ case ENTITY_GENERALE:
+ break;
+ case ENTITY_MUSSOLINI:
+ break;
+ default:
+ break;
+ }
}
void updateWorld(World * world, Game * game) {
@@ -255,8 +275,15 @@ void updateWorld(World * world, Game * game) {
entity2 = &world->entities[j];
// Collided.
- if (checkEntityCollision(entity, entity2))
- handleCollisionInWorld(entity, entity2);
+ // Only use real collision if player is there.
+ if (entity->id == ENTITY_ANTIFA || entity2->id == ENTITY_ANTIFA) {
+ if (checkEntityCollision(entity, entity2))
+ handleCollisionInWorld(entity, entity2);
+ } else {
+ if (Vector3Distance(entity->position, entity2->position) <=
+ entity->radius + entity2->radius)
+ handleCollisionInWorld(entity, entity2);
+ }
}
// It fucking died.