I have a space shooter game and I am trying to create a shield power for the player ship. My mechanic for shield is working. When I press the shield power button(P), the shield power decreases from 100 to 0 at a certain pace.
Problem is that I am trying to draw this shield power value on screen but it's just showing the starting shield power value(which is 100) and not showing the decrease in shield power value when I press the shield button.
What am I missing?
Here's the relevant code: Game.cpp
if (keyboard->IsKeyPressed('P'))
{
if (player_->shield == true)
player_->shield = false;
else
player_->shield = true;
}
if (player_->shield && player_->shieldPower > 0)
{
if (keyboard->IsKeyPressed(VK_SPACE))
{
XMVECTOR playerForward = player_->GetForwardVector();
XMVECTOR bulletPosition = player_->GetPosition() + playerForward * 10.0f;
SpawnBullet(bulletPosition, playerForward);
}
player_->shieldActive = true;
if (GetTickCount() - player_->time > 100)
{
player_->time = GetTickCount();
player_->shieldPower--;
if (player_->shieldPower <= 40)
{
player_->shieldPowerLow = true;
}
else
{
player_->shieldPowerLow = false;
}
}
}
else
player_->shieldActive = false;
TextClass.cpp
void TextClass::OnRender(System *system)
{
Graphics *graphics = system->GetGraphics();
FontEngine *fontEngine = graphics->GetFontEngine();
Player *player = new Player();
Game *game = system->GetGame();
game->RenderEverything(system->GetGraphics());
std::stringstream buffer;
buffer << player->shieldPower;
fontEngine->DrawText(buffer.str().c_str(), 50, 100, 0xffffffff,
FontEngine::FONT_TYPE_SMALL);
}
Player.h
class Player
{
public:
int shootDelay;
int time;
int shieldPower;
bool shield;
bool shieldActive;
bool fullShield;
bool shieldPowerLow;
bool shoot;
}
Player.cpp
Player::Player() :
shootDelay(500),
shieldPower(100),
fullShield(true),
shieldPowerLow(false),
shield(false),
shieldActive(false),
shoot(false)
{
}
void PlayingState::OnRender(System *system) { //...// Player *player = new Player();
...am I reading correctly that you're constructing a newPlayer
instance every timeTextClass::OnRender
is called? \$\endgroup\$Player *player = new Player();
- this creates another player and asks what that player's shield power is. So now you have two players (one of which isn't used except to get its shield power). (And since it never gets deleted, after 100 ticks you have 101 players) \$\endgroup\$