private void UpdateInput(float deltaTime) { bool wasMovingLeft = velocity.X < 0; bool wasMovingRight = velocity.X > 0; Vector2 acceleration = new Vector2(0, Game1.gravity); if (Keyboard.GetState().IsKeyDown(Keys.Left) == true) { acceleration.X -= Game1.acceleration; sprite.Play(); sprite.SetFlipped(true); } else if (wasMovingLeft == true) { acceleration.X += Game1.friction; } if (Keyboard.GetState().IsKeyDown(Keys.Right) == true) { acceleration.X += Game1.acceleration; sprite.Play(); sprite.SetFlipped(false); } else if (wasMovingRight == true) { acceleration.X -= Game1.friction; } if (Keyboard.GetState().IsKeyDown(Keys.Up) == true && this.isJumping == false && hasGravity == false || autoJump == true) { autoJump = false; acceleration.Y -= (9999999999999 * 99999); this.isJumping = true; jumpSoundInsance.Play(); } velocity += acceleration * deltaTime; velocity.X = MathHelper.Clamp(velocity.X, -Game1.maxVelocity.X, Game1.maxVelocity.X); velocity.Y = MathHelper.Clamp(velocity.Y, -Game1.maxVelocity.Y, Game1.maxVelocity.Y); sprite.position += velocity * deltaTime; if ((wasMovingLeft && (velocity.X > 0)) || (wasMovingRight && (velocity.X < 0))) { velocity.X = 0; sprite.Pause(); } int tx = game.PixelToTile(sprite.position.X); int ty = game.PixelToTile(sprite.position.Y); bool nx = (sprite.position.X) % Game1.tile != 0; bool ny = (sprite.position.Y) % Game1.tile != 0; bool cell = game.CellAtTileCoord(tx, ty) != 0; bool cellright = game.CellAtTileCoord(tx + 1, ty) != 0; bool celldown = game.CellAtTileCoord(tx, ty + 1) != 0; bool celldiag = game.CellAtTileCoord(tx + 1, ty + 1) != 0; //if (celldown == false) //{ // acceleration = new Vector2(0, Game1.gravity); //} if (this.velocity.X == 0) { sprite.Pause(); } if (this.velocity.Y > 0) { if ((celldown && !cell) || (celldiag && !cellright && nx)) { sprite.position.Y = game.TileToPixel(ty); this.velocity.Y = 0; hasGravity = false; this.isJumping = false; ny = false; this.hasGravity = false; } } else if (this.velocity.Y < 0) { if ((cell && !celldown) || (cellright && !celldiag && nx)) { sprite.position.Y = game.TileToPixel(ty + 1); this.velocity.Y = 0; cell = celldown; cellright = celldiag; ny = false; this.hasGravity = false; } } if (this.velocity.X > 0) { if ((cellright && !cell) || (celldiag && !celldown && ny)) { sprite.position.X = game.TileToPixel(tx); this.velocity.X = 0; sprite.Pause(); } } else if (this.velocity.X < 0) { if ((cell && !cellright) || (celldown && !celldiag && ny)) { sprite.position.X = game.TileToPixel(tx + 1); this.velocity.X = 0; sprite.Pause(); } } if (celldown || (nx && celldiag)) { this.hasGravity = false; stopGravity(); } else { this.hasGravity = true; } }
private void UpdateInput(float deltaTime) { bool wasMovingLeft = velocity.X < 0; bool wasMovingRight = velocity.X > 0; bool falling = isFalling; Vector2 acceleration = new Vector2(0, Game1.gravity); if (Keyboard.GetState().IsKeyDown(Keys.Left) == true) { playerSprite.SetFlipped(true); playerSprite.Play(); acceleration.X -= Game1.acceleration; } else if (wasMovingLeft == true) { acceleration.X += Game1.friction; } if (Keyboard.GetState().IsKeyDown(Keys.Right) == true) { playerSprite.SetFlipped(false); playerSprite.Play(); acceleration.X += Game1.acceleration; } else if (wasMovingRight == true) { acceleration.X -= Game1.friction; } if ((Keyboard.GetState().IsKeyDown(Keys.Up) == true && this.isJumping == false && falling == false) || autoJump == true) { autoJump = false; acceleration.Y -= Game1.jumpImpulse; this.isJumping = true; jumpSoundInstance.Play(); } // integrate the forces to calculate the new position and velocity velocity += acceleration * deltaTime; // clamp the velocity so the player doesn't go too fast velocity.X = MathHelper.Clamp(velocity.X, -Game1.maxVelocity.X, Game1.maxVelocity.X); velocity.Y = MathHelper.Clamp(velocity.Y, -Game1.maxVelocity.Y, Game1.maxVelocity.Y); playerSprite.position += velocity * deltaTime; // more code will follow // One tricky aspect of using a frictional force to slow the player down // (as opposed to just allowing a dead-stop) is that the force is highly // unlikely to be exactly the force needed to come to a halt. In fact, it’s // likely to overshoot in the opposite direction and lead to a tiny jiggling // effect instead of actually stopping the player. // In order to avoid this, we must clamp the horizontal velocity to zero if // we detect that the players direction has just changed: if ((wasMovingLeft && (velocity.X > 0)) || (wasMovingRight && (velocity.X < 0))) { // clamp at zero to prevent friction from making us jiggle side to side velocity.X = 0; playerSprite.Pause(); } // collision detection // Our collision detection logic is greatly simplified by the fact that // the player is a rectangle and is exactly the same size as a single tile. // So we know that the player can only ever occupy 1, 2 or 4 cells. // This means we can short-circuit and avoid building a general purpose // collision detection engine by simply looking at the 1 to 4 cells that // the player occupies: int tx = game.PixelToTile(playerSprite.position.X); int ty = game.PixelToTile(playerSprite.position.Y); // nx = true if player overlaps right bool nx = (playerSprite.position.X) % Game1.tile != 0; // ny = true if player overlaps below bool ny = (playerSprite.position.Y) % Game1.tile != 0; bool cell = game.CellAtTileCoord(tx, ty) != 0; bool cellright = game.CellAtTileCoord(tx + 1, ty) != 0; bool celldown = game.CellAtTileCoord(tx, ty + 1) != 0; bool celldiag = game.CellAtTileCoord(tx + 1, ty + 1) != 0; // If the player has vertical velocity, then check to see if they have hit // a platform below or above, in which case, stop their vertical velocity, // and clamp their y position: if (this.velocity.Y > 0) { if ((celldown && !cell) || (celldiag && !cellright && nx)) { // clamp the y position to avoid falling into platform below playerSprite.position.Y = game.TileToPixel(ty); this.velocity.Y = 0; // stop downward velocity this.isFalling = false; // no longer falling this.isJumping = false; // (or jumping) ny = false; // - no longer overlaps the cells below } } else if (this.velocity.Y < 0) { if ((cell && !celldown) || (cellright && !celldiag && nx)) { // clamp the y position to avoid jumping into platform above playerSprite.position.Y = game.TileToPixel(ty + 1); this.velocity.Y = 0; // stop upward velocity // player is no longer really in that cell, we clamped them // to the cell below cell = celldown; cellright = celldiag; // (ditto) ny = false; // player no longer overlaps the cells below } } // continued on next page... // Once the vertical velocity is taken care of, we can apply similar // logic to the horizontal velocity: if (this.velocity.X > 0) { if ((cellright && !cell) || (celldiag && !celldown && ny)) { // clamp the x position to avoid moving into the platform // we just hit playerSprite.position.X = game.TileToPixel(tx); this.velocity.X = 0; // stop horizontal velocity playerSprite.Pause(); } } else if (this.velocity.X < 0) { if ((cell && !cellright) || (celldown && !celldiag && ny)) { // clamp the x position to avoid moving into the platform // we just hit playerSprite.position.X = game.TileToPixel(tx + 1); this.velocity.X = 0; // stop horizontal velocity playerSprite.Pause(); } } if (this.velocity.X == 0) { playerSprite.Pause(); } // The last calculation for our update() method is to detect if the // player is now falling or not. We can do that by looking to see if // there is a platform below them this.isFalling = !(celldown || (nx && celldiag)); }
private void UpdateInput(float DeltaTime) { bool WasMovingLeft = Velocity.X < 0; bool WasMovingRight = Velocity.X > 0; bool Falling = IsFalling; Vector2 Acceleration = new Vector2(0, Game1.Gravity); if (Keyboard.GetState().IsKeyDown(Keys.Left) == true) { Acceleration.X -= Game1.Acceleration; sprite.SetFlipped(true); sprite.Play(); } else if (WasMovingLeft == true) { Acceleration.X += Game1.Friction; } if (Keyboard.GetState().IsKeyDown(Keys.Right) == true) { Acceleration.X += Game1.Acceleration; sprite.SetFlipped(false); sprite.Play(); } else if (WasMovingRight == true) { Acceleration.X -= Game1.Friction; } if ((Keyboard.GetState().IsKeyDown(Keys.Up) == true && this.IsJumping == false && Falling == false) || AutoJump == true) { AutoJump = false; Acceleration.Y -= Game1.jumpImpulse; this.IsJumping = true; JumpSoundInstance.Play(); } Velocity += Acceleration * DeltaTime; Velocity.X = MathHelper.Clamp(Velocity.X, -Game1.MaxVelocity.X, Game1.MaxVelocity.X); Velocity.Y = MathHelper.Clamp(Velocity.Y, -Game1.MaxVelocity.Y, Game1.MaxVelocity.Y); sprite.position += Velocity * DeltaTime; if ((WasMovingLeft && (Velocity.X > 0)) || (WasMovingRight && (Velocity.X < 0))) { Velocity.X = 0; sprite.Pause(); } int TX = Game.PixelToTile(sprite.position.X); int TY = Game.PixelToTile(sprite.position.Y); bool NX = (sprite.position.X) % Game1.Tile != 0; bool NY = (sprite.position.Y) % Game1.Tile != 0; bool Cell = Game.CellAtTileCoord(TX, TY) != 0; bool CellRight = Game.CellAtTileCoord(TX + 1, TY) != 0; bool CellDown = Game.CellAtTileCoord(TX, TY + 1) != 0; bool CellDiag = Game.CellAtTileCoord(TX + 1, TY + 1) != 0; if (this.Velocity.Y > 0) { if ((CellDown && !Cell) || (CellDiag && !CellRight && NX)) { sprite.position.Y = Game.TileToPixel(TY); this.Velocity.Y = 0; this.IsFalling = false; this.IsJumping = false; NY = false; } } else if (this.Velocity.Y < 0) { if ((Cell && !CellDown) || (CellRight && !CellDiag && NX)) { sprite.position.Y = Game.TileToPixel(TY + 1); this.Velocity.Y = 0; Cell = CellDown; CellRight = CellDiag; NY = false; } } if (this.Velocity.X > 0) { if ((CellRight && !Cell) || (CellDiag && !CellDown && NY)) { sprite.position.X = Game.TileToPixel(TX); this.Velocity.X = 0; sprite.Pause(); } } else if (this.Velocity.X < 0) { if ((Cell && !CellRight) || (CellDown && !CellDiag && NY)) { sprite.position.X = Game.TileToPixel(TX + 1); this.Velocity.X = 0; sprite.Pause(); } } this.IsFalling = !(CellDown || (NX && CellDiag)); }
private void UpdateInput(float deltaTime) { bool wasMovingLeft = velocity.X < 0; bool wasMovingRight = velocity.X > 0; bool falling = isFalling; Vector2 acceleration = new Vector2(0, Game1.gravity); if (Keyboard.GetState().IsKeyDown(Keys.Left) == true) { acceleration.X -= Game1.acceleration; sprite.SetFlipped(true); sprite.Play(); } else if (wasMovingLeft == true) { acceleration.X += Game1.friction; } if (Keyboard.GetState().IsKeyDown(Keys.Right) == true) { acceleration.X += Game1.acceleration; sprite.SetFlipped(false); sprite.Play(); } else if (wasMovingRight == true) { acceleration.X -= Game1.friction; } if ((Keyboard.GetState().IsKeyDown(Keys.Up) == true && this.isJumping == false && falling == false) || autoJump == true) { autoJump = false; acceleration.Y -= Game1.jumpImpulse; this.isJumping = true; jumpSoundInstance.Play(); } // integrate the forces to calculate the new position and velocity velocity += acceleration * deltaTime; // clamp the velocity so the player doesn't go too fast velocity.X = MathHelper.Clamp(velocity.X, -Game1.maxVelocity.X, Game1.maxVelocity.X); velocity.Y = MathHelper.Clamp(velocity.Y, -Game1.maxVelocity.Y, Game1.maxVelocity.Y); sprite.position += velocity * deltaTime; // more code will follow if ((wasMovingLeft && (velocity.X > 0)) || (wasMovingRight && (velocity.X < 0))) { // clamp at zero to prevent friction from making us jiggle side to side velocity.X = 0; sprite.Pause(); } int tx = game.PixelToTile(sprite.position.X); int ty = game.PixelToTile(sprite.position.Y); bool nx = (sprite.position.X) % Game1.tile != 0; bool ny = (sprite.position.Y) % Game1.tile != 0; bool cell = game.CellAtTileCoord(tx, ty) != 0; bool cellright = game.CellAtTileCoord(tx + 1, ty) != 0; bool celldown = game.CellAtTileCoord(tx, ty + 1) != 0; bool celldiag = game.CellAtTileCoord(tx + 1, ty + 1) != 0; if (this.velocity.Y > 0) { if ((celldown && !cell) || (celldiag && !cellright && nx)) { // clamp the y position to avoid falling into platform below sprite.position.Y = game.TileToPixel(ty); this.velocity.Y = 0; // stop downward velocity this.isFalling = false; // no longer falling this.isJumping = false; // (or jumping) ny = false; // - no longer overlaps the cells below } } else if (this.velocity.Y < 0) { if ((cell && !celldown) || (cellright && !celldiag && nx)) { // clamp the y position to avoid jumping into platform above sprite.position.Y = game.TileToPixel(ty + 1); this.velocity.Y = 0; // stop upward velocity // player is no longer really in that cell, we clamped them // to the cell below cell = celldown; cellright = celldiag; // (ditto) ny = false; // player no longer overlaps the cells below } } // Once the vertical velocity is taken care of, we can apply similar // logic to the horizontal velocity: if (this.velocity.X > 0) { if ((cellright && !cell) || (celldiag && !celldown && ny)) { // clamp the x position to avoid moving into the platform // we just hit sprite.position.X = game.TileToPixel(tx); this.velocity.X = 0; // stop horizontal velocity sprite.Pause(); } } else if (this.velocity.X < 0) { if ((cell && !cellright) || (celldown && !celldiag && ny)) { // clamp the x position to avoid moving into the platform // we just hit sprite.position.X = game.TileToPixel(tx + 1); this.velocity.X = 0; // stop horizontal velocity sprite.Pause(); } } // The last calculation for our update() method is to detect if the // player is now falling or not. We can do that by looking to see if // there is a platform below them this.isFalling = !(celldown || (nx && celldiag)); }
private void CollisionLayer(TiledTileLayer layer) { Object[] CollisionData = CollisionLogic(layer); int tx = (int)CollisionData[0]; int ty = (int)CollisionData[1]; bool nx = (bool)CollisionData[2]; bool ny = (bool)CollisionData[3]; bool cell = (bool)CollisionData[4]; bool cellright = (bool)CollisionData[5]; bool celldown = (bool)CollisionData[6]; bool celldiag = (bool)CollisionData[7]; //if player has vertical velocity //check to see if they have collided with a platform above or below //if they have clamp their vertical velocity if (this.velocity.Y > 0) { if ((celldown && !cell) || (celldiag && !cellright && nx)) { sprite.position.Y = game.TileToPixel(ty); this.velocity.Y = 0; this.isFalling = false; this.isJumping = false; ny = false; } } else if (this.velocity.Y < 0) { if ((cell && !celldown) || (cellright && !celldiag && nx)) { sprite.position.Y = game.TileToPixel(ty + 1); this.velocity.Y = 0; cell = celldown; cellright = celldiag; ny = false; } } //same as above but for horizontal velocity if (this.velocity.X > 0) { if ((cellright && !cell) || (celldiag && !celldown && ny)) { sprite.position.X = game.TileToPixel(tx); this.velocity.X = 0; sprite.Pause(); } } else if (this.velocity.X < 0) { if ((cell && !cellright) || (celldown && !celldiag && ny)) { sprite.position.X = game.TileToPixel(tx + 1); this.velocity.X = 0; sprite.Pause(); } } // then we need to detect if the player is falling or not // we do this by seeing if there is a cell below them this.isFalling = !(celldown || (nx && celldiag)); }