PageRenderTime 27ms CodeModel.GetById 17ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/Code/src/com/game/AI/TaskController.java

https://bitbucket.org/DeveloperUX/behaviortree
Java | 145 lines | 66 code | 16 blank | 63 comment | 0 complexity | 2536771d6322aa0579827b9db6ec68cf MD5 | raw file
  1package com.game.AI;
  2
  3/**
  4 * Class added by composition to any task, to keep track of the Task state
  5 * and logic flow. 
  6 * 
  7 * This state-control class is separated from the Task class so the Decorators
  8 * have a chance at compile-time security.
  9 * @author Ying
 10 *
 11 */
 12public class TaskController 
 13{
 14	/**
 15	 * Indicates whether the task is finished or not
 16	 */
 17	private boolean done;
 18	
 19	/**
 20	 * If finished, it indicates if it has finished with success or not
 21	 */
 22	private boolean sucess;
 23	
 24	/**
 25	 * Indicates if the task has started or not
 26	 */
 27	private boolean started;
 28	
 29	/**
 30	 * Reference to the task we monitor
 31	 */
 32	private Task task;
 33	
 34	/**
 35	 * Creates a new instance of the TaskController class
 36	 * @param task Task to controll.
 37	 */
 38	public TaskController(Task task)
 39	{
 40		SetTask(task);
 41		Initialize();
 42	}
 43	
 44	/**
 45	 * Initializes the class data
 46	 */
 47	private void Initialize()
 48	{
 49		this.done = false;
 50		this.sucess = true;
 51		this.started = false;
 52	}
 53	
 54	/**
 55	 * Sets the task reference
 56	 * @param task Task to monitor
 57	 */
 58	public void SetTask(Task task)
 59	{
 60		this.task = task;
 61	}
 62	
 63	/**
 64	 * Starts the monitored class
 65	 */
 66	public void SafeStart()
 67	{
 68		this.started = true;
 69		task.Start();
 70	}
 71	
 72	/**
 73	 * Ends the monitored task
 74	 */
 75	public void SafeEnd()
 76	{
 77		this.done = false;
 78		this.started = false;
 79		task.End();
 80	}
 81	
 82	/**
 83	 * Ends the monitored class, with success
 84	 */
 85	protected void FinishWithSuccess()
 86	{
 87		this.sucess = true;
 88		this.done = true;
 89		task.LogTask("Finished with success");
 90	}
 91
 92	/**
 93	 * Ends the monitored class, with failure
 94	 */
 95	protected void FinishWithFailure()
 96	{
 97		this.sucess = false;
 98		this.done = true;
 99		task.LogTask("Finished with failure");
100	}
101	
102	/**
103	 * Indicates whether the task finished successfully
104	 * @return True if it did, false if it didn't
105	 */
106	public boolean Succeeded() 
107	{
108		return this.sucess;
109	}
110	
111	/**
112	 * Indicates whether the task finished with failure
113	 * @return True if it did, false if it didn't
114	 */
115	public boolean Failed()
116	{
117		return !this.sucess;
118	}
119	
120	/**
121	 * Indicates whether the task finished
122	 * @return True if it did, false if it didn't
123	 */
124	public boolean Finished() 
125	{
126		return this.done;
127	}
128	
129	/**
130	 * Indicates whether the class has started or not
131	 * @return True if it has, false if it hasn't
132	 */
133	public boolean Started()
134	{
135		return this.started;
136	}
137	
138	/**
139	 * Marks the class as just started.
140	 */
141	public void Reset()
142	{
143		this.done = false;
144	}
145}