I have created a design showed below. Is this a deign pattern? If so what is its actual name? If it is not a proper design, does there exist a design pattern that does the same but better?
Not tested code
//ExampleFunction
void TestFunction(){
DefinitionCollection collection = new DefinitionCollection();
//Creates two enemies with SoldierDefinition (id == 0)
Enemy enemy1 = new Enemy(collection,0);
Enemy enemy2 = new Enemy(collection,0);
//Both enemies got the same reference to MaxHealth and Damage
//However both enemies got independent currentHealth values
//enemy2 now looses 20 health, from 70 health to 50 health
enemy1.Attack(enemy2);
}
//This class are the instances, when a new enemy is made, this is created
public class Enemy {
//Contains variables that changes per instance
int currentHealth;
EnemyDefinition definition;
public Enemy(DefinitionCollection collection, int definitionID){
definition = collection.GetDefinition(definitionID);
currentHealth = definition.MaxHealth;
}
public void Attack(Enemy receiver){
receiver.Damage(definition.Damage);
}
public void Damage(int amount){
currentHealth -= amount;
}
}
//Keeps track of all the definitions
public class DefinitionCollection{
private List<EnemyDefinition> Definitions = new List<EnemyDefinition>();
public DefinitionCollection(){
Definitions.Add(new SoldierDefinition());
}
public EnemyDefinition GetDefinition(int definitionID){
return Definitions[i];
}
}
public abstract class EnemyDefinition{
//Contains constant variables per definition or type
public int MaxHealth {get; private set;}
public int Damage {get; private set;}
}
public class SoldierDefinition : EnemyDefinition{
public SoldierDefinition(){
MaxHealth = 70;
Damage = 20;
}
}
Aucun commentaire:
Enregistrer un commentaire