From 0d5725eb977f6f6b482850ba66b66af1a722890c Mon Sep 17 00:00:00 2001 From: Doyle Thai Date: Sat, 16 Jul 2016 20:06:52 +1000 Subject: [PATCH] Entities now have health values associated --- src/WorldTraveller.c | 37 ++++++++++++++++++++++++++++++------ src/include/Dengine/Entity.h | 8 ++++++++ 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/WorldTraveller.c b/src/WorldTraveller.c index ef6a158..c088d4a 100644 --- a/src/WorldTraveller.c +++ b/src/WorldTraveller.c @@ -25,6 +25,21 @@ INTERNAL Entity *addEntity(World *world, v2 pos, v2 size, enum EntityType type, entity.tex = tex; entity.collides = collides; + switch(type) + { + case entitytype_hero: + case entitytype_mob: + { + entity.stats = PLATFORM_MEM_ALLOC(1, EntityStats); + entity.stats->maxHealth = 100; + entity.stats->health = entity.stats->maxHealth; + break; + } + + default: + break; + } + world->entities[world->freeEntityIndex++] = entity; Entity *result = &world->entities[world->freeEntityIndex-1]; @@ -497,22 +512,22 @@ void worldTraveller_gameUpdateAndRender(GameState *state, const f32 dt) char *debugString = NULL; switch (entity->type) { - case entitytype_mob: + case entitytype_mob: color = V4(1, 0, 0, 1); debugString = "MOB"; break; - case entitytype_hero: + case entitytype_hero: color = V4(0, 0, 1.0f, 1); debugString = "HERO"; break; - case entitytype_npc: + case entitytype_npc: color = V4(0, 1.0f, 0, 1); debugString = "NPC"; break; - default: + default: break; } @@ -528,18 +543,28 @@ void worldTraveller_gameUpdateAndRender(GameState *state, const f32 dt) f32 stringLineGap = 1.1f * asset_getVFontSpacing(font->metrics); strPos.y -= GLOBAL_debugState.stringLineGap; - char entityPosStr[256]; + char entityPosStr[128]; snprintf(entityPosStr, ARRAY_COUNT(entityPosStr), "%06.2f, %06.2f", entity->pos.x, entity->pos.y); renderer_string(&state->renderer, cameraBounds, font, entityPosStr, strPos, 0, color); strPos.y -= GLOBAL_debugState.stringLineGap; - char entityIDStr[256]; + char entityIDStr[32]; snprintf(entityIDStr, ARRAY_COUNT(entityIDStr), "ID: %4d/%d", i, world->maxEntities); renderer_string(&state->renderer, cameraBounds, font, entityIDStr, strPos, 0, color); + + if (entity->stats) + { + strPos.y -= GLOBAL_debugState.stringLineGap; + char entityHealth[32]; + snprintf(entityHealth, ARRAY_COUNT(entityHealth), "HP: %3.0f/%3.0f", + entity->stats->health, entity->stats->maxHealth); + renderer_string(&state->renderer, cameraBounds, font, + entityHealth, strPos, 0, color); + } } #endif } diff --git a/src/include/Dengine/Entity.h b/src/include/Dengine/Entity.h index 9ec172f..b078914 100644 --- a/src/include/Dengine/Entity.h +++ b/src/include/Dengine/Entity.h @@ -34,6 +34,12 @@ typedef struct EntityAnim f32 currDuration; } EntityAnim; +typedef struct EntityStats +{ + f32 maxHealth; + f32 health; +} EntityStats; + typedef struct Entity { v2 pos; // Position @@ -50,6 +56,8 @@ typedef struct Entity EntityAnim anim[16]; i32 freeAnimIndex; i32 currAnimIndex; + + EntityStats *stats; } Entity; INTERNAL inline v4 getEntityScreenRect(Entity entity)