Ejemplo n.º 1
0
        public static void AddScriptGameLogic(MyEntity entity, MyObjectBuilderType builderType, string subTypeName = null)
        {
            var scriptManager = Sandbox.Game.World.MyScriptManager.Static;

            if (scriptManager == null || entity == null)
            {
                return;
            }

            // both types of logic components are valid to be attached:

            // (1) those that are specific for the given subTypeName
            HashSet <Type> subEntityScripts;

            if (subTypeName != null)
            {
                var key = new Tuple <Type, string>(builderType, subTypeName);
                subEntityScripts = scriptManager.SubEntityScripts.GetValueOrDefault(key, m_emptySet);
            }
            else
            {
                subEntityScripts = m_emptySet;
            }

            // (2) and those that don't care about the subTypeName
            HashSet <Type> entityScripts = scriptManager.EntityScripts.GetValueOrDefault(builderType, m_emptySet);

            // if there are no component types to attach leave the entity as-is
            var count = subEntityScripts.Count + entityScripts.Count;

            if (count == 0)
            {
                return;
            }

            // just concatenate the two type-sets, they are disjunct by definition (see ScriptManager)
            var logicComponents = new List <MyGameLogicComponent>(count);

            foreach (var logicComponentType in entityScripts.Concat(subEntityScripts))
            {
                logicComponents.Add((MyGameLogicComponent)Activator.CreateInstance(logicComponentType));
            }

            // wrap the gamelogic-components to appear as a single component to the entity
            entity.GameLogic = MyCompositeGameLogicComponent.Create(logicComponents, entity);
        }
Ejemplo n.º 2
0
        public static void AddScriptGameLogic(MyEntity entity, MyObjectBuilderType builderType, string subTypeName = null)
        {
            MyScriptManager @static = MyScriptManager.Static;

            if ((@static != null) && (entity != null))
            {
                HashSet <Type> emptySet;
                if (subTypeName == null)
                {
                    emptySet = m_emptySet;
                }
                else
                {
                    Tuple <Type, string> key = new Tuple <Type, string>((Type)builderType, subTypeName);
                    emptySet = @static.SubEntityScripts.GetValueOrDefault <Tuple <Type, string>, HashSet <Type> >(key, m_emptySet);
                }
                HashSet <Type> first    = @static.EntityScripts.GetValueOrDefault <Type, HashSet <Type> >((Type)builderType, m_emptySet);
                int            capacity = emptySet.Count + first.Count;
                if (capacity != 0)
                {
                    List <MyGameLogicComponent> logicComponents = new List <MyGameLogicComponent>(capacity);
                    foreach (Type local1 in first.Concat <Type>(emptySet))
                    {
                        MyGameLogicComponent        item       = (MyGameLogicComponent)Activator.CreateInstance(local1);
                        MyEntityComponentDescriptor descriptor = (MyEntityComponentDescriptor)local1.GetCustomAttribute(typeof(MyEntityComponentDescriptor), false);
                        if (descriptor.EntityUpdate == null)
                        {
                            ((IMyGameLogicComponent)item).EntityUpdate = true;
                        }
                        else if (descriptor.EntityUpdate.Value)
                        {
                            ((IMyGameLogicComponent)item).EntityUpdate = true;
                        }
                        logicComponents.Add(item);
                    }
                    MyGameLogicComponent component = MyCompositeGameLogicComponent.Create(logicComponents, entity);
                    entity.GameLogic = component;
                }
            }
        }