• Advertisement

Archived

This topic is now archived and is closed to further replies.

should i consolidate my classes??

This topic is 5095 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

high, i have a simple text based RPG im working on. anyway, 2 of my classes are called Enemy and Player. these 2 classes track all the combat side of the game. anyway, both the Enemy and Player class can attack(). they also both have health. but thats the only similarities. enemys drop_loot() can can be_boss. they also have a name. where as players have 10-15 more varaibles then the Enemy does such as health/stamina/mana/bullets/etc etc. plus a player can level_up(). i just dont know if i should maybe combine these 2 to form one class called Character or something. they only share 2 common things ( attack() and health). everything else is different. but they are both characters inside the game, they could both be "classified" as being "characters" or "entities" or something. they may not have that many similar member functions/variables, but they both are similar "things". im just wondering if its bad programming that i have these as 2 seperate classes, and should i combine them and form a single class? i mean, as long as i dont screw up and have a player drop_loot() or a enemy level_up(), i should be fine. so what do you think? as it is now, its working fine. im just wondering if its bad practise to keep these 2 seperate, seeing as they are both similar "beings". thanks for any help.

Share this post


Link to post
Share on other sites
Advertisement
doh..... ive only been programming a few months so i dont know how to use inheritense.... but i think i understand the basic concept of it. wouldnt it kind of be overkill to make a base class when the 2 classes (enemy and player) only share one function and one variable?

Share this post


Link to post
Share on other sites
No, it''s not overkill. Besides, there are other good reasons to have both classes derived from the same base class. For example, at some point you are going to want to make some sort of list or array of all the characters in the game. This is a hell of a lot easier to do if they are all derived from a single class.
E.g.

class Entity
{
//common stuff

};

class Player : public Entity
{
//stuff that''s unique to Players

};

class Enemy : public Entity
{
//stuff that''s unique to Enemies

};

main()
{
Entity *Characters[10];
Characters[0] = new Player;
Characters[1] = new Enemy;

//etc etc...

}

Share this post


Link to post
Share on other sites
I have to agree, though I think it is probably a style preference. I am currently working on a clone of an old arcade game... (not so much of a clone any more... too much new stuff) and have a structure for my game pieces like this:


  • GamePiece

    • Combatant
      • Player
      • Enemy
        • SpecificEnemy1

        • SpecificEnemy2

        • ...


    • Item
      • Terrain

      • ... etc.



This might look like overkill to some of you out there, but I have to say it really helps to keep my concepts organized, and helps with object instance declaration since the specific classes can set many parameters to their default values.

As was said before, you may not have much for those classes to do yet, but are you sure you never will?

[edited by - null_void on February 6, 2004 7:55:37 AM]

[edited by - null_void on February 6, 2004 7:59:32 AM]

[edited by - null_void on February 6, 2004 8:00:13 AM]

Share this post


Link to post
Share on other sites
I don''t think that it is overkill at all. It''s considered good style and is also much more scalable as you''ll find when you want to add something in the later stages. Cheers.

Share this post


Link to post
Share on other sites
Making both Player and Enemy derive from a base class is a good idea. Given a common interface to base stats, you can pull stuff like applying the same algorithm two ways.


void UseAttackCommand(Entity &Attacker, Entity &Target)
{
// put calculations here

}

int main()
{
Player player = new Player;
Enemy enemy = new Enemy;
UseAttackCommand(player,enemy); // the player attacks

UseAttackCommand(enemy,player); // enemy retaliates in the same manner

}


This might not work as well for you, depending on how you designed your game. But it''s worth looking at.

--------------------------
{ Arena: Resurrection, my text-based RPG/Mortal Kombat-like game }
{ A Look Into The World Of Arena II, a collection of short stories about characters in Arena II }

Share this post


Link to post
Share on other sites
thanks for all the replies

creation, my attack functions work just as you expected
except their called player.attack(ENEMY &enemy) and enemy.attack(PLAYER &player)

they also are pretty much completely identical in syntax. now if i were to make a base class called ENTITY would i get rid of the player.attack() and enemy.attack(), then put entity.attack() inside the ENTITY part?

[edited by - graveyard filla on February 6, 2004 3:09:01 PM]

Share this post


Link to post
Share on other sites
I know you weren''t asking me, but that''s what I would do. I''d make the function virtual so that ''special'' Entity types could override the attack method.

Share this post


Link to post
Share on other sites
quote:
Original post by graveyard filla
thanks for all the replies

