Jump to content

  • Log In with Google      Sign In   
  • Create Account

#Actualeppo

Posted 16 December 2012 - 04:46 PM

If a creature object has no concept of if or where it is in a list, you'll have to do a linear search over all the list's elements until you find it, before you can remove it. If you have a lot of creatures, this might be too expensive. If you quickly and immediately need to remove an object, the following isn't too bad:

class Grid{

     class Cel{ List<Animal> animals; }

     Cel cels[][];
}

class Animal{

     Grid::Cel *cel;

     setPos(Coord pos){ cel->remove(this); (cel = grid->celGet(pos))->add(this); }
}

Unless even a single cel-list becomes too long to traverse, in which case you could make the animal point directly to a container object that encapsulates a pointer to itself.

#1eppo

Posted 16 December 2012 - 04:44 PM

If a creature object has no concept of if or where it is in a list, you'll have to do a linear search over all the list's elements until you find it, before you can remove it. If you have a lot of creatures, this might be too expensive. If you quickly and immediately need to remove an object, the following isn't too bad:

class Grid{

     class Cel{ List<Animal> animals; }

     Cel cels[][];
}

class Animal{

     Grid::Cel *cel;

     setPos(Coord pos){ cel->remove(this); (cel = grid->celGet(pos))->add(this); }
}

PARTNERS