コード例 #1
0
        /// <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();

            // 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
                    pickups.Add(new Pickup(pickupSprite, new Vector2(mouse.Position.X, mouse.Position.Y)));

                    // STUDENTS: if this is the first pickup in the list, set teddy target
                    if (pickups.Count == 1)
                    {
                        teddy.SetTarget(new Vector2(pickups[0].CollisionRectangle.Center.X, pickups[0].CollisionRectangle.Center.Y));
                    }
                }
            }

            // 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)
                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)
                {
                    teddy.SetTarget(new Vector2(pickups[0].CollisionRectangle.Center.X, pickups[0].CollisionRectangle.Center.Y));
                }
                else
                {
                    teddy.Collecting = false;
                    teddy.ClearTarget();
                }
            }

            teddy.Update(gameTime, mouse);

            base.Update(gameTime);
        }
コード例 #2
0
        /// <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)
        {
            // For Mobile devices, this logic will close the Game when the Back button is pressed
            // Exit() is obsolete on iOS
#if !__IOS__
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
                Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }
#endif

            // 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 pos = new Vector2(mouse.X, mouse.Y);
                    pickups.Add(new Pickup(pickupSprite, pos));

                    // STUDENTS: if this is the first pickup in the list, set teddy target
                    if (pickups.Count == 1)
                    {
                        teddy.SetTarget(pos);
                    }
                }
            }

            // 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)
                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)
                {
                    teddy.SetTarget(new Vector2(
                                        pickups[0].CollisionRectangle.X +
                                        pickups[0].CollisionRectangle.Width / 2f,
                                        pickups[0].CollisionRectangle.Y +
                                        pickups[0].CollisionRectangle.Height / 2f));
                }
                else
                {
                    teddy.ClearTarget();
                    teddy.Collecting = false;
                }
            }

            base.Update(gameTime);
        }