#file = Leader.py
#John Whitham (me@jwhitham.plus.com) 2005

lastpos = None
lobj = None
fobj = None
leader = Prompt("Enter the name of the object to be followed.", \
  "",false,"Enter Leader Name")
follower = Prompt("Enter the name of the object with the follower Eco", \
  "",false,"Enter Eco Object")

def BirdFlock(k):
  Eco = GetEcosystemOnObject(fobj)
  rot = lobj.GetRotationAngles()
  newpos = lobj.Position()
  xdiff = newpos[0] - lastpos[0]
  ydiff = newpos[1] - lastpos[1]
  zdiff = newpos[2] - lastpos[2]
  for boid in range(0,Eco.GetInstanceCount()):
    Eco.SetInstanceRotationV(boid,rot)
    if (CurrentFrame() > 0):  
      Eco.MoveInstance(boid,xdiff,ydiff,zdiff)
  global lastpos
  lastpos = newpos

if (ObjectByName(leader)!=None and ObjectByName(follower)!=None):
  lobj = ObjectByName(leader)
  lastpos = lobj.Position()
  fobj = ObjectByName(follower)
  fobj.SetInitFrameCallback(BirdFlock)
else:
  Message("Wrong","Try Again",1)
