namespace WindowsGame1 { class UserControlledSprite : Sprite { MouseState prevMouseState; protected Vector2 velocity = new Vector2(0, -5); protected Vector2 prevVelocity = new Vector2 (0, -5); protected Vector2 acceleration = new Vector2(0, -0.10f); protected Vector2 gravity = new Vector2(0, 0.30f); protected int timeSinceLastJump = 0; bool onFloor = false;
// These Constructors access all of the variables in the base class. sprite.cs public UserControlledSprite(Texture2D textureImage, Vector2 position, Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize, Vector2 speed) : base(textureImage, position, frameSize, collisionOffset, currentFrame, sheetSize, speed, null) { }
// Same here. public UserControlledSprite(Texture2D textureImage, Vector2 position, Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize, Vector2 speed, int millisecondsPerFrame) : base(textureImage, position, frameSize, collisionOffset, currentFrame, sheetSize, speed, millisecondsPerFrame, null) { }
// All of these properties must do exactly what the base class defines, and any anything extra can go here. Except for the Update and Draw methods. public override Vector2 Direction { get { // Creates a variable to store a direction to be passed back to the sprite base class. Vector2 inputDirection = Vector2.Zero; GameTime gameTime = new GameTime();
// This adds gravity at all times to the player to make sure he's not standing in thin air. if (onFloor == false) { inputDirection.Y += gravity.Y * 8; } else { inputDirection.Y = 0; }
// Simple keyboard checks here, for left, right, up and down arrow keys. if (Keyboard.GetState().IsKeyDown(Keys.Left)) { inputDirection.X -= 1; } if (Keyboard.GetState().IsKeyDown(Keys.Right)) { inputDirection.X += 1; } if (Keyboard.GetState().IsKeyDown(Keys.Up)) { inputDirection.Y -= 1; } if (Keyboard.GetState().IsKeyDown(Keys.Down)) { inputDirection.Y += 1; }
// Creates a gamepad state for player one on an Xbox 360 controller. GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
// This first if statement checks if the thumbsticks have moved. // Idle = 0, Left = -1, Right = 1. // The above values are the maximum values for the thumbsticks. if (gamePadState.ThumbSticks.Left.X != 0) { inputDirection.X += gamePadState.ThumbSticks.Left.X;
// This takes the value of the thumbsticks and adds it to the X-velocity for side-to-side movement. velocity.X += gamePadState.ThumbSticks.Left.X;
if (velocity.X > 0.5f || velocity.X < -0.5f) { if (gamePadState.ThumbSticks.Left.X > 0) { velocity.X = 0.5f; } if (gamePadState.ThumbSticks.Left.X < 0) { velocity.X = -0.5f; } } }
// This makes sure that the player is currently on the floor before even being able to move upwards. (This will probably be taken out soon.) /* if (gamePadState.ThumbSticks.Left.Y != 0 && onFloor == false) { inputDirection.Y -= gamePadState.ThumbSticks.Left.Y; } */
if (gamePadState.Buttons.A == ButtonState.Pressed && timeSinceLastJump >= 500) { onFloor = false;
if (velocity.Y >= prevVelocity.Y) { velocity = prevVelocity; }
inputDirection.X += velocity.X; inputDirection.Y += velocity.Y; velocity += acceleration;
timeSinceLastJump = 0; } else if (gamePadState.Buttons.A == ButtonState.Released) { inputDirection.Y += gravity.Y; }
if (gamePadState.Buttons.B == ButtonState.Pressed) { inputDirection.X += gamePadState.ThumbSticks.Left.X * 3; inputDirection.Y -= gamePadState.ThumbSticks.Left.Y * 3; GamePad.SetVibration(PlayerIndex.One, 1, 1); } else { GamePad.SetVibration(PlayerIndex.One, 0, 0); }
return inputDirection * speed; } }
public void collision(Rectangle player, Rectangle otherObject, GraphicsDevice graphicsDevice) { if (player.Intersects(otherObject) || player.Bottom >= graphicsDevice.PresentationParameters.BackBufferHeight) { onFloor = true; position.X = position.X; position.Y = otherObject.Y - frameSize.Y; } if (player.Bottom > otherObject.Top - 1) { onFloor = false; } }
public override void Update(GameTime gameTime, Rectangle clientBounds) { timeSinceLastJump += gameTime.ElapsedGameTime.Milliseconds; GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
// Move the sprite according to the direction property. position += Direction;
// If the mouse moved, set the position of the sprite to the mouse position. MouseState currentMouseState = Mouse.GetState();
if (currentMouseState.X != prevMouseState.X || currentMouseState.Y != prevMouseState.Y) { //position = new Vector2(currentMouseState.X, currentMouseState.Y); }
prevMouseState = currentMouseState;
// Keeps the sprite inside of the game window. if (position.X < 0) { position.X = 0; } if (position.X > clientBounds.Width - frameSize.X) { position.X = clientBounds.Width - frameSize.X; } if (position.Y < 0) { position.Y = 0; } if (position.Y > clientBounds.Height - frameSize.Y) { position.Y = clientBounds.Height - frameSize.Y; }
base.Update(gameTime, clientBounds); } } }
|