aboutsummaryrefslogtreecommitdiff
path: root/src
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
parent87b86d92c27a6fb83d0d09365a36d8a98ba0b24b (diff)
Some aim thingy
Diffstat (limited to 'src')
-rw-r--r--src/bullets.c13
-rw-r--r--src/bullets.h4
-rw-r--r--src/entities/caporale.c85
-rw-r--r--src/entities/caporale.h18
-rw-r--r--src/entities/soldato.c2
-rw-r--r--src/entity.c14
-rw-r--r--src/game.c16
-rw-r--r--src/gameScreen.c1
-rw-r--r--src/settings.c2
9 files changed, 135 insertions, 20 deletions
diff --git a/src/bullets.c b/src/bullets.c
index ec5908f..bd7fc81 100644
--- a/src/bullets.c
+++ b/src/bullets.c
@@ -13,6 +13,19 @@ Bullet createBulletFromEntity(Entity entity, float damage) {
};
}
+
+Bullet createBulletFromDirection(Entity entity, Vector3 direction, float damage) {
+ return (Bullet){
+ .ray = (Ray){
+ entity.position,
+ direction
+ },
+ .fromId = entity.id,
+ .fromFingerprint = entity.fingerprint,
+ .damage = damage
+ };
+}
+
BulletHitInfo handleBulletHit(Entity * entity, Bullet bullet) {
// Handle health.
entity->health -= bullet.damage;
diff --git a/src/bullets.h b/src/bullets.h
index fea7961..45c96ea 100644
--- a/src/bullets.h
+++ b/src/bullets.h
@@ -1,6 +1,7 @@
#include "gameCommon.h"
#include "entity.h"
#include "world.h"
+#include <raylib.h>
#ifndef BULLET_H
#define BULLET_H
@@ -23,6 +24,9 @@ typedef struct BulletHitInfo {
// Uses entity postion and direction to create bullet ray.
Bullet createBulletFromEntity(Entity entity, float damage);
+// Uses direction argument for direction indeed.
+Bullet createBulletFromDirection(Entity entity, Vector3 direction, float damage);
+
// Shoot this fucker.
BulletHitInfo shootBullet(World * world, Bullet bullet);
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;
diff --git a/src/entity.c b/src/entity.c
index bb874df..52d24f6 100644
--- a/src/entity.c
+++ b/src/entity.c
@@ -1,5 +1,6 @@
#include "entity.h"
#include "entitiesInclude.h"
+#include <raylib.h>
// This fucker is used for creating entities.
const EntityTypeInfo entityTypeInfo[ENTITY_TYPE_COUNT] = {
@@ -375,15 +376,10 @@ void entityDraw(Entity * entity) {
}
void entityUpdatePosition(Entity * entity) {
- float t = GetFrameTime();
-
- Vector3 velocity = (Vector3){
- entity->velocity.velocity.x * t,
- entity->velocity.velocity.y * t,
- entity->velocity.velocity.z * t
- };
-
- entity->position = Vector3Add(entity->position, velocity);
+ entity->position = Vector3Add(
+ entity->position,
+ Vector3Scale(entity->velocity.velocity, GetFrameTime())
+ );
}
void entityUpdateRotation(Entity * entity) {
diff --git a/src/game.c b/src/game.c
index bd1a09f..b03854f 100644
--- a/src/game.c
+++ b/src/game.c
@@ -24,18 +24,18 @@ void initGame(Game * game) {
initWorld(&game->world);
// Debug.
- //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()},
- //};
+ /* WorldEntry entries[31] = { */
+ /* (WorldEntry){ENTITY_ANTIFA, (Vector3){0.0, 0.0, -100.0}, QuaternionIdentity()}, */
+ /* (WorldEntry){ENTITY_GENERALE, (Vector3){0.0, 0.0, 6.0}, QuaternionIdentity()}, */
+ /* }; */
- //for (int i = 2; 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_MUSSOLINI, (Vector3){0.0, 0.0, 100.0}, QuaternionIdentity()}
+ (WorldEntry){ENTITY_CAPORALE, (Vector3){0.0, 0.0, 100.0}, QuaternionIdentity()}
};
addEntriesToWorld(
diff --git a/src/gameScreen.c b/src/gameScreen.c
index 65b020c..b004215 100644
--- a/src/gameScreen.c
+++ b/src/gameScreen.c
@@ -2,6 +2,7 @@
#include "game.h"
#include "world.h"
#include "bullets.h"
+#include <raylib.h>
void drawCrossHair(float size, float thick, Color color) {
Vector3 center = (Vector3){GetScreenWidth() / 2.0, GetScreenHeight() / 2.0};
diff --git a/src/settings.c b/src/settings.c
index c79b342..64d7348 100644
--- a/src/settings.c
+++ b/src/settings.c
@@ -2,7 +2,7 @@
void initSettings(Settings * settings) {
*settings = (Settings){
- .controlMode = KEYBOARD_AND_MOUSE_CONTROL,
+ .controlMode = JOYSTICK_CONTROL,
.mouseSensitivity = 0.3,
.scrollBarSpeed = 10.0,
.gamePadNum = 0,