Add notion of entity type identifier to entities

This commit is contained in:
Doyle Thai 2016-07-08 17:54:56 +10:00
parent fa7947fb90
commit ad48dd5062
2 changed files with 28 additions and 9 deletions

View File

@ -6,18 +6,20 @@
//choose to load assets outside of WorldTraveller! //choose to load assets outside of WorldTraveller!
#include <stdlib.h> #include <stdlib.h>
INTERNAL Entity *addEntity(World *world, v2 pos, v2 size, INTERNAL Entity *addEntity(World *world, v2 pos, v2 size, enum EntityType type,
enum Direction direction, Texture *tex, b32 collides) enum Direction direction, Texture *tex, b32 collides)
{ {
#ifdef WT_DEBUG #ifdef WT_DEBUG
ASSERT(tex && world); ASSERT(tex && world);
ASSERT(world->freeEntityIndex < world->maxEntities); ASSERT(world->freeEntityIndex < world->maxEntities);
ASSERT(type < entitytype_count);
#endif #endif
Entity entity = {0}; Entity entity = {0};
entity.pos = pos; entity.pos = pos;
entity.size = size; entity.size = size;
entity.type = type;
entity.direction = direction; entity.direction = direction;
entity.tex = tex; entity.tex = tex;
entity.collides = collides; entity.collides = collides;
@ -101,10 +103,12 @@ void worldTraveller_gameInit(GameState *state, v2i windowSize)
CAST(f32) y * state->tileSize); CAST(f32) y * state->tileSize);
v2 size = v2 size =
V2(CAST(f32) state->tileSize, CAST(f32) state->tileSize); V2(CAST(f32) state->tileSize, CAST(f32) state->tileSize);
enum EntityType type = entitytype_tile;
enum Direction dir = direction_null; enum Direction dir = direction_null;
Texture *tex = asset_getTexture(assetManager, world->texType); Texture *tex = asset_getTexture(assetManager, world->texType);
b32 collides = FALSE; b32 collides = FALSE;
Entity *tile = addEntity(world, pos, size, dir, tex, collides); Entity *tile =
addEntity(world, pos, size, type, dir, tex, collides);
f32 duration = 1.0f; f32 duration = 1.0f;
i32 numRects = 1; i32 numRects = 1;
@ -120,12 +124,13 @@ void worldTraveller_gameInit(GameState *state, v2i windowSize)
/* Init hero entity */ /* Init hero entity */
world->heroIndex = world->freeEntityIndex; world->heroIndex = world->freeEntityIndex;
v2 pos = V2(0.0f, 0.0f); v2 pos = V2(0.0f, 0.0f);
v2 size = V2(58.0f, 98.0f); v2 size = V2(58.0f, 98.0f);
enum Direction dir = direction_east; enum EntityType type = entitytype_hero;
Texture *tex = asset_getTexture(assetManager, texlist_hero); enum Direction dir = direction_east;
b32 collides = TRUE; Texture *tex = asset_getTexture(assetManager, texlist_hero);
Entity *hero = addEntity(world, pos, size, dir, tex, collides); b32 collides = TRUE;
Entity *hero = addEntity(world, pos, size, type, dir, tex, collides);
/* Add idle animation */ /* Add idle animation */
f32 duration = 1.0f; f32 duration = 1.0f;
@ -156,10 +161,11 @@ void worldTraveller_gameInit(GameState *state, v2i windowSize)
/* Create a NPC */ /* Create a NPC */
pos = V2(300.0f, 300.0f); pos = V2(300.0f, 300.0f);
size = hero->size; size = hero->size;
type = entitytype_hero;
dir = direction_null; dir = direction_null;
tex = hero->tex; tex = hero->tex;
collides = TRUE; collides = TRUE;
Entity *npc = addEntity(world, pos, size, dir, tex, collides); Entity *npc = addEntity(world, pos, size, type, dir, tex, collides);
/* Add npc waving animation */ /* Add npc waving animation */
duration = 0.30f; duration = 0.30f;

View File

@ -14,6 +14,16 @@ enum Direction
direction_null, direction_null,
}; };
enum EntityType
{
entitytype_null,
entitytype_hero,
entitytype_camera,
entitytype_npc,
entitytype_tile,
entitytype_count,
};
typedef struct EntityAnim typedef struct EntityAnim
{ {
v4 *rect; v4 *rect;
@ -29,7 +39,10 @@ typedef struct Entity
v2 pos; // Position v2 pos; // Position
v2 dPos; // Velocity v2 dPos; // Velocity
v2 size; v2 size;
enum EntityType type;
enum Direction direction; enum Direction direction;
Texture *tex; Texture *tex;
b32 collides; b32 collides;