Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Input;
- namespace WindowsGame4
- {
- class Controls
- {
- KeyboardState previousKeyState;
- KeyboardState currentKeyState;
- float player_speed = 4.0f;
- Vector2 gravity = new Vector2(0, 2.0f);
- Vector2 jumpVector = new Vector2(0, -16.0f);
- public void Update(PlayerObject player)
- {
- //If we were on the ground, but the Yvelocity became higher than 0, then apperantly we are not on the ground anymore
- if (player.PositionState == PositionState.onGround && player.Velocity.Y > 0)
- player.PositionState = PositionState.inAir;
- previousKeyState = currentKeyState;
- currentKeyState = Keyboard.GetState();
- int direction_x = 0;
- int direction_y = 0;
- if (currentKeyState.IsKeyDown(Keys.Left))
- direction_x = -1;
- if (currentKeyState.IsKeyDown(Keys.Right))
- direction_x = 1;
- //only in space we have free movement in all directions
- if (player.PositionState == PositionState.inSpace)
- {
- if (currentKeyState.IsKeyDown(Keys.Up))
- direction_y = -1;
- if (currentKeyState.IsKeyDown(Keys.Down))
- direction_y = 1;
- }
- Vector2 previousVelocity = player.Velocity;
- player.Velocity = new Vector2(direction_x, direction_y) * player_speed;
- //When we aren't in space, we apply gravity
- if (player.PositionState != PositionState.inSpace)
- {
- player.Velocity += gravity;
- player.VelocityY += previousVelocity.Y;
- }
- //You can only jump if you are on the ground
- if (player.PositionState == PositionState.onGround)
- {
- if (currentKeyState.IsKeyDown(Keys.Up) && previousKeyState.IsKeyUp(Keys.Up))
- {
- player.Velocity += jumpVector;
- player.PositionState = PositionState.inAir;
- }
- }
- //Max falling speed, to avoid falling though tiles
- if (player.Velocity.Y > 16)
- player.VelocityY = 16;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement