Yes, I’m about three years late to this party, but so it goes.
The tool used to write HamQuest is Microsoft Visual Web Developer 2010 Express.
It has been since I moved to this tool last year.
Originally HamQuest was a WinForms application in Microsoft Visual C# 2005 Express, later moved to 2008, then switched to Silvelight, and finally moved into 2010.
Which means that much of the HamQuest codebase is the 2005 flavor of C#, and while certain areas of the codebase have had to morph as it migrated from one tool to another, most notably the change from XmlNodes to XElements in the move to Silverlight, a lot of code remained in the older style.
Now, a lot has changed in HamQuest itself.
For instance, the representation of the items, creatures, and terrain moved from hard coded arrays into xml configuration files, and in fact became very sophisticated with the user of reflection as time went on.
Unfortunately, the added flexibility of XML made for some painfully awful constructs in order to get information as simple as hitpoints.
For context, CreatureTable is a static class, with a static member function GetCreatureDescriptor, which naturally retrieves a descriptor for a creature based on the string CreatureIdentifier. From there, I use the generic method GetProperty to retrieve the GameConstants.Properties.Health(a const string containing “health”) into a type called IStatisticHolder, from which I can finally retrieve the value.
Ugly and long.
However, it would be so much nicer to do:
The problem is that the Descriptor class doesn’t know anything about the properties it holds. It just loads them from an XML file.
In fact, I don’t actually WANT the Descriptor class to know anything about the properties it holds, because the same Descriptor class is used for Creatures, Terrain, and Items.
And this is where extension methods come in.
I have a class called DescriptorExtenders (a static class), and to it I add the following static function:
public static int GetHealth(this Descriptor theDescriptor)
if(theDescriptor==null) return 0;
IStatisticHolder holder = theDescriptor.GetProperty<IStatisticHolder>(GameConstants.Properties.Health);
if (holder == null) return 0;
And blammo. I have my nifty GetHealth function, which I can use elsewhere in the code.
In addition, my extension method can do things like check for null, and return “safe” values if I want.
Some of the properties got really strange, especially for things like the Health example above.
The reason is that an enemy creature on the map needed to look the same as the player avatar.