Exemplo n.º 1
0
        } // AddComponent

        #endregion

        #region Remove Component

        /// <summary>
        /// Removes a component to the game object.
        /// </summary>
        /// <remarks>
        /// A component is not really destroyed, is recycled, it returns to the component pool.
        /// </remarks>
        /// <typeparam name="TComponentType">Component Type</typeparam>
        public override void RemoveComponent <TComponentType>()
        {
            #region Transform

            if (typeof(TComponentType) == typeof(Transform3D))
            {
                throw new ArgumentException("Game Object 3D: Unable to remove the 3D transform component. The transform component can’t be replaced or removed.");
            }
            if (typeof(TComponentType) == typeof(Transform2D))
            {
                throw new ArgumentException("Game Object 3D: Unable to remove the 2D transform component. A 3D Game Object does not work in 2D.");
            }

            #endregion

            #region Model Filter

            if (typeof(TComponentType) == typeof(ModelFilter))
            {
                if (modelFilterAccessor == null)
                {
                    throw new InvalidOperationException("Game Object 3D: Unable to remove the model filter component. There is not one.");
                }
                ModelFilter.Uninitialize();
                Components.Remove(ModelFilter);
                ModelFilter.ComponentPool.Release(modelFilterAccessor);
                ModelFilter         = null;
                modelFilterAccessor = null;
            }

            #endregion

            #region Model Renderer

            if (typeof(TComponentType) == typeof(ModelRenderer))
            {
                if (modelRendererAccessor == null)
                {
                    throw new InvalidOperationException("Game Object 3D: Unable to remove the model renderer component. There is not one.");
                }
                ModelRenderer.Uninitialize();
                Components.Remove(ModelRenderer);
                ModelRenderer.ComponentPool.Release(modelRendererAccessor);
                ModelRenderer         = null;
                modelRendererAccessor = null;
            }

            #endregion

            #region Animation

            if (typeof(TComponentType) == typeof(RootAnimations))
            {
                if (rootAnimationAccessor == null)
                {
                    throw new InvalidOperationException("Game Object 3D: Unable to remove the root animation component. There is not one.");
                }
                RootAnimations.Uninitialize();
                Components.Remove(RootAnimations);
                RootAnimations.ComponentPool.Release(rootAnimationAccessor);
                RootAnimations        = null;
                rootAnimationAccessor = null;
            }
            if (typeof(TComponentType) == typeof(ModelAnimations))
            {
                if (modelAnimationAccessor == null)
                {
                    throw new InvalidOperationException("Game Object 3D: Unable to remove the model animation component. There is not one.");
                }
                ModelAnimations.Uninitialize();
                Components.Remove(ModelAnimations);
                ModelAnimations.ComponentPool.Release(modelAnimationAccessor);
                ModelAnimations        = null;
                modelAnimationAccessor = null;
            }

            #endregion

            #region Camera

            if (typeof(TComponentType) == typeof(Camera))
            {
                if (cameraAccessor == null)
                {
                    throw new InvalidOperationException("Game Object 3D: Unable to remove the camera component. There is not one.");
                }
                Camera.Uninitialize();
                Components.Remove(Camera);
                Camera.ComponentPool.Release(cameraAccessor);
                Camera         = null;
                cameraAccessor = null;
                Camera.SortCamerasByRenderingOrder();
            }

            #endregion

            #region Light

            if (typeof(Light).IsAssignableFrom(typeof(TComponentType)))
            {
                if (Light == null)
                {
                    throw new InvalidOperationException("Game Object 3D: Unable to remove the light component. There is not one.");
                }
                Light.Uninitialize();
                if (typeof(TComponentType) == typeof(DirectionalLight))
                {
                    DirectionalLight.ComponentPool.Release(directionalLightAccessor);
                    directionalLightAccessor = null;
                    DirectionalLight         = null;
                }
                else if (typeof(TComponentType) == typeof(PointLight))
                {
                    PointLight.ComponentPool.Release(pointLightAccessor);
                    pointLightAccessor = null;
                    PointLight         = null;
                }
                else if (typeof(TComponentType) == typeof(SpotLight))
                {
                    SpotLight.ComponentPool.Release(spotLightAccessor);
                    spotLightAccessor = null;
                    SpotLight         = null;
                }
                else
                {
                    throw new InvalidOperationException("Game Object 3D: Unable to remove the light component.");
                }
                Components.Remove(Light);
                Light = null;
            }

            #endregion

            #region Particle Emitter

            if (typeof(TComponentType) == typeof(ParticleEmitter))
            {
                if (particleEmitterAccessor == null)
                {
                    throw new InvalidOperationException("Game Object 3D: Unable to remove the particle emitter component. There is not one.");
                }
                ParticleEmitter.Uninitialize();
                Components.Remove(ParticleEmitter);
                ParticleEmitter.ComponentPool.Release(particleEmitterAccessor);
                ParticleEmitter         = null;
                particleEmitterAccessor = null;
            }

            #endregion

            #region Particle Renderer

            if (typeof(TComponentType) == typeof(ParticleRenderer))
            {
                if (particleRendererAccessor == null)
                {
                    throw new InvalidOperationException("Game Object 3D: Unable to remove the particle renderer component. There is not one.");
                }
                ParticleRenderer.Uninitialize();
                Components.Remove(ParticleRenderer);
                ParticleRenderer.ComponentPool.Release(particleRendererAccessor);
                ParticleRenderer         = null;
                particleRendererAccessor = null;
            }

            #endregion

            #region Script

            if (typeof(Script).IsAssignableFrom(typeof(TComponentType)))
            {
                Component script = XNAFinalEngine.Components.Script.ContainScript <TComponentType>(this);
                if (script == null)
                {
                    throw new ArgumentException("Game Object 3D: Unable to remove the script component. There is not one.");
                }
                script.Uninitialize();
                Components.Remove(script);
                scripts.Remove((Script)script);
            }

            #endregion

            #region Sound Emitter

            if (typeof(TComponentType) == typeof(SoundEmitter))
            {
                if (soundEmitterAccessor == null)
                {
                    throw new InvalidOperationException("Game Object 3D: Unable to remove the sound emitter component. There is not one.");
                }
                SoundEmitter.Uninitialize();
                Components.Remove(SoundEmitter);
                SoundEmitter.ComponentPool.Release(soundEmitterAccessor);
                SoundEmitter         = null;
                soundEmitterAccessor = null;
            }

            #endregion

            #region Sound Listener

            if (typeof(TComponentType) == typeof(SoundListener))
            {
                if (soundListenerAccessor == null)
                {
                    throw new InvalidOperationException("Game Object 3D: Unable to remove the sound listener component. There is not one.");
                }
                SoundListener.Uninitialize();
                Components.Remove(SoundListener);
                SoundListener.ComponentPool.Release(soundListenerAccessor);
                SoundListener         = null;
                soundListenerAccessor = null;
            }

            #endregion

            #region HUD Text

            if (typeof(TComponentType) == typeof(HudText))
            {
                if (hudTextAccessor == null)
                {
                    throw new InvalidOperationException("Game Object 3D: Unable to remove the HUD text component. There is not one.");
                }
                HudText.Uninitialize();
                Components.Remove(HudText);
                HudText.ComponentPool3D.Release(hudTextAccessor);
                HudText         = null;
                hudTextAccessor = null;
            }

            #endregion

            #region HUD Texture

            if (typeof(TComponentType) == typeof(HudTexture))
            {
                if (hudTextureAccessor == null)
                {
                    throw new InvalidOperationException("Game Object 3D: Unable to remove the HUD texture component. There is not one.");
                }
                HudTexture.Uninitialize();
                Components.Remove(HudTexture);
                HudTexture.ComponentPool3D.Release(hudTextureAccessor);
                HudTexture         = null;
                hudTextureAccessor = null;
            }

            #endregion

            #region Line Renderer

            if (typeof(TComponentType) == typeof(LineRenderer))
            {
                if (lineRendererAccessor == null)
                {
                    throw new InvalidOperationException("Game Object 3D: Unable to remove the line renderer component. There is not one.");
                }
                LineRenderer.Uninitialize();
                Components.Remove(LineRenderer);
                LineRenderer.ComponentPool3D.Release(lineRendererAccessor);
                LineRenderer         = null;
                lineRendererAccessor = null;
            }

            #endregion

            #region Rigid Body

            if (typeof(TComponentType) == typeof(RigidBody))
            {
                if (rigidBodyAccessor == null)
                {
                    throw new InvalidOperationException("Game Object 3D: Unable to remove the rigid body component. There is not one.");
                }
                RigidBody.Uninitialize();
                Components.Remove(RigidBody);
                RigidBody.ComponentPool.Release(rigidBodyAccessor);
                RigidBody         = null;
                rigidBodyAccessor = null;
            }

            #endregion

            #region Static Collider

            if (typeof(TComponentType) == typeof(StaticCollider))
            {
                if (staticColliderAccessor == null)
                {
                    throw new InvalidOperationException("Game Object 3D: Unable to remove the static collider component. There is not one.");
                }
                StaticCollider.Uninitialize();
                Components.Remove(StaticCollider);
                StaticCollider.ComponentPool.Release(staticColliderAccessor);
                StaticCollider         = null;
                staticColliderAccessor = null;
            }

            #endregion
        } // RemoveComponent
