Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Player
- {
- public static float MINACCEL = 250;//120;
- public static float MINSPEED = 40;
- public static float MAXSPEED = 400;
- public static float RUNMAXSPEED = 1000;
- public static float DECELERATION = 12f; //Divides by (1 + DEC)
- public static float ACCELERATION = 200;
- public static float RUNACCELERATION = 500;
- public static float VERTDEC = 3f; //Vertical deceleration: divides by (1 + DEC)
- public static float MINVERTSPEED = 200f;
- public static float JUMPFORCE = 550f;
- public float x = 600;
- public float y = 0;
- public float velocityX = 0;
- public float velocityY = 0;
- public float width = 0;
- public float height = 0;
- public bool isJumping = true;
- public bool canjump = false;
- public Sprite sprite;
- [...]
- public void Update(GameTime gameTime)
- {
- float seconds=(float)gameTime.ElapsedGameTime.TotalSeconds;
- //Update width/height
- this.width = this.sprite.GetImage().Width;
- this.height = this.sprite.GetImage().Height;
- //Calculate position and check for collision
- float targetX = x + velocityX * seconds;
- float targetY = y + velocityY * seconds;
- if (targetX < 0) targetX = 0;
- if (targetX + width > Main.LEVEL_EDGE_X) targetX = Main.LEVEL_EDGE_X - width;
- if(Util.CheckCollision(targetX+width-1, targetY+4, 1, height-8, // collision for right side
- Main.core.x, Main.core.y, Main.core.sprite.GetImage().Width, Main.core.sprite.GetImage().Height))// right side
- {
- if(velocityX>-1){
- targetX = x;
- }
- }
- if(Util.CheckCollision(targetX, targetY+4, 1, height-8, // collision for left side
- Main.core.x, Main.core.y, Main.core.sprite.GetImage().Width, Main.core.sprite.GetImage().Height))
- {
- if(velocityX<0){
- targetX = x;
- }
- }
- if( (Util.CheckCollision(targetX+2, targetY+height, width-4, 1, // down check
- Main.core.x, Main.core.y, Main.core.sprite.GetImage().Width, Main.core.sprite.GetImage().Height)) || (targetY+height>Main.GROUNDY))// core collision
- {
- if(velocityY>0){
- while( (Util.CheckCollision(targetX+2, targetY+height, width-4, 1, // down check
- Main.core.x, Main.core.y, Main.core.sprite.GetImage().Width, Main.core.sprite.GetImage().Height)) || (targetY+height>Main.GROUNDY) )
- {
- targetY--;
- }
- targetY++;
- velocityY = 0;
- isJumping = false;
- canjump = true;
- }
- } else
- {
- isJumping = true;
- canjump = false;
- }
- if(isJumping){ //handle jumping
- velocityY += 1300 * seconds;
- y+=velocityY*seconds;
- }
- //Update position
- x = targetX;
- y = targetY;
- //Update sprite
- //Animations
- if (this.sprite.currentAnimation != "default" && this.velocityX == 0 && this.velocityY == 0) this.sprite.ChangeAnimation("default");
- else if (this.sprite.currentAnimation != "jumping" && (this.velocityY < 0 || this.velocityY > 0)) this.sprite.ChangeAnimation("jumping");
- else if (this.sprite.currentAnimation != "running" && (this.velocityX < 0 || this.velocityX > 0) && this.velocityY == 0) this.sprite.ChangeAnimation("running");
- //Flip and position
- if (velocityX > 0) this.sprite.isFlipped = Microsoft.Xna.Framework.Graphics.SpriteEffects.None; //Don't flip - face right
- else if (velocityX < 0) this.sprite.isFlipped = Microsoft.Xna.Framework.Graphics.SpriteEffects.FlipHorizontally; //Flip - face left
- this.sprite.SetPosition(x, y); //Set position to player position
- }
- public void Jump()
- {
- Audio.PlaySound("jump");
- this.velocityY = -JUMPFORCE;
- }
- }
- -------------------------
- public static class Input
- {
- public static void Update(GameTime gameTime)
- {
- oldState = newState;
- newState = Keyboard.GetState();
- if (Main.gameState == Main.GameState.Playing)
- {
- //===================PLAYING==================
- //------------------Move left/right--------------------
- //Acceleration stuff
- float velocityMod = 0;
- if (newState.IsKeyDown(Keys.LeftShift) || newState.IsKeyDown(Keys.RightShift)) //Running
- {
- if (newState.IsKeyDown(Keys.Left)) velocityMod = -Player.RUNACCELERATION;
- if (newState.IsKeyDown(Keys.Right)) velocityMod = Player.RUNACCELERATION;
- }
- else //Normal speed
- {
- if (newState.IsKeyDown(Keys.Left)) velocityMod = -Player.ACCELERATION;
- if (newState.IsKeyDown(Keys.Right)) velocityMod = Player.ACCELERATION;
- }
- if (Main.player.velocityX < Player.MINACCEL && Main.player.velocityX > -Player.MINACCEL && velocityMod > 0) //Quick start up - right
- {
- Main.player.velocityX = Player.MINACCEL;
- }
- else if (Main.player.velocityX > -Player.MINACCEL && Main.player.velocityX < Player.MINACCEL && velocityMod < 0) //Quick start up - left
- {
- Main.player.velocityX = -Player.MINACCEL;
- }
- else //Normal acceleration
- {
- Main.player.velocityX += velocityMod * (float)gameTime.ElapsedGameTime.TotalSeconds;
- }
- //Decelerate if not pushing direction or pushing opposite direction
- if (velocityMod == 0 || (velocityMod < 0 && Main.player.velocityX > 0) || (velocityMod > 0 && Main.player.velocityX < 0))
- {
- Main.player.velocityX = Main.player.velocityX / (1 + (Player.DECELERATION * (float)gameTime.ElapsedGameTime.TotalSeconds));
- }
- //Stop the player if below min speed
- if ((Main.player.velocityX < Player.MINSPEED && Main.player.velocityX > 0) || (Main.player.velocityX > -Player.MINSPEED && Main.player.velocityX < 0))
- {
- Main.player.velocityX = 0;
- }
- //Fix speed if over limit
- float maxSpeed = Player.MAXSPEED;
- if (newState.IsKeyDown(Keys.RightShift) || newState.IsKeyDown(Keys.LeftShift)) maxSpeed = Player.RUNMAXSPEED;
- if (Main.player.velocityX > maxSpeed) Main.player.velocityX = maxSpeed;
- if (Main.player.velocityX < -maxSpeed) Main.player.velocityX = -maxSpeed;
- //----------------------------Jump-----------------
- if (KeyPressed(Keys.Space) || KeyPressed(Keys.Z) || KeyPressed(Keys.Up))
- {
- if (Main.player.canjump)
- {
- Main.player.Jump();
- Main.player.isJumping = true;
- Main.player.canjump = false;
- }
- }
- [...]
- }
- [...]
- }
- [...]
- }
Advertisement
Add Comment
Please, Sign In to add comment