I was trying to come up with a structure for a simple TD game that has following elements
Tower Each Tower has a set of properties like range, damage, health etc There could be different types of Towers with different attack types. For eg Tower A can perform attackType 1 and 2 and Tower B can perform attackType 3 and 4
Creep Each Creep has a set of properties like damage, health, bountyPoints etc. There could be different types of Creeps with different abilities just like the Towers
For now I am trying to come with a good design that is scalable and well structured for the above two game elements
This is the skeleton of a Tower class that I have come up with so far. Please comment and suggest changes. Any design patterns that could make life easier
using System.Collections;
using System;
public enum TowerType
{
tA,
tB,
tC
};
public class Tower {
private TowerType type;
private int damage;
private int range;
private int health;
public Tower(TowerType type)
{
this.type = type;
initializeVariables();
}
private void initializeVariables()
{
if (this.type != null)
{
if (this.type == TowerType.tA)
{
this.damage = 20;
this.range = 40;
this.health = 50;
}
else if (this.type == TowerType.tB)
{
this.damage = 30;
this.range = 50;
this.health = 60;
}
else if (this.type == TowerType.tC)
{
this.damage = 60;
this.range = 60;
this.health = 80;
}
}
}
public int getDamage()
{
return this.damage;
}
public int getRange()
{
return this.range;
}
public int getHealth()
{
return this.health;
}
public TowerType getTowerType()
{
return this.type;
}
public string getType(int value)
{
return Enum.GetName(typeof(TowerType), value);
}
}
So using enums to define various types of towers. But is this a good design? Each Tower would have different damage, range and health.
What if there are 100 different towers? So in my InitializeVariables() would have a cluster of 100 if else statements. How can I improve on this or is there a better way to implement this?
Answer
Here is how i think it could be done
class Class1 {
public enum TowerType { A, B, C };
static private Dictionary towerTypesInfo;
static void init()
{
towerTypesInfo= new Dictionary(100);//100 tower types
towerTypesInfo.Add(TowerType.A, new Tower(20, 40, 50));
towerTypesInfo.Add(TowerType.B, new Tower(30, 50, 60));
//... fill in the rest 98 tower types with their values ...
//example of making a tower named tower1
Tower tower1 = new Tower(TowerType.A);
}
public class Tower {
public TowerType type { get; private set; }
public int damage { get; private set; }
public int range { get; private set; }
public int health { get; private set; }
public Tower(int d, int r, int h)
{
damage = d;
range = r;
health = h;
}
public Tower(TowerType type)
{
damage = towerTypesInfo[type].damage;
range = towerTypesInfo[type].range;
health = towerTypesInfo[type].health;
} } }
You don't need to make a function for every single variable you have, like in java. Just use public int damage { get; private set; }
, C# has automated way of doing this with overwriting the default get; set; properties.
Also, You could fill this dictionary
from a .txt if you wanted to. you could format the .txt to make it a bit easier to edit
TowerTypesInfo.txt
:
A 20 40 50
B 30 50 60
C ...
No comments:
Post a Comment