您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

144 行
4.3 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using CIG3.ExtensionMethods;
  4. using UnityEngine;
  5. public class IAPPack : IPack
  6. {
  7. private IAPPack(string name, IList<IPackEffect> items)
  8. {
  9. this._name = name;
  10. this._items = new List<IPackEffect>(items);
  11. }
  12. public static IAPPack Create(string packName, Dictionary<string, object> values)
  13. {
  14. if (values == null || values.Count == 0)
  15. {
  16. UnityEngine.Debug.LogError(string.Format("Can't Create IAPPack with no values '{0}'.", packName));
  17. return new IAPPack(string.Empty, new List<IPackEffect>());
  18. }
  19. IList<string> packItemNames = IAPPack.GetPackItemNames(values);
  20. List<IPackEffect> list = new List<IPackEffect>();
  21. foreach (string text in packItemNames)
  22. {
  23. IPackEffect packEffect = IAPPack.CreateItemFromName(text, values);
  24. if (packEffect == null)
  25. {
  26. UnityEngine.Debug.LogError(string.Format("Can't create an item of type '{0}'.", text));
  27. }
  28. else
  29. {
  30. list.Add(packEffect);
  31. }
  32. }
  33. return new IAPPack(packName, list);
  34. }
  35. public IList<IPackEffect> Items
  36. {
  37. get
  38. {
  39. return this._items.AsReadOnly();
  40. }
  41. }
  42. public string Name
  43. {
  44. get
  45. {
  46. return this._name;
  47. }
  48. }
  49. public void ApplyEffects()
  50. {
  51. foreach (IPackEffect packEffect in this._items)
  52. {
  53. packEffect.ApplyEffect();
  54. }
  55. }
  56. public override string ToString()
  57. {
  58. string text = string.Empty;
  59. foreach (IPackEffect packEffect in this._items)
  60. {
  61. text = text + packEffect.ToString() + "\n";
  62. }
  63. return text.Trim();
  64. }
  65. public PackItemType FindPackItem<PackItemType>() where PackItemType : class, IPackEffect
  66. {
  67. foreach (IPackEffect packEffect in this.Items)
  68. {
  69. PackItemType packItemType = packEffect as PackItemType;
  70. if (packItemType != null)
  71. {
  72. return packItemType;
  73. }
  74. }
  75. return (PackItemType)((object)null);
  76. }
  77. private static IList<string> GetPackItemNames(Dictionary<string, object> values)
  78. {
  79. Dictionary<string, object> dictionary = values.GetValue<object>("PackItems", null) as Dictionary<string, object>;
  80. if (dictionary == null)
  81. {
  82. return new List<string>();
  83. }
  84. return new List<string>(dictionary.Keys);
  85. }
  86. private static IPackEffect CreateItemFromName(string name, Dictionary<string, object> values)
  87. {
  88. Dictionary<string, object> itemProperties = IAPPack.GetItemProperties(name, values);
  89. if (name == "FreeBuilding")
  90. {
  91. return new FreeBuildingItem(itemProperties);
  92. }
  93. if (name == "TimeBoost")
  94. {
  95. return new TimeBoostItem(itemProperties);
  96. }
  97. if (name == "LevelUp")
  98. {
  99. return new LevelUpItem(itemProperties);
  100. }
  101. if (name == "Cash")
  102. {
  103. return new CashItem(itemProperties);
  104. }
  105. if (name == "Gold")
  106. {
  107. return new GoldItem(itemProperties);
  108. }
  109. UnityEngine.Debug.LogError(string.Format("{0} is not a recognized item name. I will instantiate a DefaultItem-object.", name));
  110. return new DefaultItem();
  111. }
  112. private static Dictionary<string, object> GetItemProperties(string itemName, Dictionary<string, object> values)
  113. {
  114. if (values.ContainsKey("PackItems"))
  115. {
  116. Dictionary<string, object> dictionary = (Dictionary<string, object>)values["PackItems"];
  117. if (dictionary != null && dictionary.ContainsKey(itemName))
  118. {
  119. return dictionary[itemName] as Dictionary<string, object>;
  120. }
  121. }
  122. UnityEngine.Debug.LogWarning(string.Format("Unable to find item named {0}", itemName));
  123. return new Dictionary<string, object>();
  124. }
  125. private const string PackItemsKey = "PackItems";
  126. private const string PackValueKey = "Value";
  127. private List<IPackEffect> _items;
  128. private string _name;
  129. }