Exemplo n.º 1
0
        /// <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);
        }
Exemplo n.º 2
0
        protected override void Initialize(GameRulesBase rules, JObject data)
        {
            base.Initialize(rules, data);

            Constraint  = rules.CreateConstraint((JObject)data["Constraint"]);
            Requirement = ActionInvestmentDescriptor.FromData((JObject)data["Requirement"]);
            Returns     = ActionReturnsDescriptor.FromData((JObject)data["Returns"]);


            if (data.Value <string>("Name") == null)
            {
                _name = DefaultName();
            }
            else
            {
                _name = data.Value <string>("Name");
            }



            if (data.Value <string>("Description") == null)
            {
                _description = DefaultDescription();
            }
            else
            {
                _description = data.Value <string>("Description");
            }
        }
Exemplo n.º 3
0
        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));
        }
Exemplo n.º 4
0
        /// <summary>
        /// 初始化游戏规则数据
        /// </summary>
        /// <param name="data">游戏规则数据</param>
        protected virtual void Initialize(GameRulesBase rules, JObject data)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }


            Guid = (Guid)data.GuidValue("ID");
            Data = (JObject)data.DeepClone();
        }
Exemplo n.º 5
0
        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));
        }
Exemplo n.º 6
0
        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);
                }
            }
        }
Exemplo n.º 7
0
        public static UnitRestriction FromData(GameRulesBase rules, JToken data)
        {
            var value = data as JValue;

            if (value != null)
            {
                var unit = rules.GetDataItem <UnitDescriptor>(value.GuidValue());
                return(new UnitRestriction
                {
                    Unit = unit,
                });
            }

            else
            {
                return(null);
            }
        }
Exemplo n.º 8
0
        public static void Initialize(GameRulesBase rules, IGameDataService dataService, IGameMessageService messageService)
        {
            if (rules == null)
            {
                throw new ArgumentNullException("rules");
            }

            if (dataService == null)
            {
                throw new ArgumentNullException("dataService");
            }

            if (messageService == null)
            {
                throw new ArgumentNullException("messageService");
            }

            GameRules      = rules;
            DataService    = dataService;
            MessageService = messageService;

            rules.Initialize();
            DataService.Initialize();
        }
Exemplo n.º 9
0
        internal void InitializeData(GameRulesBase rules, JObject data)
        {
            Initialize(rules, data);

            AlreadyInitialized = true;
        }
Exemplo n.º 10
0
        /// <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));
        }