I have too many of my own projects going to be your volunteer, but I can tell you how I would program this.
So you have all these monsters running around on the map, right? Each of them should have an NPC component, even if you are going to be using slices.
The NPC holds the x,y position of the monster, and its direction.
When you load the map, you create a slice for each monster, and for each slice you store the NPC reference of the NPC that it belongs to in the slices' extra data.
You use "set parent" to attach the slices to the map same map layer the NPCs walk on.
Now you need a loop that looks like this:
Code:
script, monster update loop, begin
variable(sl)
sl := first child(lookup slice(sl:map layer 1))
while(sl) do(
update monster(sl)
sl := next sibling(sl)
)
set timer(1, 0, 1, @monster update loop)
end
This script loops through all the slices that you have attached to map layer 1, and runs the update monster script on each one. Notice that it is looping using a 1-tick timer instead of a "wait" command. Other scripts will not be blocked waiting for this cript to finish, and it will not be blocked waiting for other scripts (yay for timers!)
Update monsters should look something like this:
Code:
script, update monsters, sl, begin
variable(npc)
# Get this slice's NPC reference.
npc := get slice extra(sl, extra 0)
# update the position of this slice to match the NPC
set slice x(NPC pixel x(npc))
set slice y(NPC pixel y(npc))
switch(get NPC id(npc)) do(
case(0) do(axe mook ai(sl, npc))
case(1) do(giant mook ai(sl, npc))
case(2) do(skeleblob ai(sl, npc))
case(3) do(demon puppy ai(sl, npc))
case(4) do(grim weedwhacker ai(sl, npc))
case(5) do(baconthulhu ai(sl, npc))
)
end
So each NPC type has its own AI script. For the actual AI scripts, I suggest a "finite state machine"
You can use the other two slice extra values for state. The NPC extra values are usable too, so you can have things like HP or Magic or counters, and stuff.
Code:
script, axe mook ai, sl, npc, begin
variable(state)
# Get this monster's state
state := get slice extra(sl, extra 1)
# handle each possible state for this monster
switch(state) do(
case(0) do( # wandering around
monster wandering(sl, npc)
if(within distance from player(npc, 5)) then(
# start dashing
set slice extra(sl, extra 1, 1)
)
)
case(1) do( # start dashing
monster faces player(sl, npc)
# dash now
set slice extra(sl, extra 1, 2)
)
case(2) do( # dash now
if(not(within distance from player(npc, 6))) then(
# dash is over, back to wandering
set slice extra(sl, extra 1, 0)
)else(
monster dashing(sl, npc)
)
)
)
end
script, monster wandering, sl, npc, begin
# re-use this script for any monster that has a randomly wandering AI state
if(NPC is walking(npc) == false) then(
if(random(1, 10) <3>> NPC y(npc)) then(
set NPC direction(npc, down)
)
if(hero y(me) <<NPC>> NPC x(npc)) then(
set NPC direction(npc, right)
)
if(hero x(me) << NPC x(npc)) then(
set NPC direction(npc, left)
)
end
script, within distance from player, npc, dist, begin
# return true if the NPC is within a certain number of tiles
# from the hero
variable(diff x, diff y)
diff x := abs(hero x(me) -- NPC x(npc))
diff y := abs(hery y(me) -- NPC y(npc))
exit returning(diff x ^ 2 + diff y ^ 2 <= dist ^ 2)
end
# this script for absolute value will be a builtin in the future
# but it isn't yet, so we need it here now :P
script, abs, v, begin
if(v << 0) then(exit returning(v * -1))
exit returning(v)
end
Actually, that got pretty complicated pretty fast... but notice that a lot of that can be re-used for your other AI scripts.
Also, I left out all the stuff where you update the sprite slice to show the current frame of monster animation based on which way it is facing and what it is currently doing. You could spread that animation around, but I think it would be best to keep it all together at the very end of the AI script. Do all the AI first, then do all the animation updating last.
Also remember not to use any waits in the AI scripts. Each AI script is going to be re-run every tick. If you need the monster to do something for a while, you need another AI state for it. Like the laugh animation for example. That would need another AI state.