Jump to content

  • Log In with Google      Sign In   
  • Create Account


.XNA design Question


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Publius190   Members   -  Reputation: 104

Like
0Likes
Like

Posted 08 June 2012 - 12:36 PM

Hi, I have to admit i'm very new to programming and i'm really only in the design phase of creating my game.

So heres my question:

In the final game (top down roguelike shooter) i'm going to have a few different characters you can start out as. Each character will have a different weapon type, different movespeed, different texture, animation, etc. Also i'm planning on having each character's main weapon be somewhat customizable.

In order to code this would it be easier to create a seperate class for each character with methods for moving, collision detection, firing, etc. Or should i have a main player class with methods that call each character class.

I guess while i'm writing this it does sound better to go with the second option, but i'm having trouble deciding what i would need to do in the player class and what would definately need to be done in the character class.

Any help at all would be great.

Sponsor:

#2 wannabeprogrammer   Members   -  Reputation: 556

Like
1Likes
Like

Posted 11 June 2012 - 05:59 AM

From the basic description you've presented I think you should only use 1 class, calling it player or character.

These things you mention weapon type, different movespeed, different texture, animation would then be the members for the class and would change depending on the character. You would set these when you make an instance of you character object.

You would then have your moving, collision detection, firing, etc methods that make use of these variables. However, if these methods vary greatly between the different characters then you should consider inheritance.

You don't really provide enough information to give a complete answer.

#3 BCullis   Crossbones+   -  Reputation: 1813

Like
0Likes
Like

Posted 12 June 2012 - 09:44 AM

I agree with wannabe there, your player class (the code) sounds completely separable from the assets that will make each character unique: namely the texture and animation frames. The player class can call a generic render() method and the texture appropriate to the chosen character will be drawn. All the other variables are just that, variables, and the class can easily change those values at the point of character creation.

Just stay as simple as possible in code design until it makes sense to start breaking things out, pre-optimization and overly elaborate class hierarchies are bad traps to fall into. :)
Hazard Pay :: FPS/RTS in SharpDX
DeviantArt :: Because right-brain needs love too

#4 MrTwiggy   Members   -  Reputation: 107

Like
0Likes
Like

Posted 12 June 2012 - 10:31 AM

I have a question based off the answers given.

Would it be better to have a base character class that has universal members that all would have, texture, position, animations, sounds, move speed, etc.

Then have a different class for each character type that inherits from the base character class, but gives it`s unique values to the members? Would that be the best way?

#5 WavyVirus   Members   -  Reputation: 735

Like
0Likes
Like

Posted 12 June 2012 - 10:43 AM

If the only difference between the characters is the values of the member variables, there is no need to create a sub-class for each character type.

You can have a single character class and simply pass in the correct values when you construct it.

Only if the different characters behaved in fundamentally different ways would you need to consider adding classes specific to each character type.

#6 MrTwiggy   Members   -  Reputation: 107

Like
0Likes
Like

Posted 12 June 2012 - 11:06 AM

If the only difference between the characters is the values of the member variables, there is no need to create a sub-class for each character type.

You can have a single character class and simply pass in the correct values when you construct it.

Only if the different characters behaved in fundamentally different ways would you need to consider adding classes specific to each character type.


But, say you wanted a multiplayer game. Even if the only thing that differeniates different characters is different values, wouldn't it be better/easier to have unique classes for each character that inherits from the base character class, and then when people are selecting their character, you could set their character type to the class for the unique character, rather than constantly calling functions that are hardcoded in and would set the values for each specific character?

#7 BCullis   Crossbones+   -  Reputation: 1813

Like
0Likes
Like

Posted 12 June 2012 - 11:18 AM


If the only difference between the characters is the values of the member variables, there is no need to create a sub-class for each character type.

You can have a single character class and simply pass in the correct values when you construct it.

Only if the different characters behaved in fundamentally different ways would you need to consider adding classes specific to each character type.


But, say you wanted a multiplayer game. Even if the only thing that differeniates different characters is different values, wouldn't it be better/easier to have unique classes for each character that inherits from the base character class, and then when people are selecting their character, you could set their character type to the class for the unique character, rather than constantly calling functions that are hardcoded in and would set the values for each specific character?


What's particularly hard about this?
//player constructor
public Player(int health, float speed, String name, Texture2D spriteSheet)
{
   this.health = health;
   this.speed = speed;
   this.name = name;
   this.spriteSheet = spriteSheet;
}
//create 4 different players
Player player1 = new Player(100, 20f, "Jimbo", Content.Load<Texture2D>("ArcherSprite"));
Player player2 = new Player(120, 12f, "Tank", Content.Load<Texture2D>("KnightSprite"));
Player player3 = new Player(60, 16f, "PaperTiger", Content.Load<Texture2D>("MageSprite"));
Player player4 = new Player(75, 16f, "Healzorz", Content.Load<Texture2D>("ClericSprite"));

You CAN make as obtuse and intricate of a class hierarchy as you want, but when all the character types are essentially identical functionality-wise, and only differ on pretty basic variables, calling a constructor once with unique values is easy, cheap, and reduces headache in code maintenance. Not to mention avoiding code duplication, which you're going to do a lot if you start subclassing for each archetype.
Hazard Pay :: FPS/RTS in SharpDX
DeviantArt :: Because right-brain needs love too




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS