GPWiki.org
GPWiki.org
It is currently Sat May 18, 2013 1:32 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Design Theory Question
PostPosted: Fri Nov 25, 2011 9:58 pm 
Super-dooper pooper scooper
User avatar

Joined: Tue Oct 11, 2011 8:08 pm
Posts: 170
What are the pros and cons for the following:

I am making a simulator of sorts, in which there are workers. There are four main kinds:

-Builders
-Spearmen (read Grunts :P )
-Archers
-Knights

I came up with two ideas to simulate them. Opinions?

1. Make a very generalized class 'Citizens', make four private ints for each type, and add methods for buying each type of person, selling each type, and then figuring out what they do. I.e. Each builder gives one Action Point to the town, each Spearman gives 5 defense, etc.

2. Make a base class (person), and then create derived classes, which I then make collections or arrays for each type. Then put all that in a class. This has much more in detail potential, because each person could have health, attributes, and a randomly generated personality.

I am using C# WinForms, not XNA.


Top
 Profile  
 
PostPosted: Fri Nov 25, 2011 10:53 pm 
Funky Monkey

Joined: Thu Sep 09, 2004 1:17 pm
Posts: 1537
Location: burrowed
It depends on how different you want your workers to be. is there different behavior or are they just different in some stats?

Instead of an int for worker differentiation i'd use an enum though.

Generally i would advise to use a base class, and derive each character from that.
You could, of course, wrap it all in a single class but you'd have to switch case the citizen type for every action. That makes code hard to read and bloats classes.

You also dont need to make an array for each class.
Person[] persons; will be fine to store every entity that is derived from Person. You can use virtual or abstract methods for common actions like Sell() or whatever else you need.

_________________
Long pork is people!

wzl's burrow


Top
 Profile  
 
PostPosted: Sat Nov 26, 2011 1:51 am 
Super-dooper pooper scooper
User avatar

Joined: Tue Oct 11, 2011 8:08 pm
Posts: 170
Thanks! I came up with a mix of the both. Here it is...

Improvements? I'd like this to be as reusable and user-friendly as possible.


Top
 Profile  
 
PostPosted: Sat Nov 26, 2011 8:53 am 
Funky Monkey

Joined: Thu Sep 09, 2004 1:17 pm
Posts: 1537
Location: burrowed
I'd use a property for isAlive.

You could use it to remove your checking for (health <= 0) in update and health decrease.

Code:
public bool Alive
{
  get
  {
    return Health > 0;
  }
}


Else i'd advise to first decrease health, then check for being alive, otherwise you might end up with being alive while having less than 1 hp

Code:
public void DecreaseHealth()
{
  if ((Health -= rnd.Next(50)) <= 0)
      isAlive = false;
}


Lastly i'd try abstract the DefenseAdded/BPAdded. Something along the lines of

Code:
public class Person
{
  protected Bonus bonus;

  public Bonus GetBonus()
  {
    return bonus;
  }
}

public class Bonus
{
  public int defenseBonus;
  public int BPBonus;
}

public class Soldier : Person
{
  public Soldier(SoldierBases Type)
  {
    bonus = new Bonus(){defenseBonus = (int)Type};
  }
}


this could save you either storing each person in a different array or make checks which Type this Person is, cast it, and use the method.

Hope this helps :)

_________________
Long pork is people!

wzl's burrow


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group