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
|
#include "sergente.h"
#include "assets.h"
#include "game.h"
void initSergente(Entity * entity, Game * game) {
entity->model = &game->assets.models[SERGENTE_ASSET];
entity->collisionModel = entityCreateCollisionModel(*entity->model);
entity->transformedCollisionModel = entityCreateCollisionModel(*entity->model);
setEntityRadius(entity);
// Allocate data.
entity->data = KF_MALLOC(sizeof(Sergente));
if (entity->data == NULL) {
ALLOCATION_ERROR;
return;
}
Sergente * data = (Sergente*)entity->data;
data->flyToPoint = (EntityFlyToPointInfo){
.controller.bangbang.speed = 85.0,
.controller.bangbang.stopAt = 0.0,
.controlType = ENTITY_FLY_TO_POINT_BANG_BANG,
.rotationSpeed = 0.0,
.applyRotation = false
};
createSergenteTarget(game, entity);
}
void closeSergente(Entity * entity) {
if (entity->data != NULL)
KF_FREE(entity->data);
entityFreeCollisionModel(entity->collisionModel);
entityFreeCollisionModel(entity->transformedCollisionModel);
}
void updateRotationSergente(Game * game, Entity * entity) {
float t = GetFrameTime();
Entity * player = getEntityFromWorld(game->world, 0);
// Get rotation.
Matrix matrix = MatrixLookAt(player->position, entity->position, (Vector3){0.0, 1.0, 0.0});
Quaternion rotation = QuaternionInvert(QuaternionFromMatrix(matrix));
// Update current rotation.
entity->rotation = QuaternionSlerp(entity->rotation, rotation, t * SERGENTE_ROTATION_SPEED);
}
void updateSergente(Game * game, Entity * entity) {
entityUpdateLastValues(entity);
Sergente * data = (Sergente*)entity->data;
entityFlyToPoint(entity, data->target, &data->flyToPoint);
// Create next point.
if (Vector3Distance(entity->position, data->target) <= SERGENTE_NEXT_POINT_THRESHOLD)
createSergenteTarget(game, entity);
updateRotationSergente(game, entity);
entityCheckTransformedCollisionModel(entity);
}
void drawSergente(Game * game, Entity * entity) {
entityDraw(entity);
// Test if facing player always.
//DrawLine3D(
// entity->position,
// Vector3Add(
// entity->position,
// Vector3Scale(Vector3RotateByQuaternion((Vector3){0.0, 0.0, 1.0}, entity->rotation), 500.0)
// ),
// BLUE
//);
// The fucking debug line.
//DrawLine3D(entity->position, ((Sergente*)entity->data)->target, BLUE);
}
void comeBackToPlayerSergente(Game * game, Entity * entity) {
Entity * player = getEntityFromWorld(game->world, 0);
Sergente * data = (Sergente*)entity->data;
data->target = Vector3Subtract(player->position, entity->position);
data->target = Vector3Scale(data->target, SERGENTE_COME_BACK_PERCENT);
data->target = Vector3Add(entity->position, data->target);
}
void createSergenteTarget(Game * game, Entity * entity) {
Sergente * data = (Sergente*)entity->data;
Entity * player = getEntityFromWorld(game->world, 0);
// To far away.
if (Vector3Distance(entity->position, player->position) >= SERGENTE_COME_BACK_DIS) {
comeBackToPlayerSergente(game, entity);
return;
}
SetRandomSeed(time(NULL));
// Set target direction.
data->target = (Vector3){
GetRandomValue(-UCHAR_MAX, UCHAR_MAX),
GetRandomValue(-UCHAR_MAX, UCHAR_MAX),
GetRandomValue(-UCHAR_MAX, UCHAR_MAX)
};
data->target = Vector3Normalize(data->target);
// Scale target and transform.
float dis = GetRandomValue(SERGENTE_TARGET_DIS_MIN, SERGENTE_TARGET_DIS_MAX);
data->target = Vector3Scale(data->target, dis);
data->target = Vector3Add(entity->position, data->target);
}
|