creation, my attack functions work just as you expected
except their called player.attack(ENEMY &enemy) and enemy.attack(PLAYER &player)

they also are pretty much completely identical in syntax. now if i were to make a base class called ENTITY would i get rid of the player.attack() and enemy.attack(), then put entity.attack() inside the ENTITY part?




You could do it like that, and it wouldn''t be hard. This way you would only need to write one attack function that works for all entities (you could have NPCs attack enemies, players can attack NPCs, etc).

--------------------------
{ Arena: Resurrection, my text-based RPG/Mortal Kombat-like game }
{ A Look Into The World Of Arena II, a collection of short stories about characters in Arena II }

Share this post


Link to post
Share on other sites
quote:
Original post by null_void
I know you weren''t asking me, but that''s what I would do. I''d make the function virtual so that ''special'' Entity types could override the attack method.


what do you mean by... well all of that?

Share this post


Link to post
Share on other sites
if i were to do that i would have to implement some sort of damage mod variable... maybe pass it by refernce or maybe ill just make it a member variable of the entity class

also... if i wanted to do a damage mod thing.... i would have to use a double to calculate the damage mod like damage*1.06 for 6% damage increase........... but a player health has to be a whole number (integer) so how do i have the final damage be an int but its calculated with a double? would i get warnings or errors or what? not only that, but wouldnt it round the .06 to 0 and give me no damage mod. just wonderin how i could do this. thanks

Share this post


Link to post
Share on other sites
quote:
Original post by graveyard filla
if i were to do that i would have to implement some sort of damage mod variable... maybe pass it by refernce or maybe ill just make it a member variable of the entity class

also... if i wanted to do a damage mod thing.... i would have to use a double to calculate the damage mod like damage*1.06 for 6% damage increase........... but a player health has to be a whole number (integer) so how do i have the final damage be an int but its calculated with a double? would i get warnings or errors or what? not only that, but wouldnt it round the .06 to 0 and give me no damage mod. just wonderin how i could do this. thanks


You shouldn''t have to cast an int to float, it might be cast automatically. But I wouldn''t count on that behavior. You would have to cast the damage variable to float explicitly to avoid warnings/errors. When the result is assigned back to damage, it''ll be re-cast to int (or whatever type damage is) and the decimal portion will drop (0.06 will be 0, 1.06 will be 1, etc.).

So damage = (float)damage*1.06f would do what you ask.

--------------------------
{ Arena: Resurrection, my text-based RPG/Mortal Kombat-like game }
{ A Look Into The World Of Arena II, a collection of short stories about characters in Arena II }

Share this post


Link to post
Share on other sites
quote:
Original post by graveyard filla
quote:
Original post by null_void
I know you weren't asking me, but that's what I would do. I'd make the function virtual so that 'special' Entity types could override the attack method.


what do you mean by... well all of that?




quote:
Original post by null_void
I know you weren't asking me, but that's what I would do.


It seemed that you were specifically speaking to Creative, though I could be mistaken.

quote:
Original post by null_void
I'd make the function virtual so that 'special' Entity types could override the attack method.


Virtual functions allow you to define a default behavior for a base class and all of its derived classes. In this case, the Entity class. Derived classes, though (say, a specific type of enemy, maybe a boss?), can override that function.

At runtime, the specific type of object that is being operated on will be determined. If that object has overridden the function, the class specific function will be executed. Otherwise, the default one in the base class will be run.

Note that there is some debate over whether virtual functions incur too much cost to be efficient. According to Scott Meyers, who has written various books on C++, (and this logic makes sense to me), resolving a virtual function takes about 3 more instructions than it takes to follow a regular function pointer. I know I couldn't do these things without virtual functions and have it cost less than 3 instructions.

[edited by - null_void on February 6, 2004 7:58:08 PM]

[edited by - null_void on February 6, 2004 8:02:05 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by graveyard filla
also... if i wanted to do a damage mod thing.... i would have to use a double to calculate the damage mod like damage*1.06 for 6% damage increase........... but a player health has to be a whole number (integer) so how do i have the final damage be an int but its calculated with a double? would i get warnings or errors or what? not only that, but wouldnt it round the .06 to 0 and give me no damage mod. just wonderin how i could do this. thanks


int moddeddamage = damage * 106 / 100;


No casting, or use of FP at all this way. Be sure to do the multiplication first or you''ll get horrible precision.

Share this post


Link to post
Share on other sites

  • Advertisement