2013-12-18

Mouse control

Latest by now it starts getting obvious to me that pasting code here isn't the best way to go. So from now on, the code will be shown as GitHub Gists which seem to be pretty awesome.

This post is going to be a short one anyway; it's been quite busy lately so I haven't had too much time to play around with this. On the good news, I have used Clojure a bit outside this project and I hope that in future my code starts becoming more and more Clojurish instead of trying to write functional code without actually writing functional code...

First of all, the character data map was modified so that all movement information is now stored in a separate map as shown below. Makes the top-level map a bit more structured. I also added couple of helper functions to convert between degrees and radians:


Next up is a function that creates and returns such new movement map based on a mouse click (or touchscreen touch). The calculation is in theory very simple: just get the direction of the movement by taking atan from the delta coordinates. The complexity comes from the fact that atan doesn't return values on the range of 0 to 360; we have to decide the final direction based on the relation of the touch point and our current location.


The get-movement function is then extended to check for a touch event, convert the touch coordinates to the camera's coordinate system, and call for the previous get-movement-from-touch function to update the movement struct. In addition, we now use a target-matches-location? helper function (shown above) to decide whether we've reached our destination or not.


Also the movement function is updated to use the new map structure:


Okay, that's it for now. I know that I'm not explaining things in a lot of detail here; hopefully there's more time in the future (assuming that someone is actually sometimes reading these posts.. :) ). Next up is hopefully the already-mentioned A* for the movements. At the moment the guy just walks towards the target until the target is reached or the dude hits a wall. Not the best possible solution.