ccmny

Controls

Aug 23rd, 2011
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.77 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework.Input;
  6. using Microsoft.Xna.Framework;
  7.  
  8. namespace Platform
  9. {
  10.     static class Controls
  11.     {
  12.         static KeyboardState previousKeyState;
  13.         static KeyboardState currentKeyState;
  14.  
  15.         static MouseState previousMouseState;
  16.         static MouseState currentMouseState;
  17.  
  18.         static bool mouseLocked = false;
  19.  
  20.         static object caller = null;
  21.  
  22.         public static void Handle(Player player)
  23.         {
  24.             if (Keyboard.GetState().IsKeyDown(Keys.Space))
  25.             {
  26.                 player.Jump();
  27.             }
  28.             if (Keyboard.GetState().IsKeyDown(Keys.Right))
  29.             {
  30.                 player.MoveRight();
  31.             }            
  32.             if (Keyboard.GetState().IsKeyDown(Keys.Left))
  33.             {
  34.                 player.MoveLeft();
  35.             }
  36.             if (MouseHoldLeft)
  37.             {
  38.                 player.Move(MousePosition);
  39.             }
  40.            
  41.         }
  42.  
  43.         public static bool IsKeyPressed(Keys key)
  44.         {
  45.             bool result = currentKeyState.IsKeyDown(key) && previousKeyState.IsKeyUp(key);
  46.            // previousKeyState = currentKeyState;
  47.             return result;
  48.         }
  49.  
  50.         public static bool MousePressedLeft
  51.         {
  52.             get { return currentMouseState.LeftButton == ButtonState.Pressed && previousMouseState.LeftButton == ButtonState.Released; }
  53.             set {  }
  54.         }
  55.  
  56.         public static bool MousePressedRight
  57.         {
  58.             get { return currentMouseState.RightButton == ButtonState.Pressed && previousMouseState.RightButton == ButtonState.Released; }
  59.             set { }
  60.         }
  61.  
  62.         public static bool MouseHoldLeft
  63.         {
  64.             get
  65.             {
  66.                 if (currentMouseState.LeftButton == ButtonState.Pressed &&
  67.                     previousMouseState.LeftButton == ButtonState.Pressed &&
  68.                     !mouseLocked)
  69.                 {
  70.                     return true;
  71.                 }
  72.                 return false;
  73.             }
  74.             set { }
  75.         }
  76.  
  77.         public static bool MouseHoldRight
  78.         {
  79.             get { return currentMouseState.RightButton == ButtonState.Pressed && previousMouseState.RightButton == ButtonState.Pressed; }
  80.             set { }
  81.         }
  82.  
  83.         public static Point MousePosition
  84.         {
  85.             get { return new Point(currentMouseState.X, currentMouseState.Y); }
  86.             set { }
  87.         }
  88.  
  89.         public static Point MouseDelta
  90.         {
  91.             get
  92.             {
  93.                 return new Point(currentMouseState.X - previousMouseState.X,
  94.                                    currentMouseState.Y - previousMouseState.Y);
  95.             }
  96.             set { }
  97.         }
  98.  
  99.         public static bool MouseLocked(object call)
  100.         {
  101.             if (currentMouseState.LeftButton == ButtonState.Pressed &&
  102.                 previousMouseState.LeftButton == ButtonState.Pressed)
  103.             {
  104.                 if (caller == null)
  105.                 {
  106.                     caller = call;
  107.                     return true;
  108.                 }
  109.                 else if (caller == call)
  110.                 {
  111.                     return true;
  112.                 }
  113.                 else
  114.                 {
  115.                     return false;
  116.                 }                
  117.             }
  118.             else
  119.             {
  120.                 caller = null;
  121.                 return false;
  122.             }
  123.         }
  124.  
  125.         public static void UpdateCurrent()
  126.         {
  127.             currentKeyState = Keyboard.GetState();
  128.             currentMouseState = Mouse.GetState();
  129.         }
  130.  
  131.         public static void UpdatePrevious()
  132.         {
  133.             previousKeyState = currentKeyState;
  134.             previousMouseState = currentMouseState;
  135.         }                
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment