I want the flash light to be toggle-able, and the way I tried to make it toggleable is by using the isLight bool which checks if its already on or off. (So if the light is already on, if you press the F key it should turn off, and if the light is off, the light should turn on)
And that part mostly works, but the issue is that if you don't press and release the F key instantly, the light flickers (as it is stuck in a loop of turning on and off), and I have no idea how to prevent that.
- I did try doing Sleep() and return; after every if statement, but it makes the game look slower and the FPS drastically drops...
- I tried removing the else if statement and using only 2 if's in the toggle part, but it stopped working.
I am a beginner in this, so don't judge me if there was an easier and more efficient way of doing this.
bool isLight = false; //Definition of variable, don't want the light to be on when started.
//Toggle
if (keyboard.KeyIsPressed('F') && !isLight) {
isLight = true;
}
else if (keyboard.KeyIsPressed('F') && isLight) {
isLight = false;
}
//Actual flash light part
if (isLight) {
this->gfx.light.SetPosition(lightPosition); //Set the position of light
this->gfx.light.SetRotation(this->gfx.Camera3D.GetRotationFloat3()); //Set the rotation to the camera
this->gfx.light.lightStrength = lightStrenght; //Set the strength of the light to a predefined variable.
} else {
this->gfx.light.SetPosition(lightPosition);
this->gfx.light.SetRotation(this->gfx.Camera3D.GetRotationFloat3());
this->gfx.light.lightStrength = 0.0f; // Set it to be invisible, but it still rotates with the player.
}