GameEventManager.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // GameEventManager.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Text;
  13. using Microsoft.Xna.Framework;
  14. using Microsoft.Xna.Framework.Graphics;
  15. using RobotGameData;
  16. #endregion
  17. namespace RobotGameData.GameEvent
  18. {
  19. /// <summary>
  20. /// It supports the time and area event, which is needed by the game,
  21. /// and manages the registered events.
  22. /// GameTimeEvent class, after a specific amount of time,
  23. /// activates the registered scene object (enable and visible).
  24. /// GameAreaEvent class activates the registered scene object (enable and visible)
  25. /// when a target object comes within a specific area.
  26. /// </summary>
  27. public class GameEventManager
  28. {
  29. #region Fields
  30. bool enable = true;
  31. GameSceneNode targetScene = null;
  32. List<GameEventBase> gameEventList = new List<GameEventBase>();
  33. #endregion
  34. #region Properties
  35. public bool Enable
  36. {
  37. get { return enable; }
  38. set { enable = value; }
  39. }
  40. public GameSceneNode TargetScene
  41. {
  42. get { return targetScene; }
  43. set { targetScene = value; }
  44. }
  45. #endregion
  46. /// <summary>
  47. /// Update all events in the list
  48. /// </summary>
  49. public void Update(GameTime gameTime)
  50. {
  51. if (enable == false) return;
  52. if (gameEventList.Count > 0)
  53. {
  54. // Update game events
  55. for (int i = 0; i < gameEventList.Count; i++)
  56. {
  57. GameEventBase gameEvent = gameEventList[i];
  58. // Every "gameEvent" keeps getting updated and gets put on a hold
  59. // until the action is executable.
  60. if (gameEvent.IsWatingAction )
  61. {
  62. gameEvent.Update(gameTime);
  63. if (gameEvent is GameTimeEvent)
  64. {
  65. GameTimeEvent timeEvent = gameEvent as GameTimeEvent;
  66. // If the status is in executable position,
  67. // each event will be executed.
  68. if (timeEvent.IsExecuteAction() )
  69. {
  70. // Executes the time event
  71. timeEvent.ExecuteAction();
  72. }
  73. }
  74. else if (gameEvent is GameAreaEvent)
  75. {
  76. GameAreaEvent areaEvent = gameEvent as GameAreaEvent;
  77. // If the status is in executable position,
  78. // each event will be executed.
  79. if (areaEvent.IsExecuteAction(targetScene.Position) )
  80. {
  81. areaEvent.ExecuteAction();
  82. }
  83. }
  84. }
  85. }
  86. }
  87. }
  88. /// <summary>
  89. /// Adds a new game event to the event manager
  90. /// </summary>
  91. public void AddEvent(GameEventBase gameEvent)
  92. {
  93. gameEventList.Add(gameEvent);
  94. }
  95. /// <summary>
  96. /// Removes a game event from the event manager
  97. /// </summary>
  98. public void RemoveEvent(GameEventBase gameEvent)
  99. {
  100. gameEventList.Remove(gameEvent);
  101. }
  102. /// <summary>
  103. /// Removes a game event by the index number from the event manager
  104. /// </summary>
  105. public void RemoveEvent(int index)
  106. {
  107. gameEventList.RemoveAt(index);
  108. }
  109. /// <summary>
  110. /// Clear all game events from the event manager
  111. /// </summary>
  112. public void ClearAllEvent()
  113. {
  114. gameEventList.Clear();
  115. }
  116. }
  117. }