-
Notifications
You must be signed in to change notification settings - Fork 0
/
enemy.c
173 lines (138 loc) · 4.78 KB
/
enemy.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <stdint.h>
#include <stdbool.h>
#include "system/system.h"
#include "math/math.h"
#include "physics/physics.h"
#include "camera/camera.h"
#include "audio/audio.h"
#include "sounds.h"
#include "enemy.h"
#define NUM_ASTEROIDS 1000
extern RigidBody_t asteroids[NUM_ASTEROIDS];
extern float fTimeStep;
void FireParticleEmitter(vec3 position, vec3 direction);
// angles here are in cosine space (-1.0 to 1.0)
static const float MAX_SIGHT_DISTANCE=1000.0f;
static const float MAX_SIGHT_ANGLE=0.5f;
static const float TRACK_FORCE=1.0f;
static const float TRACK_RANGE=80.0f;
static const float ROTATION_FORCE=0.2f;
static const float ATTACK_ANGLE_THRESHOLD=0.5f;
static const float ATTACK_RANGE=100.0f;
void InitEnemy(Enemy_t *enemy, Camera_t *enemyCamera, const Camera_t playerCamera)
{
// Set initial state
enemy->state=SEARCHING;
// Assign camera pointer to "connect" it to a camera
enemy->camera=enemyCamera;
// Assign last known player camera
enemy->lastKnownPlayer=playerCamera;
}
static bool HasLineOfSight(Enemy_t *enemy, const Camera_t player)
{
// Calculate the direction from the camera to the target
vec3 direction=Vec3_Subv(player.body.position, enemy->camera->body.position);
const float distance=Vec3_Normalize(&direction);
if(distance>MAX_SIGHT_DISTANCE)
return false;
//for(uint32_t i=0;i<NUM_ASTEROIDS;i++)
//{
// RigidBody_t *obstacle=&asteroids[i];
//
// if(raySphereIntersect(enemy->camera->body.position, direction, obstacle->position, obstacle->radius)>0.0f)
// return false; // Obstacle blocks line of sight
//}
const float theta=Vec3_Dot(enemy->camera->forward, direction);
// Calculate the angle between the camera's forward vector and the direction to the target
// Check if the angle is within half of the FOV angle
return theta>MAX_SIGHT_ANGLE;
}
static float TrackPlayer(Enemy_t *enemy, const Camera_t player)
{
vec3 direction=Vec3_Subv(player.body.position, enemy->camera->body.position);
const float distance=Vec3_Normalize(&direction);
if(distance>TRACK_RANGE)
enemy->camera->body.force=Vec3_Addv(enemy->camera->body.force, Vec3_Muls(direction, TRACK_FORCE));
vec3 rotationAxis=QuatRotate(QuatInverse(enemy->camera->body.orientation), Vec3_Cross(enemy->camera->forward, direction));
float cosTheta=clampf(Vec3_Dot(enemy->camera->forward, direction), -1.0f, 1.0f);
float theta=acosf(cosTheta);
vec4 rotation=QuatAnglev(theta, rotationAxis);
enemy->camera->body.angularVelocity=Vec3_Addv(enemy->camera->body.angularVelocity, Vec3_Muls(Vec3(rotation.x, rotation.y, rotation.z), ROTATION_FORCE));
return distance;
}
static void AttackPlayer(Enemy_t *enemy, const Camera_t player)
{
static float fireCooldownTimer=0.0f;
static float fireCooldown=0.0f;
vec3 direction=Vec3_Subv(player.body.position, enemy->camera->body.position);
float distance=Vec3_Normalize(&direction);
vec3 forward=enemy->camera->forward;
Vec3_Normalize(&forward);
float theta=Vec3_Dot(direction, forward);
if(theta>ATTACK_ANGLE_THRESHOLD&&distance<ATTACK_RANGE)
{
// Fire weapon
fireCooldownTimer+=fTimeStep;
if(fireCooldownTimer>fireCooldown)
{
fireCooldown=RandFloatRange(0.25f, 2.0f);
fireCooldownTimer=0.0f;
FireParticleEmitter(Vec3_Addv(enemy->camera->body.position, Vec3_Muls(enemy->camera->forward, enemy->camera->body.radius)), enemy->camera->forward);
}
}
}
static bool IsInAttackRange(Enemy_t *enemy, const Camera_t player)
{
const float distance=Vec3_Distance(enemy->camera->body.position, player.body.position);
return distance<ATTACK_RANGE;
}
bool IsAlignedForAttack(Enemy_t *enemy, const Camera_t player)
{
vec3 direction=Vec3_Subv(player.body.position, enemy->camera->body.position);
Vec3_Normalize(&direction);
return Vec3_Dot(enemy->camera->forward, direction)>ATTACK_ANGLE_THRESHOLD;
}
void UpdateEnemy(Enemy_t *enemy, Camera_t player)
{
switch(enemy->state)
{
case PURSUING:
{
const float distance=TrackPlayer(enemy, player);
DBGPRINTF(DEBUG_WARNING, "%f\r", distance);
// Switch to attacking if within range and aligned
if(IsInAttackRange(enemy, player)&&IsAlignedForAttack(enemy, player))
enemy->state=ATTACKING;
else if(!HasLineOfSight(enemy, player))
{
enemy->state=SEARCHING;
enemy->lastKnownPlayer=player;
}
break;
}
case SEARCHING:
{
const float distance=TrackPlayer(enemy, enemy->lastKnownPlayer);
DBGPRINTF(DEBUG_WARNING, "%f\r", distance);
if(HasLineOfSight(enemy, player))
enemy->state=PURSUING;
break;
}
case ATTACKING:
{
TrackPlayer(enemy, player);
AttackPlayer(enemy, player);
// Stay in attack mode while aligned and within range
if(!IsInAttackRange(enemy, player)||!IsAlignedForAttack(enemy, player))
{
enemy->state=PURSUING;
}
else if(!HasLineOfSight(enemy, player))
{
enemy->state=SEARCHING;
enemy->lastKnownPlayer=player;
}
break;
}
}
}