#region File Description //----------------------------------------------------------------------------- // ShipInput.cs // // Microsoft XNA Community Game Platform // Copyright (C) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- #endregion #region Using Statements using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Net; #endregion namespace NetRumble { /// /// Player input abstraction for ships. /// public struct ShipInput { #region Static Constants /// /// The empty version of this structure. /// private static ShipInput empty = new ShipInput(Vector2.Zero, Vector2.Zero, false); public static ShipInput Empty { get { return empty; } } #endregion #region Input Data /// /// The left-stick value of the ship input, used for movement. /// public Vector2 LeftStick; /// /// The right-stick value of the ship input, used for firing. /// public Vector2 RightStick; /// /// If true, the player is trying to fire a mine. /// public bool MineFired; #endregion #region Initialization Methods /// /// Constructs a new ShipInput object. /// /// The left-stick value of the ship input. /// The right-stick value of the ship input. /// If true, the player is firing a mine. public ShipInput(Vector2 leftStick, Vector2 rightStick, bool mineFired) { this.LeftStick = leftStick; this.RightStick = rightStick; this.MineFired = mineFired; } /// /// Create a new ShipInput object based on the data in the packet. /// /// The packet with the data. public ShipInput(PacketReader packetReader) { // safety-check the parameters, as they must be valid if (packetReader == null) { throw new ArgumentNullException("packetReader"); } // read the data LeftStick = packetReader.ReadVector2(); RightStick = packetReader.ReadVector2(); MineFired = packetReader.ReadBoolean(); } /// /// Create a new ShipInput object based on local input state. /// /// The local gamepad state. /// The local keyboard state public ShipInput(GamePadState gamePadState, KeyboardState keyboardState) { // check for movement action bool keyHit = false; LeftStick = Vector2.Zero; if (keyboardState.IsKeyDown(Keys.W)) { LeftStick += Vector2.UnitY; keyHit = true; } if (keyboardState.IsKeyDown(Keys.A)) { LeftStick += Vector2.Multiply(Vector2.UnitX, -1f); keyHit = true; } if (keyboardState.IsKeyDown(Keys.S)) { LeftStick += Vector2.Multiply(Vector2.UnitY, -1f); keyHit = true; } if (keyboardState.IsKeyDown(Keys.D)) { LeftStick += Vector2.UnitX; keyHit = true; } if (keyHit) { if (LeftStick.LengthSquared() > 0) { LeftStick.Normalize(); } } else { LeftStick = gamePadState.ThumbSticks.Left; } // check for firing action keyHit = false; RightStick = Vector2.Zero; if (keyboardState.IsKeyDown(Keys.Up)) { RightStick += Vector2.UnitY; keyHit = true; } if (keyboardState.IsKeyDown(Keys.Left)) { RightStick += Vector2.Multiply(Vector2.UnitX, -1f); keyHit = true; } if (keyboardState.IsKeyDown(Keys.Down)) { RightStick += Vector2.Multiply(Vector2.UnitY, -1f); keyHit = true; } if (keyboardState.IsKeyDown(Keys.Right)) { RightStick += Vector2.UnitX; keyHit = true; } if (keyHit) { if (RightStick.LengthSquared() > 0) { RightStick.Normalize(); } } else { RightStick = gamePadState.ThumbSticks.Right; } // check for firing a mine keyHit = false; MineFired = false; if (keyboardState.IsKeyDown(Keys.Space)) { MineFired = true; keyHit = true; } if (!keyHit) { MineFired = gamePadState.Triggers.Right >= 0.9f; } } #endregion #region Networking Methods /// /// Serialize the object out to a packet. /// /// The packet to write to. public void Serialize(PacketWriter packetWriter) { // safety-check the parameters, as they must be valid if (packetWriter == null) { throw new ArgumentNullException("packetWriter"); } packetWriter.Write((int)World.PacketTypes.ShipInput); packetWriter.Write(LeftStick); packetWriter.Write(RightStick); packetWriter.Write(MineFired); } #endregion } }