Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Subclass Best Practice

Tags:

I am currently working on a game in XNA and I'm not sure on how I should go about doing the following...

I have a base class of buildings as such

   public class BuildingsBase
   {
     private int _hp;
     public int hp
     {
        get { return _hp; }
        set { _hp= value; }
     }

     private int _woodRequired;
     public int woodRequired
     {
        get { return _woodRequired; }
        set { _woodRequired = value; }
     }
  }

I then have multiple subclasses for building types eg.

public class TownHall:BuildingsBase
{
    public int foodHeld;

    public TownHall()
    {
        foodHeld = 100;
        woodRequired = 500;
    }
}

My question is, what is the best way of setting the default values for building subclasses.

For example, the woodRequired for a townhall is set to 500 but at various places in code I need to access this value before I have an instance of townhall declared (When checking if there is enough wood to build).

I currently have a global array of default variables for each building type but im wondering if there is a better way of doing this.

if (Globals.buildingDefaults[BuildingType.Townhall].woodRequired < Globals.currentWood)
{
    Townhall newTH = new Townhall();
}
like image 244
user3208483 Avatar asked Mar 18 '16 11:03

user3208483


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Is C programming hard?

C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.


1 Answers

Usually what happens is that they create a flyweight (see pattern). This object contains properties that are the same for every instance anyway. There's no need to change (or actually store) the required amount of wood for each instance separately.

In a very basic design it would look like:

class BuildingTemplate
{
     public int WoodRequired { get; set; }
}

class Templates
{
    public static BuildingTemplate TownHall { get; set; }
}

In the end you'd be calling a method like:

public bool CanBuildTownHall(Player player)
{
    return player.HasEnoughResources(Templates.TownHall);
}

Of course, you can use a dictionary for template retrieval, and players shouldn't really know about building requirements. I'm just illustrating the pattern here.

If the player has enough resources, you can use the template to subtract the amount and create an actual instance of the TownHall. It's nice to have an reference to the actual template, because you'd probably be accessing other global properties that are valid for all TownHalls as well (such as audio/visuals/...).

class TownHall
{
    public TownHall(BuildingTemplate template)
    {
        _template = template;
    }
}
like image 166
Caramiriel Avatar answered Oct 13 '22 20:10

Caramiriel