|
- using System;
- using System.Collections.Generic;
- using CIG3.ExtensionMethods;
- using UnityEngine;
-
- public class IAPPack : IPack
- {
- private IAPPack(string name, IList<IPackEffect> items)
- {
- this._name = name;
- this._items = new List<IPackEffect>(items);
- }
-
- public static IAPPack Create(string packName, Dictionary<string, object> values)
- {
- if (values == null || values.Count == 0)
- {
- UnityEngine.Debug.LogError(string.Format("Can't Create IAPPack with no values '{0}'.", packName));
- return new IAPPack(string.Empty, new List<IPackEffect>());
- }
- IList<string> packItemNames = IAPPack.GetPackItemNames(values);
- List<IPackEffect> list = new List<IPackEffect>();
- foreach (string text in packItemNames)
- {
- IPackEffect packEffect = IAPPack.CreateItemFromName(text, values);
- if (packEffect == null)
- {
- UnityEngine.Debug.LogError(string.Format("Can't create an item of type '{0}'.", text));
- }
- else
- {
- list.Add(packEffect);
- }
- }
- return new IAPPack(packName, list);
- }
-
- public IList<IPackEffect> Items
- {
- get
- {
- return this._items.AsReadOnly();
- }
- }
-
- public string Name
- {
- get
- {
- return this._name;
- }
- }
-
- public void ApplyEffects()
- {
- foreach (IPackEffect packEffect in this._items)
- {
- packEffect.ApplyEffect();
- }
- }
-
- public override string ToString()
- {
- string text = string.Empty;
- foreach (IPackEffect packEffect in this._items)
- {
- text = text + packEffect.ToString() + "\n";
- }
- return text.Trim();
- }
-
- public PackItemType FindPackItem<PackItemType>() where PackItemType : class, IPackEffect
- {
- foreach (IPackEffect packEffect in this.Items)
- {
- PackItemType packItemType = packEffect as PackItemType;
- if (packItemType != null)
- {
- return packItemType;
- }
- }
- return (PackItemType)((object)null);
- }
-
- private static IList<string> GetPackItemNames(Dictionary<string, object> values)
- {
- Dictionary<string, object> dictionary = values.GetValue<object>("PackItems", null) as Dictionary<string, object>;
- if (dictionary == null)
- {
- return new List<string>();
- }
- return new List<string>(dictionary.Keys);
- }
-
- private static IPackEffect CreateItemFromName(string name, Dictionary<string, object> values)
- {
- Dictionary<string, object> itemProperties = IAPPack.GetItemProperties(name, values);
- if (name == "FreeBuilding")
- {
- return new FreeBuildingItem(itemProperties);
- }
- if (name == "TimeBoost")
- {
- return new TimeBoostItem(itemProperties);
- }
- if (name == "LevelUp")
- {
- return new LevelUpItem(itemProperties);
- }
- if (name == "Cash")
- {
- return new CashItem(itemProperties);
- }
- if (name == "Gold")
- {
- return new GoldItem(itemProperties);
- }
- UnityEngine.Debug.LogError(string.Format("{0} is not a recognized item name. I will instantiate a DefaultItem-object.", name));
- return new DefaultItem();
- }
-
- private static Dictionary<string, object> GetItemProperties(string itemName, Dictionary<string, object> values)
- {
- if (values.ContainsKey("PackItems"))
- {
- Dictionary<string, object> dictionary = (Dictionary<string, object>)values["PackItems"];
- if (dictionary != null && dictionary.ContainsKey(itemName))
- {
- return dictionary[itemName] as Dictionary<string, object>;
- }
- }
- UnityEngine.Debug.LogWarning(string.Format("Unable to find item named {0}", itemName));
- return new Dictionary<string, object>();
- }
-
- private const string PackItemsKey = "PackItems";
-
- private const string PackValueKey = "Value";
-
- private List<IPackEffect> _items;
-
- private string _name;
- }
|