-
Notifications
You must be signed in to change notification settings - Fork 0
/
w_world.c
319 lines (292 loc) · 7.83 KB
/
w_world.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
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
#include "w_world.h"
#include "m_math.h"
#include "w_create.h"
#include "v_memory.h"
#include "r_render.h"
#include "w_collision.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
int w_ObjectCount;
int w_EmitableCount;
W_Object w_Objects[W_MAX_OBJ];
W_Emitable w_Emitables[W_MAX_EMIT];
Geo w_Geos[W_MAX_OBJ];
V_block* w_ObjectVertexBlock;
V_block* w_ObjectIndexBlock;
V_block* w_EmitableVertexBlock;
Vertex* w_ObjectVertexBuffer;
Index* w_ObjectIndexBuffer;
Vertex* w_EmitableVertexBuffer;
Collider w_Colliders[W_MAX_OBJ];
static void rotateGeo(const float angle, const Geo* geo)
{
for (int i = 0; i < geo->vertCount; i++)
{
m_Rotate(angle, &w_ObjectVertexBuffer[geo->vertIndex + i]);
}
}
static void translateGeo(const Vec2 t, const Geo* geo)
{
for (int i = 0; i < geo->vertCount; i++)
{
m_Translate(t, &w_ObjectVertexBuffer[geo->vertIndex + i]);
}
}
static void resetObjectGeo(const int index)
{
assert( index >= 0 );
const W_Object* object = &w_Objects[index];
const Vec2 t = { -1 * object->prevPos.x, -1 * object->prevPos.y };
translateGeo(t, &w_Geos[index]);
rotateGeo(-1 * object->prevAngle, &w_Geos[index]); // reset
}
static void updateObjectGeo(const int index)
{
assert( index >= 0 );
const W_Object* object = &w_Objects[index];
rotateGeo(object->angle, &w_Geos[index]);
translateGeo(object->pos, &w_Geos[index]);
}
static void wrapAround(W_Object* object)
{
float x = object->pos.x;
float y = object->pos.y;
if (x < -1) x += 2;
if (x > 1) x -= 2;
if (y < -1) y += 2;
if (y > 1) y -= 2;
object->pos.x = x;
object->pos.y = y;
}
static void updatePlayer(void)
{
W_Object* player = &w_Objects[0];
float speed = m_Length(player->vel);
float drag;
player->drag = (drag = speed * 5) > 1 ? 1 : drag;
}
static void updateObject(const int index)
{
assert( index >= 0 );
W_Object* object = &w_Objects[index];
object->prevAngle = object->angle;
object->prevPos = object->pos;
m_Add(object->accel, &object->vel);
m_Add(object->vel, &object->pos);
wrapAround(object);
m_Scale(1 - object->drag, &object->vel);
object->angVel += object->angAccel;
object->angle += object->angVel;
object->angVel *= (1 - object->angDrag);
}
static void updateEmitable(const int index)
{
assert( index >= 0 );
W_Emitable* emitable = &w_Emitables[index];
emitable->prevPos = emitable->pos;
m_Add(emitable->vel, &emitable->pos);
}
static void initObjects(void)
{
w_ObjectCount = 6;
w_ObjectVertexBlock = v_RequestBlock(MAX_VERTS_PER_OBJ * W_MAX_OBJ * sizeof(Vertex));
w_ObjectVertexBuffer = (Vertex*)w_ObjectVertexBlock->address;
//w_ObjectIndexBlock = z_RequestBlock(
assert(w_ObjectCount <= W_MAX_OBJ);
for (int i = 0; i < W_MAX_OBJ; i++)
{
w_Geos[i].vertCount = MAX_VERTS_PER_OBJ;
w_Geos[i].vertIndex = i * MAX_VERTS_PER_OBJ;
}
for (int i = 0; i < w_ObjectCount; i++)
{
float angVel = 0.01 * m_Rand();
float tx = m_RandNeg();
float ty = m_RandNeg();
w_Objects[i].pos = (Vec2){tx, ty};
w_Objects[i].mass = 1.0;
w_Objects[i].angle = i;
w_Objects[i].stage = BIG;
if (i == 0) //is player
{
w_GeneratePlayerShip(i);
angVel = 0.0;
w_Objects[i].drag = 0.4;
w_Objects[i].angDrag = 0.1;
}
else
{
w_GenerateAsteroidRand1(i, 0.1);
w_Objects[i].vel.x = m_RandNeg() * INIT_SPEED;
w_Objects[i].vel.y = m_RandNeg() * INIT_SPEED;
}
w_Objects[i].angVel = angVel;
updateObjectGeo(i);
}
}
static void initEmitables(void)
{
w_EmitableVertexBlock = v_RequestBlock(W_MAX_EMIT * sizeof(Vertex));
w_EmitableVertexBuffer = (Vertex*)w_EmitableVertexBlock->address;
}
static void swapEmit(int a, int b)
{
assert( a >= 0 && b >= 0 );
W_Emitable temp = w_Emitables[a];
w_Emitables[a] = w_Emitables[b];
w_Emitables[b] = temp;
Vertex tempVert = w_EmitableVertexBuffer[a];
w_EmitableVertexBuffer[a] = w_EmitableVertexBuffer[b];
w_EmitableVertexBuffer[b] = tempVert;
}
static void swapObject(int a, int b)
{
assert( a >= 0 && b >= 0 );
const W_Object tempObject = w_Objects[a];
w_Objects[a] = w_Objects[b];
w_Objects[b] = tempObject;
const Geo tempGeo = w_Geos[a];
w_Geos[a] = w_Geos[b];
w_Geos[b] = tempGeo;
const Collider tempCol = w_Colliders[a];
w_Colliders[a] = w_Colliders[b];
w_Colliders[b] = tempCol;
}
static void physicsUpdate(void)
{
const int objectCount = w_ObjectCount;
const int emitableCount = w_EmitableCount;
for (int i = 0; i < objectCount; i++)
{
updateObject(i);
}
for (int i = 0; i < emitableCount; i++)
{
updateEmitable(i);
}
}
static void collisionsUpdate(void)
{
w_DetectCollisions();
}
static void initAsteroid(W_Object* obj)
{
// only need to explicitly set what should be non-zero
W_Object newObj = {
.mass = 1.0,
.destroyed = false,
};
*obj = newObj;
}
static void spawnChildren()
{
const int childCount = 4;
const int objCount = w_ObjectCount;
assert(childCount + objCount < W_MAX_OBJ);
const Vec2 basePos = w_Objects[objCount].pos;
const Vec2 baseVel = w_Objects[objCount].vel;
const AstStage baseStage = w_Objects[objCount].stage;
const float baseAngVel = w_Objects[objCount].angVel;
const float baseRadius = w_Colliders[objCount].radius / 4;
for (int i = objCount; i < objCount + childCount; i++)
{
W_Object* obj = &w_Objects[i];
initAsteroid(obj);
obj->angle = m_Rand() * M_PI * 2;
obj->stage = baseStage + 1;
Vec2 vel = {INIT_SPEED / 2, 0};
m_Rotate(i, &vel);
//m_Add(baseVel, &vel);
obj->vel = vel;
obj->pos = basePos;
obj->angVel = baseAngVel;
w_GenerateAsteroidRand1(i, baseRadius);
}
w_ObjectCount += childCount;
}
static void reapAndSpawn(void)
{
const int objCount = w_ObjectCount;
const int emtCount = w_EmitableCount;
int deadObject = -1;
for (int i = 0; i < objCount; i++)
{
if (w_Objects[i].destroyed)
{
//destroyObject(i);
deadObject = i;
break;
}
}
if (deadObject != -1)
{
swapObject(deadObject, --w_ObjectCount);
assert(w_ObjectCount >= 0);
if (w_Objects[w_ObjectCount].stage < FINAL)
spawnChildren();
}
int deadEmit = -1;
for (int i = 0; i < emtCount; i++)
{
W_Emitable* emitable = &w_Emitables[i];
emitable->lifeTicks--;
if (w_Emitables[i].lifeTicks <= 0)
{
deadEmit = i;
}
}
if (deadEmit != -1)
{
swapEmit(deadEmit, --w_EmitableCount);
assert( w_EmitableCount >= 0 );
}
}
void w_DetectCollisions(void)
{
HitInfo hi = w_DetectBulletObjectCols();
if (hi.collision)
{
w_Emitables[hi.object1].lifeTicks = 0;
w_Objects[hi.object2].destroyed = true;
}
}
void w_Init(void)
{
initObjects();
initEmitables();
}
void w_Update(void)
{
updatePlayer();
physicsUpdate();
collisionsUpdate();
r_WaitOnQueueSubmit();
reapAndSpawn();
w_UpdateDrawables();
}
void w_UpdateDrawables(void)
{
const int objCount = w_ObjectCount;
const int emtCount = w_EmitableCount;
for (int i = 0; i < objCount; i++)
{
resetObjectGeo(i);
updateObjectGeo(i);
}
for (int i = 0; i < emtCount; i++)
{
Vertex* vert = &w_EmitableVertexBuffer[i];
vert->x = 0.0;
vert->y = 0.0;
m_Translate(w_Emitables[i].pos, vert);
}
}
void w_CleanUp()
{
for (int i = 0; i < W_MAX_OBJ; i++)
{
// nothing for now
}
}