.XNA design Question

Started by
5 comments, last by BCullis 11 years, 10 months ago
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.
Advertisement
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.
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 (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

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?
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.

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?

[quote name='WavyVirus' timestamp='1339519426' post='4948540']
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?
[/quote]

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 (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

This topic is closed to new replies.

Advertisement