Add child entity to projectiles

This commit is contained in:
Doyle Thai 2016-09-13 13:35:16 +10:00
parent 1f799302c1
commit fc6f6e086f
4 changed files with 83 additions and 16 deletions

View File

@ -86,10 +86,10 @@ void entity_addAnim(AssetManager *const assetManager, Entity *const entity,
} }
Entity *const entity_add(MemoryArena *const arena, World *const world, Entity *const entity_add(MemoryArena *const arena, World *const world,
const v2 pos, const v2 size, const v2 pos, const v2 size, const f32 scale,
const enum EntityType type, const enum EntityType type,
const enum Direction direction, const enum Direction direction, Texture *const tex,
Texture *const tex, const b32 collides) const b32 collides)
{ {
#ifdef DENGINE_DEBUG #ifdef DENGINE_DEBUG
@ -103,6 +103,7 @@ Entity *const entity_add(MemoryArena *const arena, World *const world,
entity.pos = pos; entity.pos = pos;
entity.hitboxSize = size; entity.hitboxSize = size;
entity.renderSize = size; entity.renderSize = size;
entity.scale = scale;
entity.type = type; entity.type = type;
entity.direction = direction; entity.direction = direction;
entity.tex = tex; entity.tex = tex;
@ -194,3 +195,11 @@ i32 entity_getIndex(World *const world, const i32 entityId)
return -1; return -1;
} }
Entity *entity_get(World *const world, const i32 entityId)
{
Entity *result = NULL;
i32 worldIndex = entity_getIndex(world, entityId);
if (worldIndex != -1) result = &world->entities[worldIndex];
return result;
}

View File

@ -271,7 +271,8 @@ void renderer_entity(Renderer *renderer, Rect camera, Entity *entity,
v2 posInCameraSpace = v2_sub(entity->pos, camera.pos); v2 posInCameraSpace = v2_sub(entity->pos, camera.pos);
// TODO(doyle): Scale temporarily // TODO(doyle): Scale temporarily
renderObject(renderer, posInCameraSpace, v2_scale(entity->renderSize, 2), renderObject(renderer, posInCameraSpace,
pivotPoint, entity->rotation + rotate, color, entity->tex); v2_scale(entity->renderSize, entity->scale), pivotPoint,
entity->rotation + rotate, color, entity->tex);
} }
} }

View File

