tags:

views:

51

answers:

1

I've started on a small game where a player click around on a "map" to finish tasks. The user is only allowed to move the player X squares each click. How do I limit the player to not being able to move through blocks?

Right now, it is possible for the player to move through the filled area, but I don't want that to be possible, the user should only be able to move where there are no filled squares. (Ie. the green path.)

Currently I'm calculating the length of the vector between the start point and end point to see if the user can move that far, but i don't know how to check if there's a wall in the way, and then calculate the new length.

(The red path is the shortest way, the green path is the path I'm trying to force the player to walk. The M is the player.)
moving through a wall

+4  A: 

Sounds like a problem for Path Finding! But, umm, yea. Google "A* Path finding" Should get you headed in the right direction.

TJMonk15
+1 heuristic search.
Ed Woodcock
Thank you very much :) I have now created a working algorithm which finds the path, but how do I calculate the length of it? I'm not good with recursion :(
Phoexo
And, if someone else has the same question, i used this tutorial: http://www.policyalmanac.org/games/aStarTutorial.htm
Phoexo
Nvm, I figured it out :)
Phoexo