GameBase.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 Jiang Yin. All rights reserved.
  4. // Homepage: https://gameframework.cn/
  5. // Feedback: mailto:ellan@gameframework.cn
  6. //------------------------------------------------------------
  7. using GameFramework.Event;
  8. using UnityEngine;
  9. using UnityGameFramework.Runtime;
  10. namespace MetaClient
  11. {
  12. public abstract class GameBase
  13. {
  14. public abstract GameMode GameMode
  15. {
  16. get;
  17. }
  18. protected ScrollableBackground SceneBackground
  19. {
  20. get;
  21. private set;
  22. }
  23. public bool GameOver
  24. {
  25. get;
  26. protected set;
  27. }
  28. private MyAircraft m_MyAircraft = null;
  29. public virtual void Initialize()
  30. {
  31. GameEntry.Event.Subscribe(ShowEntitySuccessEventArgs.EventId, OnShowEntitySuccess);
  32. GameEntry.Event.Subscribe(ShowEntityFailureEventArgs.EventId, OnShowEntityFailure);
  33. SceneBackground = Object.FindObjectOfType<ScrollableBackground>();
  34. if (SceneBackground == null)
  35. {
  36. Log.Warning("Can not find scene background.");
  37. return;
  38. }
  39. SceneBackground.VisibleBoundary.gameObject.GetOrAddComponent<HideByBoundary>();
  40. GameEntry.Entity.ShowMyAircraft(new MyAircraftData(GameEntry.Entity.GenerateSerialId(), 10000)
  41. {
  42. Name = "My Aircraft",
  43. Position = Vector3.zero,
  44. });
  45. GameOver = false;
  46. m_MyAircraft = null;
  47. }
  48. public virtual void Shutdown()
  49. {
  50. GameEntry.Event.Unsubscribe(ShowEntitySuccessEventArgs.EventId, OnShowEntitySuccess);
  51. GameEntry.Event.Unsubscribe(ShowEntityFailureEventArgs.EventId, OnShowEntityFailure);
  52. }
  53. public virtual void Update(float elapseSeconds, float realElapseSeconds)
  54. {
  55. if (m_MyAircraft != null && m_MyAircraft.IsDead)
  56. {
  57. GameOver = true;
  58. return;
  59. }
  60. }
  61. protected virtual void OnShowEntitySuccess(object sender, GameEventArgs e)
  62. {
  63. ShowEntitySuccessEventArgs ne = (ShowEntitySuccessEventArgs)e;
  64. if (ne.EntityLogicType == typeof(MyAircraft))
  65. {
  66. m_MyAircraft = (MyAircraft)ne.Entity.Logic;
  67. }
  68. }
  69. protected virtual void OnShowEntityFailure(object sender, GameEventArgs e)
  70. {
  71. ShowEntityFailureEventArgs ne = (ShowEntityFailureEventArgs)e;
  72. Log.Warning("Show entity failure with error message '{0}'.", ne.ErrorMessage);
  73. }
  74. }
  75. }