I'm working on a type of tower defense game and I need the enemies to move across the map towards a castle. But they can't move through any walls, over any mountains etc. I know there's a way to do it but the scripts are astronomically large. Is there a way to do this.
Offline
Well, the best way to do this is by mapping it out manually. Here is an example:
repeat (240) change x by (1) end repeat (20) change y by (1) end repeat (180) change x by (1) end repeat (20) change y by (-1) end repeat (60) change x by (1) endIf your path needs to be AI, computing the shortest unblocked way, I have a few questions:
Offline
Well you could have a preset list of directions in a format
direction
steps
direction
steps...
and then a script
set [index v] to [1] go to x:(start x) y:(start y) point in direction (start direction) repeat (length of [path list v]) turn left (item (index) of [path list v]) degrees change [index v] by (1) move (item (index) of [path list v]) steps change [index v] by (1) endand then to generate the paths, make a sprite shaped like an arrowhead and a local variable called current action and do
when gf clicked delete (all v) of [path list v] set [current action v] to [turn] add [0] to [path list v] go to x:(start x) y:(start y) point in direction (start direction) when key [up arrow v] pressed move (1) steps if <(current action) = [move]> replace item (last v) of [path list v] with ((item (last v) of [path list v]) + (1)) else set [current action v] to [move] add [1] to [path list v] end when key [down arrow v] pressed move (-1) steps if <(current action) = [move]> replace item (last v) of [path list v] with ((item (last v) of [path list v]) - (1)) else set [current action v] to [move] add [-1] to [path list v] end when key [left arrow v] pressed turn left (1) degrees if <(current action) = [turn]> replace item (last v) of [path list v] with ((item (last v) of [path list v]) + (1)) else set [current action v] to [turn] add [1] to [path list v] end when key [right arrow v] pressed turn right (1) degrees if <(current action) = [turn]> replace item (last v) of [path list v] with ((item (last v) of [path list v]) - (1)) else set [current action v] to [turn] add [-1] to [path list v] endYou can delete this sprite after the path has bees generated, or just hide it and detach the when key pressed blocks from their scripts so you could use it again later.
Offline
I also like the list suggestion. You could also expand it to make it more advanced by allowing enemies of different speeds.
Use this simple equation to work out the time taken by a sprite of a known speed over a known distance - time = distance/speed.
So:
1) Add all your positions for the AI to a list, add them as x position, y position.
(In this example, my points are (150, 0), (20,40), (0,0)...)
add (150) to [list v] add (0) to [list v] add (20) to [list v] add (40) to [list v] add (0) to [list v] add (0) to [list v]2) Now, we'll need to move to the next point. You might also want the enemy to point towards the position that they're moving to. For that, see this tutorial on how to make a sprite point towards a co-ordinate.
set [counter v] to (1) set x to (item (counter) of [list v]) set y to (item ((counter) + (1)) of [list v]) Change [counter v] by (2) Repeat (((length of [list v]) / (2)) - (1)) Set [distance v] to (Sqrt((((X position) - (item (counter) of [list v]) * (X position) - (item (counter) of [list v])) + ((Y position) - (item ((counter) + (1)) of [list v]) * (Y position) - (item ((counter) + (1)) of [list v]))))) Glide ((distance) / (speed)) secs to x: (item (counter) of [list v]) y: (item ((counter) +(1)) of [list v]) change [counter v] by (2) end
Last edited by Prestige (2012-08-01 16:27:45)
Offline
Prestige wrote:
set [counter v] to (1) set x to (item (counter) of [list v]) set y to (item ((counter) + (1)) of [list v]) Change [counter v] by (2) Repeat (((length of [list v]) / (2)) - (1)) Set [distance v] to ([sqrt v] of ((((X position) - (item (counter) of [list v])) * ((X position) - (item (counter) of [list v]))) + (((Y position) - (item ((counter) + (1)) of [list v])) * ((Y position) - (item ((counter) + (1)) of [list v]))))) Glide ((distance) / (speed)) secs to x: (item (counter) of [list v]) y: (item ((counter) +(1)) of [list v]) change [counter v] by (2) end
fixed
Offline
I have implemented a pathing algorithm in the following game: http://scratch.mit.edu/projects/BoltBait/1897019
I wrote it from the description given in this post: http://scratch.mit.edu/forums/viewtopic.php?id=35318
Hope this helps.
Offline