MicroQuest – Making a Real Map

by Marc 13. August 2008 15:25

    Our test map is a bit boring and doesn't really give us the opportunity to test out the pathfinding and see it working in practice (if it does...)

    In this instalment, we're going to load up some XML representing a map using LINQ and then amend our WPF to render the tiles according to the location content.

    Our previous test map constructed a bunch of identical tiles in a loop. Our real map will have new types of locations that may or may not be passable and will be rendered differently on screen.

    At this point, our Location object isn't very complicated, so it's easy to represent as XML. Here is an example:

    This is a hefty manual task (well, it's a lot of copy and pasting, and some biro on a notepad), so a note to myself is to build some kind of Game Editor in the near future.

    Loading and creating the map is straightforward:

    Notice that I'm not using any kind of inheritance hierarchy for Location at the moment. This will likely change over time and we can adjust later.

    We've already created a template for the button that represents a tile (giving it the green appearance). Amending that style to respond to the 'TileType' of a Location involves some DataTriggers (and some cack-handed colour selections).

    Cool. Now the map looks like the next screen shot where I've added a Lake, some Mountains with a Forest and a Beach (You'll have to use your imagination). Notice that because the 'water' tiles are marked as IsPassable=false then the pathfinding will move us around, but not through the water. (The same is true of the 'mountain' tiles). I increased the movement to 4 so you can see the effect.

    Interesting enough, but we can get smarter with some further enhancements to the map.

    The first thing I want to do is make it easier for a user to understand where they can move to in a single turn, rather than having to move the mouse around to any given location. User experience an' all that...

    In this case, I'll add new properties to Location and a method to figure this stuff out.

    The property on the Location will be IsReachable and will be changed by the Map object using the following methods:

    So the main logic here is that I'm trimming down the search set to a minimum size (those Locations within a possible Manhattan Distance) and then performing an AStar.Solve on that set.

    These methods are called during the relevant interactions on the map. (I've also refactored IHCostStrategy to use an IMappable interface (the presence of X and Y properties) so that I can reuse it here easily. Location and Node now implement IMappable.)

    A quick addition of a new Rectangle to the Tile template and binding its Visibility to the IsReachable property of Location (using the built in BooleanToVisibilityConverter) gets us most of the way to making this work.

    This is actually the first time we've encountered a situation where the UI needs to reflect a Property change. So we need to implement INotifyPropertyChanged to do this. I've created a base object - GameObject - for that purpose and then had Location inherit GameObject.

    Annoyingly, because I want to call OnPropertyChanged, I can't use the {get;set;} convenience syntax. But the result is that the user can now see where they are able to move to before they move represented by the slightly lighter tiles. Sweet.

    This has become a longish post, so I'll update the map a little more another time. I also need to start thinking about some of the other game concepts. Like... what does the unit do on the map?

Technorati Tags: ,

Tags:

Development

blog comments powered by Disqus