Esempio n. 1
0
        public static bool RemoveComponent(this IMyComponentAggregate aggregate, MyComponentBase component)
        {
            int index = aggregate.ChildList.GetComponentIndex(component);

            if (index != -1)
            {
                aggregate.BeforeComponentRemove(component);
                component.SetContainer(null);
                aggregate.ChildList.RemoveComponentAt(index);
                return(true);
            }

            foreach (var child in aggregate.ChildList.Reader)
            {
                var childAggregate = child as IMyComponentAggregate;
                if (childAggregate == null)
                {
                    continue;
                }

                bool removed = childAggregate.RemoveComponent(component);
                if (removed)
                {
                    return(true);
                }
            }

            return(false);
        }
Esempio n. 2
0
        public bool RemoveComponent(MyComponentBase component)
        {
            if (Contains(component))
            {
                component.OnBeforeRemovedFromContainer();
            }
            else
            {
                return(false);
            }

            if (m_components.Remove(component))
            {
                return(true);
            }
            foreach (var childComponent in m_components)
            {
                if (childComponent is IMyComponentAggregate)
                {
                    var childAggregate = (childComponent as IMyComponentAggregate);
                    if (childAggregate.ChildList.RemoveComponent(component))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
        public void Add(Type type, MyComponentBase component)
        {
            //System.Diagnostics.Debug.Assert(component == null || component.ContainerBase == null, "Component needs to be removed from a container before adding to a new one!");
            System.Diagnostics.Debug.Assert(typeof(MyComponentBase).IsAssignableFrom(type), "Unsupported type of component!");
            if (!typeof(MyComponentBase).IsAssignableFrom(type))
            {
                return;
            }

            if (component != null)
            {
                System.Diagnostics.Debug.Assert(type.IsAssignableFrom(component.GetType()), "Component added with wrong type!");
                if (!type.IsAssignableFrom(component.GetType()))
                {
                    return;
                }
            }

            {
                //TODO: componentTypeFromAttribute cannot be null when all components has [MyComponentType(typeof(...))] attribute.
                var componentTypeFromAttribute = MyComponentTypeFactory.GetComponentType(type);
                if (componentTypeFromAttribute != null && componentTypeFromAttribute != type)
                {
                    // Failed when component type from attribute is not the same as the given type. Means that [MyComponentType(typeof(...))]
                    // should be specified for the component class (it is probably used for base class now).
                    System.Diagnostics.Debug.Fail("Component: " + component.GetType() + " is set to container as type: " + type + " but type from attribute is: " + componentTypeFromAttribute);
                }
            }

            MyComponentBase containedComponent;

            if (m_components.TryGetValue(type, out containedComponent))
            {
                //System.Diagnostics.Debug.Assert(containedComponent != component, "Adding a component to a container twice!");

                if (containedComponent is IMyComponentAggregate)
                {
                    (containedComponent as IMyComponentAggregate).AddComponent(component);
                    return;
                }
                else if (component is IMyComponentAggregate)
                {
                    Remove(type);
                    (component as IMyComponentAggregate).AddComponent(containedComponent);
                    m_components[type] = component;
                    component.SetContainer(this);
                    OnComponentAdded(type, component);
                    return;
                }
            }

            Remove(type);
            if (component != null)
            {
                m_components[type] = component;
                component.SetContainer(this);
                OnComponentAdded(type, component);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Removes from list, but doesn't change ownership
        /// </summary>
        public static void DetachComponent(this IMyComponentAggregate aggregate, MyComponentBase component)
        {
            int index = aggregate.ChildList.GetComponentIndex(component);

            if (index != -1)
            {
                aggregate.ChildList.RemoveComponentAt(index);
            }
        }
Esempio n. 5
0
 public static void AddComponent(this IMyComponentAggregate aggregate, MyComponentBase component)
 {
     Debug.Assert(aggregate != component, "Can not add to itself!");
     if (component.ContainerBase != null)
     {
         component.OnBeforeRemovedFromContainer();
     }
     aggregate.ChildList.AddComponent(component);
     component.SetContainer(aggregate.ContainerBase);
     aggregate.AfterComponentAdd(component);
 }
Esempio n. 6
0
        public static void RemoveComponent(this IMyComponentAggregate aggregate, MyComponentBase component)
        {
            int index = aggregate.ChildList.GetComponentIndex(component);

            if (index != -1)
            {
                aggregate.BeforeComponentRemove(component);
                component.SetContainer(null);
                aggregate.ChildList.RemoveComponentAt(index);
            }
        }
Esempio n. 7
0
        protected override void OnComponentRemoved(Type t, MyComponentBase component)
        {
            base.OnComponentRemoved(t, component);

            var entityComponent = component as MyEntityComponentBase;

            var handler = ComponentRemoved;

            if (handler != null && entityComponent != null)
            {
                handler(t, entityComponent);
            }
        }
Esempio n. 8
0
        protected override void OnComponentAdded(Type t, MyComponentBase component)
        {
            base.OnComponentAdded(t, component);

            var entityComponent = component as MyEntityComponentBase;

            Debug.Assert(entityComponent != null, "The component added to the entity component container was not derived from MyEntityComponentBase!");

            var handler = ComponentAdded;

            if (handler != null && entityComponent != null)
            {
                handler(t, entityComponent);
            }
        }
Esempio n. 9
0
        public static MyObjectBuilder_ComponentBase CreateObjectBuilder(MyComponentBase instance)
        {
            var objectBuilder = m_objectFactory.CreateObjectBuilder <MyObjectBuilder_ComponentBase>(instance);

            //if (objectBuilder == null)
            //{
            //    var baseType = instance.GetType().BaseType;
            //    while (baseType != null && baseType != typeof(object) && objectBuilder == null)
            //    {
            //        objectBuilder = m_objectFactory.CreateObjectBuilder<MyObjectBuilder_ComponentBase>(baseType);
            //        baseType = baseType.BaseType;
            //    }
            //}

            return(objectBuilder);
        }
        public void Deserialize(MyObjectBuilder_ComponentContainer builder)
        {
            if (builder == null || builder.Components == null)
            {
                return;
            }

            foreach (var data in builder.Components)
            {
                MyComponentBase instance            = null;
                var             createdInstanceType = MyComponentFactory.GetCreatedInstanceType(data.Component.TypeId);

                // Old component deserialized type.
                var dictType = MyComponentTypeFactory.GetType(data.TypeId);
                // Component type can be set as attribute now
                var dictTypeFromAttr = MyComponentTypeFactory.GetComponentType(createdInstanceType);
                if (dictTypeFromAttr != null)
                {
                    dictType = dictTypeFromAttr;
                }

                bool hasComponent = TryGet(dictType, out instance);
                if (hasComponent)
                {
                    // If component is found then check also type because some components have default instances (MyNullGameLogicComponent)
                    if (createdInstanceType != instance.GetType())
                    {
                        hasComponent = false;
                    }
                }

                if (!hasComponent)
                {
                    instance = MyComponentFactory.CreateInstanceByTypeId(data.Component.TypeId);
                }

                instance.Deserialize(data.Component);

                if (!hasComponent)
                {
                    Add(dictType, instance);
                }
            }
        }
Esempio n. 11
0
 public bool Contains(MyComponentBase component)
 {
     if (m_components.Contains(component))
     {
         return(true);
     }
     foreach (var childComponent in m_components)
     {
         if (childComponent is IMyComponentAggregate)
         {
             var childAggregate = (childComponent as IMyComponentAggregate);
             if (childAggregate.ChildList.Contains(component))
             {
                 return(true);
             }
         }
     }
     return(false);
 }
        private static int GetComponentLines(MyComponentBase component, bool countAll = true)
        {
            int n = 1;

            if (component is IMyComponentAggregate)
            {
                int count = (component as IMyComponentAggregate).ChildList.Reader.Count;
                int i     = 0;
                foreach (var childComponent in (component as IMyComponentAggregate).ChildList.Reader)
                {
                    i++;
                    if (i < count || countAll)
                    {
                        n += GetComponentLines(childComponent);
                    }
                    else
                    {
                        n += 1;
                    }
                }
            }
            return(n);
        }
        private static void DebugDrawComponent(MyComponentBase component, Vector3D origin, Vector3D rightVector, Vector3D upVector, double lineSize, float textSize)
        {
            Vector3D offset       = rightVector * 0.025f;
            Vector3D offsetOrigin = origin + offset * 3.5f;
            Vector3D textOrigin   = origin + 2.0f * offset + rightVector * 0.015f;

            MyRenderProxy.DebugDrawLine3D(origin, origin + 2.0f * offset, Color.White, Color.White, false);
            MyRenderProxy.DebugDrawText3D(textOrigin, component.ToString(), Color.White, textSize, false, MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_CENTER);

            if (component is IMyComponentAggregate && (component as IMyComponentAggregate).ChildList.Reader.Count != 0)
            {
                int lines = GetComponentLines(component, false) - 1;
                MyRenderProxy.DebugDrawLine3D(offsetOrigin - 0.5f * lineSize * upVector, offsetOrigin - lines * lineSize * upVector, Color.White, Color.White, false);

                offsetOrigin -= 1 * lineSize * upVector;
                foreach (var childComponent in (component as IMyComponentAggregate).ChildList.Reader)
                {
                    int n = GetComponentLines(childComponent);
                    DebugDrawComponent(childComponent, offsetOrigin, rightVector, upVector, lineSize, textSize);
                    offsetOrigin -= n * lineSize * upVector;
                }
            }
        }
        public void Remove(Type t, MyComponentBase component)
        {
            MyComponentBase storedComponent = null;

            m_components.TryGetValue(t, out storedComponent);
            if (storedComponent == null)
            {
                System.Diagnostics.Debug.Assert(false, "Removing component from a container, but that container does not contain the component!");
                return;
            }

            IMyComponentAggregate storedAggregate = storedComponent as IMyComponentAggregate;

            if (storedAggregate == null)
            {
                System.Diagnostics.Debug.Assert(storedComponent == component, "Removing component from a container, but that container does not contain the component!");
                RemoveComponentInternal(t, component);
            }
            else
            {
                bool removed = storedAggregate.RemoveComponent(component);
                System.Diagnostics.Debug.Assert(removed, "Component could not be removed because it was not present in the container!");
            }
        }
 public RegisteredEvents(MyStringHash eventType, MyComponentBase component, EntityEventHandler handler)
 {
     this[eventType] = new List <RegisteredComponent>();
     this[eventType].Add(new RegisteredComponent(component, handler));
 }
 public RegisteredComponent(MyComponentBase component, EntityEventHandler handler)
 {
     Component = component;
     Handler   = handler;
 }
 private void RemoveComponentInternal(Type t, MyComponentBase c)
 {
     c.SetContainer(null);
     m_components.Remove(t);
     OnComponentRemoved(t, c);
 }
 protected virtual void OnComponentRemoved(Type t, MyComponentBase component)
 {
 }
Esempio n. 19
0
 public void AddComponent(MyComponentBase component)
 {
     m_components.Add(component);
 }
Esempio n. 20
0
        public static void DebugDraw()
        {
            if (MyDebugDrawSettings.ENABLE_DEBUG_DRAW && MyDebugDrawSettings.DEBUG_DRAW_ENTITY_COMPONENTS && MySector.MainCamera != null)
            {
                double fontSize = 1.5;
                double lineSize = fontSize * 0.045;
                double hoffset  = 0.5f;

                Vector3D playerPos   = MySector.MainCamera.Position;
                Vector3D upVector    = MySector.MainCamera.WorldMatrix.Up;
                Vector3D rightVector = MySector.MainCamera.WorldMatrix.Right;
                Vector3D fwVector    = MySector.MainCamera.ForwardVector;

                BoundingSphereD bSphere  = new BoundingSphereD(playerPos, 5.0f);
                var             entities = MyEntities.GetEntitiesInSphere(ref bSphere);
                foreach (var entity in entities)
                {
                    if (entity.PositionComp == null)
                    {
                        continue;
                    }

                    Vector3D originalPos = entity.PositionComp.GetPosition();
                    Vector3D pos2        = originalPos + upVector * 0.1f;
                    Vector3D pos         = pos2 - rightVector * hoffset;
                    Vector3D viewVector  = Vector3D.Normalize(originalPos - playerPos);
                    double   dot         = Vector3D.Dot(viewVector, fwVector);
                    if (dot < 0.9995)
                    {
                        MyRenderProxy.DebugDrawSphere(originalPos, 0.01f, Color.White, 1.0f, false);
                        continue;
                    }

                    double dist     = Vector3D.Distance(pos, playerPos);
                    double textSize = Math.Atan(fontSize / Math.Max(dist, 0.001));

                    float n = 0;
                    {
                        var             enumerator = entity.Components.GetEnumerator();
                        MyComponentBase component  = null;
                        while (enumerator.MoveNext())
                        {
                            component = enumerator.Current;
                            n        += GetComponentLines(component);
                        }
                        n += 1;
                        n -= GetComponentLines(component); // The last component should not make the line longer
                        enumerator.Dispose();
                    }

                    Vector3D topPos     = pos + (n + 0.5f) * upVector * lineSize;
                    Vector3D currentPos = pos + (n + 1) * upVector * lineSize + 0.01f * rightVector;

                    MyRenderProxy.DebugDrawLine3D(originalPos, pos2, Color.White, Color.White, false);
                    MyRenderProxy.DebugDrawLine3D(pos, pos2, Color.White, Color.White, false);
                    MyRenderProxy.DebugDrawLine3D(pos, topPos, Color.White, Color.White, false);
                    MyRenderProxy.DebugDrawLine3D(topPos, topPos + rightVector * 1.0f, Color.White, Color.White, false);
                    MyRenderProxy.DebugDrawText3D(currentPos, entity.GetType().ToString() + " - " + entity.DisplayName, Color.Orange, (float)textSize, false, MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_CENTER);

                    foreach (var component in entity.Components)
                    {
                        currentPos = pos + n * upVector * lineSize;
                        DebugDrawComponent(component, currentPos, rightVector, upVector, lineSize, (float)textSize);
                        var    entityComponent = component as MyEntityComponentBase;
                        string compType        = entityComponent == null ? "" : entityComponent.ComponentTypeDebugString;
                        MyRenderProxy.DebugDrawText3D(currentPos - 0.02f * rightVector, compType, Color.Yellow, (float)textSize, false, MyGuiDrawAlignEnum.HORISONTAL_RIGHT_AND_VERTICAL_CENTER);
                        n -= GetComponentLines(component);
                    }
                }
                entities.Clear();
            }
        }
        public static void DebugDraw()
        {
            if (MyDebugDrawSettings.ENABLE_DEBUG_DRAW && MyDebugDrawSettings.DEBUG_DRAW_ENTITY_COMPONENTS && MySector.MainCamera != null)
            {
                double fontSize = 1.5;
                double lineSize = fontSize * 0.045;
                double hoffset  = 0.5f;

                Vector3D playerPos   = MySector.MainCamera.Position;
                Vector3D upVector    = MySector.MainCamera.WorldMatrix.Up;
                Vector3D rightVector = MySector.MainCamera.WorldMatrix.Right;
                Vector3D fwVector    = MySector.MainCamera.ForwardVector;

                BoundingSphereD bSphere       = new BoundingSphereD(playerPos, 5.0f);
                var             entities      = MyEntities.GetEntitiesInSphere(ref bSphere);
                Vector3D        lastEntityPos = Vector3D.Zero;
                Vector3D        offset        = Vector3D.Zero;

                var mat = MySector.MainCamera.ViewProjectionMatrix;

                var   fullscreenRect = Sandbox.Graphics.MyGuiManager.GetSafeGuiRectangle();
                float aspect         = (float)fullscreenRect.Height / fullscreenRect.Width;
                float scaleX         = 600;
                float scaleY         = scaleX * aspect;

                Vector3D worldAxisPos = playerPos + 1.0f * fwVector;

                // Draw the world-space axis in the middle of the screen

                /*MyRenderProxy.DebugDrawArrow3D(worldAxisPos, worldAxisPos + Vector3D.Right * 0.1f, Color.Red, Color.Red, false, text: "World X");
                 * MyRenderProxy.DebugDrawArrow3D(worldAxisPos, worldAxisPos + Vector3D.Up * 0.1f, Color.Green, Color.Green, false, text: "World Y");
                 * MyRenderProxy.DebugDrawArrow3D(worldAxisPos, worldAxisPos + Vector3D.Backward * 0.1f, Color.Blue, Color.Blue, false, text: "World Z");*/

                Vector3D posView    = Vector3D.Transform(worldAxisPos, mat);
                Vector3D rtView     = Vector3D.Transform(worldAxisPos + Vector3D.Right * 0.1f, mat);
                Vector3D upView     = Vector3D.Transform(worldAxisPos + Vector3D.Up * 0.1f, mat);
                Vector3D bwView     = Vector3D.Transform(worldAxisPos + Vector3D.Backward * 0.1f, mat);
                var      center2D   = new Vector2((float)posView.X * scaleX, (float)posView.Y * -scaleY * aspect);
                var      right2D    = new Vector2((float)rtView.X * scaleX, (float)rtView.Y * -scaleY * aspect) - center2D;
                var      up2D       = new Vector2((float)upView.X * scaleX, (float)upView.Y * -scaleY * aspect) - center2D;
                var      backward2D = new Vector2((float)bwView.X * scaleX, (float)bwView.Y * -scaleY * aspect) - center2D;

                var frameSize   = 150.0f;
                var frameBR     = Sandbox.Graphics.MyGuiManager.GetScreenCoordinateFromNormalizedCoordinate(new Vector2(1.0f, 1.0f));
                var frameBL     = frameBR + new Vector2(-frameSize, 0.0f);
                var frameTR     = frameBR + new Vector2(0.0f, -frameSize);
                var frameTL     = frameBR + new Vector2(-frameSize, -frameSize);
                var frameCenter = (frameBR + frameTL) * 0.5f;

                // Draw frame around the world-space axis

                /*MyRenderProxy.DebugDrawLine2D(frameTL, frameTR, Color.White, Color.White);
                *  MyRenderProxy.DebugDrawLine2D(frameTR, frameBR, Color.White, Color.White);
                *  MyRenderProxy.DebugDrawLine2D(frameBR, frameBL, Color.White, Color.White);
                *  MyRenderProxy.DebugDrawLine2D(frameBL, frameTL, Color.White, Color.White);*/

                // Draw the world-space axis in the corner
                MyRenderProxy.DebugDrawLine2D(frameCenter, frameCenter + right2D, Color.Red, Color.Red);
                MyRenderProxy.DebugDrawLine2D(frameCenter, frameCenter + up2D, Color.Green, Color.Green);
                MyRenderProxy.DebugDrawLine2D(frameCenter, frameCenter + backward2D, Color.Blue, Color.Blue);
                MyRenderProxy.DebugDrawText2D(frameCenter + right2D, "World X", Color.Red, 0.5f, MyGuiDrawAlignEnum.HORISONTAL_CENTER_AND_VERTICAL_CENTER);
                MyRenderProxy.DebugDrawText2D(frameCenter + up2D, "World Y", Color.Green, 0.5f, MyGuiDrawAlignEnum.HORISONTAL_CENTER_AND_VERTICAL_CENTER);
                MyRenderProxy.DebugDrawText2D(frameCenter + backward2D, "World Z", Color.Blue, 0.5f, MyGuiDrawAlignEnum.HORISONTAL_CENTER_AND_VERTICAL_CENTER);

                MyComponentsDebugInputComponent.DetectedEntities.Clear();

                foreach (var entity in entities)
                {
                    if (entity.PositionComp == null)
                    {
                        continue;
                    }

                    Vector3D originalPos = entity.PositionComp.GetPosition();
                    Vector3D pos2        = originalPos + upVector * 0.1f;
                    Vector3D pos         = pos2 - rightVector * hoffset;
                    Vector3D viewVector  = Vector3D.Normalize(originalPos - playerPos);

                    double dot = Vector3D.Dot(viewVector, fwVector);
                    if (dot < 0.9995)
                    {
                        Vector3D rightObject    = entity.PositionComp.WorldMatrix.Right * 0.3f;
                        Vector3D upObject       = entity.PositionComp.WorldMatrix.Up * 0.3f;
                        Vector3D backwardObject = entity.PositionComp.WorldMatrix.Backward * 0.3f;

                        MyRenderProxy.DebugDrawSphere(originalPos, 0.01f, Color.White, 1.0f, false);
                        MyRenderProxy.DebugDrawArrow3D(originalPos, originalPos + rightObject, Color.Red, Color.Red, false, text: "X");
                        MyRenderProxy.DebugDrawArrow3D(originalPos, originalPos + upObject, Color.Green, Color.Green, false, text: "Y");
                        MyRenderProxy.DebugDrawArrow3D(originalPos, originalPos + backwardObject, Color.Blue, Color.Blue, false, text: "Z");
                        continue;
                    }

                    if (Vector3D.Distance(originalPos, lastEntityPos) < 0.01)
                    {
                        offset  += rightVector * 0.3f;
                        upVector = -upVector;
                        pos2     = originalPos + upVector * 0.1f;
                        pos      = pos2 - rightVector * hoffset;
                    }
                    lastEntityPos = originalPos;


                    double dist     = Vector3D.Distance(pos, playerPos);
                    double textSize = Math.Atan(fontSize / Math.Max(dist, 0.001));

                    float n = 0;
                    {
                        var             enumerator = entity.Components.GetEnumerator();
                        MyComponentBase component  = null;
                        while (enumerator.MoveNext())
                        {
                            component = enumerator.Current;
                            n        += GetComponentLines(component);
                        }
                        n += 1;
                        n -= GetComponentLines(component); // The last component should not make the line longer
                        enumerator.Dispose();
                    }

                    Vector3D topPos     = pos + (n + 0.5f) * upVector * lineSize;
                    Vector3D currentPos = pos + (n + 1) * upVector * lineSize + 0.01f * rightVector;

                    MyRenderProxy.DebugDrawLine3D(originalPos, pos2, Color.White, Color.White, false);
                    MyRenderProxy.DebugDrawLine3D(pos, pos2, Color.White, Color.White, false);
                    MyRenderProxy.DebugDrawLine3D(pos, topPos, Color.White, Color.White, false);
                    MyRenderProxy.DebugDrawLine3D(topPos, topPos + rightVector * 1.0f, Color.White, Color.White, false);
                    MyRenderProxy.DebugDrawText3D(currentPos, entity.GetType().ToString() + " - " + entity.DisplayName, Color.Orange, (float)textSize, false, MyGuiDrawAlignEnum.HORISONTAL_LEFT_AND_VERTICAL_CENTER);

                    MyComponentsDebugInputComponent.DetectedEntities.Add(entity);

                    foreach (var component in entity.Components)
                    {
                        currentPos = pos + n * upVector * lineSize;
                        DebugDrawComponent(component, currentPos, rightVector, upVector, lineSize, (float)textSize);
                        var    entityComponent = component as MyEntityComponentBase;
                        string compType        = entityComponent == null ? "" : entityComponent.ComponentTypeDebugString;
                        MyRenderProxy.DebugDrawText3D(currentPos - 0.02f * rightVector, compType, Color.Yellow, (float)textSize, false, MyGuiDrawAlignEnum.HORISONTAL_RIGHT_AND_VERTICAL_CENTER);
                        n -= GetComponentLines(component);
                    }
                }
                entities.Clear();
            }
        }
Esempio n. 22
0
 public int GetComponentIndex(MyComponentBase component)
 {
     return(m_components.IndexOf(component));
 }
 public bool TryGet(Type type, out MyComponentBase component)
 {
     return(m_components.TryGetValue(type, out component));
 }
Esempio n. 24
0
 /// <summary>
 /// Adds to list but doesn't change ownership
 /// </summary>
 public static void AttachComponent(this IMyComponentAggregate aggregate, MyComponentBase component)
 {
     Debug.Assert(aggregate != component, "Can not add to itself!");
     aggregate.ChildList.AddComponent(component);
 }