/// <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>() && !Coll.CollidingWith <Water>()) { Acceleration = airAcceleration; state = PlayerState.InAir; } } break; case PlayerState.InAir: break; case PlayerState.InWater: break; case PlayerState.IsDead: break; default: break; } }
/// <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> /// 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"); } }
public override void Update(GameTime gameTime) { this.gameTime = gameTime; //add update logic here GameObject player = GameManager.Get("Current"); int distX = this.Location.X - player.Location.X; int distY = this.Location.X - player.Location.X; switch (state) { case EnemyState.Roaming: //Check to see if player is within range of detection if (Math.Abs(distX) <= MAX_DETECTION || Math.Abs(distY) <= MAX_DETECTION) { state = EnemyState.PlayerDetected; } //Call wander method to move enemy Wander(gameTime); break; case EnemyState.PlayerDetected: //Call Chase player method ChasePlayer(); //Checks if player is still withing detection range if (Math.Abs(distX) > MAX_DETECTION || Math.Abs(distY) > MAX_DETECTION) { state = EnemyState.Returning; } break; case EnemyState.Returning: //Call ReturnHome Method ReturnHome(); //find distance between enemy and home int disX = Location.X - (int)HomePoint.X; int disY = Location.Y - (int)HomePoint.Y; //If in range set to roaming if (Math.Abs(disX) <= 5 || Math.Abs(disY) <= 5) { state = EnemyState.Roaming; } break; case EnemyState.Waiting: collActive = false; //disable collisions delayTimer += gameTime.ElapsedGameTime.TotalMilliseconds; if (delayTimer >= 1500) { state = EnemyState.PlayerDetected; delayTimer = 0; collActive = true; //re-enable collisions } break; default: break; } //Don't go leavin dat water do if (!Coll.CollidingWith <Water>()) { state = EnemyState.Returning; } //Death logic if (Health <= 0) { IsAlive = false; } base.Update(gameTime); }