public ActionConstraint(GameRulesBase rules, JObject data) { _data = data; BuildingDescriptors = new HashSet <Guid>(); TerrainDescriptors = new HashSet <Guid>(); UnitDescriptors = new HashSet <Guid>(); { var constraint = _data["Building"] as JObject; if (constraint != null) { var descriptors = constraint["Descriptors"] as JArray ?? new JArray(); foreach (JValue item in descriptors) { var guid = item.GuidValue(); if (guid.HasValue) { BuildingDescriptors.Add(guid.Value); } } var typeName = (string)constraint["Type"]; BuildingType = rules.GetType(typeName); } } { var constraint = _data["Terrain"] as JObject; if (constraint != null) { var descriptors = constraint["Descriptors"] as JArray ?? new JArray(); foreach (JValue item in descriptors) { var guid = item.GuidValue(); if (guid.HasValue) { TerrainDescriptors.Add(guid.Value); } } var typeName = (string)constraint["Type"]; TerrainType = rules.GetType(typeName); } } }
/// <summary> /// 协助解析类型的帮助方法 /// </summary> /// <param name="rules">游戏规则集</param> /// <param name="typeName"></param> /// <param name="restrict"></param> /// <param name="defaultType"></param> /// <returns></returns> public static Type GetType(this GameRulesBase rules, string typeName, Type restrict, Type defaultType = null) { if (restrict == null) { throw new ArgumentNullException("restrict"); } var type = defaultType ?? restrict; if (string.IsNullOrWhiteSpace(typeName) == false) { type = rules.GetType(typeName); } if (type == null) { throw new Exception(string.Format("type {0} is not found.", typeName)); } if (restrict != null && restrict.IsAssignableFrom(type) == false) { throw new Exception(string.Format("type {0} is not unit instance type, data load failed.", typeName)); } return(type); }
protected override void Initialize(GameRulesBase rules, JObject data) { base.Initialize(rules, data); Name = data.Value <string>("Name"); Description = data.Value <string>("Description"); InstanceType = rules.GetType((string)data.Value <string>("InstanceType"), typeof(Item)); }
protected override void Initialize(GameRulesBase rules, JObject data) { base.Initialize(rules, data); dynamic d = data; Name = d.Name; Description = d.Description; MobilityMaximum = d.Mobility.Maximum; MobilityRecoveryCycle = d.Mobility.RecoveryCycle; MobilityRecoveryScale = d.Mobility.RecoveryScale; InstanceType = rules.GetType((string)d.InstanceType, typeof(Unit)); }
/// <summary> /// 协助创建指定类型实例的方法 /// </summary> /// <typeparam name="T">实例类型</typeparam> /// <param name="rules">游戏规则集</param> /// <param name="typeName">类型名称</param> /// <returns></returns> public static T CreateInstance <T>(this GameRulesBase rules, string typeName) { var type = rules.GetType(typeName); return((T)Activator.CreateInstance(type)); }