private static Single CalculateRotation(Single currentRotation, Vector2 rotationVector, Single topTurningSpeed) { rotationVector.Normalize(); Single desiredRotation = FloatMathHelper.Atan2(rotationVector.Y, rotationVector.X); if (Math.Abs(currentRotation - desiredRotation) < topTurningSpeed) { currentRotation = desiredRotation; } else { if (FloatMathHelper.CounterClockwiseAngularDistance(currentRotation, desiredRotation) < FloatMathHelper.ClockwiseAngularDistance(currentRotation, desiredRotation)) { currentRotation -= topTurningSpeed; } else { currentRotation += topTurningSpeed; } } return(MathHelper.WrapAngle(currentRotation)); }
private static Single CalculateRotation(Single rotation, Single gamePadLeftX, Single gamePadLeftY, Boolean turnLeft, Boolean turnRight) { if (gamePadLeftX >= TURN_AXIS_DEADBAND || gamePadLeftX <= -TURN_AXIS_DEADBAND || gamePadLeftY >= TURN_AXIS_DEADBAND || gamePadLeftY <= -TURN_AXIS_DEADBAND) { Single desiredRotation = FloatMathHelper.Atan2(gamePadLeftY, gamePadLeftX); if (Math.Abs(rotation - desiredRotation) < TOP_TURN_SPEED) { rotation = desiredRotation; } else { if (FloatMathHelper.CounterClockwiseAngularDistance(rotation, desiredRotation) < FloatMathHelper.ClockwiseAngularDistance(rotation, desiredRotation)) { rotation -= TOP_TURN_SPEED; } else { rotation += TOP_TURN_SPEED; } } } else { if (turnLeft) { rotation -= TOP_TURN_SPEED * 0.5f; } else if (turnRight) { rotation += TOP_TURN_SPEED * 0.5f; } } return(MathHelper.WrapAngle(rotation)); }
public override void Update(GameTime gameTime) { if (this.Alive) { Vector2 homingPosition = this.Game.SurvivorSubsystem.PlayerOneSurvivorSprite.Location.Position; Vector2 oldPosition = Location.Position; Single oldRotation = Location.Rotation; Single currentRotation = Location.Rotation; Vector2 currentPosition = Location.Position; Vector2 deltaPosition = homingPosition - currentPosition; Single rotation = CalculateRotation(currentRotation, deltaPosition, TopTurningSpeed()); Vector2 velocityVector = new Vector2(FloatMathHelper.Cos(rotation), FloatMathHelper.Sin(rotation)); if (Math.Abs(deltaPosition.Length()) > TopMovementSpeed()) { Single speed = 0.0f; Single cwAngularDistance = FloatMathHelper.ClockwiseAngularDistance(currentRotation, rotation); Single ccwAngularDistance = FloatMathHelper.CounterClockwiseAngularDistance(currentRotation, rotation); Single deltaRotation = MathHelper.Min(cwAngularDistance, ccwAngularDistance); if (deltaRotation <= TURNING_MOVEMENT_LIMIT) { speed = TopMovementSpeed() * ((TURNING_MOVEMENT_LIMIT - deltaRotation) / TURNING_MOVEMENT_LIMIT); } Location.Position += (velocityVector * speed); Location.Rotation = rotation; if (Game.SurvivorSubsystem.SurvivorsCollisionManager.CheckForCollisionsWith(this) || Game.ZombiesSubsystem.ZombiesCollisionManager.CheckForCollisionsWith(this)) { Location.Position = oldPosition; Location.Rotation = oldRotation; zombieStationaryTime += gameTime.ElapsedGameTime.TotalSeconds; } else { zombieStationaryTime = 0.0; } } if (zombieStationaryTime >= ZOMBIE_MAXIMUM_STATIONARY_TIME) { Alive = false; } Single zombieAttackValue = 0.0f; if (zombieAttackTime >= ZOMBIE_ATTACK_DELAY_TIME[(int)Game.Options.GameDifficulty]) { zombieAttackValue = ZOMBIE_ATTACK_VALUE; } if (Game.SurvivorSubsystem.SurvivorsAttackManager.AttackWithMelee(this, zombieAttackValue) == AttackResults.Damage) { zombieAttackTime += gameTime.ElapsedGameTime.TotalSeconds; } else { zombieAttackTime = 0.0; } if (animating) { zombieAnimationTime += gameTime.ElapsedGameTime.TotalSeconds; if (zombieAnimationTime >= ANIMATION_DELAY) { animationIndex++; if (animationIndex < ANIMATION_CELL_INDICIES.Length) { currentCellIndex = ANIMATION_CELL_INDICIES[animationIndex]; zombieAnimationTime = 0.0; } else { currentCellIndex = 0; animating = false; } } } } }