Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

117 Zeilen
2.8 KiB

  1. using System;
  2. using CIG;
  3. using UnityEngine;
  4. public abstract class BalloonFactory : ICanSerialize
  5. {
  6. public BalloonFactory()
  7. {
  8. this._lastCollectTimestamp = DateTime.MinValue;
  9. this._lastShownTimestamp = DateTime.MinValue;
  10. }
  11. public BalloonFactory(StorageDictionary storage)
  12. {
  13. this._storage = storage;
  14. this._lastCollectTimestamp = this._storage.GetDateTime("LastCollectDateTime", DateTime.MinValue);
  15. this._lastShownTimestamp = this._storage.GetDateTime("LastShownDateTime", DateTime.MinValue);
  16. }
  17. public virtual bool CanProduce()
  18. {
  19. return (DateTime.UtcNow - this._lastCollectTimestamp).TotalMinutes >= (double)this.CoolDownAfterCollectInMinutes && (DateTime.UtcNow - this._lastShownTimestamp).TotalMinutes >= (double)this.CoolDownAfterShowInMinutes;
  20. }
  21. public virtual int CoolDownAfterShowInMinutes
  22. {
  23. get
  24. {
  25. return 0;
  26. }
  27. }
  28. public virtual int CoolDownAfterCollectInMinutes
  29. {
  30. get
  31. {
  32. return 0;
  33. }
  34. }
  35. public Balloon ProduceBalloon()
  36. {
  37. if (!this.CanProduce())
  38. {
  39. UnityEngine.Debug.LogError("Requested to produce balloon while factory can't produce. Did you forget to check CanProduce?");
  40. return null;
  41. }
  42. Balloon balloon = this.CreateBalloonInstance();
  43. balloon.CollectedEvent += this.OnBalloonCollected;
  44. balloon.ExpiredEvent += this.OnBalloonExpired;
  45. this.UpdateLastShownBalloonTimestamp();
  46. return balloon;
  47. }
  48. protected abstract Balloon CreateBalloonInstance();
  49. private void UpdateLastCollectedBalloonTimestamp()
  50. {
  51. this._lastCollectTimestamp = DateTime.UtcNow;
  52. }
  53. private void UpdateLastShownBalloonTimestamp()
  54. {
  55. this._lastShownTimestamp = DateTime.UtcNow;
  56. }
  57. protected virtual void OnBalloonExpired(Balloon balloon)
  58. {
  59. balloon.CollectedEvent -= this.OnBalloonCollected;
  60. balloon.ExpiredEvent -= this.OnBalloonExpired;
  61. }
  62. protected virtual void OnBalloonCollected(Balloon balloon)
  63. {
  64. this.UpdateLastCollectedBalloonTimestamp();
  65. balloon.CollectedEvent -= this.OnBalloonCollected;
  66. balloon.ExpiredEvent -= this.OnBalloonExpired;
  67. }
  68. protected DateTime LastCollectedTimestamp
  69. {
  70. get
  71. {
  72. return this._lastCollectTimestamp;
  73. }
  74. }
  75. protected DateTime LastShownTimestamp
  76. {
  77. get
  78. {
  79. return this._lastShownTimestamp;
  80. }
  81. }
  82. public virtual StorageDictionary Serialize()
  83. {
  84. if (this._storage == null)
  85. {
  86. this._storage = new StorageDictionary();
  87. }
  88. this._storage.Set("LastCollectDateTime", this._lastCollectTimestamp);
  89. this._storage.Set("LastShownDateTime", this._lastShownTimestamp);
  90. return this._storage;
  91. }
  92. private DateTime _lastCollectTimestamp;
  93. private DateTime _lastShownTimestamp;
  94. private const string LastCollectDateTimeKey = "LastCollectDateTime";
  95. private const string LastShownDateTimeKey = "LastShownDateTime";
  96. private StorageDictionary _storage;
  97. }