Ejemplo n.º 1
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
Ejemplo 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(Transform2D))
            {
                throw new ArgumentException("Game Object exception. Unable to create the 2D transform component. The transform component can’t be replaced or removed.");
            }
            if (typeof(TComponentType) == typeof(Transform3D))
            {
                throw new ArgumentException("Game Object exception. Unable to create the 3D transform component. A 2D Game Object does not work in 3D.");
            }

            #endregion

            #region HUD Text

            if (typeof(TComponentType) == typeof(HudText))
            {
                if (hudTextAccessor != null)
                {
                    throw new ArgumentException("Game Object 2D: Unable to create the HUD text component. There is one already.");
                }
                // Search for an empty component in the pool.
                hudTextAccessor = HudText.ComponentPool2D.Fetch();
                // A component is a reference value, so no problem to do this.
                HudText = HudText.ComponentPool2D[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 2D: Unable to create the HUD texture component. There is one already.");
                }
                // Search for an empty component in the pool.
                hudTextureAccessor = HudTexture.ComponentPool2D.Fetch();
                // A component is a reference value, so no problem to do this.
                HudTexture = HudTexture.ComponentPool2D[hudTextureAccessor];
                // Initialize the component to the default values.
                HudTexture.Initialize(this);
                result = HudTexture;
            }

            #endregion

            #region Video Renderer

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

            #endregion

            #region Line Renderer

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

            #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

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