Jump to content
  • Advertisement

Archived

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

graveyard filla

should i consolidate my classes??

This topic is 5246 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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!