Advertisement
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;
- using Microsoft.Xna.Framework.Storage;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.Graphics;
- namespace Tadv01
- {
- //inherits from Sprite.cs
- class Tad : Sprite
- {
- //Tad's Constants
- const string TAD_ASSETNAME = "Tad";
- const int START_POSITION_X = 125;
- const int START_POSITION_Y = 245;
- const int TAD_SPEED = 160;
- const int MOVE_UP = -1;
- const int MOVE_DOWN = 1;
- const int MOVE_LEFT = -1;
- const int MOVE_RIGHT = 1;
- //State storage
- enum State
- {
- Walking
- }
- State mCurrentState = State.Walking;
- //direction storage
- Vector2 mDirection = Vector2.Zero;
- //speed storage
- Vector2 mSpeed = Vector2.Zero;
- // Initializing it give you a blank KeyboardState with all keys unpressed - useful for keypresses on first update
- KeyboardState mPreviousKeyboardState = new KeyboardState();
- GamePadState mPreviousGamePadState = new GamePadState();
- public void LoadContent(ContentManager theContentManager)
- {
- Position = new Vector2(START_POSITION_X, START_POSITION_X);
- base.LoadContent(theContentManager, TAD_ASSETNAME);
- }
- public void Update(GameTime theGameTime)
- {
- //update keyboard state
- KeyboardState aCurrentKeyboardState = Keyboard.GetState();
- //update gamepadstate
- GamePadState aCurrentGamepadState = GamePad.GetState(PlayerIndex.One);
- UpdateMovement(aCurrentKeyboardState, aCurrentGamepadState);
- mPreviousKeyboardState = aCurrentKeyboardState;
- mPreviousGamePadState = aCurrentGamepadState;
- base.Update(theGameTime, mSpeed, mDirection);
- }
- private void UpdateMovement(KeyboardState aCurrentKeyboardState,
- GamePadState aCurrentGamepadState)
- {
- if (mCurrentState == State.Walking)
- {
- mSpeed = Vector2.Zero;
- mDirection = Vector2.Zero;
- // Updated to check the left thumbstick, right thumbstick, and Dpad
- if ((aCurrentKeyboardState.IsKeyDown(Keys.Left) == true) ||
- (aCurrentGamepadState.ThumbSticks.Left.X < 0.0f) ||
- (aCurrentGamepadState.ThumbSticks.Right.X < 0.0f) ||
- (aCurrentGamePadState.IsButtonDown(Buttons.DPadLeft))
- {
- mSpeed.X = TAD_SPEED;
- mDirection.X = MOVE_LEFT;
- }
- else if ((aCurrentKeyboardState.IsKeyDown(Keys.Right) == true) ||
- (aCurrentGamepadState.ThumbSticks.Left.X > 0.0f) ||
- (aCurrentGamepadState.ThumbSticks.Right.X > 0.0f) ||
- (aCurrentGamePadState.IsButtonDown(Buttons.DPadRight))
- {
- mSpeed.X = TAD_SPEED;
- mDirection.X = MOVE_RIGHT;
- }
- if ((aCurrentKeyboardState.IsKeyDown(Keys.Up) == true) ||
- (aCurrentGamepadState.ThumbSticks.Left.Y > 0.0f) ||
- (aCurrentGamepadState.ThumbSticks.Right.Y > 0.0f) ||
- (aCurrentGamePadState.IsButtonDown(Buttons.DPadUp))
- {
- mSpeed.Y = TAD_SPEED;
- mDirection.Y = MOVE_UP;
- }
- else if ((aCurrentKeyboardState.IsKeyDown(Keys.Down) == true) ||
- (aCurrentGamepadState.ThumbSticks.Left.Y < 0.0f) ||
- (aCurrentGamepadState.ThumbSticks.Right.Y < 0.0f) ||
- (aCurrentGamePadState.IsButtonDown(Buttons.DPadDown))
- {
- mSpeed.Y = TAD_SPEED;
- mDirection.Y = MOVE_DOWN;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement