I have an interface (ICamera), which is implemented by two classes (FreeCamera, StaticCamera). Classes inherit from GameComponent.
Define example:
public class FreeCamera : GameComponent, ICamera
{
...
}
Now I add classes to the components of the game and register one of the components in the game service
private FreeCamera freeCam;
private StaticCamera staticCam;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
freeCam = new FreeCamera(this) { Enabled = true };
staticCam = new StaticCamera(this) { Enabled = false };
Services.AddService(typeof(ICamera, freeCam);
Components.Add(freeCam);
Components.Add(staticCam);
...
}
Then I want to change the provider for the service during application flow using the switch function
namespace Game1
{
protected override void Update(GameTime gameTime)
{
var keyboard = Keyboard.GetState();
if(keyboard.IsKeyDown(Keys.C))
{
if(freeCam.Enabled)
{
Services.RemoveService(typeof(ICamera));
Services.AddService(typeof(ICamera, staticCam);
freeCam.Enabled = !freeCam.Enabled;
staticCam.Enabled = !staticCam.Enabled;
}
else
{
Services.RemoveService(typeof(ICamera));
Services.AddService(typeof(ICamera, freeCam);
freeCam.Enabled = !freeCam.Enabled;
staticCam.Enabled = !staticCam.Enabled;
}
}
base.Update(gameTime);
}
}
StaticCamera only accepts mouse input (you can rotate the camera), FreeCamera can also be moved using keyboard input. When I call the method above (by pressing C on the keyboard), the FreeCamera class is deactivated, but the viewport seems to be frozen and does not respond to any input. When I call the method again after a while, FreeCamera is activated again, and everything works as expected.
2 :
.