public void Update(float deltaTime) { sprite.Update(deltaTime); if (pause > 0) { pause -= deltaTime; } else { float ddx = 0; //acceleration int tx = game.PixelToTile(Position.X); int ty = game.PixelToTile(Position.Y); bool nx = (Position.X) % Game1.tile != 0; bool ny = (Position.Y) % Game1.tile != 0; bool cell = game.CellAtTileCoord(tx, ty, Game1.current.collisionLayer) != 0; bool cellright = game.CellAtTileCoord(tx + 1, ty, Game1.current.collisionLayer) != 0; bool celldown = game.CellAtTileCoord(tx, ty + 1, Game1.current.collisionLayer) != 0; bool celldiag = game.CellAtTileCoord(tx + 1, ty + 1, Game1.current.collisionLayer) != 0; if (moveRight) { if (celldiag && !cellright) { ddx = ddx + enemyAcceleration; } else { this.velocity.X = 0; this.moveRight = false; this.pause = 0.3f; sprite.SetFlipped(true); } } if (!moveRight) { if (celldown && !cell) { ddx = ddx - enemyAcceleration; } else { this.velocity.X = 0; this.moveRight = true; this.pause = 0.3f; sprite.SetFlipped(false); } } Position = new Vector2((float)Math.Floor(Position.X + (velocity.X)), Position.Y); velocity.X = MathHelper.Clamp(velocity.X + (deltaTime * ddx), -enemyMaxVelocity.X, enemyMaxVelocity.X); } }
public void Update(float DeltaTime) { Sprite.Update(DeltaTime); if (Pause > 0) { Pause -= DeltaTime; } else { float DDX = 0; int TX = Game.PixelToTile(Position.X); int TY = Game.PixelToTile(Position.Y); bool NX = (Position.X) % Game1.Tile != 0; bool NY = (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 (MoveRight) { if (CellDiag && !CellRight) { DDX = DDX = EnemyAcceleration; } else { this.Velocity.X = 0; this.MoveRight = false; this.Pause = 0.5f; } } if (!this.MoveRight) { if (CellDown && !Cell) { DDX = DDX - EnemyAcceleration; } else { this.Velocity.X = 0; this.MoveRight = true; this.Pause = 0.5f; } } Position = new Vector2((float)Math.Floor(Position.X + (DeltaTime * Velocity.X)), Position.Y); Velocity.X = MathHelper.Clamp(Velocity.X + (DeltaTime * DDX), -EnemyMaxVelocity.X, EnemyMaxVelocity.X); } }
public void Update(float deltaTime) { sprite.Update(deltaTime); if (pause > 0) { pause -= deltaTime; } else { float ddx = 0; int tx = game.PixelToTile(Position.X); int ty = game.PixelToTile(Position.Y); bool nx = (Position.X) % Game1.tile != 0; bool ny = (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 (moveRight) { if (celldiag && !cellright) { ddx = ddx + zombieAcceleration; } else { this.velocity.X = 0; this.moveRight = false; this.pause = 0.5f; } } if (!this.moveRight) { if (celldown && !cell) { ddx = ddx + zombieAcceleration; } else { this.velocity.X = 0; this.moveRight = true; this.pause = 0.5f; } } Position = new Vector2((float)Math.Floor(Position.X + (deltaTime * velocity.X)), Position.Y); velocity.X = MathHelper.Clamp(velocity.X + (deltaTime * ddx), -zombieMaxVelocity.X, zombieMaxVelocity.X); } }
public void Update(float deltaTime) { enemySprite.Update(deltaTime); float ddx = 0; // acceleration int tx = game.PixelToTile(Position.X); int ty = game.PixelToTile(Position.Y); bool nx = (Position.X) % Game1.tile != 0; // zombie overlaps right? bool ny = (Position.Y) % Game1.tile != 0; // zombie overlaps below? 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 (moveRight) { if (celldiag && !cellright) { ddx = ddx + acceleration; // zombie wants to go right } else { velocity.X = 0; moveRight = false; } } if (!moveRight) { if (celldown && !cell) { ddx = ddx - acceleration; // zombie wants to go left } else { velocity.X = 0; moveRight = true; } } Position = new Vector2((float)Math.Floor( Position.X + (deltaTime * velocity.X)), Position.Y); velocity.X = MathHelper.Clamp(velocity.X + (deltaTime * ddx), -maxVelocity.X, maxVelocity.X); }
private Object[] CollisionLogic(TiledTileLayer layer) { int tx = game.PixelToTile(sprite.position.X); int ty = game.PixelToTile(sprite.position.Y); // x % y divides x by y then looks at the remainder // because tile = 64, when the sprite's position (a single pixel) // is equal to 64, the remainder is zero, // therefore the sprite is only on one tile // in any other case, it is overlapping two or more tiles // therefore the bools are true // nx checks left/right overlapping // ny checks up/down overlapping bool nx = (sprite.position.X) % Game1.tile != 0; bool ny = (sprite.position.Y) % Game1.tile != 0; bool cell = game.CellAtTileCoord(tx, ty, layer) != 0; bool cellright = game.CellAtTileCoord(tx + 1, ty, layer) != 0; bool celldown = game.CellAtTileCoord(tx, ty + 1, layer) != 0; bool celldiag = game.CellAtTileCoord(tx + 1, ty + 1, layer) != 0; return(new Object[] { tx, ty, nx, ny, cell, cellright, celldown, celldiag }); }
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(); } // 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 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)); }