public void TargetNearest(List <Pickup> pickups) { Vector2 nearestTarget = pickups[0].CollisionRectangle.Center.ToVector2(); Pickup nearestPickup = pickups[0]; foreach (Pickup pickup in pickups) { float nearestDistance = Vector2.Distance(location, nearestTarget); Vector2 newTarget = pickup.CollisionRectangle.Center.ToVector2(); float distance = Vector2.Distance(location, newTarget); if (distance < nearestDistance) { nearestTarget = newTarget; nearestPickup = pickup; } } SetTarget(nearestTarget); target = nearestPickup; }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } // STUDENTS: get current mouse state and update teddy MouseState mouse = Mouse.GetState(); teddy.Update(gameTime, mouse); // check for right click started if (mouse.RightButton == ButtonState.Pressed && rightButtonReleased) { rightClickStarted = true; rightButtonReleased = false; } else if (mouse.RightButton == ButtonState.Released) { rightButtonReleased = true; // if right click finished, add new pickup to list if (rightClickStarted) { rightClickStarted = false; // STUDENTS: add a new pickup to the end of the list of pickups Vector2 pickupLocation = new Vector2(mouse.X - pickupSprite.Width / 2, mouse.Y - pickupSprite.Height / 2); Pickup newPickup = new Pickup(pickupSprite, pickupLocation); pickups.Add(newPickup); // STUDENTS: if this is the first pickup in the list, set teddy target //one pickup in pickups means this is the first pickup if (pickups.Count == 1) { Rectangle pickupRect = pickups[0].CollisionRectangle; teddy.SetTarget(pickupLocation); } } } // check for collision between collecting teddy and targeted pickup if (teddy.Collecting && teddy.CollisionRectangle.Intersects(pickups[0].CollisionRectangle)) { // STUDENTS: remove targeted pickup from list (it's always at location 0) //RemoveAt method allows you to remove pickup using index pickups.RemoveAt(0); // STUDENTS: if there's another pickup to collect, set teddy target // If not, clear teddy target and stop the teddy from collecting if (pickups.Count > 0) { Rectangle pickupRect = pickups[0].CollisionRectangle; Vector2 pickupLocation = new Vector2(pickupRect.X, pickupRect.Y); teddy.SetTarget(pickupLocation); } else { teddy.ClearTarget(); teddy.Collecting = false; } } base.Update(gameTime); }
public void TargetNearest(List<Pickup> pickups) { Vector2 nearestTarget = pickups[0].CollisionRectangle.Center.ToVector2(); Pickup nearestPickup = pickups[0]; foreach (Pickup pickup in pickups) { float nearestDistance = Vector2.Distance(location, nearestTarget); Vector2 newTarget = pickup.CollisionRectangle.Center.ToVector2(); float distance = Vector2.Distance(location, newTarget); if (distance < nearestDistance) { nearestTarget = newTarget; nearestPickup = pickup; } } SetTarget(nearestTarget); target = nearestPickup; }