/
:
444 lines (353 loc) · 12.4 KB
/
:
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#include "EnvironmentBuilder.h"
#include <math.h>
#include <float.h>
#include "../Data/LinkedList.h"
#include "../Manager/AssetManager.h"
#include "../Manager/RenderingManager.h"
#include "ProximityRemove.h"
struct State_EnvironmentBuilder_Members
{
LinkedList* objs;
float currentOffset;
float offsetToSpawn;
float currentSize;
unsigned short floorPartitions;
};
static enum ObstacleType
{
HURDLE,
WALLCLIMB,
};
///
//Randomly alters the EnvironmentBuilder's current set of objects
//to modify the environment
//
//Parameters:
// members: The state members struct of the EnvironmentBuilder state being modified
static void State_EnvironmentBuilder_Mutate(struct State_EnvironmentBuilder_Members* members);
///
//Spawns another set of the EnvironmentBuilder's object set at the current offset + the current size
//
//Parameters:
// members: The state members struct of the EnvironmentBuilder state being modified
// attached: A pointer to the gameObject this state is attached to
static void State_EnvironmentBuilder_Spawn(struct State_EnvironmentBuilder_Members* members, GObject* attached);
///
//Adds a hurdle obstacle to the list of objects in the set
//at the specified z offset
//
//Parameters:
// members: A pointer to the members of the environmentBuilder state to which the hudle is being added
// zOffset: the z position of the new hurdle within the set
// insertionIndex: The index in thelinked list to insert the hurdle at
static void State_EnvironmentBuilder_AddHurdle(struct State_EnvironmentBuilder_Members* members, float zOffset, int insertionIndex);
///
//Allocates and initializes a new GObject in the environment set at the specified location
//
//Parameters:
// x: x coordinate within the set
// y: y coordinate within the set
// z: z coordinate within the set
// sX: x scale
// sY: y scale
// sZ: z scale
static GObject* State_EnvironmentBuilder_GenerateBlock(float x, float y, float z, float sX, float sY, float sZ);
///
//Determines a suitable index in the environment builder's linked list of objects
//where it would make the most sense to insert the next obstacle
//
//Parameters:
// members: A pointer to the eNvironmentBuilder members which contains the linked list to search
//
//Returns:
// An integer containing the index in the linked list that the new obstacle should be added
// this index, i, indicates that the obstacle belongs between the objects at i - 1 and i
static unsigned int State_EnvironmentBuilder_DetermineSuitableIndex(struct State_EnvironmentBuilder_Members* members);
///
//Initializes a EnvironmentBuilder state
//
//Parameters:
// s: A pointer to the state to initialize
void State_EnvironmentBuilder_Initialize(State* state)
{
struct State_EnvironmentBuilder_Members* members = malloc(sizeof(struct State_EnvironmentBuilder_Members));
state->members = members;
members->objs = LinkedList_Allocate();
LinkedList_Initialize(members->objs);
members->currentOffset = -50.0f;
members->offsetToSpawn = 50.0f;
members->currentSize = 100.0f;
members->floorPartitions = 1;
state->State_Update = State_EnvironmentBuilder_Update;
state->State_Members_Free = State_EnvironmentBuilder_Free;
//Create floor
GObject* block = GObject_Allocate();
GObject_Initialize(block);
block->mesh = AssetManager_LookupMesh("Cube");
block->material = Material_Allocate();
Material_Initialize(block->material, AssetManager_LookupTexture("Test"));
block->material->tile->components[0] = 1.0f;
block->material->tile->components[1] = 3.0f;
*Matrix_Index(block->material->colorMatrix, 1, 1) = 0.0f;
*Matrix_Index(block->material->colorMatrix, 2, 2) = 0.0f;
block->collider = Collider_Allocate();
AABBCollider_Initialize(block->collider, 2.0f, 2.0f, 2.0f, &Vector_ZERO);
block->body = RigidBody_Allocate();
RigidBody_Initialize(block->body, block->frameOfReference, 0.0f);
block->body->freezeTranslation = block->body->freezeRotation = 1;
block->body->dynamicFriction = block->body->staticFriction = 0.5f;
block->body->rollingResistance = 0.0f;
Vector v;
Vector_INIT_ON_STACK(v, 3);
v.components[0] = 10.0f;
v.components[2] = 50.0f;
v.components[1] = 1.0f;
GObject_Scale(block, &v);
Vector_Copy(&v, &Vector_ZERO);
v.components[0] = 0.0f;
v.components[1] = -0.5f;
//v.components[2] = -50.0f;
GObject_Translate(block, &v);
LinkedList_Append(members->objs, block);
}
///
//Updates a GObject using a EnvironmentBuilder state
//
//Parameters:
// obj: A pointer to the GObject being updated
// state: A pointer to the state updating the GObject
void State_EnvironmentBuilder_Update(GObject* obj, State* state)
{
//Attached gameobject will not be used
(void)obj;
//Get reference to camera & state members
//Camera* cam = RenderingManager_GetRenderingBuffer()->camera;
struct State_EnvironmentBuilder_Members* members = (struct State_EnvironmentBuilder_Members*)state->members;
//Determine if it is time to spawn the next set of objects
float dist = fabs(obj->frameOfReference->position->components[2]) - members->currentOffset;
if(dist >= 0.0f)
{
printf("Spawning\n\n");
State_EnvironmentBuilder_Mutate(members);
State_EnvironmentBuilder_Spawn(members, obj);
members->currentOffset += members->currentSize;
}
}
///
//Frees memory allocated by a EnvironmentBuilder state
//
//Parameters:
// state: The Template state to free
void State_EnvironmentBuilder_Free(State* state)
{
struct State_EnvironmentBuilder_Members* members = (struct State_EnvironmentBuilder_Members*)state->members;
//Free GObjects in LinkedList
struct LinkedList_Node* current = members->objs->head;
struct LinkedList_Node* next = NULL;
GObject* obj = NULL;
while(current != NULL)
{
next = current->next;
obj = (GObject*)current->data;
GObject_Free(obj);
current = next;
}
LinkedList_Free(members->objs);
free(members);
}
///
//Randomly alters the EnvironmentBuilder's current set of objects
//to modify the environment
//
//Parameters:
// members: The state members struct of the EnvironmentBuilder state being modified
static void State_EnvironmentBuilder_Mutate(struct State_EnvironmentBuilder_Members* members)
{
int insertIndex = State_EnvironmentBuilder_DetermineSuitableIndex(members);
//Step 2: Determine the central Z location of new obstacle
//For now we will just take the average of the bounds
float minBound, maxBound;
struct LinkedList_Node* node = LinkedList_SearchByIndex(members->objs, insertIndex - 1);
GObject* obj = node->data;
if(insertIndex <= members->floorPartitions)
{
minBound = 0.0f;
}
else
{
minBound = obj->frameOfReference->position->components[2];
}
node = node->next;
if(node != NULL)
{
obj = node->data;
maxBound = obj->frameOfReference->position->components[2];
}
else
{
maxBound = -members->currentSize;
}
float zPos = (maxBound + minBound) / 2.0f;
printf("\n%f\t%d\n", zPos, members->objs->size);
//Step 3: Choose an obstacle to place
int random = 0;
//TODO: If segment is less than certain distance force extension obstacle
switch(random)
{
case 0: //Hurdle
State_EnvironmentBuilder_AddHurdle(members, zPos, insertIndex);
break;
default:
break;
}
}
///
//Determines a suitable index in the environment builder's linked list of objects
//where it would make the most sense to insert the next obstacle
//
//Parameters:
// members: A pointer to the eNvironmentBuilder members which contains the linked list to search
//
//Returns:
// An integer containing the index in the linked list that the new obstacle should be added
// this index, i, indicates that the obstacle belongs between the objects at i - 1 and i
static unsigned int State_EnvironmentBuilder_DetermineSuitableIndex(struct State_EnvironmentBuilder_Members* members)
{
//Step 1: Determine how many obstacles there are
int numObstacles = members->objs->size - members->floorPartitions;
if(numObstacles == 0)
{
return members->floorPartitions; //If there are no obstacles, put the object after the final floor partition
}
else if(numObstacles == 1)
{
//Get the first object
struct LinkedList_Node* node = LinkedList_SearchByIndex(members->objs, members->floorPartitions);
GObject* obj = node->data;
//get the z offset of object
float zOff = fabs(obj->frameOfReference->position->components[2]);
//Determine if the larger gap is between the set start and the single object
//or the single object and the set end
float d1 = zOff;
float d2 = members->currentSize - zOff;
if(d1 > d2) return members->floorPartitions;
else return members->floorPartitions + 1;
}
else
{
//Determine which gap between any two adjacent obstacles is the largest and return the index of
//the "larger" bound
int maxIndex = members->floorPartitions;
int currentIndex = maxIndex;
struct LinkedList_Node* currentNode = LinkedList_SearchByIndex(members->objs, members->floorPartitions);
GObject* obj = currentNode->data;
float zOff1 = 0.0f;
float zOff2 = fabs(obj->frameOfReference->position->components[2]);
float maxDistance = zOff2 - zOff1; //Current maximum distance found between two obstacles
float currentDistance = maxDistance;
currentNode = currentNode->next;
currentIndex++;
zOff1 = zOff2;
while(currentNode != NULL)
{
obj = currentNode->data;
zOff2 = fabs(obj->frameOfReference->position->components[2]);
currentDistance = zOff2 - zOff1;
if(currentDistance > maxDistance)
{
maxIndex = currentIndex;
maxDistance = currentDistance;
}
currentNode = currentNode->next;
currentIndex++;
zOff1 = zOff2;
}
zOff2 = members->currentSize;
currentDistance = zOff2 - zOff1;
if(currentDistance > maxDistance)
{
maxIndex = currentIndex;
}
return maxIndex;
}
}
///
//Allocates and initializes a new GObject in the environment set at the specified location
//
//Parameters:
// x: x coordinate within the set
// y: y coordinate within the set
// z: z coordinate within the set
// sX: x sca
// sY: y scale
// sZ: z scale
static GObject* State_EnvironmentBuilder_GenerateBlock(float x, float y, float z, float sX, float sY, float sZ)
{
GObject* block = GObject_Allocate();
GObject_Initialize(block);
block->mesh = AssetManager_LookupMesh("Cube");
block->material = Material_Allocate();
Material_Initialize(block->material, AssetManager_LookupTexture("Test"));
block->collider = Collider_Allocate();
AABBCollider_Initialize(block->collider, 2.0f, 2.0f, 2.0f, &Vector_ZERO);
block->body = RigidBody_Allocate();
RigidBody_Initialize(block->body, block->frameOfReference, 0.0f);
block->body->freezeTranslation = block->body->freezeRotation = 1;
block->body->dynamicFriction = block->body->staticFriction = 0.5f;
block->body->rollingResistance = 0.0f;
block->body->coefficientOfRestitution = 0.01f;
Vector v;
Vector_INIT_ON_STACK(v, 3);
v.components[0] = sX;
v.components[2] = sY;
v.components[1] = sZ;
GObject_Scale(block, &v);
Vector_Copy(&v, &Vector_ZERO);
v.components[0] = x;
v.components[1] = y;
v.components[2] = z;
GObject_Translate(block, &v);
return block;
}
///
//Adds a hurdle obstacle to the list of objects in the set
//at the specified z offset
//
//Parameters:
// members: A pointer to the members of the environmentBuilder state to which the hudle is being added
// zOffset: the z position of the new hurdle within the set
// index: the index to insert the new obstacle at
static void State_EnvironmentBuilder_AddHurdle(struct State_EnvironmentBuilder_Members* members, float zOffset, int index)
{
GObject* block = State_EnvironmentBuilder_GenerateBlock(0.0f, 1.0f, zOffset, 10.0f, 2.0f, 1.0f);
LinkedList_Insert(members->objs, index, block);
}
///
//Spawns another set of the EnvironmentBuilder's object set at the current offset + the current size
//
//Parameters:
// members: The state members struct of the EnvironmentBuilder state being modified
static void State_EnvironmentBuilder_Spawn(struct State_EnvironmentBuilder_Members* members, GObject* attached)
{
//for every object in the current set
struct LinkedList_Node* current = members->objs->head;
GObject* obj = NULL;
while(current != NULL)
{
obj = (GObject*)current->data;
//Make a copy
GObject* copy = GObject_Allocate();
GObject_InitializeDeepCopy(copy, obj);
//Translate to current offset + the current set size
Vector trans;
Vector_INIT_ON_STACK(trans, 3);
trans.components[2] -= members->currentOffset + members->currentSize;
GObject_Translate(copy, &trans);
State* s = State_Allocate();
State_ProximityRemove_Initialize(s, attached->frameOfReference->position, 150.0f);
GObject_AddState(copy, s);
//Add new copy to object manager
ObjectManager_AddObject(copy);
current = current->next;
}
}