Commits

ptrelford  committed d6a4731

WP7 - move Pacman with accelerometer

  • Participants
  • Parent commits a642131

Comments (0)

Files changed (3)

File PacMan.Wp7/PacMan.Wp7.App/MainPhonePage.xaml.cs

-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Net;
-using System.Windows;
-using System.Windows.Controls;
-using System.Windows.Documents;
-using System.Windows.Input;
+using System.Windows;
 using System.Windows.Media;
-using System.Windows.Media.Animation;
-using System.Windows.Shapes;
 using Microsoft.Phone.Controls;
 
 namespace PacMan.App

File PacMan.Wp7/PacMan.Wp7.App/PacMan.App.Wp7.csproj

     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   <ItemGroup>
+    <Reference Include="Microsoft.Devices.Sensors" />
     <Reference Include="Microsoft.Phone" />
     <Reference Include="Microsoft.Phone.Interop" />
+    <Reference Include="Microsoft.Xna.Framework" />
     <Reference Include="System.Windows" />
     <Reference Include="system" />
     <Reference Include="System.Core" />

File PacMan.Xaml/Input.cs

 using Key = Windows.System.VirtualKey;
 #else
 using System.Windows.Input;
+#if WINDOWS_PHONE
+using Microsoft.Devices.Sensors;
+using Microsoft.Xna.Framework;
+#endif 
 #endif
 
 namespace PacMan.App
 {
     public class Input : IInput
     {
+        #if WINDOWS_PHONE
+        Accelerometer acc;
+        float x;
+        float y;
+        #endif
+
         private readonly Keys _keys;
 
         internal Input(Keys keys)
         {
             _keys = keys;
+            #if WINDOWS_PHONE
+            acc = new Accelerometer();
+            acc.CurrentValueChanged += (s, e) =>
+                {
+                    x = e.SensorReading.Acceleration.X;
+                    y = e.SensorReading.Acceleration.Z;
+                };
+            acc.Start();
+            #endif
         }
 
         public bool IsDown
         {
-            get { return _keys.IsKeyDown(Key.Down); }
+            get 
+            { 
+#if WINDOWS_PHONE
+                return y > 0;
+#else
+                return _keys.IsKeyDown(Key.Down); 
+#endif
+            }
         }
 
         public bool IsLeft
         {
-            get { return _keys.IsKeyDown(Key.Left); }
+            get 
+            {
+#if WINDOWS_PHONE
+                return x < 0;
+#else
+                return _keys.IsKeyDown(Key.Left);             
+#endif
+            }
         }
 
         public bool IsRight
         {
-            get { return _keys.IsKeyDown(Key.Right); }
+            get
+            {
+#if WINDOWS_PHONE
+                return x > 0;
+#else
+                return _keys.IsKeyDown(Key.Right);             
+#endif
+            }
         }
 
         public bool IsUp
         {
-            get { return _keys.IsKeyDown(Key.Up); }
+            get
+            {
+#if WINDOWS_PHONE
+                return y < 0;
+#else
+                return _keys.IsKeyDown(Key.Up);             
+#endif
+            }
         }
     }
 }