The name archtype was taken from the game Freelancer where an archtype is sort of a template for a ship, station, planet, gun, whatever
Yes and
the word is still "archetype"
Looking at your code, a few constructive criticisms (please take in the spirit they are intended)
public static void LoadEquip(Dictionary<string, string> saveEquip, ulong parentId)
// becomes
public static void LoadEquipment(IDictionary<string, string> savedEquipment, ulong parentId)
No need to shorten to LoadEquip. Call it LoadEquipment and let your IDE complete it for you.
In general, program to an interface rather than a concrete class.
"saveEquip" sounds like a verb (method, delegate) rather than a noun (variable, data, etc).
foreach (KeyValuePair<string, string> equip in saveEquip)
{
var arch = GameServices.Equip.GetArchtype(equip.Value);
This is a personal preference, but I would argue there is no need to specify the exact type of equip. You're clearly ok with using var, so you may as well use it in the foreach loop too. Remember, the best code is the code you don't have to write.
if (arch == typeof(WeaponArch))
{
var weapon = GameServices.Equip.LoadArchtype<WeaponArch>(equip.Value);
// weapon stuff
}
else if (arch == typeof (ShieldArch))
{
var shield = GameServices.Equip.LoadArchtype<ShieldArch>(equip.Value);
// shield stuff
}
else if (arch == typeof (EngineArch))
{
// engine stuff
}
// etc.
If you see code like this, massive alarm bells should be ringing. LoadEquipment suddenly has to know how to load every different entity you have in the game.
Basically, don't do this.
Here's a different way to do this.
public abstract class Archetype
{
public string Nickname { get; set; }
public abstract void BuildEntity(string entityKey, ulong parentId);
}
public class WeaponArchetype : Archetype
{
public override void BuildEntity(string entityKey, ulong parentId)
{
var entity = GameServices.EntityManager.Create<EntityWeapon>();
var parent = GameServices.EntityManager.GetEntity(parentId);
entity.Hardpoint.Hardpoint = entityKey;
Vector3 off;
HardpointHelper.GetHardpointOffset(parent.GetComponent<RenderComponent>().Model, entityKey,
out off);
entity.Offset.Offset = off;
entity.Parent.ParentID = parent.Id;
entity.Position.Position = parent.GetComponent<PositionComponent>().Position + off;
entity.Rotation.Rotation = parent.GetComponent<RotationComponent>().Rotation;
Texture2D[] textures;
entity.Render.Model = ModelLoader.LoadModel(Model, GameplayScreen.Effect,
out textures);
entity.Render.Textures = textures;
}
protected string Model { get; set; }
}
public class ShieldArchetype : Archetype
{
public override void BuildEntity(string entityKey, ulong parentId)
{
var entity = GameServices.EntityManager.Create<EntityShield>();
entity.Parent.ParentID = parentId;
entity.Shield.Capacity = Capacity;
entity.Shield.ChargeRate = RechargeRate;
entity.PowerDraw.PowerDraw = PowerDraw;
}
protected int PowerDraw { get; set; }
protected int RechargeRate { get; set; }
protected int Capacity { get; set; }
}
public class EquipmentArchetypeManager
{
public enum WeaponType
{
Gun,
Missile,
Torpedo,
Mine,
Countermeasure
}
internal Dictionary<string, Archetype> Archetypes = new Dictionary<string, Archetype>();
public EquipmentArchetypeManager()
{
// Load Weapon Archetypes.
var weaponArchetypesRaw = string.Empty;
// loading weapon stuff, etc
///....
JsonConvert.DeserializeObject<List<WeaponArchetype>>(weaponArchetypesRaw)
.ForEach(archetype => Archetypes.Add(archetype.Nickname, archetype));
// Load shield Archetypes.
var shieldArchetypesRaw = string.Empty;
// loading weapon stuff, etc
///....
JsonConvert.DeserializeObject<List<ShieldArchetype>>(shieldArchetypesRaw)
.ForEach(archetype => Archetypes.Add(archetype.Nickname, archetype));
}
public void LoadEquipment(IDictionary<string, string> savedEquipment, ulong parentId) { foreach (var equipment in savedEquipment) { Archetypes[equipment.Value].BuildEntity(equipment.Key, parentId); } } }
Advantages:
- dictionary lookup for your archetypes
- separation of concerns
- can add new archetypes without needing to change LoadEquipment
Now, this is really just a first step. A better system again would be to follow Sandmans advice and flatten the hierarchy. Then you can start to move towards an
entity system.
HTH
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight