/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

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