Sitting

From AI Wiki

Jump to: navigation, search

It used to be a lot of work to make an Iam sit properly. While it is still not entirely trivial to do it right, it has become a lot easier. The main thing required for sitting to work is that the map has a list of chairs. In the XML file that configures your Iam you may already have noticed the second parameter to the SparseMap class passing in the "objectlist.xml". This file is created by the City Editor, see the section Creating a city map for more details. In the future this XML file may contain more types of objects, but at least for now it only contains chairs. The way for your Iam to access this list is to call getMap().getObjectList() which returns List<BMObject>. Making your Iam sit on the first of the chairs in this list, you simply execute the following few lines of code:

List<BMObject> chairList = getMap().getObjectList();
sit(chairList.get(0));

This is not exactly exemplary code. The first chair may be located who knows where. So ideally you'll want to make sure you're located near to it before you do this. This is easy to do by getting the location of the BMObject. At some point we may change the IamFramework to disallow sitting in chairs more than a certain distance away. But for now there are no restrictions.

Standing up from a chair is easy too, you just call sit(null);

Calling the sit() function will make your Iam sit, but it will sit completely still. If you'd like it to shift in its chair and such, you'll have to specifically call these animations from time to time. The IamLibrary has a solution that will reduce the amount of work you have to do for this. Instead of calling sit, you add the SittingBehavior to your Iam in its XML file and you make the following calls instead of sit():

List<BMObject> chairList = getMap().getObjectList();
BMObject chair = sit(chairList.get(0));
SittingBehavior sittingBehavior = (SittingBehavior)getDecisionUnit().getBehavior(SittingBehavior.NAME);
if (sittingBehavior!=null)
	sittingBehavior.sitDown(chair);

Now you Iam will not only sit, but shift around the chair a little bit from time to time.

Personal tools