aboutsummaryrefslogtreecommitdiff
path: root/src/entities
diff options
context:
space:
mode:
authornathansmithsmith <nathansmith7@mailfence.com>2023-08-18 00:18:53 -0600
committernathansmithsmith <nathansmith7@mailfence.com>2023-08-18 00:18:53 -0600
commitf98e6d4ea4ee52b298a7d9a9731da4c020bb4dfe (patch)
treefdb76e94e4d00cbe2ab652a9f65d6afaad779552 /src/entities
parent87b86d92c27a6fb83d0d09365a36d8a98ba0b24b (diff)
Some aim thingy
Diffstat (limited to 'src/entities')
-rw-r--r--src/entities/caporale.c85
-rw-r--r--src/entities/caporale.h18
-rw-r--r--src/entities/soldato.c2
3 files changed, 103 insertions, 2 deletions
diff --git a/src/entities/caporale.c b/src/entities/caporale.c
index 32f0f20..1203b61 100644
--- a/src/entities/caporale.c
+++ b/src/entities/caporale.c
@@ -1,6 +1,8 @@
#include "caporale.h"
#include "assets.h"
#include "game.h"
+#include <raylib.h>
+#include <raymath.h>
void initCaporale(Entity * entity, Game * game) {
entity->model = &game->assets.models[CAPORATE_ASSET];
@@ -25,6 +27,24 @@ void initCaporale(Entity * entity, Game * game) {
.rotationSpeed = 0.5,
.applyRotation = true
};
+
+ data->timeSinceLastShot = 0.0;
+
+ PIDConfig aimPIDConfig = (PIDConfig){
+ .kP = 2.0,
+ .kI = 0.0,
+ .kD = 0.0,
+ .angleMode = false,
+ .doClamp = false,
+ .min = 0.0,
+ .max = 0.0
+ };
+
+ data->aimXPID = createPID(aimPIDConfig);
+ data->aimYPID = createPID(aimPIDConfig);
+ data->aimZPID = createPID(aimPIDConfig);
+
+ data->initTarget = true;
}
void closeCaporale(Entity * entity) {
@@ -35,17 +55,80 @@ void closeCaporale(Entity * entity) {
entityFreeCollisionModel(entity->transformedCollisionModel);
}
+void updateGunsCaporale(Game * game, Entity * entity) {
+ double t = GetTime();
+ Caporale * data = (Caporale*)entity->data;
+ Entity * player = getEntityFromWorld(game->world, 0);
+
+ DrawRay(data->ray, BLUE);
+
+ // Get target.
+ Vector3 target = Vector3Subtract(player->position, entity->position);
+ target = Vector3Normalize(target);
+ target = Vector3Scale(target, CAPORALE_TARGET_POINTER_DIS);
+ data->targetSetpoint = target;
+
+ // Cool down.
+ if (t - data->timeSinceLastShot < CAPORALE_COOLDOWN)
+ return;
+
+ // Get direction.
+ target = Vector3Add(entity->position, data->target); // Transform to entity.
+ Vector3 direction = Vector3Subtract(target, entity->position);
+ direction = Vector3Normalize(direction);
+
+ // Create bullet and shoot.
+ Bullet bullet = createBulletFromDirection(*entity, direction, CAPORALE_BULLET_DAMAGE);
+ BulletHitInfo info = shootBulletAtEntity(player, bullet);
+
+ printf("%d\n", info.hit);
+
+ data->ray = bullet.ray;
+ data->timeSinceLastShot = t;
+}
+
+void updateAimCaporale(Game * game, Entity * entity) {
+ float t = GetFrameTime();
+ Caporale * data = (Caporale*)entity->data;
+ Entity * player = getEntityFromWorld(game->world, 0);
+
+ // Init.
+ if (data->initTarget) {
+ data->initTarget = false;
+ data->target = Vector3Zero();
+ data->targetVelocity = Vector3Zero();
+ return;
+ }
+
+ // Move target closer to player.
+ data->targetVelocity = (Vector3){
+ runPID(data->targetSetpoint.x, data->target.x, &data->aimXPID),
+ runPID(data->targetSetpoint.y, data->target.y, &data->aimYPID),
+ runPID(data->targetSetpoint.z, data->target.z, &data->aimZPID)
+ };
+
+ data->target = Vector3Add(data->target, Vector3Scale(data->targetVelocity, t));
+
+ // debug.
+ Vector3 target = Vector3Add(entity->position, data->target);
+ DrawCubeV(target, Vector3One(), BLUE);
+ DrawLine3D(entity->position, target, BLUE);
+}
+
void updateCaporale(Game * game, Entity * entity) {
entityUpdateLastValues(entity);
Caporale * data = (Caporale*)entity->data;
Entity * player = getEntityFromWorld(game->world, 0);
- entityFlyToPoint(entity, player->position, &data->flyToPlayer);
+ //entityFlyToPoint(entity, player->position, &data->flyToPlayer);
entityCheckTransformedCollisionModel(entity);
}
void drawCaporale(Game * game, Entity * entity) {
entityDraw(entity);
+
+ updateAimCaporale(game, entity);
+ updateGunsCaporale(game, entity);
}
diff --git a/src/entities/caporale.h b/src/entities/caporale.h
index e855b30..3999436 100644
--- a/src/entities/caporale.h
+++ b/src/entities/caporale.h
@@ -1,11 +1,29 @@
#include "gameCommon.h"
#include "entity.h"
+#include "PID.h"
+#include <raylib.h>
#ifndef CAPORALE_H
#define CAPORALE_H
+#define CAPORALE_COOLDOWN 0.5
+#define CAPORALE_BULLET_DAMAGE 0.01
+#define CAPORALE_TARGET_POINTER_DIS 10.0
+
typedef struct Caporale {
EntityFlyToPointInfo flyToPlayer;
+ double timeSinceLastShot;
+
+ bool initTarget;
+ Vector3 targetSetpoint;
+ Vector3 target;
+ Vector3 targetVelocity;
+
+ PID aimXPID;
+ PID aimYPID;
+ PID aimZPID;
+
+ Ray ray;
} Caporale;
void initCaporale(Entity * entity, Game * game);
diff --git a/src/entities/soldato.c b/src/entities/soldato.c
index 46a0e23..2122a5a 100644
--- a/src/entities/soldato.c
+++ b/src/entities/soldato.c
@@ -108,7 +108,7 @@ 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;