105 lines
4.0 KiB
C#
105 lines
4.0 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");
|
|
bool hasMinRarity = TryGetString(param, "MinRarity", out string minRarityRaw);
|
|
bool hasMaxRarity = TryGetString(param, "MaxRarity", out string maxRarityRaw);
|
|
if (hasMinRarity || hasMaxRarity)
|
|
{
|
|
if (!hasMinRarity || !hasMaxRarity)
|
|
{
|
|
throw new System.InvalidOperationException(
|
|
"AddRandomComps requires both MinRarity and MaxRarity when using ranged rarity config.");
|
|
}
|
|
|
|
RarityType minRarity = EnumUtility<RarityType>.Get(minRarityRaw);
|
|
RarityType maxRarity = EnumUtility<RarityType>.Get(maxRarityRaw);
|
|
return new AddRandomCompsEffect(new AddRandomCompsParam(count, minRarity, maxRarity), probability);
|
|
}
|
|
|
|
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>();
|
|
}
|
|
|
|
private static bool TryGetString(JObject param, string key, out string value)
|
|
{
|
|
value = string.Empty;
|
|
if (param == null || !param.TryGetValue(key, out JToken token) || token.Type == JTokenType.Null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
value = token.Value<string>();
|
|
return !string.IsNullOrWhiteSpace(value);
|
|
}
|
|
}
|
|
}
|