Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework;
- namespace Platform
- {
- static class Controls
- {
- static KeyboardState previousKeyState;
- static KeyboardState currentKeyState;
- static MouseState previousMouseState;
- static MouseState currentMouseState;
- static bool mouseLocked = false;
- static object caller = null;
- public static void Handle(Player player)
- {
- if (Keyboard.GetState().IsKeyDown(Keys.Space))
- {
- player.Jump();
- }
- if (Keyboard.GetState().IsKeyDown(Keys.Right))
- {
- player.MoveRight();
- }
- if (Keyboard.GetState().IsKeyDown(Keys.Left))
- {
- player.MoveLeft();
- }
- if (MouseHoldLeft)
- {
- player.Move(MousePosition);
- }
- }
- public static bool IsKeyPressed(Keys key)
- {
- bool result = currentKeyState.IsKeyDown(key) && previousKeyState.IsKeyUp(key);
- // previousKeyState = currentKeyState;
- return result;
- }
- public static bool MousePressedLeft
- {
- get { return currentMouseState.LeftButton == ButtonState.Pressed && previousMouseState.LeftButton == ButtonState.Released; }
- set { }
- }
- public static bool MousePressedRight
- {
- get { return currentMouseState.RightButton == ButtonState.Pressed && previousMouseState.RightButton == ButtonState.Released; }
- set { }
- }
- public static bool MouseHoldLeft
- {
- get
- {
- if (currentMouseState.LeftButton == ButtonState.Pressed &&
- previousMouseState.LeftButton == ButtonState.Pressed &&
- !mouseLocked)
- {
- return true;
- }
- return false;
- }
- set { }
- }
- public static bool MouseHoldRight
- {
- get { return currentMouseState.RightButton == ButtonState.Pressed && previousMouseState.RightButton == ButtonState.Pressed; }
- set { }
- }
- public static Point MousePosition
- {
- get { return new Point(currentMouseState.X, currentMouseState.Y); }
- set { }
- }
- public static Point MouseDelta
- {
- get
- {
- return new Point(currentMouseState.X - previousMouseState.X,
- currentMouseState.Y - previousMouseState.Y);
- }
- set { }
- }
- public static bool MouseLocked(object call)
- {
- if (currentMouseState.LeftButton == ButtonState.Pressed &&
- previousMouseState.LeftButton == ButtonState.Pressed)
- {
- if (caller == null)
- {
- caller = call;
- return true;
- }
- else if (caller == call)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- else
- {
- caller = null;
- return false;
- }
- }
- public static void UpdateCurrent()
- {
- currentKeyState = Keyboard.GetState();
- currentMouseState = Mouse.GetState();
- }
- public static void UpdatePrevious()
- {
- previousKeyState = currentKeyState;
- previousMouseState = currentMouseState;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment