78 lines
2.7 KiB
C#
78 lines
2.7 KiB
C#
using GeometryTD.CustomUtility;
|
|
using GeometryTD.Definition;
|
|
using Newtonsoft.Json.Linq;
|
|
using UnityGameFramework.Runtime;
|
|
|
|
namespace GeometryTD.Factory
|
|
{
|
|
public static class EventEffectFactory
|
|
{
|
|
public static EventEffectBase Create(string rawType, JObject param, float? probability = null)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(rawType))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
EventEffectType type = EnumUtility<EventEffectType>.Get(rawType);
|
|
switch (type)
|
|
{
|
|
case EventEffectType.AddGold:
|
|
{
|
|
int count = GetInt(param, "Count");
|
|
return new AddGoldEffect(new AddGoldParam(count), probability);
|
|
}
|
|
case EventEffectType.RemoveRandomComps:
|
|
{
|
|
int count = GetInt(param, "Count");
|
|
RarityType rarity = EnumUtility<RarityType>.Get(GetString(param, "Rarity"));
|
|
return new RemoveRandomCompsEffect(new RemoveRandomCompsParam(count, rarity), probability);
|
|
}
|
|
case EventEffectType.AddRandomComps:
|
|
{
|
|
int count = GetInt(param, "Count");
|
|
RarityType rarity = EnumUtility<RarityType>.Get(GetString(param, "Rarity"));
|
|
return new AddRandomCompsEffect(new AddRandomCompsParam(count, rarity), probability);
|
|
}
|
|
case EventEffectType.DamageRandomTowersEndurance:
|
|
{
|
|
int count = GetInt(param, "Count");
|
|
int amount = GetInt(param, "Amount");
|
|
return new DamageRandomTowerEnduranceEffect(new DamageRandomTowerEnduranceParam(count, amount), probability);
|
|
}
|
|
default:
|
|
Log.Warning("Unsupported EventEffectType '{0}'.", rawType);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private static int GetInt(JObject param, params string[] keys)
|
|
{
|
|
if (param == null)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
foreach (string key in keys)
|
|
{
|
|
if (param.TryGetValue(key, out JToken token) && token.Type != JTokenType.Null)
|
|
{
|
|
return token.Value<int>();
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
private static string GetString(JObject param, string key)
|
|
{
|
|
if (param == null || !param.TryGetValue(key, out JToken token) || token.Type == JTokenType.Null)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
return token.Value<string>();
|
|
}
|
|
}
|
|
}
|