Passing a parameter for my health bar

Started by
7 comments, last by jaymystro 18 years ago
The idea is this: I have a class which is responsible for my Enemy within my game. It binds a texture in the contsructor and then has a member function that creates the quad to be textured to. So when I want an enemy in a game I just call the Draw() function. Now I have collisions setup so that when an enemy vertex reaches the player, the system is flagged a collision. This works fine but when I have a collision I want a health bar to decrease (which is just simply a quad which I have tracked the verticies using variables). Also when I hit an enemy I want to make the health bar go back up. Here is the idea I cam up with: from the Enemy class: CEnemy::DrawEnemy(int dx1, int topRx, int botLx, int dx2, int &playerX1, int &ScoreX1, int &ScoreX2, bool isAttacking) { voidif ((!isAttacking) && (botLx == playerX1)) { ScoreX1 -= 25; ScoreX2 -= 25; } else { if((isAttacking) && (botLx == playerX1)) { ScoreX1 += 25; ScoreX2 += 25; } } glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex2i(botLx , 0); glTexCoord2f(1.0f, 0.0f); glVertex2i(dx2 , 0); glTexCoord2f(1.0f, 1.0f); glVertex2i(topRx , 100); glTexCoord2f(0.0f, 1.0f); glVertex2i(dx1 , 100); glEnd(); } // dx1, topRx, botLx, dx2 are all decremented by 1 then passed to the Enemy draw // function. This enables the translation from right to left. from main.cpp... { bool isAttacking = false; if (keyPressed[VK_LEFT]) { isAttacking = true; } EnemyInstance[0]->DrawEnemy(dx1, topRx, botLx, dx2, playerX1, ScoreX1, ScoreX2, isAttacking); } So basically the main function detects a keypress (spacebar) and flags isAttacking to true. This should then pass into the instance of the Enemy class and then the IF statements should be able to access it. When there is a collision, the variables that draw the health bar are increased or decreased. However, when the collision occurs, the health bar shrinks as expected, but it does not go back up if the space bar is down. Im sure this is a simply thing, I thought about putting the whole thing in the class but it wont hav access to the keydown functionality, hence the bool variable. Thanks for help, sorry i went on forever. Jay
Advertisement
" voidif ((!isAttacking) && (botLx == playerX1)) "

Is that really correct? Try removing the "void" and see if it helps...
This isnt really an opengl question thou.. :)
"Game Maker For Life, probably never professional thou." =)
no sorry thats a mistake while pasting the code. I was gonna put this in the game programming one but i thought it might confuse because of the openGL drawing command.
You really should decouple your graphical/rendering representation from your game representation. I see 3 systems here interacting that you've placed in a single class.

I would split your enemy up into 2 seperate classes, one that handles game events, and one that knows how to be drawn.

On the game side:

class CGameObject;

Would be a base class that would handle the game aspects of objects. It would contain game relevant attributes of an object: Active, Inactive, Position, Orientation, etc..

Derive a CGameCharacterObject from CGameObject, and add a member variable "health" to it.

From this you can derive your CGameEnemyObject (and a CGamePlayerObject). Because they both derive from CGameCharacterObject, they both automagically contain the health variable.

Now add to the CGameCharacterObject class, an OnCollision(CGameObject* colliding_object) function, and in that function you can determine what has collided with your object (it's been passed in as an argument), whether it is dangerous, and what sort of damage it should do. Then this function will modify the heath variable with the new health value.

So you have a way for your collision system to tell a game object that it has collided, and a way for the game object to set it's health, but you don't have anything in here that does drawing. This is where you need a new type of class:

class CRenderObject;

The render object would contain the character's mesh, and the draw() function, that would actually draw the character to the screen. Your CGameObject would contain a pointer to this CRenderObject.

So you would derive a CRenderEnemyObject class from the CRenderObject class.

(I will assume that your health bar is part of the character's mesh... I would put status bars and HUD elements in a seperate HUD object that handled display of stuff that wasn't actually in the game world, but i'm trying to keep things simple)

Now that these things are seperated, when you call Draw() on CRenderEnemyObject, all you need to do is take the health variable of the enemy, and scale your health bar based on that value. It doesn't care what the number is, as far as the renderer is concerned, it's just a scale value.

As far as your code goes, how are you deriving dx1 and dx2, it looks like those are passed into the draw function and not manipulated in any way, you set ScoreX1 and ScoreX2, but never use those variables.
Also, in my example above, I would make your main loop detect keystrokes, and call a function to set the attacking state on the Player character's GameObject, so on keydown of the spacebar, the player's state is set to IsAttacking = true;

Then in your OnCollision() function you can determine if the player is attacking, and know whether or not to add or subtract from the health.
Thats some great info there thanks, i have forgot to state that my main() increments those dx2 variables before passing them to the class, because i couldnt get them to increment within a member function. I have now setup a seperate class that draw the health bar like u said. I have decided that i am gonna try just making a different bar to show the score, instead of scaling that one up and down. I would implement the things u said but im short on time now so i dunno. Your info is great tho so thanks. Ill let you know how i get along or if i have any porblems. Ive only got a couple of days and my game still needs sound!

cheers!

Just out of interest tho, you know when I seperate your .cpp from your .h, i get errors where any openGL functions are not recognised. I included the GL headers in the cpp as well as the class.h file, and still got the errors. At the moment i have to put all my member functions in the .h file. Any ideas?
Say you have myclass.h and myclass.cpp

At the top of myclass.cpp do you have this line?

#include "myclass.h"

I'm not sure why it wouldn't be recognising the opengl functions.. thats odd..

make sure you have all your semicolons and curly braces correct in your .h file.. sometimes if you miss one, the compiler decides that your .cpp file is still part of the class declaration and gets confused.
yeh ive done that an yeh i have included the header in the .cpp. I cant understand it either, i have checked my semicolons and everything, it does this with every header file tho, i dont get it. If I strip out any openGL functionalities it will compile fine. Im stumpd!

This topic is closed to new replies.

Advertisement