/// <summary> /// What do when a collision ends /// </summary> /// <param name="sender">Who WAS I colliding with?</param> /// <param name="e">Ignore this</param> protected override void HandleCollisionExit(object sender, EventArgs e) { //Cast to a Collider Collider other = (Collider)sender; switch (state) { case PlayerState.OnLand: //If you walk off a platform, you should fall if you're no longer colliding below with anything if (other.Host is Platform) { if (!CollBelow.CollidingWith <Platform>()) { Acceleration = airAcceleration; state = PlayerState.InAir; } } break; case PlayerState.InAir: break; case PlayerState.InWater: break; case PlayerState.IsDead: break; default: break; } }
/// <summary> /// Clear all the collisions for each collider /// </summary> protected override void ClearCollisions() { base.ClearCollisions(); CollAbove.ResetCollider(); CollBelow.ResetCollider(); CollLeft.ResetCollider(); CollRight.ResetCollider(); CollCenter.ResetCollider(); }
public override void Update(GameTime gameTime) { //Update the rest of the colliders as well CollAbove.Update(gameTime); CollBelow.Update(gameTime); CollLeft.Update(gameTime); CollRight.Update(gameTime); base.Update(gameTime); }
/// <summary> /// Check for a collision with water when we're not swimming /// </summary> /// <param name="other">Collider to check with</param> private void CheckForWaterWhenNotSwimming() { if (Coll.CollidingWith <Water>() && !CollBelow.CollidingWith <Platform>()) { state = PlayerState.InWater; Acceleration = .05f * airAcceleration; Velocity.Y = MathHelper.Clamp(Velocity.Y, -MoveSpeed, MoveSpeed); //Turn blue to match water DrawColor = Color.Blue; //Loop ambient swim noise GameManager.LoopSFX("WaterLoop"); } }
/// <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> /// What do for collisions /// </summary> /// <param name="sender">Who am I colliding with?</param> /// <param name="e">Ignore this</param> /// If you need more details, use CollAbove, CollLeft, etc. /// Alternatively, here are some useful comparisions: /// <example> /// if (Location.Y < c.Host.Location.Y) Is true when colliding with something below /// if (Location.Y > c.Host.Location.Y) Is true when colliding with something above /// if (Location.X < c.Host.Location.X) Is true when colliding with something on the right /// if (Location.X > c.Host.Location.X) Is true when colliding with something on the left /// </example> protected override void HandleCollisionEnter(object sender, EventArgs e) { //Cast to a Collider Collider other = (Collider)sender; switch (state) { case PlayerState.OnLand: break; case PlayerState.InAir: //Stop on a platform if (other.Host is Platform) { //When colliding below, stop if (CollBelow.CollidingWith(other.Host)) { Acceleration = Vector2.Zero; Velocity.Y = 0; state = PlayerState.OnLand; //Snap to the top of the platform we just landed on Location.Y = (int)(other.Host.Location.Y - (.95f * Location.Height)); } //When colliding above, reset y velocity else if (CollAbove.CollidingWith(other.Host)) { Velocity.Y = 0; } } break; case PlayerState.InWater: break; case PlayerState.IsDead: break; default: break; } }
/// <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; } }
/// <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"); } }