コード例 #1
0
        /// <summary>
        /// Processes the reactor definitions and update the reactors to match the definitions.
        /// </summary>
        public void InstantiateReactors()
        {
            int lDefCount = _ReactorDefinitions.Count;

            // First, remove any extra items that may exist
            for (int i = _Reactors.Count - 1; i >= lDefCount; i--)
            {
                _Reactors.RemoveAt(i);
            }

            // We need to match the definitions to the item
            for (int i = 0; i < lDefCount; i++)
            {
                string lDefinition = _ReactorDefinitions[i];

                Type lType = JSONSerializer.GetType(lDefinition);
                if (lType == null)
                {
                    continue;
                }

                ReactorAction lReactor = null;

                // If we don't have an item matching the type, we need to create one
                if (_Reactors.Count <= i || !lType.Equals(_Reactors[i].GetType()))
                {
                    lReactor       = Activator.CreateInstance(lType) as ReactorAction;
                    lReactor.Owner = this.gameObject;

                    if (_Reactors.Count <= i)
                    {
                        _Reactors.Add(lReactor);
                    }
                    else
                    {
                        _Reactors[i] = lReactor;
                    }
                }
                // Grab the matching item
                else
                {
                    lReactor = _Reactors[i];
                }

                // Fill the item with data from the definition
                if (lReactor != null)
                {
                    lReactor.Deserialize(lDefinition);
                }
            }

            // Allow each item to initialize now that it has been deserialized
            for (int i = 0; i < _Reactors.Count; i++)
            {
                _Reactors[i].Owner = this.gameObject;
                _Reactors[i].Awake();
            }
        }
コード例 #2
0
ファイル: ActorCore.cs プロジェクト: cjpxyz/redpanda-game
        /// <summary>
        /// Processes the reactor definitions and update the reactors to match the definitions.
        /// </summary>
        public void InstantiateReactors()
        {
            int lDefCount = _ReactorDefinitions.Count;

            // First, remove any extra items that may exist
            for (int i = _Reactors.Count - 1; i >= lDefCount; i--)
            {
                _Reactors.RemoveAt(i);
            }

            // CDL 07/27/2018 - keep track of any invalid reactor definitions
            List <int> lInvalidDefinitions = new List <int>();

            // We need to match the definitions to the item
            for (int i = 0; i < lDefCount; i++)
            {
                string   lDefinition     = _ReactorDefinitions[i];
                JSONNode lDefinitionNode = JSONNode.Parse(lDefinition);
                if (lDefinitionNode == null)
                {
                    continue;
                }

                string lTypeString = lDefinitionNode["__Type"].Value;

                // CDL 07/27/2018 - Use AssemblyHelper.ResolveType() intead so that the Type can still
                // be obtained if the reactor was serialized as belonging to a different assembly
                bool lUpdateType;
                Type lType = AssemblyHelper.ResolveType(lTypeString, out lUpdateType);
                if (lType == null)
                {
                    // CDL 07/27/2018 - save this definition for removal afterwards, as we can't resolve the Type from any assembly.
                    lInvalidDefinitions.Add(i);
                    continue;
                }

                ReactorAction lReactor = null;

                // If we don't have an item matching the type, we need to create one
                if (_Reactors.Count <= i || !lType.Equals(_Reactors[i].GetType()))
                {
                    lReactor       = Activator.CreateInstance(lType) as ReactorAction;
                    lReactor.Owner = this.gameObject;

                    if (_Reactors.Count <= i)
                    {
                        _Reactors.Add(lReactor);
                    }
                    else
                    {
                        _Reactors[i] = lReactor;
                    }
                }
                // Grab the matching item
                else
                {
                    lReactor = _Reactors[i];
                }

                // Fill the item with data from the definition
                if (lReactor != null)
                {
                    lReactor.Deserialize(lDefinition);

                    // CDL 07/27/2018 - update the serialized Type if necessary
                    if (lUpdateType)
                    {
                        _ReactorDefinitions[i] = lReactor.Serialize();
                    }
                }
            }

            // CDL 07/27.2018 - Clean up any invalid Motion Definitions
            // this is causing an error, but maybe not needed, as I don't think there are
            // many people who have been using custom reactors
            //if (lInvalidDefinitions.Count > 0)
            //{
            //    for (int i = 0; i < lInvalidDefinitions.Count; i++)
            //    {
            //        int lIndex = lInvalidDefinitions[i];
            //        _ReactorDefinitions.RemoveAt(lIndex);
            //    }
            //}

            // Allow each item to initialize now that it has been deserialized
            for (int i = 0; i < _Reactors.Count; i++)
            {
                _Reactors[i].Owner = this.gameObject;
                _Reactors[i].Awake();
            }
        }