From 78b7aef32f79e39631c9c906ec19ddfc683797b0 Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Thu, 20 Apr 2023 16:57:26 -0500 Subject: [PATCH] open and closing of powerup capsule. --- Faceball2030/main.cpp | 99 ++++++++++++++++++++++++++++--------------- Faceball2030/main.h | 3 ++ 2 files changed, 67 insertions(+), 35 deletions(-) diff --git a/Faceball2030/main.cpp b/Faceball2030/main.cpp index 3dc4f76..80c66f6 100644 --- a/Faceball2030/main.cpp +++ b/Faceball2030/main.cpp @@ -158,6 +158,7 @@ bool Powerup::Update(float fElapsedTime) { aliveTime += fElapsedTime; colorCycle = std::sinf(10 * aliveTime); col = game->powerupColorData[type] * colorCycle; + opened = false; //Open state is false until a player causes it to open. return true; } @@ -188,6 +189,7 @@ void FaceBall::LoadLevel(int level) objects.clear(); bullets.clear(); enemies.clear(); + powerups.clear(); exitCoords = { 0,0 }; for (int y = 0; y < MAP_SIZE.y; y++) { std::vectorrow; @@ -835,7 +837,7 @@ void FaceBall::RenderPowerupMesh(mat4x4& matView, std::vector& vecTria rotMat = Matrix_MultiplyMatrix(rotMat, flipMat); } localMat = Matrix_MultiplyMatrix(localMat, rotMat); - mat4x4 matTrans = Matrix_MakeTranslation(p.pos.x, p.pos.y+(i?0.375f:0), p.pos.z); + mat4x4 matTrans = Matrix_MakeTranslation(p.pos.x, p.pos.y+(i?p.opened?0.65f:0.375f:p.opened?-0.125f:0), p.pos.z); localMat = Matrix_MultiplyMatrix(localMat, matTrans); triTransformed.p[0] = Matrix_MultiplyVector(localMat, tri.p[0]); @@ -1292,7 +1294,8 @@ void FaceBall::HandleKeys(float fElapsedTime) { freeRoamCamera = Vector_Add(freeRoamCamera, vLeftStrafe); } else { - if (!CheckCollision({ vLeftStrafe.x,0,0 }, { player.GetPos().x,player.GetPos().z }, player.GetRadius())) { + if (!CheckCollision({ vLeftStrafe.x,0,0 }, { player.GetPos().x,player.GetPos().z }, player.GetRadius())&& + CheckPowerupCollision({ vLeftStrafe.x,0,0 }, { player.GetPos().x,player.GetPos().z }, player.GetRadius())==-1) { vec3d xMovement{ vLeftStrafe.x,0,0 }; int enemyCollisionIndex = CheckExplosiveEnemyCollision(xMovement, { player.GetPos().x,player.GetPos().z }, player.GetRadius()); if (enemyCollisionIndex != -1) { @@ -1304,7 +1307,8 @@ void FaceBall::HandleKeys(float fElapsedTime) { } player.UpdatePos(Vector_Add(player.GetPos(), xMovement)); } - if (!CheckCollision({ 0,0,vLeftStrafe.z }, { player.GetPos().x,player.GetPos().z }, player.GetRadius())) { + if (!CheckCollision({ 0,0,vLeftStrafe.z }, { player.GetPos().x,player.GetPos().z }, player.GetRadius()) && + CheckPowerupCollision({ 0,0,vLeftStrafe.z }, { player.GetPos().x,player.GetPos().z }, player.GetRadius()) == -1) { vec3d zMovement{ 0,0,vLeftStrafe.z }; int enemyCollisionIndex = CheckExplosiveEnemyCollision(zMovement, { player.GetPos().x,player.GetPos().z }, player.GetRadius()); if (enemyCollisionIndex != -1) { @@ -1326,7 +1330,8 @@ void FaceBall::HandleKeys(float fElapsedTime) { freeRoamCamera = Vector_Add(freeRoamCamera, vRightStrafe); } else { - if (!CheckCollision({ vRightStrafe.x,0,0 }, { player.GetPos().x,player.GetPos().z }, player.GetRadius())) { + if (!CheckCollision({ vRightStrafe.x,0,0 }, { player.GetPos().x,player.GetPos().z }, player.GetRadius()) && + CheckPowerupCollision({ vRightStrafe.x,0,0 }, { player.GetPos().x,player.GetPos().z }, player.GetRadius()) == -1) { vec3d xMovement{ vRightStrafe.x,0,0 }; int enemyCollisionIndex = CheckExplosiveEnemyCollision(xMovement, { player.GetPos().x,player.GetPos().z }, player.GetRadius()); if (enemyCollisionIndex != -1) { @@ -1338,7 +1343,8 @@ void FaceBall::HandleKeys(float fElapsedTime) { } player.UpdatePos(Vector_Add(player.GetPos(), xMovement)); } - if (!CheckCollision({ 0,0,vRightStrafe.z }, { player.GetPos().x,player.GetPos().z }, player.GetRadius())) { + if (!CheckCollision({ 0,0,vRightStrafe.z }, { player.GetPos().x,player.GetPos().z }, player.GetRadius()) && + CheckPowerupCollision({ 0,0,vRightStrafe.z }, { player.GetPos().x,player.GetPos().z }, player.GetRadius()) == -1) { vec3d zMovement{ 0,0,vRightStrafe.z }; int enemyCollisionIndex = CheckExplosiveEnemyCollision(zMovement, { player.GetPos().x,player.GetPos().z }, player.GetRadius()); if (enemyCollisionIndex != -1) { @@ -1357,7 +1363,8 @@ void FaceBall::HandleKeys(float fElapsedTime) { freeRoamCamera = Vector_Add(freeRoamCamera, vForward); } else { - if (!CheckCollision({ vForward.x,0,0 }, {player.GetPos().x,player.GetPos().z},player.GetRadius())) { + if (!CheckCollision({ vForward.x,0,0 }, {player.GetPos().x,player.GetPos().z},player.GetRadius()) && + CheckPowerupCollision({ vForward.x,0,0 }, { player.GetPos().x,player.GetPos().z }, player.GetRadius()) == -1) { vec3d xMovement{ vForward.x,0,0 }; int enemyCollisionIndex = CheckExplosiveEnemyCollision(xMovement, { player.GetPos().x,player.GetPos().z }, player.GetRadius()); if (enemyCollisionIndex != -1) { @@ -1369,7 +1376,8 @@ void FaceBall::HandleKeys(float fElapsedTime) { } player.UpdatePos(Vector_Add(player.GetPos(), xMovement)); } - if (!CheckCollision({ 0,0,vForward.z }, { player.GetPos().x,player.GetPos().z }, player.GetRadius())) { + if (!CheckCollision({ 0,0,vForward.z }, { player.GetPos().x,player.GetPos().z }, player.GetRadius()) && + CheckPowerupCollision({ 0,0,vForward.z }, { player.GetPos().x,player.GetPos().z }, player.GetRadius()) == -1) { vec3d zMovement{ 0,0,vForward.z }; int enemyCollisionIndex = CheckExplosiveEnemyCollision(zMovement, { player.GetPos().x,player.GetPos().z }, player.GetRadius()); if (enemyCollisionIndex != -1) { @@ -1393,7 +1401,8 @@ void FaceBall::HandleKeys(float fElapsedTime) { freeRoamCamera = Vector_Add(freeRoamCamera, vReverse); } else { - if (!CheckCollision({ vReverse.x,0,0 }, { player.GetPos().x,player.GetPos().z }, player.GetRadius())) { + if (!CheckCollision({ vReverse.x,0,0 }, { player.GetPos().x,player.GetPos().z }, player.GetRadius()) && + CheckPowerupCollision({ vReverse.x,0,0 }, { player.GetPos().x,player.GetPos().z }, player.GetRadius()) == -1) { vec3d xMovement{ vReverse.x,0,0 }; int enemyCollisionIndex = CheckExplosiveEnemyCollision(xMovement, { player.GetPos().x,player.GetPos().z }, player.GetRadius()); if (enemyCollisionIndex != -1) { @@ -1405,7 +1414,8 @@ void FaceBall::HandleKeys(float fElapsedTime) { } player.UpdatePos(Vector_Add(player.GetPos(), xMovement)); } - if (!CheckCollision({ 0,0,vReverse.z }, { player.GetPos().x,player.GetPos().z }, player.GetRadius())) { + if (!CheckCollision({ 0,0,vReverse.z }, { player.GetPos().x,player.GetPos().z }, player.GetRadius()) && + CheckPowerupCollision({ 0,0,vReverse.z }, { player.GetPos().x,player.GetPos().z }, player.GetRadius()) == -1) { vec3d zMovement{ 0,0,vReverse.z }; int enemyCollisionIndex = CheckExplosiveEnemyCollision(zMovement, { player.GetPos().x,player.GetPos().z }, player.GetRadius()); if (enemyCollisionIndex != -1) { @@ -1516,6 +1526,19 @@ bool FaceBall::OnUserCreate() return true; } +int FaceBall::CheckPowerupCollision(vec3d movementVector, vf2d pos, float radius) { + vf2d newpos = { pos.x + movementVector.x,pos.y + movementVector.z }; + for (int i = 0; i < game->powerups.size(); i++) { + Powerup& p = game->powerups[i]; + float dist = std::sqrtf(std::powf(newpos.x - p.pos.x, 2) + std::powf(newpos.y - p.pos.z, 2)); + if (dist < radius + p.radius) { + lastPowerupCollidedWith = i; //This function is only meant for the player to collide with things. If this is needed for other things, we will have to refactor/reconsider this line as they would modify the player's last powerup touched variable which is a no-no. + return i; + } + } + return -1; +} + //Collisions will not occur if the player is dead. bool FaceBall::CheckPlayerCollision(vec3d movementVector, vf2d pos, float radius) { if (game->hp <= 0) { return false; } @@ -2003,34 +2026,40 @@ void FaceBall::RunEnemyAI(Enemy& e,float fElapsedTime,int myIndex) { bool FaceBall::OnUserUpdate(float fElapsedTime) { gameTimer += fElapsedTime; - for (std::vector::iterator it = bullets.begin(); it != bullets.end();) { - Bullet& b = *it; - if (!b.Update(fElapsedTime)) { - it=bullets.erase(it); - } - else { - it++; - } - } - for (int i = 0; i < enemies.size(); i++) { - Enemy& e = enemies[i]; - e.Update(fElapsedTime); - if (e.isLastHitTimerActive()) { - e.reduceLastHitTimer(fElapsedTime); - } - RunEnemyAI(e, fElapsedTime,i); - } - for (std::vector::iterator it = powerups.begin(); it != powerups.end();) { - Powerup& p = *it; - if (!p.Update(fElapsedTime)) { - it = powerups.erase(it); - } - else { - it++; - } - } switch (mode) { case GAME: { + for (std::vector::iterator it = bullets.begin(); it != bullets.end();) { + Bullet& b = *it; + if (!b.Update(fElapsedTime)) { + it = bullets.erase(it); + } + else { + it++; + } + } + for (int i = 0; i < enemies.size(); i++) { + Enemy& e = enemies[i]; + e.Update(fElapsedTime); + if (e.isLastHitTimerActive()) { + e.reduceLastHitTimer(fElapsedTime); + } + RunEnemyAI(e, fElapsedTime, i); + } + for (std::vector::iterator it = powerups.begin(); it != powerups.end();) { + Powerup& p = *it; + if (!p.Update(fElapsedTime)) { + it = powerups.erase(it); + } + else { + it++; + } + } + if (lastPowerupCollidedWith != -1) { + vec3d vForward = Vector_Mul(vLookDir, std::min(player.GetRadius() - 0.00001f, moveSpd * fElapsedTime)); + if (CheckPowerupCollision({ vForward.x,0,vForward.z }, { player.GetPos().x,player.GetPos().z }, player.GetRadius() * 1.25) != -1) { + powerups[lastPowerupCollidedWith].opened = true; + } + } if (hp > 0) { HandleKeys(fElapsedTime); RenderWorld(); diff --git a/Faceball2030/main.h b/Faceball2030/main.h index 05d0053..250db53 100644 --- a/Faceball2030/main.h +++ b/Faceball2030/main.h @@ -174,6 +174,7 @@ struct Powerup : Object { Pixel col; float colorCycle=1; float aliveTime=0; + bool opened = false; bool Update(float fElapsedTime); }; @@ -278,6 +279,7 @@ class FaceBall : public PixelGameEngine int tagsRemaining = 10; int lives = 3; double gameTimer = 0; + int lastPowerupCollidedWith = -1; mat4x4 matProj; @@ -374,6 +376,7 @@ class FaceBall : public PixelGameEngine void RenderHud(float fElapsedTime); void ConvertBulletColor(Mesh& bullet, Pixel col); void Display3DKillerModel(); + int CheckPowerupCollision(vec3d movementVector, vf2d pos, float radius); public: vi2d MAP_SIZE; float restingTriangleYDepth = 0.f;