As mentioned in my previous post, there is support for mouse input in the XNA framework. I have added code to check for mouse input in the UpdateInput() method of Game1.cs as shown below:
protected void UpdateInput()
{
//get the gamepad state
GamePadState current_state = GamePad.GetState(PlayerIndex.One);
if (current_state.IsConnected)
{
//original gamepad checking goes here
}
else
{
MouseState mouseState = Mouse.GetState();
Rectangle rt = this.Window.ClientBounds;
if (mouseState.LeftButton == ButtonState.Pressed)
{
modelrotation -= (mouseState.X - rt.Width/2) * 0.01f;
//create some velocity if the right trigger is down
Vector3 modelvelocity_add = Vector3.Zero;
//find out what direction we should be thrusting, using rotation
modelvelocity_add.X = -(float)Math.Sin(modelrotation);
modelvelocity_add.Z = -(float)Math.Cos(modelrotation);
//finally, add this vector to our velocity.
modelvelocity += modelvelocity_add;
}
//in case you get lost, warp back to the center
if (mouseState.RightButton == ButtonState.Pressed)
{
modelposition = Vector3.Zero;
modelvelocity = Vector3.Zero;
modelrotation = 0.0f;
}
//set some audio based on whether we're pressing trigger
if (mouseState.LeftButton == ButtonState.Pressed)
{
if (engine_sound == null)
{
engine_sound = sound_bank.GetCue("engine_2");
engine_sound.Play();
}
else if (engine_sound.IsPaused)
{
engine_sound.Resume();
}
}
else
{
if (engine_sound != null && engine_sound.IsPlaying)
{
engine_sound.Pause();
}
}
//in case you get lost, press A to warp back to the center
if (mouseState.RightButton == ButtonState.Pressed)
{
//make a sound when we warp
sound_bank.PlayCue("hyperspace_activate");
}
Mouse.SetPosition(rt.Width / 2, rt.Height / 2);
}
}
Hold down the left button of the mouse adds thrust to the space ship. Turning the mouse while holding down the left button rotates the ship. Right click to warp the ship back to the center of the screen.
The first part of the code computes the ship's velocity based on the mouse's position and button states. The second added the appropriate audio cues based on the mouse events.