In my first attempt, I simply created the segments of the snake and ordered each one to walk_towards the one in front of it. This kinda worked, but the segments didn't follow the original path of the head. If they were arranged vertically, and the head stepped southeast, they would all step southeast.
So I am trying to devise a way to do it manually, and I'm having a little trouble. Here's the system I have right now:
snake
Move()
if(text == "<FONT COLOR=#0A0>@</FONT>" && back)
var/oldloc = loc
var/oldloc2
..()
if(oldloc != loc)
oldloc2 = back.loc
walk_towards(back,oldloc,0)
var/check = 0
var/swix = 0
var/mob/newback
if(back.back)
newback = back.back
check = 1
while(check)
check = 0
if(swix)
oldloc2 = newback.loc
newback.loc = oldloc
else
oldloc = newback.loc
newback.loc = oldloc2
if(newback.back)
check = 1
newback = newback.back
swix = ~ swix
And it's bad, man. Real bad. In case you didn't guess, "back" is a variable that stores the next segment in the snake. So why is it bad?
1. It's laggy. When the heads run into obstacles they start using pathfinding, and it seems to lag out when multiple snakes get tangled up with each other and the map.
2. It's buggy. I think I got one of the snakes to move all segments to the same space as the head mob.
3. It's crappy. I just feel terrible about this code, I want it to be sleek and sexy but instead it is fat and sassy.
I was toying with the idea of somehow keeping track of the moves done by the head mob, and using that in conjunction with each segment's position in the snake, to step() them all in the right direction. But I haven't really thought that one out yet...maybe you think it's a good idea? Or should I take a different approach altogether?
The head moves and the segment tracking it moves to its previous location. The segment tracking the first one moves to the 1st ones previous location, and so on. You'll need to keep track of the previous step_x and step_y and adjust this as well, if using pixel movement.
Though all of this may just sound better in my head.