Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.07 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;
  6. using Microsoft.Xna.Framework.Storage;
  7. using Microsoft.Xna.Framework.Input;
  8. using Microsoft.Xna.Framework.Content;
  9. using Microsoft.Xna.Framework.Graphics;
  10.  
  11. namespace Tadv01
  12. {
  13.     //inherits from Sprite.cs
  14.     class Tad : Sprite
  15.     {
  16.         //Tad's Constants
  17.         const string TAD_ASSETNAME = "Tad";
  18.         const int START_POSITION_X = 125;
  19.         const int START_POSITION_Y = 245;
  20.         const int TAD_SPEED = 160;
  21.         const int MOVE_UP = -1;
  22.         const int MOVE_DOWN = 1;
  23.         const int MOVE_LEFT = -1;
  24.         const int MOVE_RIGHT = 1;
  25.  
  26.         //State storage
  27.         enum State
  28.         {
  29.             Walking
  30.         }
  31.         State mCurrentState = State.Walking;
  32.  
  33.         //direction storage
  34.         Vector2 mDirection = Vector2.Zero;
  35.         //speed storage
  36.         Vector2 mSpeed = Vector2.Zero;
  37.  
  38.      // Initializing it give you a blank KeyboardState with all keys unpressed - useful for keypresses on first update
  39.      KeyboardState mPreviousKeyboardState = new KeyboardState();
  40.      GamePadState mPreviousGamePadState = new GamePadState();
  41.  
  42.         public void LoadContent(ContentManager theContentManager)
  43.         {
  44.             Position = new Vector2(START_POSITION_X, START_POSITION_X);
  45.             base.LoadContent(theContentManager, TAD_ASSETNAME);
  46.         }
  47.  
  48.         public void Update(GameTime theGameTime)
  49.         {
  50.             //update keyboard state
  51.             KeyboardState aCurrentKeyboardState = Keyboard.GetState();
  52.             //update gamepadstate
  53.             GamePadState aCurrentGamepadState = GamePad.GetState(PlayerIndex.One);
  54.             UpdateMovement(aCurrentKeyboardState, aCurrentGamepadState);
  55.             mPreviousKeyboardState = aCurrentKeyboardState;
  56.             mPreviousGamePadState = aCurrentGamepadState;
  57.  
  58.             base.Update(theGameTime, mSpeed, mDirection);
  59.         }
  60.  
  61.         private void UpdateMovement(KeyboardState aCurrentKeyboardState,
  62.             GamePadState aCurrentGamepadState)
  63.         {
  64.             if (mCurrentState == State.Walking)
  65.             {
  66.                 mSpeed = Vector2.Zero;
  67.                 mDirection = Vector2.Zero;
  68.  
  69.                 // Updated to check the left thumbstick, right thumbstick, and Dpad
  70.                 if ((aCurrentKeyboardState.IsKeyDown(Keys.Left) == true) ||
  71.                     (aCurrentGamepadState.ThumbSticks.Left.X < 0.0f) ||
  72.                     (aCurrentGamepadState.ThumbSticks.Right.X < 0.0f) ||
  73.                     (aCurrentGamePadState.IsButtonDown(Buttons.DPadLeft))
  74.                 {
  75.                     mSpeed.X = TAD_SPEED;
  76.                     mDirection.X = MOVE_LEFT;
  77.                 }
  78.                 else if ((aCurrentKeyboardState.IsKeyDown(Keys.Right) == true) ||                        
  79.                     (aCurrentGamepadState.ThumbSticks.Left.X > 0.0f) ||
  80.                     (aCurrentGamepadState.ThumbSticks.Right.X > 0.0f) ||
  81.                     (aCurrentGamePadState.IsButtonDown(Buttons.DPadRight))
  82.                 {
  83.                     mSpeed.X = TAD_SPEED;
  84.                     mDirection.X = MOVE_RIGHT;
  85.                 }
  86.  
  87.                 if ((aCurrentKeyboardState.IsKeyDown(Keys.Up) == true) ||
  88.                     (aCurrentGamepadState.ThumbSticks.Left.Y > 0.0f) ||
  89.                     (aCurrentGamepadState.ThumbSticks.Right.Y > 0.0f) ||
  90.                     (aCurrentGamePadState.IsButtonDown(Buttons.DPadUp))
  91.                 {
  92.                     mSpeed.Y = TAD_SPEED;
  93.                     mDirection.Y = MOVE_UP;
  94.                 }
  95.                 else if ((aCurrentKeyboardState.IsKeyDown(Keys.Down) == true) ||
  96.                     (aCurrentGamepadState.ThumbSticks.Left.Y < 0.0f) ||
  97.                     (aCurrentGamepadState.ThumbSticks.Right.Y < 0.0f) ||
  98.                     (aCurrentGamePadState.IsButtonDown(Buttons.DPadDown))
  99.                 {
  100.                     mSpeed.Y = TAD_SPEED;
  101.                     mDirection.Y = MOVE_DOWN;
  102.                 }
  103.             }
  104.         }
  105.        
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement