/// <summary> /// Update the enemy location depending on AI logic and play sound effects /// </summary> /// <param name="gameTime">Snapshot of game timing state</param> /// <param name="maze">The maze the enemy is in</param> /// <param name="listener">The listener to enemy's footstep sound effect and music</param> /// <param name="music">The music that the enemy objec will be effecting depends on distance to the listener</param> public void Update(GameTime gameTime, Maze maze, AudioListener listener, SoundEffectInstance music) { float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds; Vector3 moveAmount = Vector3.Zero; // first we looking into future time so that enemy won't walk into walls moveAmount.Z += MOVE_SCALE * elapsedTime * PREVIEW_FRAMES; Vector3 newLocation = PreviewMove(moveAmount); willCollide = false; foreach (BoundingBox box in maze.GetBoundsForCell((int)newLocation.X, (int)newLocation.Z)) { if (box.Contains(newLocation) == ContainmentType.Contains) { willCollide = true; } } // if walking stright will collide with wall, try turning left if ( willCollide ) { // try turn left Direction = (Direction + 1) % 4; moveAmount = Vector3.Zero; moveAmount.Z += MOVE_SCALE * elapsedTime * PREVIEW_FRAMES; newLocation = PreviewMove(moveAmount); willCollide = false; foreach (BoundingBox box in maze.GetBoundsForCell((int)newLocation.X, (int)newLocation.Z)) { if (box.Contains(newLocation) == ContainmentType.Contains) { willCollide = true; } } } // if turn left fails, try opposite direction (turn right from original direction) if ( willCollide ) { // try turn right Direction = (Direction + 2) % 4; moveAmount = Vector3.Zero; moveAmount.Z += MOVE_SCALE * elapsedTime * PREVIEW_FRAMES; newLocation = PreviewMove(moveAmount); willCollide = false; foreach (BoundingBox box in maze.GetBoundsForCell((int)newLocation.X, (int)newLocation.Z)) { if (box.Contains(newLocation) == ContainmentType.Contains) { willCollide = true; } } } // if still going to collide, make a u-turn if ( willCollide ) { // try walk backward Direction = (Direction + 3) % 4; moveAmount = Vector3.Zero; moveAmount.Z += MOVE_SCALE * elapsedTime * PREVIEW_FRAMES; newLocation = PreviewMove(moveAmount); willCollide = false; foreach (BoundingBox box in maze.GetBoundsForCell((int)newLocation.X, (int)newLocation.Z)) { if (box.Contains(newLocation) == ContainmentType.Contains) { willCollide = true; } } } // Animate legs movement if (isLeftLegForward) { leftLegRotation -= LEG_ROTATION_SCALE; rightLegRotation += LEG_ROTATION_SCALE; } else { rightLegRotation -= LEG_ROTATION_SCALE; leftLegRotation += LEG_ROTATION_SCALE; } // reset move amount to just timing the elapsed time instead of a future time moveAmount = Vector3.Zero; moveAmount.Z += MOVE_SCALE * elapsedTime; newLocation = PreviewMove(moveAmount); Apply3DAudio(listener, newLocation, music); position = newLocation; }
public void Update(GameTime gameTime, Maze maze, AudioListener listener) { float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds; wallCollisionElapsedTime += gameTime.ElapsedGameTime.Milliseconds; footStepElapsedTime += (float)gameTime.ElapsedGameTime.TotalMilliseconds; // Restrain movement to the maze width and height if (moveAmount.Z != 0 || moveAmount.X != 0) { Vector3 newLocation = PreviewMove(moveAmount); bool moveOk = true; if (newLocation.X < 0 || newLocation.X > Maze.MAZE_WIDTH) moveOk = false; if (newLocation.Z < 0 || newLocation.Z > Maze.MAZE_HEIGHT) moveOk = false; // Handle Collision based on bounding box if (collision) foreach (BoundingBox box in maze.GetBoundsForCell((int)newLocation.X, (int)newLocation.Z)) { if (box.Contains(newLocation) == ContainmentType.Contains) { moveOk = false; // check to play wall sounds if wallsound is true and there is a delap. dealys stops constant looping if (wallSound && wallCollisionElapsedTime > WALL_COLLISION_DELAY) { wallCollisionSound.Play(); wallSound = false; // reset elapsedTime to provide a delay wallCollisionElapsedTime = 0; } } else { // enable sounds again wallSound = true; } } // if no collision and ok to move the move and play footstep sound if (moveOk) { MoveForward(moveAmount); listener.Velocity = (newLocation - position) / elapsedTime; // Play footstep sounds if (footStepElapsedTime > FOOT_STEP_DELAY) { if (footsteps) leftFootstepSound.Play(); else rightFootstepSound.Play(); footsteps = !footsteps; footStepElapsedTime = 0; } } else { listener.Velocity = Vector3.Zero; } } else { listener.Velocity = Vector3.Zero; } listener.Position = Position; listener.Up = View.Up; listener.Forward = View.Forward; }