Panzer Redux: WinRT
September 18, 2012 1 Comment
I was thinking of porting the Windows7 game that I created onto Windwos RT. Since they are both XAML based, I thought it would be a snap. I thought wrong. Part of the problem is that I wrote the original game with poor architecture. The other part of the problem is that Windows RT makes it hard to port existing code.
I first wanted to separate the game interface from the core game functions. I created a new project and brought in all of the non-graphical components:
The problem is that every actor seems to have a dependency on the Game class. For example:
So I need to break the dependencies between these factories and their implementation. I decided on Property Injection versus constructor injection as a matter of convention because I am not sure on the number of dependencies for each factory and I hate really long constructors. So my refactoring looks like:
public class ArmyFactory { public ScenarioFactory ScenarioFactory { get; set; } public UnitFactory UnitFactory { get; set; } public ArmyInfo CreateArmy(int scenarioId, int sideId) { ArmyInfo armyInfo = new ArmyInfo(); armyInfo.SideId = sideId; List<ScenarioUnit> scenarioUnits = ScenarioFactory.ScenarioUnitFactory.GetScenarioUnits(scenarioId); foreach (ScenarioUnit scenarioUnit in scenarioUnits) { int unitId = 0; if (scenarioUnit.SideId == sideId) { armyInfo.Units.Add(UnitFactory.CreateUnit(unitId, scenarioUnit)); unitId++; } } return armyInfo; } }
I then was confronted with the fact that my game engine has dependencies on Windows Phone:
Looking at the original code:
So I need a way of loading from an XML file – or do I? What if the EquipmentClass data was stored in something other than XML? Do I really want my factory breaking? I then looked at ScenarioTile – I had loaded data from either a web service or from an XML file.
I then realized that the factory is a translator –> In goes XML, SOAP, ADO.NET recordset, whatever, out always pops the instantiated objects. XML was used because that is the de-facto standard for Win Phones. I also see that my Lookup data is in XML. Therefore, what I need is a platform-agnostic XML Stream Reader. Unfortunately, that is not in the cards: Windows Phone has its implementation, System.IO has its implementation, and Windows 8 RT has its implementation. I then refactored the factory to take in an XML file of a certain structure and spit out the classes. All of the implementation that is platform specific I removed.
So where do I implement the actual XML load? In the calling application. Windows Phone 7.x uses XElement, WindowsRT does, and I have to assume Windows Phone 8 does too. I wonder if all platforms shared the same serialization implementation?
Ugh! looks like I picked the wrong week to quit drinkin…
You familiar with Portable Class Libraries?? http://www.hanselman.com/blog/HiddenGemsInVisualStudio11BetaNETPortableClassLibraries.aspx
What I’m planning on doing is putting as much of “the application” in there, and leave the platform-specific stuff in those platform-specific projects. So true, you can’t share I/O or serialization – but you can at least create your data structures/domain model in a portable place – and then use it from any other platform. Just a thought…