tasting on MonoGame
[tastes.git] / Platformer2D / Platformer2D.Core / Game / Accelerometer.cs
blob4cc525812133c19ed0188556de7c8b820bbe54b5
1 #region File Description
2 //-----------------------------------------------------------------------------
3 // Accelerometer.cs
4 //
5 // Microsoft XNA Community Game Platform
6 // Copyright (C) Microsoft Corporation. All rights reserved.
7 //-----------------------------------------------------------------------------
8 #endregion
10 #region Using Statements
11 using Microsoft.Xna.Framework;
12 using System;
13 #endregion
15 namespace Platformer2D
17 /// <summary>
18 /// A static encapsulation of accelerometer input to provide games with a polling-based
19 /// accelerometer system.
20 /// </summary>
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;
29 /// <summary>
30 /// Initializes the Accelerometer for the current game. This method can only be called once per game.
31 /// </summary>
32 public static void Initialize()
34 // make sure we don't initialize the Accelerometer twice
35 if (isInitialized)
37 throw new InvalidOperationException("Initialize can only be called once");
40 // remember that we are initialized
41 isInitialized = true;
44 /// <summary>
45 /// Gets the current state of the accelerometer.
46 /// </summary>
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
51 if (!isInitialized)
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);
63 /// <summary>
64 /// An encapsulation of the accelerometer's current state.
65 /// </summary>
66 public struct AccelerometerState
68 /// <summary>
69 /// Gets the accelerometer's current value in G-force.
70 /// </summary>
71 public Vector3 Acceleration { get; private set; }
73 /// <summary>
74 /// Gets whether or not the accelerometer is active and running.
75 /// </summary>
76 public bool IsActive { get; private set; }
78 /// <summary>
79 /// Initializes a new AccelerometerState.
80 /// </summary>
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)
84 : this()
86 Acceleration = acceleration;
87 IsActive = isActive;
90 /// <summary>
91 /// Returns a string containing the values of the Acceleration and IsActive properties.
92 /// </summary>
93 /// <returns>A new string describing the state.</returns>
94 public override string ToString()
96 return string.Format("Acceleration: {0}, IsActive: {1}", Acceleration, IsActive);