Exemple #1
0
        public void UpdateInputWindows(RenderingState renderingState, GameTime time)
        {
            MouseState touchGesture = Mouse.GetState();

            if (touchGesture.LeftButton == ButtonState.Pressed
                && lastButtonState == ButtonState.Released)
            {
                playerClicked = true;
                lastButtonState = ButtonState.Pressed;

                if (firstClick == 0)
                    firstClick = time.ElapsedGameTime.TotalMilliseconds;
                else
                    secondClick = time.ElapsedGameTime.TotalMilliseconds;
            }

            if (playerClicked)
                myTimer += time.ElapsedGameTime.Milliseconds;

            if (myTimer > maxTimeForDoubleClick)
            {
                if (secondClick != 0 && secondClick - firstClick < maxTimeForDoubleClick)
                {
                    /*doubleclick*/
                    doubleClick(renderingState, time);
                }
                else
                {
                    /*singleclick*/
                    singleClick(renderingState, time);
                }

                playerClicked = false;
                firstClick = 0;
                secondClick = 0;
                myTimer = 0;
            }

            if (Mouse.GetState().LeftButton == ButtonState.Released)
            {
                lastButtonState = ButtonState.Released;
            }

            /*Move around*/
            if (Mouse.GetState().X > renderingState.spriteBatch.GraphicsDevice.Viewport.Width-50)
            {
                renderingState.TranslateX(movingSpeed);
            }

            if (Mouse.GetState().X < 50)
            {
                renderingState.TranslateX(-movingSpeed);
            }

            if (Mouse.GetState().Y > renderingState.spriteBatch.GraphicsDevice.Viewport.Height-50)
            {
                renderingState.TranslateZ(movingSpeed);
            }

            if (Mouse.GetState().Y < 50)
            {
                renderingState.TranslateZ(-movingSpeed);
            }

            /* Zoom */
            int prova = Mouse.GetState().ScrollWheelValue;
            if (Mouse.GetState().ScrollWheelValue > 100)
                renderingState.zoomIn();
            else
            {
                if(Mouse.GetState().ScrollWheelValue < 0)
                renderingState.zoomOut();
            }
        }
