'How do I make an enemy damage me instead of killing me?
I'm using OpenGl and C.
I want the player to lose some health, when he gets in the vicinity of the enemy, but only lose the health once. Then if the player leaves that vicinity and comes back he loses the rest of the health. There is a global variable pHealth =100;
void damage()
{
int dmg =50;
pHealth-=dmg;
if(pHealth ==0)
{
gameState=4;
}
}
void drawSprite()
{
int x;
int y;
int s;
if(px<sp[0].x+30 && px>sp[0].x-30&& py<sp[0].y+30&&py>sp[0].y-30) // pick up key
{
sp[0].state=0;
}
if(px<sp[1].x+30 && px>sp[1].x-30&& py<sp[1].y+30&&py>sp[1].y-30) // enemy damage
{
damage();
}
This just kills me instantly please help
Solution 1:[1]
This doesn’t really have to do with opengl. Currently the player takes damage at every loop iteration (ie every frame), if they’re in the vincinity of the enemy. This results in them almost instantly losing all their health (there should be at least 20~60 frame in a second, so the hp loss is almost instantaneous).
You need to ensure the player loses their health only once. For that, two possibilities:
- Push back the player when they encounter the enemy, so that they only spend one frame there.
- have a variable "has the player already taken the damage?". Set it to
FALSEat the beginning, then when the player takes damage, set it toTRUE, and while it is true, don’t give more damage to the player. When the player gets away from the enemy, reset it toFalseso they’ll take damage again if they get close again
EDIT: There is also solution 3
- Limit the damage rate. It means instead of checking and giving damage at every frame, you could check it every 1/10 second. Thus you could limit the damage rate
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