Exemplo n.º 2
0
        } // DisposeManagedResources

        #endregion

        #region Add Component

        /// <summary>
        /// Adds a component of type TComponentType to the game object.
        /// </summary>
        /// <typeparam name="TComponentType">Component Type</typeparam>
        public override Component AddComponent <TComponentType>()
        {
            Component result = null;

            #region Transform

            // Get from a pool or create the component.
            if (typeof(TComponentType) == typeof(Transform3D))
            {
                throw new ArgumentException("Game Object 3D: Unable to create the 3D transform component. The transform component can’t be replaced or removed.");
            }
            if (typeof(TComponentType) == typeof(Transform2D))
            {
                throw new ArgumentException("Game Object 3D: Unable to create the 2D transform component. A 3D Game Object does not work in 2D.");
            }

            #endregion

            #region Model Filter

            if (typeof(TComponentType) == typeof(ModelFilter))
            {
                if (modelFilterAccessor != null)
                {
                    throw new ArgumentException("Game Object 3D: Unable to create the model filter component. There is one already.");
                }
                // Search for an empty component in the pool.
                modelFilterAccessor = ModelFilter.ComponentPool.Fetch();
                // A component is a reference value, so no problem to do this.
                ModelFilter = ModelFilter.ComponentPool[modelFilterAccessor];
                // Initialize the component to the default values.
                ModelFilter.Initialize(this);
                result = ModelFilter;
            }

            #endregion

            #region Model Renderer

            if (typeof(TComponentType) == typeof(ModelRenderer))
            {
                if (modelRendererAccessor != null)
                {
                    throw new ArgumentException("Game Object 3D: Unable to create the model renderer component. There is one already.");
                }
                // Search for an empty component in the pool.
                modelRendererAccessor = ModelRenderer.ComponentPool.Fetch();
                // A component is a reference value, so no problem to do this.
                ModelRenderer = ModelRenderer.ComponentPool[modelRendererAccessor];
                // Initialize the component to the default values.
                ModelRenderer.Initialize(this);
                result = ModelRenderer;
            }

            #endregion

            #region Root Animation

            if (typeof(TComponentType) == typeof(RootAnimations))
            {
                if (rootAnimationAccessor != null)
                {
                    throw new ArgumentException("Game Object 3D: Unable to create the root animation component. There is one already.");
                }
                // Search for an empty component in the pool.
                rootAnimationAccessor = RootAnimations.ComponentPool.Fetch();
                // A component is a reference value, so no problem to do this.
                RootAnimations = RootAnimations.ComponentPool[rootAnimationAccessor];
                // Initialize the component to the default values.
                RootAnimations.Initialize(this);
                result = RootAnimations;
            }

            #endregion

            #region Model Animation

            if (typeof(TComponentType) == typeof(ModelAnimations))
            {
                if (modelAnimationAccessor != null)
                {
                    throw new ArgumentException("Game Object 3D: Unable to create the model animation component. There is one already.");
                }
                // Search for an empty component in the pool.
                modelAnimationAccessor = ModelAnimations.ComponentPool.Fetch();
                // A component is a reference value, so no problem to do this.
                ModelAnimations = ModelAnimations.ComponentPool[modelAnimationAccessor];
                // Initialize the component to the default values.
                ModelAnimations.Initialize(this);
                result = ModelAnimations;
            }

            #endregion

            #region Camera

            if (typeof(TComponentType) == typeof(Camera))
            {
                if (cameraAccessor != null)
                {
                    throw new ArgumentException("Game Object 3D: Unable to create the camera component. There is one already.");
                }
                // Search for an empty component in the pool.
                cameraAccessor = Camera.ComponentPool.Fetch();
                // A component is a reference value, so no problem to do this.
                Camera = Camera.ComponentPool[cameraAccessor];
                // Initialize the component to the default values.
                Camera.Initialize(this);
                result = Camera;
            }

            #endregion

            #region Light

            if (typeof(Light).IsAssignableFrom(typeof(TComponentType)))
            {
                if (Light != null)
                {
                    throw new ArgumentException("Game Object 3D: Unable to create the light component. There is one already.");
                }
                if (typeof(TComponentType) == typeof(DirectionalLight))
                {
                    // Search for an empty component in the pool.
                    directionalLightAccessor = DirectionalLight.ComponentPool.Fetch();
                    // A component is a reference value, so no problem to do this.
                    Light            = DirectionalLight.ComponentPool[directionalLightAccessor];
                    DirectionalLight = DirectionalLight.ComponentPool[directionalLightAccessor];
                }
                else if (typeof(TComponentType) == typeof(PointLight))
                {
                    // Search for an empty component in the pool.
                    pointLightAccessor = PointLight.ComponentPool.Fetch();
                    // A component is a reference value, so no problem to do this.
                    Light      = PointLight.ComponentPool[pointLightAccessor];
                    PointLight = PointLight.ComponentPool[pointLightAccessor];
                }
                else if (typeof(TComponentType) == typeof(SpotLight))
                {
                    // Search for an empty component in the pool.
                    spotLightAccessor = SpotLight.ComponentPool.Fetch();
                    // A component is a reference value, so no problem to do this.
                    Light     = SpotLight.ComponentPool[spotLightAccessor];
                    SpotLight = SpotLight.ComponentPool[spotLightAccessor];
                }
                else
                {
                    throw new ArgumentException("Game Object 3D: Unable to create the light component.");
                }
                // Initialize the component to the default values.
                Light.Initialize(this);
                result = Light;
            }

            #endregion

            #region Particle Emitter

            if (typeof(ParticleEmitter).IsAssignableFrom(typeof(TComponentType)))
            {
                if (ParticleEmitter != null)
                {
                    throw new ArgumentException("Game Object 3D: Unable to create the particle emitter component. There is one already.");
                }
                // Search for an empty component in the pool.
                particleEmitterAccessor = ParticleEmitter.ComponentPool.Fetch();
                // A component is a reference value, so no problem to do this.
                ParticleEmitter = ParticleEmitter.ComponentPool[particleEmitterAccessor];
                // Initialize the component to the default values.
                ParticleEmitter.Initialize(this);
                result = ParticleEmitter;
            }

            #endregion

            #region Particle Renderer

            if (typeof(TComponentType) == typeof(ParticleRenderer))
            {
                if (particleRendererAccessor != null)
                {
                    throw new ArgumentException("Game Object 3D: Unable to create the particle renderer component. There is one already.");
                }
                // Search for an empty component in the pool.
                particleRendererAccessor = ParticleRenderer.ComponentPool.Fetch();
                // A component is a reference value, so no problem to do this.
                ParticleRenderer = ParticleRenderer.ComponentPool[particleRendererAccessor];
                // Initialize the component to the default values.
                ParticleRenderer.Initialize(this);
                result = ParticleRenderer;
            }

            #endregion

            #region Script

            if (typeof(Script).IsAssignableFrom(typeof(TComponentType)))
            {
                Component script = XNAFinalEngine.Components.Script.ContainScript <TComponentType>(this);
                if (script != null)
                {
                    throw new ArgumentException("Game Object 3D: Unable to create the script component. There is one already.");
                }
                script = XNAFinalEngine.Components.Script.FetchScript <TComponentType>();
                if (script == null)
                {
                    script = new TComponentType();
                    XNAFinalEngine.Components.Script.ScriptList.Add((Script)script);
                }
                script.Initialize(this);
                scripts.Add((Script)script);
                result = script;
            }

            #endregion

            #region Sound Emitter

            if (typeof(TComponentType) == typeof(SoundEmitter))
            {
                if (soundEmitterAccessor != null)
                {
                    throw new ArgumentException("Game Object 3D: Unable to create the sound emitter component. There is one already.");
                }
                // Search for an empty component in the pool.
                soundEmitterAccessor = SoundEmitter.ComponentPool.Fetch();
                // A component is a reference value, so no problem to do this.
                SoundEmitter = SoundEmitter.ComponentPool[soundEmitterAccessor];
                // Initialize the component to the default values.
                SoundEmitter.Initialize(this);
                result = SoundEmitter;
            }

            #endregion

            #region Sound Listener

            if (typeof(TComponentType) == typeof(SoundListener))
            {
                if (soundListenerAccessor != null)
                {
                    throw new ArgumentException("Game Object 3D: Unable to create the sound listener component. There is one already.");
                }
                // Search for an empty component in the pool.
                soundListenerAccessor = SoundListener.ComponentPool.Fetch();
                // A component is a reference value, so no problem to do this.
                SoundListener = SoundListener.ComponentPool[soundListenerAccessor];
                // Initialize the component to the default values.
                SoundListener.Initialize(this);
                result = SoundListener;
            }

            #endregion

            #region HUD Text

            if (typeof(TComponentType) == typeof(HudText))
            {
                if (hudTextAccessor != null)
                {
                    throw new ArgumentException("Game Object 3D: Unable to create the HUD text component. There is one already.");
                }
                // Search for an empty component in the pool.
                hudTextAccessor = HudText.ComponentPool3D.Fetch();
                // A component is a reference value, so no problem to do this.
                HudText = HudText.ComponentPool3D[hudTextAccessor];
                // Initialize the component to the default values.
                HudText.Initialize(this);
                result = HudText;
            }

            #endregion

            #region HUD Texture

            if (typeof(TComponentType) == typeof(HudTexture))
            {
                if (hudTextureAccessor != null)
                {
                    throw new ArgumentException("Game Object 3D: Unable to create the HUD texture component. There is one already.");
                }
                // Search for an empty component in the pool.
                hudTextureAccessor = HudTexture.ComponentPool3D.Fetch();
                // A component is a reference value, so no problem to do this.
                HudTexture = HudTexture.ComponentPool3D[hudTextureAccessor];
                // Initialize the component to the default values.
                HudTexture.Initialize(this);
                result = HudTexture;
            }

            #endregion

            #region Line Renderer

            if (typeof(TComponentType) == typeof(LineRenderer))
            {
                if (lineRendererAccessor != null)
                {
                    throw new ArgumentException("Game Object 3D: Unable to create the line renderer component. There is one already.");
                }
                // Search for an empty component in the pool.
                lineRendererAccessor = LineRenderer.ComponentPool3D.Fetch();
                // A component is a reference value, so no problem to do this.
                LineRenderer = LineRenderer.ComponentPool3D[lineRendererAccessor];
                // Initialize the component to the default values.
                lineRenderer.Initialize(this);
                result = lineRenderer;
            }

            #endregion

            #region Rigid Body

            if (typeof(TComponentType) == typeof(RigidBody))
            {
                if (StaticCollider != null)
                {
                    throw new ArgumentException("Game Object 3D: Unable to create the rigid body component. There is one static collider component already.");
                }
                if (rigidBodyAccessor != null)
                {
                    throw new ArgumentException("Game Object 3D: Unable to create the rigid body component. There is one already.");
                }
                // Search for an empty component in the pool.
                rigidBodyAccessor = RigidBody.ComponentPool.Fetch();
                // A component is a reference value, so no problem to do this.
                RigidBody = RigidBody.ComponentPool[rigidBodyAccessor];
                // Initialize the component to the default values.
                rigidBody.Initialize(this);
                result = rigidBody;
            }

            #endregion

            #region Static Collider

            if (typeof(TComponentType) == typeof(StaticCollider))
            {
                if (RigidBody != null)
                {
                    throw new ArgumentException("Game Object 3D: Unable to create the static collider component. There is one rigid body component already.");
                }
                if (staticColliderAccessor != null)
                {
                    throw new ArgumentException("Game Object 3D: Unable to create the static collider component. There is one already.");
                }
                // Search for an empty component in the pool.
                staticColliderAccessor = StaticCollider.ComponentPool.Fetch();
                // A component is a reference value, so no problem to do this.
                StaticCollider = StaticCollider.ComponentPool[staticColliderAccessor];
                // Initialize the component to the default values.
                staticCollider.Initialize(this);
                result = staticCollider;
            }

            #endregion

            if (result == null)
            {
                throw new ArgumentException("Game Object 3D: Unknown component type.");
            }
            Components.Add(result);
            return(result);
        } // AddComponent