/// <summary> /// Manage movement for a variable speed /// Jump also activated here, but only on Land /// </summary> /// <param name="speed"></param> public void Move(int speed) { if (InputManager.GetButton("Right") && !CollRight.CollidingWith <Platform>()) { Velocity.X = speed; SpriteFX = SpriteEffects.FlipHorizontally; direction = Direction.Right; } else if (InputManager.GetButton("Left") && !CollLeft.CollidingWith <Platform>()) { Velocity.X = -speed; SpriteFX = SpriteEffects.None; direction = Direction.Left; } else { Velocity.X = 0; } //Handle jumping, but ONLY on land if (InputManager.GetButtonDown("Jump") && state == PlayerState.OnLand) { state = PlayerState.InAir; //Start pulling the player down Acceleration = airAcceleration; //But give them an initial upward velocity Velocity = jumpVelocity; //Play a sound effect GameManager.PlaySFX("Jump"); } }
/// <summary> /// Finds the distance between enemy and player and advances toward player /// Once player exits range method ends /// </summary> public void ChasePlayer() { if (!CollBelow.CollidingWith <Platform>()) { return; } GameObject player = GameManager.Get("Current"); //store the distance between int distX = player.Location.X - this.Location.X; int distY = player.Location.Y - this.Location.Y; //Check to see if player is within range //This checks to see if player is within a 200x200 square around the enemy if (Math.Abs(distX) <= MAX_DETECTION || Math.Abs(distY) <= MAX_DETECTION) { //Check if to the left or right of the player, set speed to player direction and move if (distX < 0 && !CollLeft.CollidingWith <Platform>()) { direction = Direction.Left; SetSpeed(); } else if (distX > 0 && !CollRight.CollidingWith <Platform>()) { direction = Direction.Right; SetSpeed(); } else { return; } Location.X += (int)Speed; if (Speed <= 0) { SpriteFX = SpriteEffects.FlipHorizontally; } else { SpriteFX = SpriteEffects.None; } } }
/// <summary> /// Swim Update /// </summary> public void Swim(int speed) { int horz = 0; int vert = 0; if (InputManager.GetButton("Right")) { horz = 1; SpriteFX = SpriteEffects.FlipHorizontally; direction = Direction.Right; } if (InputManager.GetButton("Left")) { horz = -1; SpriteFX = SpriteEffects.None; direction = Direction.Left; } if (InputManager.GetButton("Up")) { vert = 1; } if (InputManager.GetButton("Down")) { vert = -1; } if (horz != 0 || vert != 0) { Velocity = new Vector2(horz * speed, -vert * speed); } //Check for collisions and stop appropriate component of velocity while swimming if (CollLeft.CollidingWith <Platform>() && Velocity.X < 0) { Velocity.X = 0; } if (CollRight.CollidingWith <Platform>() && Velocity.X > 0) { Velocity.X = 0; } if (CollAbove.CollidingWith <Platform>() && Velocity.Y < 0) { Velocity.Y = 0; } if (CollBelow.CollidingWith <Platform>() && Velocity.Y > 0) { Velocity.Y = 0; } //Exit swimming state when not colliding with any water objects //Done here rather than in HandleCollision events because there //will be a lot of water tiles near each other. if (!Coll.CollidingWith <Water>()) { state = PlayerState.InAir; Acceleration = airAcceleration; if (Velocity.Y <= 0) { Velocity.Y = .5f * jumpVelocity.Y; } //Turn back to normal color DrawColor = Color.White; //Stop ambient swim noise GameManager.StopSFX("WaterLoop"); } }
/// <summary> /// Has enemy move back and forth in a time interval /// </summary> /// <param name="gameTime"></param> public void Wander(GameTime gameTime) { TotalElapsedSeconds += gameTime.ElapsedGameTime.TotalSeconds; if (TotalElapsedSeconds >= MoveChangeTime || !CollBelow.CollidingWith <Platform>() || (Speed > 0 && CollRight.CollidingWith <Platform>()) || (Speed < 0 && CollLeft.CollidingWith <Platform>())) { TotalElapsedSeconds -= MoveChangeTime; ChangeDirection(); SetSpeed(); } this.Location.X += (int)Speed; if (Speed <= 0) { SpriteFX = SpriteEffects.FlipHorizontally; } else { SpriteFX = SpriteEffects.None; } }