XNA changes game service dynamically at run time

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 :

  • ?
  • ?

.

+5
3

, . factory . . :

public class CameraManager
{
    private Dictionary<Type, ICamera> _cameras;
    private ICamera _current;

    public ICamera Current
    {
        get
        {
            return _current;
        }
    }

    // Sets the current cammera to the internal instance of the camera type
    public void SetCurrent<T>() where T : ICamera
    {
        if (!_cameras.ContainsKey(typeof(T)))
        {
            // TODO: Instantiate a new camera class here...
        }

        _current = _cameras[typeof(T)];
    }
}

- . - . enum .

+1

. IDE , , , ..

- . - , .

: "manager" (, CameraManager),

public void SetCameraType(CameraType type) //CameraType could be an enum
public ICamera GetCamera()

( , ICameraManager).

: ... , , -

0

, , , Visible. " " "", "" "".

, :

 protected override void Update(GameTime gameTime)
  {
    var keyboard = Keyboard.GetState();
    if(keyboard.IsKeyDown(Keys.C))
    {
      if(freeCam.Enabled)
      {
        freeCam.Enabled = false;
        freeCam.Visible = false;

        staticCam.Enabled = true;
        staticCam.Visible= true;
      }
      else
      {
        freeCam.Enabled = true;
        freeCam.Visible = true;

        staticCam.Enabled = false;
        staticCam.Visible= false;
      }         
    }
    base.Update(gameTime);
  }
0
source

All Articles