should i consolidate my classes??

Started by
14 comments, last by graveyard filla 20 years, 2 months ago
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.
FTA, my 2D futuristic action MMORPG
Advertisement
You could make a base class called Entity and inherit both player and enemy from that.
Dat is off da hizzle fo shizzle dizzle
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?
FTA, my 2D futuristic action MMORPG
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...}
You are not the one beautiful and unique snowflake who, unlike the rest of us, doesn't have to go through the tedious and difficult process of science in order to establish the truth. You're as foolable as anyone else. And since you have taken no precautions to avoid fooling yourself, the self-evident fact that countless millions of humans before you have also fooled themselves leads me to the parsimonious belief that you have too.--Daniel Rutter
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]
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.
If a plant cannot live according to its nature, it dies; so a man.
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 }
--------------------------{ 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 }
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]
FTA, my 2D futuristic action MMORPG
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.
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 }
--------------------------{ 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 }

This topic is closed to new replies.

Advertisement