I was looking in on Ziggyware and found this post by noobie01 and so decided to post my ideas on this. Now this is no big solution, it’s just a method to place “units” randomly on the screen and also how to “randomly” move them around.
Now the first part of the post was asking how to place a unit on the screen at a random location. In this sample this is how I am doing it. First thing I do is set up 4 floats to hold the minimum and maximum screen positions that are possible.
Then in the LoadContent method I set them.
I then randomly set the position of the unit.
GetRandomPosition() looks like this:
The next thing in the post was random movement. As you might have read in the post, I don’t think that this is really what is wanted, but I have done it here any was as a matter of completeness.
In the Update method of the sample, depending on the movement type selected (Default is random movement) I move the unit. So when in random movement the position of the object is altered like this:
This is a pretty clumsy movement method and was done out of haste really. A much better way to move any game avatar is by using a velocity which I will cover in a later related post. In fact the BaseDrawable2DObject that is used in this sample is a foundation of this up coming post.
Random Patrol Path
Now I think a much better and maybe useful method is for the unit to follow a patrol path. This is just a number of points on the screen that the unit will move to in turn. Now if you have any sense you will tie this in with your A.I. and depending on the various states you can send the unit to specified points. This is out of the scope of this simple sample, but I am sure you will get what I mean.
Anyway onto the patrol path stuff. We first need a list of patrol points as well as some way of marking what point we want to head for..
We now have a list of patrol points and a marker to denote the point we want to be moving to. So our movement code now looks like this:
I get the direction from the units position to the desired location, normalize it and then move in that direction (the * 4 is there to speed it up a bit). I then check if the unit has arrived at the required destination, if it has I move the pathTarget on one. I then see if my pathTarget is still in the range of the list of patrol points and if it is beyond that I set it back to 0.
To set the patrol path up I use the following method:
What this does is clear out the old list, then populate the list with between 4 to 8 patrol points, each point being randomly placed on the screen.
Here is the ClearPatrolPoints() method so you wont think I am over loading the Components collection :)
That is pretty much it, do forgive the clumsy movement mechanism, this was written to show the random position generation and patrol paths. In the next related post I intend to cover simple physics and sprite animation, this may end up being spread over a number of posts.
You can find the solution for this post here