Beispiel #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)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.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 positionMouse = new Vector2(mouse.X, mouse.Y);
                    pickups.Add(new Pickup(pickupSprite, positionMouse));

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

            // STUDENTS: Delete the line saying if (true) and uncomment the three
            // lines below that AFTER you've created a teddy object in the
            // LoadContent method
            // check for collision between collecting teddy and targeted pickup
            //if (true)
            if (teddy.Collecting &&
                pickups.Count > 0 &&
                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, stop the teddy from collecting
                if (pickups.Count > 0)
                {
                    Point newTargetPosition = pickups[0].CollisionRectangle.Center;
                    teddy.SetTarget(new Vector2(newTargetPosition.X, newTargetPosition.Y));
                }
                else
                {
                    teddy.SetTarget(Vector2.Zero);
                }
            }

            base.Update(gameTime);
        }
Beispiel #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)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.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
                    pickups.Add(new Pickup(pickupSprite, new Vector2(mouse.X, mouse.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.X, pickups[0].CollisionRectangle.Y));
                    }
                }
            }

            // STUDENTS: Delete the line saying if (true) and uncomment the three
            // lines below that AFTER you've created a teddy object in the
            // LoadContent method
            // check for collision between collecting teddy and targeted pickup
            //if (true)
            if (teddy.Collecting &&
                pickups.Count > 0 &&
                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, stop the teddy from collecting
                if (pickups.Count > 0)
                {
                    teddy.SetTarget(new Vector2(pickups[0].CollisionRectangle.X, pickups[0].CollisionRectangle.Y));
                }
                else
                {
                    teddy.Collecting = false;
                }
                teddy.Update(gameTime, mouse);
            }
            else if (pickups.Count == 0)
            {
                //also stop collecting if no pickups remain and the other conditions are not met
                //see https://class.coursera.org/gameprogramming-002/forum/thread?thread_id=1314
                teddy.Collecting = false;
            }

            base.Update(gameTime);
        }