MicroQuest – Adding the Unit

by marc 2. July 2008 06:01

Now that the map is displaying in the way we want, the next step is to add some kind of representation of the player or unit.

This is going to require a little bit more plumbing, as we haven't actually created a Unit object yet. And it's probably about time to think about tying things together in a representation of the GameWorld.

I've created a simple GameWorld class which will create a Unit and a Map (a List<Location>) when instantiated. At this point I've also implemented a Singleton utility class (found in C# 3.0 Design Patterns). I'm sure I don't really need a Singleton, but this implementation makes things easy for the time being, and can be refactored later.

I'm going to expose this GameWorld as an ObjectDataProvider to the WPF application in a resource dictionary. Here is the ODP:

I'm now making a few decisions about the content of the WPF application too. Here I'm beginning to create a series of resource dictionaries, which I'm splitting by general concern. This makes things a little easier to navigate and understand as the number of objects, styles and templates increases.

These are then merged in App.xaml.

To render a Unit on screen, we can add some more XAML to the MapViewer control. Notice here that I'm overlaying a new Canvas on top of the main panel, and then adding the Unit to that. This is for convenience, but also some separation of concerns in case I decide to alter either of these parts (or use them separately). This canvas and its contents have IsHitTestVisibile=false as I am only interested in using the Locations (Tiles) for interactivity though it will look like the Unit is interactive.

The MapViewer now plots a Unit as follows.

A quick change to the logic for Unit means it will initialise at X, Y = 2,2. We can bind up the properties on Unit as follows:

And we can see the rendered result.

clip_image008

At this point I decided to refactor some of the MapViewer bindings to a series of constants which allow me to change the size of a tile, and the dimensions of the map as I wish without impacting on the rendering and interaction logic.

Here are a few useful constants:

And here is an example of a binding to enable that flexibility. This means I can amend my 'real' GameWorld to 50px tiles on a 16x12 grid just by changing these parameters.

Next time we'll make the unit move around.

Technorati Tags: , ,

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.4.5.0
Original Theme by Mads Kristensen and adjusted by me