Fun with XML

screen087

Tonight, I decided to go ahead and work on the Level Editor some more, so that I can start churning out some levels for a playable demo.  In Resistor, I used Mappy to generate arrays that I hardcoded as constants in my level class.  This probably wasn’t the best coding style, even though I’d bet it is faster than loading from disk.  In addition, I want players to be able to create their own levels in Blasting Bits, which means that having constant arrays is not an option.

screen085

To keep my data clean and organized, I decided to go with the XML format.  I was glad to find out that XNA has methods for handling reading XML files.  I found a good tutorial video by Richard Key of Paranoid Ferret Productions on how to do this.  I tried using this to load a String object from XML stored in my Content project area and it worked perfectly.  The “Boo” message that I entered into the XML file was loaded into the String variable I created using the Content.Load message, similar to a Sprite, Font, or Model.

screen086

However, I quickly realized that there is a problem when trying to create an XML data file for one of my own custom classes.  When trying to set the asset type to one of my classes, it is not able to find my class.  I created a simple Foo class with a Sting and int instance variables.  At first I thought the problem was due to my class not being public, but changing the scope of the class made no difference.  Doing a quick web search showed that this problem has been encountered before.  After digging deeper and consulting the video mentioned before, I believe that the reason why it can’t find my class is because the Content project doesn’t have a Reference defined to my main game project.  I tried adding a Reference, but it doesn’t allow this because it says that it would create a circular reference.  The only solution I was able to find is to create a third project just to hold the XML files.  This seemed really excessive, and this method probably won’t support loading XML files generated by the player so I found another approach.

screen083After finding a great example written by Microsoft, I was able to take  a new Foo object and serialize it.  In the example, it writes the XML to the Console.  Unfortunately, that isn’t very helpful aside from displaying the serialized object.  After some more trial and error, I found that instead of passing Console.Out to the Serialize method I could instead pass another Stream.  At first I tried using StreamReader and StreamWriter, but both of those require a filename, while I just want to store it as a String.  Finally, I found the StringWriter class, which takes no parameters and can hold the value of the serialized object.  The XML can be extracted using the ToString() method on the StringWriter object.  I was able to display the serialized data on the level editor screen.  From there, I just pass the String to my FileSaver class in ResistorKit, which handles opening and writing to the storage device.

screen084

That was enough to melt my brain for one night, so I will continue on later with reading the XML value back into my game object.  Last night I was able to get the remainder of my armor models (helmet, hands, boots, chest) to display in the game.  I also got the models for the basic collectible for money and the gun to display as well.  For now, I just used the second level helmet as the enemy model.  All of these models continually rotate during gameplay.

XNA Model Animation

https://www.youtube.com/watch?v=qndvb8N91j0

Demonstration of switching between 2D and 3D rendering, along displaying an animated Blender model in XNA

Today, I worked on getting my simple character model animation to display in the game world.  Currently, the player model displays fine, but it does not display the arms and legs moving as the player moves.  I found quite a few references for animating a Model, but none were very simplistic.  One thing in common with all of the examples is that model animations are not handled “out of the box” by XNA.  The technique used requires that the animation information be extracted from the model using the “Tag” property, which is sent to a custom Pipeline animation processor.  Two good examples of how to do this are the Microsoft SkinnedSample project and Michael Neel’s XNA 3D Primer.  Using these references, I was able to get my player model moving in a XNA game window.

screen081

However, I found that the method I was using to generate the sprite animations was not compatible with importing for 3D animations.  The process for preparing a Blender model for export to FBX format for XNA is nicely explained in this article by StormCode.  Using that information, I was able to use the Action Editor in Blender to create a named animation.  In the 2.6 version of Blender, this is located under the DopeSheet screen.  The article also gives valuable tips on how to create a keyframe containing all bone information and how to duplicate keyframes.  Previously, I used the record button in Blender to create keyframes, but that only records the bones that are actually changed.

After exporting my model to FBX format, I was able to import it into the SkinnedSample project.  I created new Model and Animation objects for my model.  That project is covered by the Microsoft Permissive License (Ms-PL), so I think I can use that code in my own project.  However, before I do that I want to compile it into its own library, so I can just link it and pass my models to it without having to add Microsoft’s code to my own project.  The only problem I’ve seen so far is that some polygons will sometimes incorrectly overlap each other, so that is something I will need to resolve.

screen082

While I was working on the game screen, I was able to resolve the issue with the model appearing so dark.  This was because the diffuse property on the mesh effect needed to be set to White.

Scaling Models

The enemy HP values have been removed from above the enemies on the game screen, and a damage value that floats over the enemy when hit has been added.  I created a new class for overlay text, which is used by both the 2D and 3D game screen.  This prevents the code from being duplicated for things like the status text and enemy damage.  I added a new property to the enemy that is the dying state and counter.  This was needed because the damage value does not display if the enemy is not alive.  This kept the last damage number from displaying.  Now I’ve changed it so that the damage value displays if the enemy is alive or dying, so the final damage number now correctly displays.  The dying counter will also be needed to display a death/blowup animation.  On the 2D game display, I added logic so that the enemy just fades away using an alpha color value based on the percentage of the dying animation passed.

screen080

I created a simple graphic for the projectile.  I mapped this graphic to a simple 2D plane in Blender.  It’s important to remember that all points of the Blender model must have a Z coordinate equal or less than 0 (non-positive) to appear.  This may be backwards for my game, since I am rendering the 3D world with the Y axis facing downward, which makes translating from the 2D to 3D coordinates simpler.  Remember, the model coordinates are relative to the small orange dot, which is not necessarily the location where the axes cross in Blender.  It is a good idea to put the orange dot at coordinate 0,0,0 in Blender to keep from getting confused.  Therefore, all vertices must be moved in edit mode (not object mode) for the changes to appear in the game.  Similarly, scaling an object in Blender’s object mode will have not effect on the model in the game.  The model must be scaled in edit mode with all vertices selected for the size change to appear in the game.

Also, I converted the List of loaded Model objects to a Dictionary object.  I thought that a Dictionary was the same as a hash table, but Chris Gardner (@freestylecoder) explained to me that Dictionaries don’t allow multiple values to be assigned to a key like a hash table.  He said that Dictionaries should be used like symbolic links, which is what I am doing with my constants which represent each model in the game.  The previous method I was using was kludgy and bad style, which was adding the Model to the List and setting the constant ID of that object to the order number which it was added to the list.  That process was a hassle to maintain and a recipe for disaster later.