PageRenderTime 17ms CodeModel.GetById 12ms app.highlight 2ms RepoModel.GetById 2ms app.codeStats 0ms

/Code/src/com/game/Scenes/Scene.java

https://bitbucket.org/DeveloperUX/behaviortree
Java | 101 lines | 41 code | 15 blank | 45 comment | 2 complexity | 10e304bd20ec5587e49b6ad1f789cdf9 MD5 | raw file
  1package com.game.Scenes;
  2
  3import com.game.MessageHandler;
  4import com.game.MsgType;
  5import com.game.MessageHandler.MsgReceiver;
  6
  7import android.app.Activity;
  8import android.os.Handler;
  9
 10/**
 11 * Base class for any scene in the game. Scenes are owned and controlled by the 
 12 * scene manager.
 13 * 
 14 * @author Ying
 15 *
 16 */
 17public abstract class Scene 
 18{
 19	/**
 20	 * Handler for messages to the scene
 21	 */
 22	protected Handler handler = null;
 23	
 24	/**
 25	 * Used to ensure thread safety on scene shutdown
 26	 */
 27	protected boolean 	runScene;
 28	
 29	/**
 30	 * Used to ensure thread safety on scene shutdown
 31	 */
 32	private boolean 	stopScene;
 33	
 34	/**
 35	 * Reference to the activity to load resources.
 36	 */
 37	protected Activity refActivity = null;
 38	
 39	/**
 40	 * Initializes variables
 41	 */
 42	public Scene()
 43	{
 44		runScene = true;
 45		stopScene = false;
 46	}
 47	
 48	/**
 49	 * Override in the base class. Called on scene start.
 50	 */
 51	public abstract void Start();
 52	
 53	/**
 54	 * Override in the base class. Called each update cycle of the game logic.
 55	 */
 56	public abstract void Update();
 57	
 58	/**
 59	 * Override in the base class. Called on scene end.
 60	 */
 61	public abstract void End();
 62	
 63	/**
 64	 * Gets the message handler
 65	 * @return message handler.
 66	 */
 67	public Handler getHandler(){return handler;}
 68	
 69	
 70	/**
 71	 * Updates the scene only if the activity has not asked us to stop.
 72	 * If we have been asked to stop, notify the activity (only once, remember
 73	 * that until the scene is swapped this code is called each frame) when
 74	 * we are ready.
 75	 */
 76	public void safeUpdate()
 77	{
 78		if(runScene)
 79		{
 80			this.Update();
 81		}
 82		else
 83		{
 84			if(!stopScene)
 85			{
 86				stopScene = true;
 87				MessageHandler.Get().Send(MsgReceiver.ACTIVITY, MsgType.SCENE_STOPED_READY_FOR_CHANGE);				
 88			}
 89		}
 90	}
 91
 92	/**
 93	 * Sets the reference to the activity
 94	 * @param refActivity
 95	 */
 96	public void setRefActivity(Activity refActivity) 
 97	{
 98		this.refActivity = refActivity;
 99	}
100	
101}