@ -52,11 +52,13 @@ INTERNAL void addGenericMob(MemoryArena *arena, AssetManager *assetManager,
Entity *hero = &world->entities[entity_getIndex(world, world->heroId)]; Entity *hero = &world->entities[entity_getIndex(world, world->heroId)];
v2 size = V2(58.0f, 98.0f); v2 size = V2(58.0f, 98.0f);
f32 scale = 2;
enum EntityType type = entitytype_mob; enum EntityType type = entitytype_mob;
enum Direction dir = direction_west; enum Direction dir = direction_west;
Texture *tex = asset_getTex(assetManager, "ClaudeSprite.png"); Texture *tex = asset_getTex(assetManager, "ClaudeSprite.png");
b32 collides = TRUE; b32 collides = TRUE;
Entity *mob = entity_add(arena, world, pos, size, type, dir, tex, collides); Entity *mob =
entity_add(arena, world, pos, size, scale, type, dir, tex, collides);
mob->numAudioRenderers = 4; mob->numAudioRenderers = 4;
mob->audioRenderer = mob->audioRenderer =
@ -477,12 +479,13 @@ INTERNAL void entityInit(GameState *state, v2 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);
f32 scale = 1.0f;
enum EntityType type = entitytype_tile; enum EntityType type = entitytype_tile;
enum Direction dir = direction_null; enum Direction dir = direction_null;
Texture *tex = asset_getTex(assetManager, "terrain.png"); Texture *tex = asset_getTex(assetManager, "terrain.png");
b32 collides = FALSE; b32 collides = FALSE;
Entity *tile = entity_add(arena, world, pos, size, type, dir, Entity *tile = entity_add(arena, world, pos, size, scale, type,
tex, collides); dir, tex, collides);
entity_addAnim(assetManager, tile, "terrainGrass"); entity_addAnim(assetManager, tile, "terrainGrass");
entity_setActiveAnim(tile, "terrainGrass"); entity_setActiveAnim(tile, "terrainGrass");
@ -498,12 +501,13 @@ INTERNAL void entityInit(GameState *state, v2 windowSize)
Renderer *renderer = &state->renderer; Renderer *renderer = &state->renderer;
v2 size = V2(10.0f, 10.0f); v2 size = V2(10.0f, 10.0f);
v2 pos = V2(0, 0); v2 pos = V2(0, 0);
f32 scale = 0.0f;
enum EntityType type = entitytype_soundscape; enum EntityType type = entitytype_soundscape;
enum Direction dir = direction_null; enum Direction dir = direction_null;
Texture *tex = NULL; Texture *tex = NULL;
b32 collides = FALSE; b32 collides = FALSE;
Entity *soundscape = Entity *soundscape =
entity_add(arena, world, pos, size, type, dir, tex, collides); entity_add(arena, world, pos, size, scale, type, dir, tex, collides);
world->soundscape = soundscape; world->soundscape = soundscape;
soundscape->numAudioRenderers = 1; soundscape->numAudioRenderers = 1;
@ -515,12 +519,13 @@ INTERNAL void entityInit(GameState *state, v2 windowSize)
/* Init hero entity */ /* Init hero entity */
size = V2(58.0f, 98.0f); size = V2(58.0f, 98.0f);
pos = V2(size.x, CAST(f32) state->tileSize); pos = V2(size.x, CAST(f32) state->tileSize);
scale = 2.0f;
type = entitytype_hero; type = entitytype_hero;
dir = direction_east; dir = direction_east;
tex = asset_getTex(assetManager, "ClaudeSprite.png"); tex = asset_getTex(assetManager, "ClaudeSprite.png");
collides = TRUE; collides = TRUE;
Entity *hero = Entity *hero =
entity_add(arena, world, pos, size, type, dir, tex, collides); entity_add(arena, world, pos, size, scale, type, dir, tex, collides);
#if 0 #if 0
Entity *heroWeapon = Entity *heroWeapon =
@ -559,7 +564,8 @@ INTERNAL void entityInit(GameState *state, v2 windowSize)
dir = direction_null; dir = direction_null;
tex = hero->tex; tex = hero->tex;
collides = FALSE; collides = FALSE;
Entity *npc = entity_add(arena, world, pos, size, type, dir, tex, collides); Entity *npc =
entity_add(arena, world, pos, size, scale, type, dir, tex, collides);
/* Populate npc animation references */ /* Populate npc animation references */
entity_addAnim(assetManager, npc, "claudeVictory"); entity_addAnim(assetManager, npc, "claudeVictory");
@ -1090,9 +1096,11 @@ INTERNAL void beginAttack(AssetManager *assetManager, MemoryArena *arena,
case entityattack_airSlash: case entityattack_airSlash:
{ {
entity_setActiveAnim(attacker, "claudeAirSlash"); entity_setActiveAnim(attacker, "claudeAirSlash");
f32 scale = 1.5f;
v2 size = V2(20, 20);
Entity *projectile = entity_add( Entity *projectile = entity_add(
arena, world, attacker->pos, V2(20, 20), entitytype_projectile, arena, world, attacker->pos, size, scale,
attacker->direction, attacker->tex, TRUE); entitytype_projectile, attacker->direction, attacker->tex, TRUE);
projectile->collidesWith[entitytype_hero] = FALSE; projectile->collidesWith[entitytype_hero] = FALSE;
projectile->collidesWith[entitytype_mob] = TRUE; projectile->collidesWith[entitytype_mob] = TRUE;
@ -1100,6 +1108,29 @@ INTERNAL void beginAttack(AssetManager *assetManager, MemoryArena *arena,
projectile->stats->entityIdToAttack = attacker->stats->entityIdToAttack; projectile->stats->entityIdToAttack = attacker->stats->entityIdToAttack;
entity_addAnim(assetManager, projectile, "claudeAirSlashVfx"); entity_addAnim(assetManager, projectile, "claudeAirSlashVfx");
entity_setActiveAnim(projectile, "claudeAirSlashVfx"); entity_setActiveAnim(projectile, "claudeAirSlashVfx");
v2 initialOffset = V2(size.x * 0.5f, 0);
f32 deltaScale = 0.3f;
projectile->numChilds = 3;
for (i32 i = 0; i < projectile->numChilds; i++)
{
v2 childOffset = v2_scale(initialOffset, CAST(f32) i + 1);
scale -= deltaScale;
Entity *child =
entity_add(arena, world, v2_sub(projectile->pos, childOffset),
V2(20, 20), scale, entitytype_projectile,
projectile->direction, projectile->tex, FALSE);
child->collidesWith[entitytype_hero] = FALSE;
child->collidesWith[entitytype_mob] = TRUE;
child->stats->entityIdToAttack =
projectile->stats->entityIdToAttack;
entity_addAnim(assetManager, child, "claudeAirSlashVfx");
entity_setActiveAnim(child, "claudeAirSlashVfx");
projectile->childIds[i] = child->id;
}
break; break;
} }
@ -1774,8 +1805,10 @@ void worldTraveller_gameUpdateAndRender(GameState *state, f32 dt)
ddPos = v2_scale(ddPos, 0.70710678118f); ddPos = v2_scale(ddPos, 0.70710678118f);
} }
if (moveEntityAndReturnCollision(world, projectile, ddPos, b32 collided = moveEntityAndReturnCollision(
projectileSpeed, dt)) world, projectile, ddPos, projectileSpeed, dt);
if (collided)
{ {
// TODO(doyle): Unify concept of dead entity for mobs and // TODO(doyle): Unify concept of dead entity for mobs and
// projectiles // projectiles
@ -1979,6 +2012,25 @@ void worldTraveller_gameUpdateAndRender(GameState *state, f32 dt)
} }
entity_clearData(&state->arena, world, entity); entity_clearData(&state->arena, world, entity);
numDeadEntities++; numDeadEntities++;
for (i32 i = 0; i < entity->numChilds; i++)
{
Entity *child = entity_get(world, entity->childIds[i]);
if (child)
{
for (i32 i = 0; i < child->numAudioRenderers; i++)
{
audio_stopVorbis(&state->arena, audioManager,
&child->audioRenderer[i]);
}
entity_clearData(&state->arena, world, child);
numDeadEntities++;
}
else
{
DEBUG_LOG("Entity child expected but not found");
}
}
break; break;
} }
default: default:

