1 #region File Description
2 //-----------------------------------------------------------------------------
5 // Microsoft XNA Community Game Platform
6 // Copyright (C) Microsoft Corporation. All rights reserved.
7 //-----------------------------------------------------------------------------
10 #region Using Statements
11 using Microsoft
.Xna
.Framework
;
15 namespace Platformer2D
18 /// A static encapsulation of accelerometer input to provide games with a polling-based
19 /// accelerometer system.
21 public static class Accelerometer
23 // we want to prevent the Accelerometer from being initialized twice.
24 private static bool isInitialized
= false;
26 // whether or not the accelerometer is active
27 private static bool isActive
= false;
30 /// Initializes the Accelerometer for the current game. This method can only be called once per game.
32 public static void Initialize()
34 // make sure we don't initialize the Accelerometer twice
37 throw new InvalidOperationException("Initialize can only be called once");
40 // remember that we are initialized
45 /// Gets the current state of the accelerometer.
47 /// <returns>A new AccelerometerState with the current state of the accelerometer.</returns>
48 public static AccelerometerState
GetState()
50 // make sure we've initialized the Accelerometer before we try to get the state
53 throw new InvalidOperationException("You must Initialize before you can call GetState");
56 // create a new value for our state
57 Vector3 stateValue
= new Vector3();
59 return new AccelerometerState(stateValue
, isActive
);
64 /// An encapsulation of the accelerometer's current state.
66 public struct AccelerometerState
69 /// Gets the accelerometer's current value in G-force.
71 public Vector3 Acceleration { get; private set; }
74 /// Gets whether or not the accelerometer is active and running.
76 public bool IsActive { get; private set; }
79 /// Initializes a new AccelerometerState.
81 /// <param name="acceleration">The current acceleration (in G-force) of the accelerometer.</param>
82 /// <param name="isActive">Whether or not the accelerometer is active.</param>
83 public AccelerometerState(Vector3 acceleration
, bool isActive
)
86 Acceleration
= acceleration
;
91 /// Returns a string containing the values of the Acceleration and IsActive properties.
93 /// <returns>A new string describing the state.</returns>
94 public override string ToString()
96 return string.Format("Acceleration: {0}, IsActive: {1}", Acceleration
, IsActive
);