Exemple #2
0
        public void UpdateInput(RenderingState renderingState, GameTime time)
        {
            while (TouchPanel.IsGestureAvailable)
            {
                var touchGesture = TouchPanel.ReadGesture();
                Ray pickRay;

                switch (touchGesture.GestureType)
                {
                    /*
                     * Single tap select a factory
                     */
                    case GestureType.Tap:
                        {
                            if (stateGame.state.enabled)
                            {
                                if (renderingState.speedBonusRect.Contains(
                                    new Point((int)touchGesture.Position.X, (int)touchGesture.Position.Y)))
                                {
                                    /*
                                     * Player clicked on the speed bonus.
                                     */
                                    manageBonus(0, renderingState);
                                    return;
                                }

                                if (renderingState.freezeBonusRect.Contains(
                                    new Point((int)touchGesture.Position.X, (int)touchGesture.Position.Y)))
                                {
                                    /*
                                     * Player clicked on the attack bonus
                                     */
                                    manageBonus(1, renderingState);
                                    return;
                                }

                                if (renderingState.halfFactoryBonusRect.Contains(
                                    new Point((int)touchGesture.Position.X, (int)touchGesture.Position.Y)))
                                {
                                    /*
                                     * Player clicked on the shield bonus
                                     */
                                    manageBonus(2, renderingState);
                                    return;
                                }

                                if (renderingState.armageddonBonusRect.Contains(
                                new Point((int)touchGesture.Position.X, (int)touchGesture.Position.Y)))
                                {
                                    manageBonus(3, renderingState);
                                    return;
                                }

                                StateGameLayer.Factory targeted = null;

                                pickRay = calculateRay(new Vector2(touchGesture.Position.X, touchGesture.Position.Y), renderingState);

                                targeted = calculateTargetedFactory(pickRay, renderingState);

                                if (targeted != null && targeted.ObjectType == 1)
                                {
                                    /*
                                    * ...player selected the factory "targeted".
                                    * If here, "targeted" is property of player. A player
                                    * can only select his own factories (type == 1).
                                    * If firstFactory is null, this is the first factory
                                    * the player selected.
                                    */
                                    if (firstFactory == null)
                                    {
                                        firstFactory = targeted;
                                        firstFactory.Selected = true;
                                        firstFactory.selectionSound = true;
                                    }
                                    else
                                    {
                                        /*
                                         * ...else this is the second factory.
                                         * Player can only double select his own factories.
                                         * Cannot select: firstFactory and an enemy factory.
                                         * Cannot select: firstFactory and a neutral factory.
                                         */

                                        /*
                                         * Player maybe have already tapped a second factory and
                                         * selects another secondFactory. Reset previous secondFactory.
                                         */
                                        if (secondFactory != null)
                                            secondFactory.Selected = false;

                                        secondFactory = targeted;
                                        if (secondFactory.ObjectType == 1)
                                        {
                                            secondFactory.Selected = true;
                                            secondFactory.selectionSound = true;
                                        }
                                        else
                                        {
                                            secondFactory.Selected = false;
                                            secondFactory = null;
                                        }
                                    }

                                    return;
                                }

                                /*
                                * If here, player targeted an enemy or neutral factory.
                                * Or maybe player tapped on nothing (no collision).
                                * Reset.
                                */
                                if (firstFactory != null)
                                {
                                    firstFactory.Selected = false;
                                    firstFactory = null;
                                }

                                if (secondFactory != null)
                                {
                                    secondFactory.Selected = false;
                                    secondFactory = null;
                                }

                                if (thirdFactory != null)
                                {
                                    thirdFactory.Selected = false;
                                    thirdFactory = null;
                                }
                            }
                        }
                        if (renderingState.pauseButton.Contains(
                        new Point((int)touchGesture.Position.X, (int)touchGesture.Position.Y)))
                        {
                            /*
                            * Player clicked on pause button
                            */
                            if (stateGame.state.enabled)
                                stop();
                            else
                                start();

                            return;
                        }
                        break;

                    /*
                     * Double tap to do an action
                     */
                    case GestureType.Hold:
                        {
                            if (stateGame.state.enabled)
                            {
                                /*
                                * Player wants to do an action.
                                * If a firstFactory was not selected, then cannot do anything
                                */
                                if (firstFactory == null)
                                    return;

                                pickRay = calculateRay(new Vector2(touchGesture.Position.X, touchGesture.Position.Y), renderingState);

                                StateGameLayer.Factory targeted = calculateTargetedFactory(pickRay, renderingState);

                                if (targeted == null)
                                    return;

                                /*Here targeted is not null*/
                                if (secondFactory == null)
                                {
                                    /*
                                    * If here, player has only selected a first factory.
                                    * Selecting the second factory with a double tap
                                    * means he wants to attack an enemy factory (type == 2) or defend
                                    * his own factory (type == 1).
                                    */

                                    secondFactory = targeted;
                                    //secondFactory.Selected = true;

                                    /*
                                     * Player cannot move tropps from and to the same factory
                                     */
                                    if (secondFactory.Equals(firstFactory))
                                        return;

                                    List<StateGameLayer.Factory> order = new List<StateGameLayer.Factory>();
                                    order.Add(firstFactory);
                                    order.Add(secondFactory);
                                    stateGame.state.addOrder(order, 1);

                                    arrowPosition.Clear();
                                    arrowPosition.Add(firstFactory.CurrentPos);
                                    arrowPosition.Add(secondFactory.CurrentPos);

                                    stateGame.state.touchBegin = true;
                                }
                                else
                                {
                                    /*Double attack*/

                                    /*
                                     * ...else the player has already selected a second factory and
                                     * wants to do a double attack or move his troops from
                                     * two factories to another one of his.
                                     */

                                    thirdFactory = targeted;
                                    //thirdFactory.Selected = true;

                                    /*Player cannot move troops to one of the two selected factories*/
                                    if (thirdFactory.Equals(firstFactory) || thirdFactory.Equals(secondFactory))
                                        return;

                                    List<StateGameLayer.Factory> order = new List<StateGameLayer.Factory>();
                                    order.Add(firstFactory);
                                    order.Add(secondFactory);
                                    order.Add(thirdFactory);

                                    stateGame.state.addOrder(order, 1);

                                    arrowPosition.Clear();
                                    arrowPosition.Add(firstFactory.CurrentPos);
                                    arrowPosition.Add(secondFactory.CurrentPos);
                                    arrowPosition.Add(thirdFactory.CurrentPos);
                                }

                                /*
                                * If here, player launched a single, or double attack, or
                                * moved his troops or double tapped on nothing.
                                * Reset.
                                */
                                if (firstFactory != null)
                                {
                                    firstFactory.Selected = false;
                                    firstFactory = null;
                                }

                                if (secondFactory != null)
                                {
                                    secondFactory.Selected = false;
                                    secondFactory = null;
                                }

                                if (thirdFactory != null)
                                {
                                    thirdFactory.Selected = false;
                                    thirdFactory = null;
                                }

                                stateGame.state.touchBegin = true;
                            }
                        }
                        break;

                    case GestureType.Pinch:
                        {
                            float distance;

                            Vector2 firstFinger = new Vector2(touchGesture.Delta.X, touchGesture.Delta2.Y);
                            Vector2 secondFinger = new Vector2(touchGesture.Delta2.X, touchGesture.Delta2.Y);

                            distance = Vector2.Distance(firstFinger, secondFinger);

                            if (stateGame.state.previousDistance == null)
                                stateGame.state.previousDistance = distance;

                            if (distance < stateGame.state.previousDistance)
                            {
                                /*Aumenta il fieldOfVIew*/
                                renderingState.zoomOut();
                            }
                            else
                                renderingState.zoomIn();
                        }
                        break;

                    case GestureType.FreeDrag:
                        {
                            if (stateGame.state.enabled)
                            {
                                float speedOfDragging = 7.8f;
                                if (stateGame.state.dragging.Count < 2)
                                {
                                    stateGame.state.dragging.Add(new Vector2(touchGesture.Position.X, touchGesture.Position.Y));
                                }
                                else
                                {
                                    /*If here, 2 drag position*/
                                    float x = stateGame.state.dragging[0].X - stateGame.state.dragging[1].X;

                                    /*Calc the Z translation (Z for the world, Y for the touchscreen*/
                                    float z = stateGame.state.dragging[0].Y - stateGame.state.dragging[1].Y;

                                    /*See you.*/
                                    renderingState.TranslateX(x * speedOfDragging);
                                    renderingState.TranslateZ(z * speedOfDragging);

                                    /*Remove the ancient position*/
                                    stateGame.state.dragging.RemoveAt(0);
                                }
                            }
                        }
                        break;

                    case GestureType.DragComplete:
                        /*Reset info about dragging*/
                        stateGame.state.dragging.Clear();
                        break;
                }

            }
        }