2016-11-09 11:36:41 +00:00
|
|
|
#include "Dengine/Asteroid.h"
|
|
|
|
#include "Dengine/Debug.h"
|
|
|
|
|
|
|
|
void initAssetManager(GameState *state)
|
|
|
|
{
|
|
|
|
AssetManager *assetManager = &state->assetManager;
|
|
|
|
MemoryArena_ *arena = &state->persistentArena;
|
|
|
|
|
|
|
|
i32 texAtlasEntries = 8;
|
|
|
|
assetManager->texAtlas.size = texAtlasEntries;
|
|
|
|
assetManager->texAtlas.entries =
|
|
|
|
memory_pushBytes(arena, texAtlasEntries * sizeof(HashTableEntry));
|
|
|
|
|
|
|
|
i32 animEntries = 1024;
|
|
|
|
assetManager->anims.size = animEntries;
|
|
|
|
assetManager->anims.entries =
|
|
|
|
memory_pushBytes(arena, animEntries * sizeof(HashTableEntry));
|
|
|
|
|
2016-11-24 11:54:07 +00:00
|
|
|
{ // Init texture assets
|
|
|
|
i32 texEntries = 32;
|
|
|
|
assetManager->textures.size = texEntries;
|
|
|
|
assetManager->textures.entries =
|
|
|
|
memory_pushBytes(arena, texEntries * sizeof(HashTableEntry));
|
2016-11-09 11:36:41 +00:00
|
|
|
|
2016-11-24 11:54:07 +00:00
|
|
|
/* Create empty 1x1 4bpp black texture */
|
|
|
|
u32 bitmap = (0xFF << 24) | (0xFF << 16) | (0xFF << 8) | (0xFF << 0);
|
|
|
|
Texture *tex = asset_getFreeTexSlot(assetManager, arena, "nullTex");
|
|
|
|
*tex = texture_gen(1, 1, 4, CAST(u8 *)(&bitmap));
|
2016-11-11 14:17:30 +00:00
|
|
|
|
2016-11-24 11:54:07 +00:00
|
|
|
i32 result = asset_loadTTFont(assetManager, arena,
|
|
|
|
"C:/Windows/Fonts/Arialbd.ttf");
|
|
|
|
}
|
2016-11-09 11:36:41 +00:00
|
|
|
|
2016-11-24 11:54:07 +00:00
|
|
|
{ // Init shaders assets
|
|
|
|
asset_loadShaderFiles(
|
|
|
|
assetManager, arena, "data/shaders/default_tex.vert.glsl",
|
|
|
|
"data/shaders/default_tex.frag.glsl", shaderlist_default);
|
|
|
|
|
|
|
|
asset_loadShaderFiles(
|
|
|
|
assetManager, arena, "data/shaders/default_no_tex.vert.glsl",
|
|
|
|
"data/shaders/default_no_tex.frag.glsl", shaderlist_default_no_tex);
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // Init audio assets
|
|
|
|
|
|
|
|
i32 audioEntries = 32;
|
|
|
|
assetManager->audio.size = audioEntries;
|
|
|
|
assetManager->audio.entries =
|
|
|
|
memory_pushBytes(arena, audioEntries * sizeof(HashTableEntry));
|
|
|
|
|
|
|
|
i32 result = asset_loadVorbis(assetManager, arena,
|
|
|
|
"data/audio/Asteroids/bang_large.ogg",
|
|
|
|
"bang_large");
|
|
|
|
ASSERT(!result);
|
|
|
|
result = asset_loadVorbis(assetManager, arena,
|
|
|
|
"data/audio/Asteroids/bang_medium.ogg",
|
|
|
|
"bang_medium");
|
|
|
|
ASSERT(!result);
|
|
|
|
result = asset_loadVorbis(assetManager, arena,
|
|
|
|
"data/audio/Asteroids/bang_small.ogg",
|
|
|
|
"bang_small");
|
|
|
|
ASSERT(!result);
|
|
|
|
result = asset_loadVorbis(assetManager, arena,
|
|
|
|
"data/audio/Asteroids/beat1.ogg", "beat1");
|
|
|
|
ASSERT(!result);
|
|
|
|
result = asset_loadVorbis(assetManager, arena,
|
|
|
|
"data/audio/Asteroids/beat2.ogg", "beat2");
|
|
|
|
ASSERT(!result);
|
|
|
|
result = asset_loadVorbis(assetManager, arena,
|
|
|
|
"data/audio/Asteroids/extra_ship.ogg",
|
|
|
|
"extra_ship");
|
|
|
|
ASSERT(!result);
|
|
|
|
result = asset_loadVorbis(assetManager, arena,
|
|
|
|
"data/audio/Asteroids/fire.ogg", "fire");
|
|
|
|
ASSERT(!result);
|
|
|
|
result = asset_loadVorbis(assetManager, arena,
|
|
|
|
"data/audio/Asteroids/saucer_big.ogg",
|
|
|
|
"saucer_big");
|
|
|
|
ASSERT(!result);
|
|
|
|
result = asset_loadVorbis(assetManager, arena,
|
|
|
|
"data/audio/Asteroids/saucer_small.ogg",
|
|
|
|
"saucer_small");
|
|
|
|
ASSERT(!result);
|
|
|
|
result = asset_loadVorbis(assetManager, arena,
|
|
|
|
"data/audio/Asteroids/thrust.ogg", "thrust");
|
|
|
|
ASSERT(!result);
|
|
|
|
}
|
2016-11-09 11:36:41 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 11:54:07 +00:00
|
|
|
void initRenderer(GameState *state, v2 windowSize)
|
|
|
|
{
|
2016-11-09 11:36:41 +00:00
|
|
|
AssetManager *assetManager = &state->assetManager;
|
|
|
|
Renderer *renderer = &state->renderer;
|
|
|
|
renderer->size = windowSize;
|
|
|
|
|
|
|
|
// NOTE(doyle): Value to map a screen coordinate to NDC coordinate
|
|
|
|
renderer->vertexNdcFactor =
|
|
|
|
V2(1.0f / renderer->size.w, 1.0f / renderer->size.h);
|
2016-11-17 10:03:55 +00:00
|
|
|
renderer->groupIndexForVertexBatch = -1;
|
2016-11-09 11:36:41 +00:00
|
|
|
|
|
|
|
const mat4 projection =
|
|
|
|
mat4_ortho(0.0f, renderer->size.w, 0.0f, renderer->size.h, 0.0f, 1.0f);
|
2016-11-11 14:17:30 +00:00
|
|
|
for (i32 i = 0; i < shaderlist_count; i++)
|
|
|
|
{
|
|
|
|
renderer->shaderList[i] = asset_getShader(assetManager, i);
|
|
|
|
shader_use(renderer->shaderList[i]);
|
|
|
|
shader_uniformSetMat4fv(renderer->shaderList[i], "projection",
|
|
|
|
projection);
|
|
|
|
GL_CHECK_ERROR();
|
|
|
|
}
|
|
|
|
|
|
|
|
renderer->activeShaderId = renderer->shaderList[shaderlist_default];
|
2016-11-09 11:36:41 +00:00
|
|
|
GL_CHECK_ERROR();
|
|
|
|
|
|
|
|
/* Create buffers */
|
2016-11-11 10:43:03 +00:00
|
|
|
glGenVertexArrays(ARRAY_COUNT(renderer->vao), renderer->vao);
|
|
|
|
glGenBuffers(ARRAY_COUNT(renderer->vbo), renderer->vbo);
|
2016-11-09 11:36:41 +00:00
|
|
|
GL_CHECK_ERROR();
|
|
|
|
|
2016-11-16 02:00:59 +00:00
|
|
|
// Bind buffers and configure vao, vao automatically intercepts
|
|
|
|
// glBindCalls and associates the state with that buffer for us
|
|
|
|
for (enum RenderMode mode = 0; mode < rendermode_count; mode++)
|
2016-11-11 10:43:03 +00:00
|
|
|
{
|
2016-11-16 02:00:59 +00:00
|
|
|
glBindVertexArray(renderer->vao[mode]);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, renderer->vbo[mode]);
|
2016-11-11 10:43:03 +00:00
|
|
|
|
|
|
|
glEnableVertexAttribArray(0);
|
|
|
|
u32 numVertexElements = 4;
|
2016-11-23 02:42:46 +00:00
|
|
|
u32 stride = sizeof(RenderVertex);
|
2016-11-11 10:43:03 +00:00
|
|
|
|
|
|
|
glVertexAttribPointer(0, numVertexElements, GL_FLOAT,
|
|
|
|
GL_FALSE, stride, (GLvoid *)0);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
glBindVertexArray(0);
|
|
|
|
}
|
2016-11-09 11:36:41 +00:00
|
|
|
|
|
|
|
/* Unbind */
|
|
|
|
GL_CHECK_ERROR();
|
|
|
|
|
|
|
|
// TODO(doyle): Lazy allocate render group capacity
|
|
|
|
renderer->groupCapacity = 4096;
|
|
|
|
for (i32 i = 0; i < ARRAY_COUNT(renderer->groups); i++)
|
|
|
|
{
|
2016-11-23 02:42:46 +00:00
|
|
|
renderer->groups[i].vertexList =
|
|
|
|
memory_pushBytes(&state->persistentArena,
|
|
|
|
renderer->groupCapacity * sizeof(RenderVertex));
|
2016-11-09 11:36:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ReadKeyType
|
|
|
|
{
|
|
|
|
readkeytype_oneShot,
|
|
|
|
readkeytype_delayedRepeat,
|
|
|
|
readkeytype_repeat,
|
|
|
|
readkeytype_count,
|
|
|
|
};
|
|
|
|
|
|
|
|
#define KEY_DELAY_NONE 0.0f
|
|
|
|
INTERNAL b32 getKeyStatus(KeyState *key, enum ReadKeyType readType,
|
|
|
|
f32 delayInterval, f32 dt)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!key->endedDown) return FALSE;
|
|
|
|
|
|
|
|
switch(readType)
|
|
|
|
{
|
|
|
|
case readkeytype_oneShot:
|
|
|
|
{
|
|
|
|
if (key->newHalfTransitionCount > key->oldHalfTransitionCount)
|
|
|
|
return TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case readkeytype_repeat:
|
|
|
|
case readkeytype_delayedRepeat:
|
|
|
|
{
|
|
|
|
if (key->newHalfTransitionCount > key->oldHalfTransitionCount)
|
|
|
|
{
|
|
|
|
if (readType == readkeytype_delayedRepeat)
|
|
|
|
{
|
|
|
|
// TODO(doyle): Let user set arbitrary delay after initial input
|
|
|
|
key->delayInterval = 2 * delayInterval;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
key->delayInterval = delayInterval;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else if (key->delayInterval <= 0.0f)
|
|
|
|
{
|
|
|
|
key->delayInterval = delayInterval;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
key->delayInterval -= dt;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
#ifdef DENGINE_DEBUG
|
|
|
|
DEBUG_LOG("getKeyStatus() error: Invalid ReadKeyType enum");
|
|
|
|
ASSERT(INVALID_CODE_PATH);
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2016-11-16 13:50:49 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
2016-11-17 07:15:36 +00:00
|
|
|
v2 *createAsteroidVertexList(MemoryArena_ *arena, i32 iterations,
|
|
|
|
i32 asteroidRadius)
|
2016-11-16 13:50:49 +00:00
|
|
|
{
|
|
|
|
f32 iterationAngle = 360.0f / iterations;
|
|
|
|
iterationAngle = DEGREES_TO_RADIANS(iterationAngle);
|
2016-11-23 12:47:23 +00:00
|
|
|
v2 *result = memory_pushBytes(arena, iterations * sizeof(v2));
|
2016-11-16 13:50:49 +00:00
|
|
|
|
|
|
|
for (i32 i = 0; i < iterations; i++)
|
|
|
|
{
|
2016-11-17 07:15:36 +00:00
|
|
|
i32 randValue = rand();
|
2016-11-19 05:42:13 +00:00
|
|
|
|
|
|
|
// NOTE(doyle): Sin/cos generate values from +-1, we want to create
|
|
|
|
// vertices that start from 0, 0 (i.e. strictly positive)
|
|
|
|
result[i] = V2(((math_cosf(iterationAngle * i) + 1) * asteroidRadius),
|
|
|
|
((math_sinf(iterationAngle * i) + 1) * asteroidRadius));
|
2016-11-16 13:50:49 +00:00
|
|
|
|
2016-11-24 07:57:44 +00:00
|
|
|
ASSERT(result[i].x >= 0 && result[i].y >= 0);
|
|
|
|
|
2016-11-16 13:50:49 +00:00
|
|
|
#if 1
|
2016-11-17 07:15:36 +00:00
|
|
|
f32 displacementDist = 0.50f * asteroidRadius;
|
2016-11-16 13:50:49 +00:00
|
|
|
i32 vertexDisplacement =
|
2016-11-17 07:15:36 +00:00
|
|
|
randValue % (i32)displacementDist + (i32)(displacementDist * 0.25f);
|
2016-11-16 13:50:49 +00:00
|
|
|
|
|
|
|
i32 quadrantSize = iterations / 4;
|
|
|
|
|
|
|
|
i32 firstQuadrant = quadrantSize;
|
|
|
|
i32 secondQuadrant = quadrantSize * 2;
|
|
|
|
i32 thirdQuadrant = quadrantSize * 3;
|
|
|
|
i32 fourthQuadrant = quadrantSize * 4;
|
|
|
|
|
|
|
|
if (i < firstQuadrant)
|
|
|
|
{
|
|
|
|
result[i].x += vertexDisplacement;
|
|
|
|
result[i].y += vertexDisplacement;
|
|
|
|
}
|
|
|
|
else if (i < secondQuadrant)
|
|
|
|
{
|
|
|
|
result[i].x -= vertexDisplacement;
|
|
|
|
result[i].y += vertexDisplacement;
|
|
|
|
}
|
|
|
|
else if (i < thirdQuadrant)
|
|
|
|
{
|
|
|
|
result[i].x -= vertexDisplacement;
|
|
|
|
result[i].y -= vertexDisplacement;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result[i].x += vertexDisplacement;
|
|
|
|
result[i].y -= vertexDisplacement;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-11-23 02:42:46 +00:00
|
|
|
v2 *createNormalEdgeList(MemoryArena_ *transientArena, v2 *vertexList,
|
|
|
|
i32 vertexListSize)
|
2016-11-18 06:42:11 +00:00
|
|
|
{
|
2016-11-23 02:42:46 +00:00
|
|
|
v2 *result = memory_pushBytes(transientArena, sizeof(v2) * vertexListSize);
|
|
|
|
for (i32 i = 0; i < vertexListSize - 1; i++)
|
2016-11-18 06:42:11 +00:00
|
|
|
{
|
2016-11-23 02:42:46 +00:00
|
|
|
ASSERT((i + 1) < vertexListSize);
|
|
|
|
result[i] = v2_sub(vertexList[i + 1], vertexList[i]);
|
|
|
|
result[i] = v2_perpendicular(result[i]);
|
2016-11-18 06:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE(doyle): Creating the last edge requires using the first
|
|
|
|
// vertex point which is at index 0
|
2016-11-23 02:42:46 +00:00
|
|
|
result[vertexListSize - 1] =
|
|
|
|
v2_sub(vertexList[0], vertexList[vertexListSize - 1]);
|
|
|
|
result[vertexListSize - 1] = v2_perpendicular(result[vertexListSize - 1]);
|
2016-11-18 06:42:11 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-11-23 02:42:46 +00:00
|
|
|
v2 calculateProjectionRangeForEdge(v2 *vertexList, i32 vertexListSize,
|
|
|
|
v2 edgeNormal)
|
2016-11-19 05:42:13 +00:00
|
|
|
{
|
|
|
|
v2 result = {0};
|
2016-11-23 02:42:46 +00:00
|
|
|
result.min = v2_dot(vertexList[0], edgeNormal);
|
2016-11-19 05:42:13 +00:00
|
|
|
result.max = result.min;
|
|
|
|
|
2016-11-23 02:42:46 +00:00
|
|
|
for (i32 vertexIndex = 0; vertexIndex < vertexListSize; vertexIndex++)
|
2016-11-19 05:42:13 +00:00
|
|
|
{
|
2016-11-23 02:42:46 +00:00
|
|
|
f32 dist = v2_dot(vertexList[vertexIndex], edgeNormal);
|
2016-11-19 05:42:13 +00:00
|
|
|
|
|
|
|
if (dist < result.min)
|
|
|
|
result.min = dist;
|
|
|
|
else if (dist > result.max)
|
|
|
|
result.max = dist;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-11-23 02:42:46 +00:00
|
|
|
b32 checkEdgeProjectionOverlap(v2 *vertexList, i32 listSize,
|
|
|
|
v2 *checkVertexList, i32 checkListSize,
|
|
|
|
v2 *edgeList, i32 totalNumEdges)
|
2016-11-18 06:42:11 +00:00
|
|
|
{
|
|
|
|
b32 result = TRUE;
|
2016-11-23 02:42:46 +00:00
|
|
|
for (i32 edgeIndex = 0; edgeIndex < totalNumEdges && result; edgeIndex++)
|
2016-11-18 06:42:11 +00:00
|
|
|
{
|
2016-11-23 02:42:46 +00:00
|
|
|
v2 projectionRange = calculateProjectionRangeForEdge(
|
|
|
|
vertexList, listSize, edgeList[edgeIndex]);
|
|
|
|
|
|
|
|
v2 checkProjectionRange = calculateProjectionRangeForEdge(
|
|
|
|
checkVertexList, checkListSize, edgeList[edgeIndex]);
|
2016-11-18 06:42:11 +00:00
|
|
|
|
2016-11-23 02:42:46 +00:00
|
|
|
if (!v2_intervalsOverlap(projectionRange, checkProjectionRange))
|
2016-11-18 06:42:11 +00:00
|
|
|
{
|
|
|
|
result = FALSE;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-11-24 10:05:14 +00:00
|
|
|
INTERNAL u32 moveEntity(World *world, MemoryArena_ *transientArena,
|
2016-11-24 07:57:44 +00:00
|
|
|
Entity *entity, i32 entityIndex, v2 ddP, f32 dt,
|
|
|
|
f32 ddPSpeed)
|
2016-11-18 02:43:50 +00:00
|
|
|
{
|
|
|
|
ASSERT(ABS(ddP.x) <= 1.0f && ABS(ddP.y) <= 1.0f);
|
|
|
|
/*
|
|
|
|
Assuming acceleration A over t time, then integrate twice to get
|
|
|
|
|
|
|
|
newVelocity = a*t + oldVelocity
|
|
|
|
newPos = (a*t^2)/2 + oldVelocity*t + oldPos
|
|
|
|
*/
|
|
|
|
|
2016-11-25 02:17:24 +00:00
|
|
|
if (ddP.x > 0.0f && ddP.y > 0.0f)
|
|
|
|
{
|
|
|
|
// NOTE(doyle): Cheese it and pre-compute the vector for
|
|
|
|
// diagonal using pythagoras theorem on a unit triangle 1^2
|
|
|
|
// + 1^2 = c^2
|
|
|
|
ddP = v2_scale(ddP, 0.70710678118f);
|
|
|
|
}
|
|
|
|
|
2016-11-24 07:57:44 +00:00
|
|
|
ddP = v2_scale(ddP, world->pixelsPerMeter * ddPSpeed);
|
2016-11-18 02:43:50 +00:00
|
|
|
v2 oldDp = entity->dP;
|
|
|
|
v2 resistance = v2_scale(oldDp, 2.0f);
|
|
|
|
ddP = v2_sub(ddP, resistance);
|
|
|
|
|
|
|
|
v2 newDp = v2_add(v2_scale(ddP, dt), oldDp);
|
|
|
|
|
|
|
|
v2 ddPHalf = v2_scale(ddP, 0.5f);
|
|
|
|
v2 ddPHalfDtSquared = v2_scale(ddPHalf, (SQUARED(dt)));
|
|
|
|
v2 oldDpDt = v2_scale(oldDp, dt);
|
|
|
|
v2 oldPos = entity->pos;
|
|
|
|
|
|
|
|
v2 newPos = v2_add(v2_add(ddPHalfDtSquared, oldDpDt), oldPos);
|
|
|
|
|
2016-11-24 10:05:14 +00:00
|
|
|
i32 collisionIndex = -1;
|
2016-11-23 12:47:23 +00:00
|
|
|
// TODO(doyle): Collision for rects, (need to create vertex list for it)
|
2016-11-24 10:05:14 +00:00
|
|
|
for (i32 i = 1; i < world->entityIndex; i++)
|
2016-11-18 02:43:50 +00:00
|
|
|
{
|
2016-11-24 10:05:14 +00:00
|
|
|
if (i == entityIndex) continue;
|
|
|
|
|
2016-11-24 07:57:44 +00:00
|
|
|
Entity *checkEntity = &world->entityList[i];
|
|
|
|
ASSERT(checkEntity->id != entity->id);
|
2016-11-18 06:42:11 +00:00
|
|
|
|
2016-11-24 07:57:44 +00:00
|
|
|
if (world->collisionTable[entity->type][checkEntity->type])
|
|
|
|
{
|
|
|
|
ASSERT(entity->vertexPoints);
|
|
|
|
ASSERT(checkEntity->vertexPoints);
|
|
|
|
|
|
|
|
/* Create entity edge lists */
|
|
|
|
v2 *entityVertexListOffsetToP = entity_generateUpdatedVertexList(
|
|
|
|
transientArena, entity);
|
|
|
|
|
|
|
|
v2 *checkEntityVertexListOffsetToP =
|
|
|
|
entity_generateUpdatedVertexList(transientArena,
|
|
|
|
checkEntity);
|
|
|
|
|
|
|
|
v2 *entityEdgeList = createNormalEdgeList(transientArena,
|
|
|
|
entityVertexListOffsetToP,
|
|
|
|
entity->numVertexPoints);
|
|
|
|
|
|
|
|
v2 *checkEntityEdgeList = createNormalEdgeList(
|
|
|
|
transientArena, checkEntityVertexListOffsetToP,
|
|
|
|
checkEntity->numVertexPoints);
|
|
|
|
|
|
|
|
/* Combine both edge lists into one */
|
|
|
|
i32 totalNumEdges =
|
|
|
|
checkEntity->numVertexPoints + entity->numVertexPoints;
|
|
|
|
v2 *edgeList =
|
|
|
|
memory_pushBytes(transientArena, totalNumEdges * sizeof(v2));
|
|
|
|
for (i32 i = 0; i < entity->numVertexPoints; i++)
|
2016-11-18 06:42:11 +00:00
|
|
|
{
|
2016-11-24 07:57:44 +00:00
|
|
|
edgeList[i] = entityEdgeList[i];
|
|
|
|
}
|
2016-11-18 06:42:11 +00:00
|
|
|
|
2016-11-24 07:57:44 +00:00
|
|
|
for (i32 i = 0; i < checkEntity->numVertexPoints; i++)
|
|
|
|
{
|
|
|
|
edgeList[i + entity->numVertexPoints] = checkEntityEdgeList[i];
|
2016-11-18 06:42:11 +00:00
|
|
|
}
|
2016-11-18 02:43:50 +00:00
|
|
|
|
2016-11-24 07:57:44 +00:00
|
|
|
if (checkEdgeProjectionOverlap(
|
|
|
|
entityVertexListOffsetToP, entity->numVertexPoints,
|
|
|
|
checkEntityVertexListOffsetToP,
|
|
|
|
checkEntity->numVertexPoints, edgeList, totalNumEdges))
|
|
|
|
{
|
2016-11-24 10:05:14 +00:00
|
|
|
collisionIndex = i;
|
2016-11-19 05:42:13 +00:00
|
|
|
}
|
2016-11-18 06:42:11 +00:00
|
|
|
}
|
2016-11-24 07:57:44 +00:00
|
|
|
|
2016-11-24 10:05:14 +00:00
|
|
|
if (collisionIndex != -1) break;
|
2016-11-23 02:42:46 +00:00
|
|
|
}
|
|
|
|
|
2016-11-23 12:47:23 +00:00
|
|
|
entity->dP = newDp;
|
|
|
|
entity->pos = newPos;
|
|
|
|
|
2016-11-24 10:05:14 +00:00
|
|
|
return collisionIndex;
|
2016-11-18 02:43:50 +00:00
|
|
|
}
|
|
|
|
|
2016-11-25 02:17:24 +00:00
|
|
|
enum AsteroidSize
|
|
|
|
{
|
|
|
|
asteroidsize_small,
|
|
|
|
asteroidsize_medium,
|
|
|
|
asteroidsize_large,
|
|
|
|
asteroidsize_count,
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
v2 pos;
|
|
|
|
v2 dP;
|
|
|
|
enum Direction direction;
|
|
|
|
} AsteroidSpec;
|
|
|
|
|
|
|
|
INTERNAL void addAsteroidWithSpec(World *world, enum AsteroidSize asteroidSize,
|
|
|
|
AsteroidSpec *spec)
|
2016-11-23 13:27:24 +00:00
|
|
|
{
|
2016-11-24 10:05:14 +00:00
|
|
|
world->asteroidCounter++;
|
|
|
|
|
2016-11-25 02:17:24 +00:00
|
|
|
enum EntityType type;
|
|
|
|
v2 size;
|
|
|
|
v2 **vertexCache = NULL;
|
|
|
|
|
|
|
|
if (asteroidSize == asteroidsize_small)
|
|
|
|
{
|
|
|
|
size = V2i(25, 25);
|
|
|
|
type = entitytype_asteroid_small;
|
|
|
|
vertexCache = world->asteroidSmallVertexCache;
|
|
|
|
}
|
|
|
|
else if (asteroidSize == asteroidsize_medium)
|
|
|
|
{
|
|
|
|
size = V2i(50, 50);
|
|
|
|
type = entitytype_asteroid_medium;
|
|
|
|
vertexCache = world->asteroidMediumVertexCache;
|
|
|
|
}
|
|
|
|
else if (asteroidSize == asteroidsize_large)
|
|
|
|
{
|
|
|
|
type = entitytype_asteroid_large;
|
|
|
|
size = V2i(100, 100);
|
|
|
|
vertexCache = world->asteroidLargeVertexCache;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ASSERT(INVALID_CODE_PATH);
|
|
|
|
}
|
|
|
|
|
2016-11-24 07:57:44 +00:00
|
|
|
Entity *asteroid = &world->entityList[world->entityIndex++];
|
|
|
|
asteroid->id = world->entityIdCounter++;
|
2016-11-23 13:27:24 +00:00
|
|
|
|
|
|
|
i32 randValue = rand();
|
2016-11-25 02:17:24 +00:00
|
|
|
if (!spec)
|
|
|
|
{
|
|
|
|
i32 randX = (randValue % (i32)world->worldSize.w);
|
|
|
|
i32 randY = (randValue % (i32)world->worldSize.h);
|
|
|
|
|
|
|
|
v2 midpoint = v2_scale(world->worldSize, 0.5f);
|
|
|
|
|
|
|
|
Rect topLeftQuadrant = {V2(0, midpoint.y),
|
|
|
|
V2(midpoint.x, world->worldSize.y)};
|
|
|
|
Rect botLeftQuadrant = {V2(0, 0), midpoint};
|
|
|
|
Rect topRightQuadrant = {midpoint, world->worldSize};
|
|
|
|
Rect botRightQuadrant = {V2(midpoint.x, 0),
|
|
|
|
V2(world->worldSize.x, midpoint.y)};
|
|
|
|
|
|
|
|
// NOTE(doyle): Off-screen so asteroids "float" into view. There's no
|
|
|
|
// particular order, just pushing things offscreen when they get
|
|
|
|
// generated
|
|
|
|
// to float back into game space
|
|
|
|
v2 newP = V2i(randX, randY);
|
|
|
|
if (math_pointInRect(topLeftQuadrant, newP))
|
|
|
|
{
|
|
|
|
newP.y += midpoint.y;
|
|
|
|
}
|
|
|
|
else if (math_pointInRect(botLeftQuadrant, newP))
|
|
|
|
{
|
|
|
|
newP.x -= midpoint.x;
|
|
|
|
}
|
|
|
|
else if (math_pointInRect(topRightQuadrant, newP))
|
|
|
|
{
|
|
|
|
newP.y -= midpoint.y;
|
|
|
|
}
|
|
|
|
else if (math_pointInRect(botRightQuadrant, newP))
|
|
|
|
{
|
|
|
|
newP.x += midpoint.x;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ASSERT(INVALID_CODE_PATH);
|
|
|
|
}
|
|
|
|
asteroid->pos = newP;
|
|
|
|
asteroid->direction = direction_null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
asteroid->pos = spec->pos;
|
|
|
|
asteroid->direction = spec->direction;
|
|
|
|
}
|
2016-11-23 13:27:24 +00:00
|
|
|
|
2016-11-25 02:17:24 +00:00
|
|
|
asteroid->size = size;
|
|
|
|
asteroid->hitbox = asteroid->size;
|
|
|
|
asteroid->offset = v2_scale(asteroid->size, -0.5f);
|
|
|
|
asteroid->type = type;
|
|
|
|
asteroid->renderMode = rendermode_polygon;
|
2016-11-23 13:27:24 +00:00
|
|
|
asteroid->numVertexPoints = 10;
|
|
|
|
|
2016-11-25 02:17:24 +00:00
|
|
|
i32 cacheIndex = randValue % ARRAY_COUNT(world->asteroidSmallVertexCache);
|
|
|
|
ASSERT(ARRAY_COUNT(world->asteroidSmallVertexCache) ==
|
|
|
|
ARRAY_COUNT(world->asteroidMediumVertexCache));
|
|
|
|
ASSERT(ARRAY_COUNT(world->asteroidSmallVertexCache) ==
|
|
|
|
ARRAY_COUNT(world->asteroidLargeVertexCache));
|
|
|
|
|
|
|
|
if (!vertexCache[cacheIndex])
|
2016-11-24 10:05:14 +00:00
|
|
|
{
|
2016-11-25 02:17:24 +00:00
|
|
|
vertexCache[cacheIndex] = createAsteroidVertexList(
|
2016-11-24 10:05:14 +00:00
|
|
|
&world->entityArena, asteroid->numVertexPoints,
|
|
|
|
(i32)(asteroid->size.w * 0.5f));
|
|
|
|
}
|
|
|
|
|
2016-11-25 02:17:24 +00:00
|
|
|
asteroid->vertexPoints = vertexCache[cacheIndex];
|
2016-11-24 07:57:44 +00:00
|
|
|
asteroid->color = V4(0.0f, 0.5f, 0.5f, 1.0f);
|
|
|
|
}
|
|
|
|
|
2016-11-25 02:17:24 +00:00
|
|
|
INTERNAL void addAsteroid(World *world, enum AsteroidSize asteroidSize)
|
|
|
|
{
|
|
|
|
addAsteroidWithSpec(world, asteroidSize, NULL);
|
|
|
|
}
|
|
|
|
|
2016-11-24 07:57:44 +00:00
|
|
|
INTERNAL void addBullet(World *world, Entity *shooter)
|
|
|
|
{
|
|
|
|
Entity *bullet = &world->entityList[world->entityIndex++];
|
|
|
|
bullet->id = world->entityIdCounter++;
|
|
|
|
|
|
|
|
bullet->offset = v2_scale(bullet->size, -0.5f);
|
|
|
|
bullet->pos = v2_add(shooter->pos, bullet->offset);
|
|
|
|
bullet->hitbox = bullet->size;
|
|
|
|
bullet->size = V2(2.0f, 20.0f);
|
|
|
|
bullet->rotation = shooter->rotation;
|
|
|
|
bullet->renderMode = rendermode_quad;
|
|
|
|
|
2016-11-24 10:05:14 +00:00
|
|
|
if (!world->bulletVertexCache)
|
|
|
|
{
|
|
|
|
world->bulletVertexCache =
|
|
|
|
memory_pushBytes(&world->entityArena, sizeof(v2) * 4);
|
|
|
|
world->bulletVertexCache[0] = V2(0, bullet->size.h);
|
|
|
|
world->bulletVertexCache[1] = V2(0, 0);
|
|
|
|
world->bulletVertexCache[2] = V2(bullet->size.w, 0);
|
|
|
|
world->bulletVertexCache[3] = bullet->size;
|
|
|
|
}
|
2016-11-24 07:57:44 +00:00
|
|
|
|
2016-11-24 10:05:14 +00:00
|
|
|
bullet->vertexPoints = world->bulletVertexCache;
|
2016-11-24 07:57:44 +00:00
|
|
|
bullet->numVertexPoints = 4;
|
|
|
|
|
|
|
|
bullet->type = entitytype_bullet;
|
|
|
|
bullet->color = V4(1.0f, 1.0f, 0, 1.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
INTERNAL void setCollisionRule(World *world, enum EntityType a,
|
|
|
|
enum EntityType b, b32 rule)
|
|
|
|
{
|
|
|
|
ASSERT(a <= entitytype_count);
|
|
|
|
ASSERT(b <= entitytype_count);
|
|
|
|
world->collisionTable[a][b] = rule;
|
|
|
|
world->collisionTable[b][a] = rule;
|
2016-11-23 13:27:24 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 11:54:07 +00:00
|
|
|
INTERNAL AudioRenderer *getFreeAudioRenderer(World *world)
|
|
|
|
{
|
|
|
|
for (i32 i = 0; i < world->numAudioRenderers; i++)
|
|
|
|
{
|
|
|
|
AudioRenderer *renderer = &world->audioRenderer[i];
|
|
|
|
if (renderer->state == audiostate_stopped)
|
|
|
|
{
|
|
|
|
return renderer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-11-09 11:36:41 +00:00
|
|
|
void asteroid_gameUpdateAndRender(GameState *state, Memory *memory,
|
|
|
|
v2 windowSize, f32 dt)
|
|
|
|
{
|
2016-11-24 07:57:44 +00:00
|
|
|
MemoryIndex globalTransientArenaSize =
|
|
|
|
(MemoryIndex)((f32)memory->transientSize * 0.5f);
|
2016-11-17 07:15:36 +00:00
|
|
|
memory_arenaInit(&state->transientArena, memory->transient,
|
2016-11-24 07:57:44 +00:00
|
|
|
globalTransientArenaSize);
|
2016-11-17 07:15:36 +00:00
|
|
|
|
2016-11-24 07:57:44 +00:00
|
|
|
World *world = &state->world;
|
2016-11-16 02:00:59 +00:00
|
|
|
if (!state->init)
|
|
|
|
{
|
2016-11-23 12:47:23 +00:00
|
|
|
srand((u32)time(NULL));
|
2016-11-09 11:36:41 +00:00
|
|
|
initAssetManager(state);
|
|
|
|
initRenderer(state, windowSize);
|
2016-11-24 11:54:07 +00:00
|
|
|
audio_init(&state->audioManager);
|
2016-11-09 11:36:41 +00:00
|
|
|
|
2016-11-24 07:57:44 +00:00
|
|
|
world->pixelsPerMeter = 70.0f;
|
|
|
|
|
|
|
|
MemoryIndex entityArenaSize =
|
|
|
|
(MemoryIndex)((f32)memory->transientSize * 0.5f);
|
|
|
|
|
|
|
|
u8 *arenaBase = state->transientArena.base + state->transientArena.size;
|
|
|
|
memory_arenaInit(&world->entityArena, arenaBase, entityArenaSize);
|
2016-11-11 10:43:03 +00:00
|
|
|
|
2016-11-24 10:05:14 +00:00
|
|
|
{ // Init null entity
|
|
|
|
Entity *nullEntity = &world->entityList[world->entityIndex++];
|
|
|
|
nullEntity->id = world->entityIdCounter++;
|
|
|
|
}
|
2016-11-24 11:54:07 +00:00
|
|
|
|
2016-11-23 12:47:23 +00:00
|
|
|
{ // Init asteroid entities
|
2016-11-24 10:05:14 +00:00
|
|
|
world->numAsteroids = 15;
|
2016-11-23 12:47:23 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 11:54:07 +00:00
|
|
|
{ // Init audio renderer
|
|
|
|
world->numAudioRenderers = 6;
|
|
|
|
world->audioRenderer = MEMORY_PUSH_ARRAY(
|
|
|
|
&world->entityArena, world->numAudioRenderers, AudioRenderer);
|
|
|
|
}
|
|
|
|
|
2016-11-09 11:36:41 +00:00
|
|
|
{ // Init ship entity
|
2016-11-24 07:57:44 +00:00
|
|
|
Entity *ship = &world->entityList[world->entityIndex++];
|
|
|
|
ship->id = world->entityIdCounter++;
|
2016-11-23 02:42:46 +00:00
|
|
|
ship->pos = V2(100, 100);
|
2016-11-16 02:00:59 +00:00
|
|
|
ship->size = V2(25.0f, 50.0f);
|
|
|
|
ship->hitbox = ship->size;
|
2016-11-24 07:57:44 +00:00
|
|
|
ship->offset = v2_scale(ship->size, -0.5f);
|
2016-11-18 03:33:11 +00:00
|
|
|
|
|
|
|
ship->numVertexPoints = 3;
|
|
|
|
ship->vertexPoints = memory_pushBytes(
|
2016-11-24 07:57:44 +00:00
|
|
|
&world->entityArena, sizeof(v2) * ship->numVertexPoints);
|
2016-11-18 03:33:11 +00:00
|
|
|
|
2016-11-19 05:42:13 +00:00
|
|
|
v2 triangleBaseP = V2(0, 0);
|
|
|
|
v2 triangleTopP = V2(ship->size.w * 0.5f, ship->size.h);
|
|
|
|
v2 triangleRightP = V2(ship->size.w, triangleBaseP.y);
|
2016-11-18 03:33:11 +00:00
|
|
|
|
2016-11-19 05:42:13 +00:00
|
|
|
ship->vertexPoints[0] = triangleBaseP;
|
|
|
|
ship->vertexPoints[1] = triangleRightP;
|
|
|
|
ship->vertexPoints[2] = triangleTopP;
|
2016-11-18 03:33:11 +00:00
|
|
|
|
2016-11-16 02:00:59 +00:00
|
|
|
ship->scale = 1;
|
|
|
|
ship->type = entitytype_ship;
|
|
|
|
ship->direction = direction_null;
|
2016-11-18 03:33:11 +00:00
|
|
|
ship->renderMode = rendermode_polygon;
|
2016-11-24 07:57:44 +00:00
|
|
|
ship->color = V4(1.0f, 0.5f, 0.5f, 1.0f);
|
|
|
|
}
|
2016-11-17 07:15:36 +00:00
|
|
|
|
2016-11-24 07:57:44 +00:00
|
|
|
{ // Global Collision Rules
|
2016-11-25 02:17:24 +00:00
|
|
|
setCollisionRule(world, entitytype_ship, entitytype_asteroid_small,
|
|
|
|
TRUE);
|
|
|
|
setCollisionRule(world, entitytype_ship, entitytype_asteroid_medium,
|
|
|
|
TRUE);
|
|
|
|
setCollisionRule(world, entitytype_ship, entitytype_asteroid_large,
|
2016-11-24 07:57:44 +00:00
|
|
|
TRUE);
|
2016-11-25 02:17:24 +00:00
|
|
|
setCollisionRule(world, entitytype_bullet,
|
|
|
|
entitytype_asteroid_small, TRUE);
|
|
|
|
setCollisionRule(world, entitytype_bullet,
|
|
|
|
entitytype_asteroid_medium, TRUE);
|
|
|
|
setCollisionRule(world, entitytype_bullet,
|
|
|
|
entitytype_asteroid_large, TRUE);
|
2016-11-09 11:36:41 +00:00
|
|
|
}
|
2016-11-23 12:47:23 +00:00
|
|
|
|
2016-11-24 07:57:44 +00:00
|
|
|
world->camera.min = V2(0, 0);
|
|
|
|
world->camera.max = state->renderer.size;
|
|
|
|
world->worldSize = windowSize;
|
2016-11-09 11:36:41 +00:00
|
|
|
|
2016-11-16 05:57:24 +00:00
|
|
|
state->init = TRUE;
|
2016-11-09 11:36:41 +00:00
|
|
|
|
2016-11-16 02:00:59 +00:00
|
|
|
debug_init(&state->persistentArena, windowSize,
|
|
|
|
state->assetManager.font);
|
2016-11-16 13:50:49 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 10:05:14 +00:00
|
|
|
for (u32 i = world->asteroidCounter; i < world->numAsteroids; i++)
|
2016-11-25 02:17:24 +00:00
|
|
|
addAsteroid(world, (rand() % asteroidsize_count));
|
2016-11-24 10:05:14 +00:00
|
|
|
|
2016-11-09 11:36:41 +00:00
|
|
|
{
|
|
|
|
KeyState *keys = state->input.keys;
|
|
|
|
for (enum KeyCode code = 0; code < keycode_count; code++)
|
|
|
|
{
|
|
|
|
KeyState *keyState = &keys[code];
|
|
|
|
|
|
|
|
u32 halfTransitionCount = keyState->newHalfTransitionCount -
|
|
|
|
keyState->oldHalfTransitionCount;
|
|
|
|
|
|
|
|
if (halfTransitionCount > 0)
|
|
|
|
{
|
|
|
|
b32 transitionCountIsOdd = ((halfTransitionCount & 1) == 1);
|
|
|
|
|
|
|
|
if (transitionCountIsOdd)
|
|
|
|
{
|
|
|
|
if (keyState->endedDown) keyState->endedDown = FALSE;
|
|
|
|
else keyState->endedDown = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
keyState->oldHalfTransitionCount =
|
|
|
|
keyState->newHalfTransitionCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-23 13:27:24 +00:00
|
|
|
if (getKeyStatus(&state->input.keys[keycode_left_square_bracket],
|
|
|
|
readkeytype_repeat, 0.2f, dt))
|
|
|
|
{
|
2016-11-25 02:17:24 +00:00
|
|
|
addAsteroid(world, (rand() % asteroidsize_count));
|
2016-11-23 13:27:24 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 10:05:14 +00:00
|
|
|
ASSERT(world->entityList[0].id == NULL_ENTITY_ID);
|
|
|
|
for (i32 i = 1; i < world->entityIndex; i++)
|
2016-11-09 11:36:41 +00:00
|
|
|
{
|
2016-11-24 07:57:44 +00:00
|
|
|
Entity *entity = &world->entityList[i];
|
2016-11-09 11:36:41 +00:00
|
|
|
ASSERT(entity->type != entitytype_invalid);
|
|
|
|
|
2016-11-16 02:00:59 +00:00
|
|
|
v2 pivotPoint = {0};
|
2016-11-18 02:43:50 +00:00
|
|
|
f32 ddPSpeedInMs = 0;
|
|
|
|
v2 ddP = {0};
|
2016-11-16 13:50:49 +00:00
|
|
|
if (entity->type == entitytype_ship)
|
|
|
|
{
|
2016-11-11 10:43:03 +00:00
|
|
|
if (getKeyStatus(&state->input.keys[keycode_up], readkeytype_repeat,
|
|
|
|
0.0f, dt))
|
|
|
|
{
|
2016-11-16 02:00:59 +00:00
|
|
|
// TODO(doyle): Renderer creates upfacing triangles by default,
|
|
|
|
// but we need to offset rotation so that our base "0 degrees"
|
|
|
|
// is right facing for trig to work
|
2016-11-16 13:50:49 +00:00
|
|
|
Radians rotation =
|
|
|
|
DEGREES_TO_RADIANS((entity->rotation + 90.0f));
|
|
|
|
|
|
|
|
v2 direction = V2(math_cosf(rotation), math_sinf(rotation));
|
|
|
|
ddP = direction;
|
2016-11-09 11:36:41 +00:00
|
|
|
}
|
|
|
|
|
2016-11-17 07:15:36 +00:00
|
|
|
Degrees rotationsPerSecond = 180.0f;
|
2016-11-11 10:43:03 +00:00
|
|
|
if (getKeyStatus(&state->input.keys[keycode_left],
|
|
|
|
readkeytype_repeat, 0.0f, dt))
|
|
|
|
{
|
2016-11-17 07:15:36 +00:00
|
|
|
entity->rotation += (rotationsPerSecond) * dt;
|
2016-11-11 10:43:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (getKeyStatus(&state->input.keys[keycode_right],
|
|
|
|
readkeytype_repeat, 0.0f, dt))
|
|
|
|
{
|
2016-11-17 07:15:36 +00:00
|
|
|
entity->rotation -= (rotationsPerSecond) * dt;
|
2016-11-11 10:43:03 +00:00
|
|
|
}
|
|
|
|
|
2016-11-24 07:57:44 +00:00
|
|
|
if (getKeyStatus(&state->input.keys[keycode_space],
|
2016-11-24 10:05:14 +00:00
|
|
|
readkeytype_delayedRepeat, 0.05f, dt))
|
2016-11-24 07:57:44 +00:00
|
|
|
{
|
|
|
|
addBullet(world, entity);
|
2016-11-24 11:54:07 +00:00
|
|
|
|
|
|
|
AudioRenderer *audioRenderer = getFreeAudioRenderer(world);
|
|
|
|
if (audioRenderer)
|
|
|
|
{
|
|
|
|
AudioVorbis *fire =
|
|
|
|
asset_getVorbis(&state->assetManager, "fire");
|
|
|
|
// TODO(doyle): Atm transient arena is not used, this is
|
|
|
|
// just to fill out the arguments
|
|
|
|
audio_playVorbis(&state->transientArena,
|
|
|
|
&state->audioManager, audioRenderer, fire,
|
|
|
|
1);
|
|
|
|
}
|
2016-11-24 07:57:44 +00:00
|
|
|
}
|
|
|
|
|
2016-11-18 02:43:50 +00:00
|
|
|
ddPSpeedInMs = 25;
|
2016-11-16 13:50:49 +00:00
|
|
|
DEBUG_PUSH_VAR("Pos: %5.2f, %5.2f", entity->pos, "v2");
|
|
|
|
DEBUG_PUSH_VAR("Velocity: %5.2f, %5.2f", entity->dP, "v2");
|
|
|
|
DEBUG_PUSH_VAR("Rotation: %5.2f", entity->rotation, "f32");
|
2016-11-24 07:57:44 +00:00
|
|
|
|
|
|
|
renderer_rect(&state->renderer, world->camera, entity->pos,
|
|
|
|
V2(5, 5), V2(0, 0),
|
|
|
|
DEGREES_TO_RADIANS(entity->rotation), NULL,
|
|
|
|
V4(1.0f, 1.0f, 1.0f, 1.0f), renderflag_no_texture);
|
2016-11-17 07:15:36 +00:00
|
|
|
}
|
2016-11-25 02:17:24 +00:00
|
|
|
else if (entity->type >= entitytype_asteroid_small &&
|
|
|
|
entity->type <= entitytype_asteroid_large)
|
2016-11-17 07:15:36 +00:00
|
|
|
{
|
2016-11-09 11:36:41 +00:00
|
|
|
|
2016-11-17 07:15:36 +00:00
|
|
|
i32 randValue = rand();
|
|
|
|
if (entity->direction == direction_null)
|
|
|
|
{
|
|
|
|
entity->direction = randValue % direction_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
v2 ddP = {0};
|
|
|
|
switch (entity->direction)
|
|
|
|
{
|
|
|
|
|
2016-11-25 02:17:24 +00:00
|
|
|
case direction_north:
|
2016-11-17 07:15:36 +00:00
|
|
|
case direction_northwest:
|
|
|
|
{
|
|
|
|
ddP.x = 1.0f;
|
|
|
|
ddP.y = 1.0f;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case direction_west:
|
|
|
|
case direction_southwest:
|
|
|
|
{
|
|
|
|
ddP.x = -1.0f;
|
|
|
|
ddP.y = -1.0f;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case direction_south:
|
|
|
|
case direction_southeast:
|
|
|
|
{
|
|
|
|
ddP.x = 1.0f;
|
|
|
|
ddP.y = -1.0f;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case direction_east:
|
|
|
|
case direction_northeast:
|
|
|
|
{
|
|
|
|
ddP.x = 1.0f;
|
|
|
|
ddP.y = 1.0f;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
ASSERT(INVALID_CODE_PATH);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-11-25 02:17:24 +00:00
|
|
|
f32 dirOffsetX = ((randValue % 10) + 1) / 100.0f;
|
|
|
|
f32 dirOffsetY = ((randValue % 10) + 1) / 100.0f;
|
|
|
|
v2_hadamard(ddP, V2(dirOffsetX, dirOffsetY));
|
2016-11-17 07:15:36 +00:00
|
|
|
|
2016-11-24 07:57:44 +00:00
|
|
|
// NOTE(doyle): Make asteroids start and move at constant speed by
|
|
|
|
// ensuring that dP is "refreshed" with non-decaying acceleration
|
2016-11-23 12:47:23 +00:00
|
|
|
ddPSpeedInMs = 1;
|
2016-11-24 07:57:44 +00:00
|
|
|
entity->dP = v2_scale(ddP, world->pixelsPerMeter * ddPSpeedInMs);
|
2016-11-23 12:47:23 +00:00
|
|
|
entity->rotation += (60 * dt);
|
2016-11-16 13:50:49 +00:00
|
|
|
}
|
2016-11-24 07:57:44 +00:00
|
|
|
else if (entity->type == entitytype_bullet)
|
|
|
|
{
|
2016-11-24 10:05:14 +00:00
|
|
|
if (!math_pointInRect(world->camera, entity->pos))
|
|
|
|
{
|
|
|
|
world->entityList[i--] =
|
|
|
|
world->entityList[--world->entityIndex];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-11-24 11:54:07 +00:00
|
|
|
ddPSpeedInMs = 10;
|
2016-11-24 07:57:44 +00:00
|
|
|
Radians rotation = DEGREES_TO_RADIANS((entity->rotation + 90.0f));
|
|
|
|
ddP = V2(math_cosf(rotation), math_sinf(rotation));
|
|
|
|
entity->dP = v2_scale(ddP, world->pixelsPerMeter * ddPSpeedInMs);
|
2016-11-24 10:05:14 +00:00
|
|
|
|
2016-11-24 07:57:44 +00:00
|
|
|
}
|
2016-11-09 11:36:41 +00:00
|
|
|
|
2016-11-25 02:17:24 +00:00
|
|
|
/* Loop entity around world */
|
2016-11-24 10:05:14 +00:00
|
|
|
if (entity->pos.y >= world->worldSize.h)
|
|
|
|
entity->pos.y = 0;
|
|
|
|
else if (entity->pos.y < 0)
|
|
|
|
entity->pos.y = world->worldSize.h;
|
|
|
|
|
|
|
|
if (entity->pos.x >= world->worldSize.w)
|
|
|
|
entity->pos.x = 0;
|
|
|
|
else if (entity->pos.x < 0)
|
|
|
|
entity->pos.x = world->worldSize.w;
|
|
|
|
|
|
|
|
i32 collisionIndex = moveEntity(world, &state->transientArena, entity,
|
|
|
|
i, ddP, dt, ddPSpeedInMs);
|
|
|
|
|
2016-11-24 07:57:44 +00:00
|
|
|
v4 collideColor = {0};
|
2016-11-24 10:05:14 +00:00
|
|
|
if (collisionIndex != -1)
|
2016-11-23 12:47:23 +00:00
|
|
|
{
|
2016-11-24 10:05:14 +00:00
|
|
|
ASSERT(collisionIndex < world->entityIndex);
|
2016-11-24 07:57:44 +00:00
|
|
|
|
2016-11-24 10:05:14 +00:00
|
|
|
Entity *collideEntity = &world->entityList[collisionIndex];
|
|
|
|
|
|
|
|
Entity *colliderA;
|
|
|
|
Entity *colliderB;
|
|
|
|
|
|
|
|
if (collideEntity->type < entity->type)
|
2016-11-24 07:57:44 +00:00
|
|
|
{
|
2016-11-24 10:05:14 +00:00
|
|
|
colliderA = collideEntity;
|
|
|
|
colliderB = entity;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
colliderA = entity;
|
|
|
|
colliderB = collideEntity;
|
|
|
|
}
|
|
|
|
|
2016-11-25 02:17:24 +00:00
|
|
|
if (colliderA->type >= entitytype_asteroid_small &&
|
|
|
|
colliderA->type <= entitytype_asteroid_large)
|
2016-11-24 10:05:14 +00:00
|
|
|
{
|
2016-11-25 02:17:24 +00:00
|
|
|
if (colliderA->type == entitytype_asteroid_medium)
|
|
|
|
{
|
|
|
|
AsteroidSpec spec = {0};
|
|
|
|
spec.pos = colliderA->pos;
|
|
|
|
spec.dP = v2_scale(colliderA->dP, -1.0f);
|
|
|
|
spec.direction = invertDirection(colliderA->direction);
|
|
|
|
addAsteroidWithSpec(world, asteroidsize_small, &spec);
|
|
|
|
}
|
|
|
|
else if (colliderA->type == entitytype_asteroid_large)
|
|
|
|
{
|
|
|
|
AsteroidSpec spec = {0};
|
|
|
|
spec.pos = colliderA->pos;
|
|
|
|
spec.dP = v2_scale(colliderA->dP, -1.0f);
|
|
|
|
spec.direction = invertDirection(colliderA->direction);
|
|
|
|
addAsteroidWithSpec(world, asteroidsize_medium, &spec);
|
|
|
|
|
|
|
|
spec.dP = v2_perpendicular(spec.dP);
|
|
|
|
spec.direction = rotateDirectionWest90(spec.direction);
|
|
|
|
addAsteroidWithSpec(world, asteroidsize_small, &spec);
|
|
|
|
|
|
|
|
spec.dP = v2_perpendicular(colliderA->dP);
|
|
|
|
spec.direction = invertDirection(spec.direction);
|
|
|
|
addAsteroidWithSpec(world, asteroidsize_small, &spec);
|
|
|
|
}
|
|
|
|
|
2016-11-24 10:05:14 +00:00
|
|
|
ASSERT(colliderB->type == entitytype_bullet);
|
|
|
|
world->entityList[collisionIndex] =
|
|
|
|
world->entityList[--world->entityIndex];
|
|
|
|
world->entityList[i--] =
|
|
|
|
world->entityList[--world->entityIndex];
|
|
|
|
world->asteroidCounter--;
|
|
|
|
ASSERT(world->asteroidCounter >= 0);
|
2016-11-24 11:54:07 +00:00
|
|
|
|
|
|
|
AudioRenderer *audioRenderer = getFreeAudioRenderer(world);
|
|
|
|
if (audioRenderer)
|
|
|
|
{
|
|
|
|
char *sound;
|
|
|
|
i32 choice = rand() % 3;
|
|
|
|
if (choice == 0)
|
|
|
|
{
|
|
|
|
sound = "bang_small";
|
|
|
|
}
|
|
|
|
else if (choice == 1)
|
|
|
|
{
|
|
|
|
sound = "bang_medium";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sound = "bang_large";
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioVorbis *explode =
|
|
|
|
asset_getVorbis(&state->assetManager, sound);
|
|
|
|
audio_playVorbis(&state->transientArena,
|
|
|
|
&state->audioManager, audioRenderer,
|
|
|
|
explode, 1);
|
|
|
|
}
|
|
|
|
|
2016-11-24 07:57:44 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-11-23 12:47:23 +00:00
|
|
|
}
|
2016-11-18 02:43:50 +00:00
|
|
|
|
2016-11-17 07:15:36 +00:00
|
|
|
RenderFlags flags = renderflag_wireframe | renderflag_no_texture;
|
2016-11-24 07:57:44 +00:00
|
|
|
renderer_entity(&state->renderer, &state->transientArena, world->camera,
|
2016-11-19 05:42:13 +00:00
|
|
|
entity, V2(0, 0), 0,
|
2016-11-24 07:57:44 +00:00
|
|
|
collideColor, flags);
|
2016-11-17 07:15:36 +00:00
|
|
|
}
|
2016-11-16 13:50:49 +00:00
|
|
|
|
2016-11-24 11:54:07 +00:00
|
|
|
for (i32 i = 0; i < world->numAudioRenderers; i++)
|
|
|
|
{
|
|
|
|
AudioRenderer *audioRenderer = &world->audioRenderer[i];
|
|
|
|
audio_updateAndPlay(&state->transientArena, &state->audioManager,
|
|
|
|
audioRenderer);
|
|
|
|
}
|
|
|
|
|
2016-11-23 13:27:24 +00:00
|
|
|
#if 1
|
2016-11-16 02:00:59 +00:00
|
|
|
debug_drawUi(state, dt);
|
|
|
|
debug_clearCounter();
|
2016-11-23 12:47:23 +00:00
|
|
|
#endif
|
2016-11-16 02:00:59 +00:00
|
|
|
|
2016-11-09 11:36:41 +00:00
|
|
|
renderer_renderGroups(&state->renderer);
|
|
|
|
}
|