diff --git a/Faceball2030/main.cpp b/Faceball2030/main.cpp index 98aa7b3..cc7d992 100644 --- a/Faceball2030/main.cpp +++ b/Faceball2030/main.cpp @@ -31,6 +31,26 @@ bool Enemy::isDead() { return health <= 0; } +void Enemy::increaseDeathTimer(float fElapsedTime) { + deathTimer += fElapsedTime; +} + +bool Enemy::deathAnimationOver() { + return deathTimer > 3.0f; +} + +void Enemy::increaseColorFactor(float fElapsedTime) { + colorFactor += 50 * fElapsedTime; +} + +float Enemy::getColorFactor() { + return colorFactor; +} + +void Enemy::decreaseColorFactor() { + colorFactor--; +} + void FaceBall::InitializeEnemyData() { enemyData[EnemyID::NONE] = { "VOID",undefined,BLACK }; enemyData[EXIT] = { "EXIT",undefined,GREEN }; @@ -915,11 +935,10 @@ bool Bullet::Update(float fElapsedTime) { int collided_enemy = FaceBall::CheckEnemyCollision(adjustedSpd, { pos.x,pos.z }, 0.1); if (collided_enemy!=-1) { Enemy& enemy = game->enemies[collided_enemy]; - enemy.Hurt(); - if (enemy.isDead()) { - game->enemies.erase(game->enemies.begin() + collided_enemy); + if (!enemy.isDead()) { + enemy.Hurt(); + return false; } - return false; } } if (!FaceBall::CheckCollision(adjustedSpd, {pos.x,pos.z}, 0.05)) { @@ -933,10 +952,55 @@ bool Bullet::Update(float fElapsedTime) { } void FaceBall::RunEnemyAI(Enemy& e,float fElapsedTime) { - switch (e.GetID()) { - case SHOOTME: { - e.rot += 0.5 * fElapsedTime; - }break; + if (e.isDead()) { + if (!e.deathAnimationOver()) { + std::vector& tris = e.mesh.tris; + uint8_t darkenAmt = 0; + while (e.getColorFactor() >= 1) { + darkenAmt++; + e.decreaseColorFactor(); + } + for (Triangle& t : tris) { + if (t.p[0].y > 0.1) { + t.p[0].y = std::max(0.1f, t.p[0].y - 0.3f * fElapsedTime); + } + else { + float dir = std::atan2f(t.p[0].z, t.p[0].x); + t.p[0].x += std::cosf(dir)*0.04f*fElapsedTime; + t.p[0].z += std::sinf(dir) * 0.04f * fElapsedTime; + } + if (t.p[1].y > 0.1) { + t.p[1].y = std::max(0.1f, t.p[1].y - 0.3f * fElapsedTime); + } + else { + float dir = std::atan2f(t.p[1].z, t.p[1].x); + t.p[1].x += std::cosf(dir) * 0.06f * fElapsedTime; + t.p[1].z += std::sinf(dir) * 0.06f * fElapsedTime; + } + if (t.p[2].y > 0.1) { + t.p[2].y = std::max(0.1f, t.p[2].y - 0.3f * fElapsedTime); + } + else { + float dir = std::atan2f(t.p[2].z, t.p[2].x); + t.p[2].x += std::cosf(dir) * 0.05f * fElapsedTime; + t.p[2].z += std::sinf(dir) * 0.05f * fElapsedTime; + } + if (darkenAmt>0){ + t.col[0] -= {darkenAmt, darkenAmt, darkenAmt}; + t.col[1] -= {darkenAmt, darkenAmt, darkenAmt}; + t.col[2] -= {darkenAmt, darkenAmt, darkenAmt}; + } + } + e.increaseDeathTimer(fElapsedTime); + e.increaseColorFactor(fElapsedTime); + } + } + else { + switch (e.GetID()) { + case SHOOTME: { + e.rot += 0.5 * fElapsedTime; + }break; + } } } diff --git a/Faceball2030/main.h b/Faceball2030/main.h index 3e919e1..abb6bcd 100644 --- a/Faceball2030/main.h +++ b/Faceball2030/main.h @@ -175,11 +175,18 @@ struct Enemy : Object { int health=1; std::vector shots; float fireDelay=0; + float deathTimer=0; + float colorFactor = 0; public: Enemy(EnemyID id, vec3d pos, float rot, float radius); EnemyID GetID(); void Hurt(); bool isDead(); + void increaseDeathTimer(float fElapsedTime); + bool deathAnimationOver(); + void increaseColorFactor(float fElapsedTime); + float getColorFactor(); + void decreaseColorFactor(); }; class FaceBall : public PixelGameEngine