by Marc
16. July 2008 13:11
In this exciting instalment of MicroQuest activity, we're finally going to do something useful and make the Unit move around on the map, using the pathfinding code from a while ago.
We need to get our coding fingers on a little this time.
The interaction we're trying to achieve is as follows:
- User clicks on a Location containing the Unit.
- As User moves his mouse over other Locations, a "best path" to the Location is shown (subject to the constraints of the maximum distance the Unit can move, and the obstacles on the map).
- The next click will then animate the Unit along that path.
So we need 2 event handlers in MapViewer - Click and MouseEnter. We also need a way of rendering the path on screen, and a reusable animation.
To do this, I've added a PathGeometry, and a Storyboard to animate X and Y values against that PathGeometry to the resources of MapViewer:
I've then added a Path to the Unit canvas:
With that done, I can write some logic for the Click and MouseEnter events for a Location.
The Click event is either going to start pathfinding, or complete the move. I've broken out this handler as I imagine I'm going to have a series of handlers depending on the Game state (which we need to think about later).
My main issue with this function is that I haven't got the binding working between the animation and the Unit so I have to manually tell the Unit to move. I'll think about that later.
The MouseEnter function is responsible for generating paths (using AStar) if appropriate. It looks as follows:
I've hardcoded the "3" as a maximum movement cost here and will parameterise later. Finally, a convenience method performs a conversion on the output of the AStar solve to power the path rendering.
And there we have it. Finally we can move the unit around on the map.

There's tons more we can do with the map and it's interactivity. We'll think about that in the next article.
Technorati Tags:
microquest,
wpf,
game