View File

@ -82,10 +82,14 @@ struct Entity
{ {
i32 id; i32 id;
i32 childIds[8];
i32 numChilds;
v2 pos; // Position v2 pos; // Position
v2 dPos; // Velocity v2 dPos; // Velocity
v2 hitboxSize; v2 hitboxSize;
v2 renderSize; v2 renderSize;
f32 scale;
f32 rotation; f32 rotation;
b32 invisible; b32 invisible;
@ -119,11 +123,12 @@ void entity_updateAnim(Entity *const entity, const f32 dt);
void entity_addAnim(AssetManager *const assetManager, Entity *const entity, void entity_addAnim(AssetManager *const assetManager, Entity *const entity,
const char *const animName); const char *const animName);
Entity *const entity_add(MemoryArena *const arena, World *const world, Entity *const entity_add(MemoryArena *const arena, World *const world,
const v2 pos, const v2 size, const v2 pos, const v2 size, const f32 scale,
const enum EntityType type, const enum EntityType type,
const enum Direction direction, Texture *const tex, const enum Direction direction, Texture *const tex,
const b32 collides); const b32 collides);
void entity_clearData(MemoryArena *const arena, World *const world, void entity_clearData(MemoryArena *const arena, World *const world,
Entity *const entity); Entity *const entity);
Entity *entity_get(World *const world, const i32 entityId);
i32 entity_getIndex(World *const world, const i32 entityId); i32 entity_getIndex(World *const world, const i32 entityId);
#endif #endif