SHOW:
|
|
- or go back to the newest paste.
| 1 | public class Player | |
| 2 | {
| |
| 3 | public static float MINACCEL = 250;//120; | |
| 4 | public static float MINSPEED = 40; | |
| 5 | public static float MAXSPEED = 400; | |
| 6 | public static float RUNMAXSPEED = 1000; | |
| 7 | public static float DECELERATION = 12f; //Divides by (1 + DEC) | |
| 8 | public static float ACCELERATION = 200; | |
| 9 | public static float RUNACCELERATION = 500; | |
| 10 | public static float VERTDEC = 3f; //Vertical deceleration: divides by (1 + DEC) | |
| 11 | public static float MINVERTSPEED = 200f; | |
| 12 | public static float JUMPFORCE = 550f; | |
| 13 | ||
| 14 | ||
| 15 | public float x = 600; | |
| 16 | public float y = 0; | |
| 17 | public float velocityX = 0; | |
| 18 | public float velocityY = 0; | |
| 19 | public float width = 0; | |
| 20 | public float height = 0; | |
| 21 | public bool isJumping = true; | |
| 22 | public bool canjump = false; | |
| 23 | ||
| 24 | public Sprite sprite; | |
| 25 | ||
| 26 | [...] | |
| 27 | ||
| 28 | public void Update(GameTime gameTime) | |
| 29 | {
| |
| 30 | float seconds=(float)gameTime.ElapsedGameTime.TotalSeconds; | |
| 31 | ||
| 32 | //Update width/height | |
| 33 | this.width = this.sprite.GetImage().Width; | |
| 34 | this.height = this.sprite.GetImage().Height; | |
| 35 | ||
| 36 | //Calculate position and check for collision | |
| 37 | float targetX = x + velocityX * seconds; | |
| 38 | float targetY = y + velocityY * seconds; | |
| 39 | ||
| 40 | ||
| 41 | if (targetX < 0) targetX = 0; | |
| 42 | if (targetX + width > Main.LEVEL_EDGE_X) targetX = Main.LEVEL_EDGE_X - width; | |
| 43 | ||
| 44 | ||
| 45 | if(Util.CheckCollision(targetX+width-1, targetY+4, 1, height-8, // collision for right side | |
| 46 | Main.core.x, Main.core.y, Main.core.sprite.GetImage().Width, Main.core.sprite.GetImage().Height))// right side | |
| 47 | {
| |
| 48 | if(velocityX>-1){
| |
| 49 | targetX = x; | |
| 50 | } | |
| 51 | } | |
| 52 | ||
| 53 | if(Util.CheckCollision(targetX, targetY+4, 1, height-8, // collision for left side | |
| 54 | Main.core.x, Main.core.y, Main.core.sprite.GetImage().Width, Main.core.sprite.GetImage().Height)) | |
| 55 | {
| |
| 56 | if(velocityX<0){
| |
| 57 | targetX = x; | |
| 58 | } | |
| 59 | } | |
| 60 | ||
| 61 | if( (Util.CheckCollision(targetX+2, targetY+height, width-4, 1, // down check | |
| 62 | Main.core.x, Main.core.y, Main.core.sprite.GetImage().Width, Main.core.sprite.GetImage().Height)) || (targetY+height>Main.GROUNDY))// core collision | |
| 63 | {
| |
| 64 | if(velocityY>0){
| |
| 65 | while( (Util.CheckCollision(targetX+2, targetY+height, width-4, 1, // down check | |
| 66 | Main.core.x, Main.core.y, Main.core.sprite.GetImage().Width, Main.core.sprite.GetImage().Height)) || (targetY+height>Main.GROUNDY) ) | |
| 67 | {
| |
| 68 | targetY--; | |
| 69 | } | |
| 70 | ||
| 71 | ||
| 72 | targetY++; | |
| 73 | velocityY = 0; | |
| 74 | isJumping = false; | |
| 75 | canjump = true; | |
| 76 | } | |
| 77 | } else | |
| 78 | {
| |
| 79 | isJumping = true; | |
| 80 | canjump = false; | |
| 81 | } | |
| 82 | ||
| 83 | if(isJumping){ //handle jumping
| |
| 84 | velocityY += 1300 * seconds; | |
| 85 | y+=velocityY*seconds; | |
| 86 | } | |
| 87 | ||
| 88 | //Update position | |
| 89 | x = targetX; | |
| 90 | y = targetY; | |
| 91 | ||
| 92 | //Update sprite | |
| 93 | ||
| 94 | //Animations | |
| 95 | if (this.sprite.currentAnimation != "default" && this.velocityX == 0 && this.velocityY == 0) this.sprite.ChangeAnimation("default");
| |
| 96 | else if (this.sprite.currentAnimation != "jumping" && (this.velocityY < 0 || this.velocityY > 0)) this.sprite.ChangeAnimation("jumping");
| |
| 97 | else if (this.sprite.currentAnimation != "running" && (this.velocityX < 0 || this.velocityX > 0) && this.velocityY == 0) this.sprite.ChangeAnimation("running");
| |
| 98 | ||
| 99 | //Flip and position | |
| 100 | if (velocityX > 0) this.sprite.isFlipped = Microsoft.Xna.Framework.Graphics.SpriteEffects.None; //Don't flip - face right | |
| 101 | else if (velocityX < 0) this.sprite.isFlipped = Microsoft.Xna.Framework.Graphics.SpriteEffects.FlipHorizontally; //Flip - face left | |
| 102 | this.sprite.SetPosition(x, y); //Set position to player position | |
| 103 | } | |
| 104 | ||
| 105 | public void Jump() | |
| 106 | {
| |
| 107 | Audio.PlaySound("jump");
| |
| 108 | this.velocityY = -JUMPFORCE; | |
| 109 | } | |
| 110 | ||
| 111 | } | |
| 112 | ||
| 113 | ------------------------- | |
| 114 | ||
| 115 | public static class Input | |
| 116 | {
| |
| 117 | public static void Update(GameTime gameTime) | |
| 118 | {
| |
| 119 | oldState = newState; | |
| 120 | newState = Keyboard.GetState(); | |
| 121 | ||
| 122 | if (Main.gameState == Main.GameState.Playing) | |
| 123 | {
| |
| 124 | //===================PLAYING================== | |
| 125 | //------------------Move left/right-------------------- | |
| 126 | //Acceleration stuff | |
| 127 | float velocityMod = 0; | |
| 128 | if (newState.IsKeyDown(Keys.LeftShift) || newState.IsKeyDown(Keys.RightShift)) //Running | |
| 129 | {
| |
| 130 | if (newState.IsKeyDown(Keys.Left)) velocityMod = -Player.RUNACCELERATION; | |
| 131 | if (newState.IsKeyDown(Keys.Right)) velocityMod = Player.RUNACCELERATION; | |
| 132 | } | |
| 133 | else //Normal speed | |
| 134 | {
| |
| 135 | if (newState.IsKeyDown(Keys.Left)) velocityMod = -Player.ACCELERATION; | |
| 136 | if (newState.IsKeyDown(Keys.Right)) velocityMod = Player.ACCELERATION; | |
| 137 | } | |
| 138 | ||
| 139 | if (Main.player.velocityX < Player.MINACCEL && Main.player.velocityX > -Player.MINACCEL && velocityMod > 0) //Quick start up - right | |
| 140 | {
| |
| 141 | Main.player.velocityX = Player.MINACCEL; | |
| 142 | } | |
| 143 | else if (Main.player.velocityX > -Player.MINACCEL && Main.player.velocityX < Player.MINACCEL && velocityMod < 0) //Quick start up - left | |
| 144 | {
| |
| 145 | Main.player.velocityX = -Player.MINACCEL; | |
| 146 | } | |
| 147 | else //Normal acceleration | |
| 148 | {
| |
| 149 | Main.player.velocityX += velocityMod * (float)gameTime.ElapsedGameTime.TotalSeconds; | |
| 150 | } | |
| 151 | ||
| 152 | //Decelerate if not pushing direction or pushing opposite direction | |
| 153 | if (velocityMod == 0 || (velocityMod < 0 && Main.player.velocityX > 0) || (velocityMod > 0 && Main.player.velocityX < 0)) | |
| 154 | {
| |
| 155 | Main.player.velocityX = Main.player.velocityX / (1 + (Player.DECELERATION * (float)gameTime.ElapsedGameTime.TotalSeconds)); | |
| 156 | } | |
| 157 | ||
| 158 | //Stop the player if below min speed | |
| 159 | if ((Main.player.velocityX < Player.MINSPEED && Main.player.velocityX > 0) || (Main.player.velocityX > -Player.MINSPEED && Main.player.velocityX < 0)) | |
| 160 | {
| |
| 161 | Main.player.velocityX = 0; | |
| 162 | } | |
| 163 | ||
| 164 | //Fix speed if over limit | |
| 165 | float maxSpeed = Player.MAXSPEED; | |
| 166 | if (newState.IsKeyDown(Keys.RightShift) || newState.IsKeyDown(Keys.LeftShift)) maxSpeed = Player.RUNMAXSPEED; | |
| 167 | if (Main.player.velocityX > maxSpeed) Main.player.velocityX = maxSpeed; | |
| 168 | if (Main.player.velocityX < -maxSpeed) Main.player.velocityX = -maxSpeed; | |
| 169 | ||
| 170 | //----------------------------Jump----------------- | |
| 171 | if (KeyPressed(Keys.Space) || KeyPressed(Keys.Z) || KeyPressed(Keys.Up)) | |
| 172 | {
| |
| 173 | if (Main.player.canjump) | |
| 174 | {
| |
| 175 | Main.player.Jump(); | |
| 176 | Main.player.isJumping = true; | |
| 177 | Main.player.canjump = false; | |
| 178 | } | |
| 179 | } | |
| 180 | [...] | |
| 181 | } | |
| 182 | [...] | |
| 183 | } | |
| 184 | [...] | |
| 185 | } | |
| 186 | ||
| 187 | -------- | |
| 188 | ||
| 189 | public static class Util | |
| 190 | {
| |
| 191 | public static bool CheckCollision(float x1, float y1, float w1, float h1, float x2, float y2, float w2, float h2) | |
| 192 | {
| |
| 193 | if (x1 + w1 > x2) | |
| 194 | {
| |
| 195 | if (x2 + w2 > x1) | |
| 196 | {
| |
| 197 | if (y1 + h1 > y2) | |
| 198 | {
| |
| 199 | if (y2 + h2 > y1) | |
| 200 | {
| |
| 201 | return true; | |
| 202 | } | |
| 203 | } | |
| 204 | } | |
| 205 | } | |
| 206 | return false; | |
| 207 | } | |
| 208 | } |