/// <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> /// 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"); } }