View Javadoc

1   package cz.cuni.amis.pogamut.udk.agent.navigation;
2   
3   import java.util.List;
4   
5   import cz.cuni.amis.pogamut.base.agent.navigation.IPathExecutorHelper;
6   import cz.cuni.amis.pogamut.base3d.worldview.object.ILocated;
7   import cz.cuni.amis.pogamut.udk.bot.impl.UDKBot;
8   import cz.cuni.amis.pogamut.udk.communication.messages.gbinfomessages.Self;
9   import cz.cuni.amis.utils.exception.PogamutException;
10  
11  /**
12   * Simple stub of the {@link IUDKPathNavigator} that implements {@link AbstractUDKPathNavigator#setBot(UDKBot)} and 
13   * {@link AbstractUDKPathNavigator#setExecutor(IPathExecutorHelper)}.
14   * 
15   * @author Jimmy
16   *
17   * @param <PATH_ELEMENT>
18   */
19  public abstract class AbstractUDKPathNavigator<PATH_ELEMENT extends ILocated> implements IUDKPathNavigator<PATH_ELEMENT>{
20  
21  	/**
22  	 * Bot, the navigator is navigating. Set by {@link AbstractUDKPathNavigator#setBot(UDKBot)}.
23  	 * See {@link IUDKPathNavigator#setBot(UDKBot)}.
24  	 */
25  	protected UDKBot bot;
26  	
27  	/**
28  	 * Executor who is using the navigator. See {@link IUDKPathNavigator#setExecutor(IPathExecutorHelper)}
29  	 * for more info.
30  	 */
31  	protected IPathExecutorHelper<PATH_ELEMENT> executor;
32  
33  	/**
34  	 * {@link Self} object that is lazy-initialized inside {@link AbstractUDKPathNavigator#self}.
35  	 */
36  	protected Self self;
37  	
38  	@Override
39  	public void setBot(UDKBot bot) {
40  		this.bot = bot;
41  	}
42  
43  	@Override
44  	public void setExecutor(IPathExecutorHelper<PATH_ELEMENT> owner) {
45  		this.executor = owner;		
46  	}
47  	
48  	@Override
49  	public void navigate() {
50  		if (bot == null) throw new PogamutException("The 'bot' field is null (or was not set by the executor), can't navigate.", this);
51  		if (executor == null) throw new PogamutException("The 'executor' field is null (ow was not set by the executor), can't navigate.", this);
52  		// check that we're running in correct context
53  		int pathElementIndex = executor.getPathElementIndex();
54  		if (pathElementIndex < 0 || pathElementIndex >= executor.getPath().size()) throw new PogamutException("Can't navigate as the current path element index is out of path range (index = " + pathElementIndex + ", path.size() = " + executor.getPath().size() + ".", this);
55  		if (self == null) {
56  			self = bot.getWorldView().getSingle(Self.class);
57  			if (self == null) throw new PogamutException("Can't navigate the bot, no Self instance is available in the world view.", this);
58  		}
59  		// fire the navigation
60  		navigate(pathElementIndex);
61  	}
62  		
63  	/**
64  	 * Does the actual navigation of the bot, it should steer it towards path element of the index 'pathElementIndex'.
65  	 * Called (after several checks) from {@link AbstractUDKPathNavigator#navigate()}.
66  	 * @param pathElementIndex
67  	 */
68  	protected abstract void navigate(int pathElementIndex);
69  
70  }