} // 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
} // 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(Transform2D)) { throw new ArgumentException("Game Object 2D: Unable to remove the 2D transform component. The transform component can’t be replaced or removed."); } if (typeof(TComponentType) == typeof(Transform3D)) { throw new ArgumentException("Game Object 2D: Unable to remove 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 InvalidOperationException("Game Object 2D: Unable to remove the HUD text component. There is not one."); } HudText.Uninitialize(); Components.Remove(HudText); HudText.ComponentPool2D.Release(hudTextAccessor); HudText = null; hudTextAccessor = null; } #endregion #region HUD Texture if (typeof(TComponentType) == typeof(HudTexture)) { if (hudTextureAccessor == null) { throw new InvalidOperationException("Game Object 2D: Unable to remove the HUD texture component. There is not one."); } HudTexture.Uninitialize(); Components.Remove(HudTexture); HudTexture.ComponentPool2D.Release(hudTextureAccessor); HudTexture = null; hudTextureAccessor = null; } #endregion #region Video Renderer if (typeof(TComponentType) == typeof(VideoRenderer)) { if (videoRendererAccessor == null) { throw new InvalidOperationException("Game Object 2D: Unable to remove the video renderer component. There is not one."); } VideoRenderer.Uninitialize(); Components.Remove(VideoRenderer); VideoRenderer.ComponentPool.Release(videoRendererAccessor); VideoRenderer = null; videoRendererAccessor = null; } #endregion #region Line Renderer if (typeof(TComponentType) == typeof(LineRenderer)) { if (lineRendererAccessor == null) { throw new InvalidOperationException("Game Object 2D: Unable to remove the line renderer component. There is not one."); } LineRenderer.Uninitialize(); Components.Remove(LineRenderer); LineRenderer.ComponentPool2D.Release(lineRendererAccessor); LineRenderer = null; lineRendererAccessor = 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 2D: Unable to remove the script component. There is not one."); } script.Uninitialize(); Components.Remove(script); scripts.Remove((Script)script); } #endregion } // RemoveComponent