float vi = 0; // vi - initial velocity #endregion Fields #region Methods // Apply the rules of gravity public void Apply(ref Vector2 spritepos, GameTime gameTime, bool isOnGround) { // Is the player jumping up if (direction == LevelLibrary.Directions.up) { // Apply the velocity spritepos.Y += vi; // Erode the velocity (it's negative at the mo') vi += gravityStrength; // Once we reach zero(ish) velocity we will start to come back down if (vi >= 0f) { // Now headed down direction = LevelLibrary.Directions.down; vi = 1; } } else if (direction == LevelLibrary.Directions.down) { // Apply the velocity spritepos.Y += vi; // This time increase velocity as speed increases over time vi += gravityStrength; } // If we are not moving else if (direction == LevelLibrary.Directions.none && isOnGround == false) { // Apply the velocity spritepos.Y += vi; // If so we will start to fall direction = LevelLibrary.Directions.down; // Increase at rate of fall vi = gravityStrength; } }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Read the keypresses ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public void UpdateInput(GameTime gameTime, Camera camera) { currentDirection = LevelLibrary.Directions.none; jumpKeyPressed = false; KeyboardState newState = Keyboard.GetState(); camera.Update(gameTime, newState, oldState); // Is the Q key down? if (newState.IsKeyDown(jumpKeyMapping)) { jumpKeyPressed = true; } else if (newState.IsKeyDown(leftKeyMapping)) { currentDirection = LevelLibrary.Directions.left; } else if (newState.IsKeyDown(rightKeyMapping)) { currentDirection = LevelLibrary.Directions.right; } else if (newState.IsKeyDown(exitKeyMapping)) { exitKeyPressed = true; } else if (newState.IsKeyDown(fullKeyMapping)) { fullScreenPressed = true; } // Update saved state. oldState = newState; }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Initialise method // Get the current state of the keyboard; any keys pressed // Set the direction to static ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public Input() { leftKeyMapping = Keys.N; rightKeyMapping = Keys.M; jumpKeyMapping = Keys.Space; exitKeyMapping = Keys.Escape; fullKeyMapping = Keys.F; oldState = Keyboard.GetState(); currentDirection = LevelLibrary.Directions.none; }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Initialise method // animation - texture atlas of all the animation frames ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public void Initialize(LevelLibrary.SpriteAnimator animation, Vector2 startPosition, int xSize, int ySize) { PlayerAnimation = animation; // Set the starting position of the player around the middle of the screen and to the back position = startPosition; direction = LevelLibrary.Directions.none; currentSpeed = 1f; gravity = new LevelLibrary.Gravity(); gravity.windowHeight = ySize; gravity.objectHeight = PlayerAnimation.FrameHeight; // Set the player health Health = 100; Lives = 3; // Can I hover hoverAbility = false; PlayerAnimation.Active = true; PlayerAnimation.Position = Position; }
public void Initialize(Texture2D textureLeft, Texture2D textureRight, Vector2 position, int frameWidth, int frameHeight, int frameCount, int frametime, Color color, float scale, bool looping) { // Keep a local copy of the values passed in this.color = color; this.FrameWidth = frameWidth; this.FrameHeight = frameHeight; this.frameCount = frameCount; this.frameTime = frametime; this.scale = scale; Looping = looping; Position = position; spriteStripLeft = textureLeft; spriteStripRight = textureRight; direction = LevelLibrary.Directions.right; currentFrame = 0; // Set the Animation to active by default Active = true; Animating = true; }
// Handle player key presses private void UpdatePlayerSpeedAndDirection(bool jumpKey, bool leftKey, bool rightKey) { // We're jumping so set our velocity to upwards if (jumpKey == true && !jumping) { jumping = true; gravity.SetVelocity(jumpPower, LevelLibrary.Directions.up); } // Left key press if (leftKey == true) { // Not moving - start moving if (direction == LevelLibrary.Directions.none) { currentSpeed = initialSpeed; direction = LevelLibrary.Directions.left; } else if (direction == LevelLibrary.Directions.left) { // Allow increase in speed up until max speed if (currentSpeed < maxSpeed) { currentSpeed++; } } else { currentSpeed--; if (currentSpeed <= 1) { direction = LevelLibrary.Directions.none; } } } // Right key press else if (rightKey == true) { // Not moving - start moving if (direction == LevelLibrary.Directions.none) { currentSpeed = initialSpeed; direction = LevelLibrary.Directions.right; } else if (direction == LevelLibrary.Directions.right) { // Allow increase in speed up until max speed if (currentSpeed < maxSpeed) { currentSpeed++; } } else { currentSpeed--; if (currentSpeed <= 1) { direction = LevelLibrary.Directions.none; } } } else { // Player slows down if they don't press a key if (currentSpeed > 0) { currentSpeed -= 0.1f; } } }
public void SetVelocity(float v, LevelLibrary.Directions dir) { vi = v; direction = dir; }