Пример #1
0
        /// <summary>
        /// Update method
        /// </summary>
        /// <param name="gameTime">game time</param>
        protected override void Update(TimeSpan gameTime)
        {
            this.input = WaveServices.Input;

            KeyboardState currentKeyboardState = this.input.KeyboardState;

            if (currentKeyboardState.IsConnected)
            {
                if (currentKeyboardState.IsKeyPressed(Keys.O) &&
                    this.lastKeyboardState.IsKeyReleased(Keys.O))
                {
                    this.RenderManager.DebugLines = !this.RenderManager.DebugLines;
                }

                this.lastKeyboardState = currentKeyboardState;
            }

            if (this.input.TouchPanelState.IsConnected)
            {
                this.touchState = this.input.TouchPanelState;

                if (this.touchState.Count > 0 && this.mouseJoint == null)
                {
                    this.TouchPosition = this.touchState[0].Position;
                    this.vsm.ToVirtualPosition(ref this.TouchPosition);

                    foreach (Entity entity in this.Owner.Scene.EntityManager.FindAllByTag("Draggable"))
                    {
                        Collider2D collider = entity.FindComponent <Collider2D>(false);
                        if (collider != null)
                        {
                            if (collider.Contain(TouchPosition))
                            {
                                RigidBody2D rigidBody = entity.FindComponent <RigidBody2D>();
                                if (rigidBody != null)
                                {
                                    if (rigidBody.PhysicBodyType == WaveEngine.Common.Physics2D.RigidBodyType2D.Dynamic)
                                    {
                                        this.ConnectedEntity = entity;

                                        //Create Joint
                                        this.mouseJoint = new MouseJoint2D()
                                        {
                                            Target = this.TouchPosition,
                                        };
                                        this.ConnectedEntity.AddComponent(mouseJoint);

                                        break;
                                    }
                                }
                            }
                        }
                    }
                }

                if (this.touchState.Count == 0 && this.mouseJoint != null)
                {
                    if (!this.ConnectedEntity.IsDisposed)
                    {
                        this.ConnectedEntity.RemoveComponent(this.mouseJoint);
                    }

                    this.mouseJoint = null;
                }

                if (this.mouseJoint != null)
                {
                    this.TouchPosition = this.touchState[0].Position;
                    this.vsm.ToVirtualPosition(ref this.TouchPosition);
                    this.mouseJoint.Target = this.TouchPosition;
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Update method
        /// </summary>
        /// <param name="gameTime">game time</param>
        protected override void Update(TimeSpan gameTime)
        {
            this.input = WaveServices.Input;

            KeyboardState currentKeyboardState = this.input.KeyboardState;
            if (currentKeyboardState.IsConnected)
            {
                if (currentKeyboardState.IsKeyPressed(Keys.O) &&
                   this.lastKeyboardState.IsKeyReleased(Keys.O))
                {
                    this.RenderManager.DebugLines = !this.RenderManager.DebugLines;
                }

                this.lastKeyboardState = currentKeyboardState;
            }

            if (this.input.TouchPanelState.IsConnected)
            {
                this.touchState = this.input.TouchPanelState;

                if (this.touchState.Count > 0 && this.mouseJoint == null)
                {
                    this.TouchPosition = this.touchState[0].Position;
                    this.vsm.ToVirtualPosition(ref this.TouchPosition);

                    foreach (Entity entity in this.Owner.Scene.EntityManager.FindAllByTag("Draggable"))
                    {
                        Collider2D collider = entity.FindComponent<Collider2D>(false);
                        if (collider != null)
                        {
                            if (collider.Contain(TouchPosition))
                            {
                                RigidBody2D rigidBody = entity.FindComponent<RigidBody2D>();
                                if (rigidBody != null)
                                {
                                    if (rigidBody.PhysicBodyType == WaveEngine.Common.Physics2D.RigidBodyType2D.Dynamic)
                                    {
                                        this.ConnectedEntity = entity;

                                        //Create Joint
                                        this.mouseJoint = new MouseJoint2D()
                                        {
                                            Target = this.TouchPosition,
                                            //MaxForce = 100,
                                            //DampingRatio = 0.5f,
                                            //FrequencyHz = 2000,
                                        };
                                        this.ConnectedEntity.AddComponent(mouseJoint);

                                        break;
                                    }
                                }
                            }
                        }
                    }
                }

                if (this.touchState.Count == 0 && this.mouseJoint != null)
                {
                    if (!this.ConnectedEntity.IsDisposed)
                    {
                        this.ConnectedEntity.RemoveComponent(this.mouseJoint);
                    }

                    this.mouseJoint = null;
                }

                if (this.mouseJoint != null)
                {
                    this.TouchPosition = this.touchState[0].Position;
                    this.vsm.ToVirtualPosition(ref this.TouchPosition);
                    this.mouseJoint.Target = this.TouchPosition;
                }
            }
        }
        /// <summary>
        /// Update Method
        /// </summary>
        /// <param name="gameTime">Current Game Time</param>
        protected override void Update(TimeSpan gameTime)
        {
            WaveServices.Layout.PerformLayout();

            switch (_scene.GameState)
            {
            case GameStateEnum.Automatic:
                if (_timer == null)
                {
                    _soundManager.PlaySound(SimpleSoundService.SoundType.Timer, 1f, true);

                    _timer = WaveServices.TimerFactory.CreateTimer("TimeToWait", TimeSpan.FromSeconds(1f), () =>
                    {
                        _time--;
                        _scene.TimerLeftTextBlock.Text = _time + " s";
                    });
                }
                else if (_time <= 0)
                {
                    WaveServices.TimerFactory.RemoveTimer("TimeToWait");
                    _scene.TimerLeftTextBlock.Text = "- s";

                    if (_timer2 == null)
                    {
                        _soundManager.StopAllSounds();

                        _revoluteJoint.EnableMotor = true;

                        _timer2 = WaveServices.TimerFactory.CreateTimer("TimeToAceletare", TimeSpan.FromSeconds(1f), () =>
                        {
                            _aceleratingTime--;
                        });
                    }

                    if (_aceleratingTime <= 0)
                    {
                        WaveServices.TimerFactory.RemoveTimer("TimeToAceletare");

                        _revoluteJoint.EnableMotor = false;
                    }
                }

                break;

            case GameStateEnum.Manual:
                WaveServices.TimerFactory.RemoveAllTimers();
                _timer  = null;
                _timer2 = null;
                _scene.TimerLeftTextBlock.Text = "- s";
                _revoluteJoint.EnableMotor     = false;

                _touchState = WaveServices.Input.TouchPanelState;
                if (_touchState.IsConnected)
                {
                    // Checks Mouse Left Button Click and anyone entity linked
                    if (_touchState.Count > 0 && _mouseJoint == null)
                    {
                        foreach (var touch in _touchState)
                        {
                            // Updates Mouse Position
                            _touchPosition = touch.Position;

                            // Adjust the position to the Viewport.
                            _scene.VirtualScreenManager.ToVirtualPosition(ref _touchPosition);

                            // Collider Test
                            var collider = _needleImage.FindComponent <CircleCollider2D>();
                            if (collider != null && collider.Contain(_touchPosition))
                            {
                                var rigidBody = _needleImage.FindComponent <RigidBody2D>();
                                if (rigidBody != null)
                                {
                                    _touchingId = touch.Id;

                                    // Create Mouse Joint
                                    _mouseJoint = new MouseJoint2D()
                                    {
                                        Target = _touchPosition
                                    };
                                    _needleImage.AddComponent(_mouseJoint);

                                    // We break after collide because no more than one touch can be Joint to entity.
                                    break;
                                }
                            }
                        }
                    }

                    // If joint exists then update joint anchor position.
                    // If touchReleased Then touchFound = false; so Remove the Joint to conserve physics.
                    if (_mouseJoint != null)
                    {
                        TouchLocation touchLocation;

                        if (_touchState.TryGetTouch(_touchingId, out touchLocation))
                        {
                            _touchPosition = touchLocation.Position;
                            _scene.VirtualScreenManager.ToVirtualPosition(ref _touchPosition);

                            _mouseJoint.Target = _touchPosition;
                        }
                        else
                        {
                            if (!_needleImage.IsDisposed)
                            {
                                _needleImage.RemoveComponent <MouseJoint2D>();
                            }

                            _mouseJoint = null;
                        }
                    }
                }

                break;
            }

            // Collision with pieces when the needle stops.
            var angularVelocity = _needleImageRigidBody2D.AngularVelocity;

            if (!_stopRepeat && angularVelocity > -0.005f && angularVelocity < 0.005f)
            {
                _soundManager.StopAllSounds();

                Movement movement = RouletteHelper.GetMovementFromRotation(_needleImage.FindComponent <Transform2D>().Rotation);

                movement.Number          = _movementsCount++;
                movement.NumberOfPlayers = _numberOfPlayers;

                Movement repeatedFingerMovement = null;
                switch (_numberOfPlayers)
                {
                case NumberPlayerEnum.OnePlayer:
                    repeatedFingerMovement = _movementsList.Find(x => x.Finger == movement.Finger);
                    if (repeatedFingerMovement != null)
                    {
                        if (repeatedFingerMovement.Color != movement.Color)
                        {
                            _movementToRemove = repeatedFingerMovement;
                            _movementsList.Add(movement);
                        }
                    }
                    else
                    {
                        _movementsList.Add(movement);
                    }
                    break;

                case NumberPlayerEnum.TwoPlayers:
                    repeatedFingerMovement = _movementsList.Find(x => x.PlayerNumber != _playerPlaying && x.Finger == movement.Finger);
                    if (repeatedFingerMovement != null)
                    {
                        if (repeatedFingerMovement.Color != movement.Color)
                        {
                            _movementToRemove = repeatedFingerMovement;
                            _movementsList.Add(movement);
                        }
                    }
                    else
                    {
                        _movementsList.Add(movement);
                    }
                    break;
                }

                _noMovementsTextBlock.IsVisible = false;
                _movementsTextBlockText.Text   += "\n" + movement.ToString();

                if (_numberOfPlayers == NumberPlayerEnum.TwoPlayers)
                {
                    if (_playerPlaying == 1)
                    {
                        _playerPlaying++;
                    }
                    else
                    {
                        _playerPlaying--;
                    }
                }

                if (_scene.GameState == GameStateEnum.Automatic)
                {
                    ResetAutomaticTimers();
                }

                _soundManager.PlaySound(SimpleSoundService.SoundType.Pick);

                _stopRepeat = true;
            }

            // When needle start to ride, it's time to another movement
            if (angularVelocity > 0.01f || angularVelocity < -0.01f)
            {
                _stopRepeat = false;

                if (_movementsList.Any(x => x.CirclePressed == null))
                {
                    GameOver();
                }
            }

            if (angularVelocity > 2f || angularVelocity < -2f)
            {
                if (_sound == null || _sound.State == SoundState.Stopped)
                {
                    _sound = _soundManager.PlaySound(SimpleSoundService.SoundType.Rotating);
                }
            }

            List <Entity> circlesPressed = new List <Entity>();

            // We check the Circles to assign the collider with touch.
            _touchState = WaveServices.Input.TouchPanelState;
            if (_touchState.IsConnected)
            {
                foreach (Entity entity in _scene.EntityManager.FindAllByTag("MatCircles"))
                {
                    var opacity = 1f;

                    foreach (var touch in _touchState)
                    {
                        // Updates Mouse Position
                        _touchPosition = touch.Position;

                        _scene.VirtualScreenManager.ToVirtualPosition(ref _touchPosition);

                        var collider = entity.FindComponent <CircleCollider2D>();
                        if (collider != null && collider.Contain(_touchPosition))
                        {
                            circlesPressed.Add(entity);
                            opacity = 0.5f;

                            break;
                        }
                    }

                    entity.FindComponent <Transform2D>().Opacity = opacity;
                }
            }

            // Check if user adds a new circle touch.
            foreach (var circle in circlesPressed)
            {
                if (!_movementsList.Any(x => x.CirclePressed == circle))
                {
                    var emptyMovement = _movementsList.Find(x => x.CirclePressed == null);
                    if (emptyMovement != null && emptyMovement.Color == circle.FindComponent <Sprite>().TintColor)
                    {
                        emptyMovement.CirclePressed = circle;
                        _movementsList.Remove(_movementToRemove);
                        _movementToRemove = null;
                    }
                    else
                    {
                        GameOver();
                    }
                }
            }

            // Check if user removes a circle touch.
            if (circlesPressed.Count < _lastCirclesPressed.Count)
            {
                Entity circleRemoved = null;

                foreach (var lastCircle in _lastCirclesPressed)
                {
                    if (!circlesPressed.Contains(lastCircle))
                    {
                        circleRemoved = lastCircle;
                        break;
                    }
                }

                if (circleRemoved != null)
                {
                    var movement = _movementsList.Find(x => x.CirclePressed == circleRemoved);

                    if (movement != null && movement != _movementToRemove)
                    {
                        GameOver();
                    }
                }
            }

            _lastCirclesPressed = circlesPressed.ToList();
        }
        /// <summary>
        /// Update method
        /// </summary>
        /// <param name="gameTime"></param>
        protected override void Update(TimeSpan gameTime)
        {
            this.input = WaveServices.Input;

            var activeCamera2D = this.RenderManager.ActiveCamera2D;

            if (this.input.TouchPanelState.IsConnected)
            {
                this.touchState = this.input.TouchPanelState;

                if (this.touchState.Count > 0 && this.mouseJoint == null)
                {
                    // Gets the virtual screen touch position
                    this.TouchPosition = this.touchState[0].Position;
                    this.vsm.ToVirtualPosition(ref this.TouchPosition);

                    // Launchs a ray from the virtual screen position
                    Ray r;
                    activeCamera2D.CalculateRay(ref this.TouchPosition, out r);
                    this.WorldPosition = r.IntersectionZPlane(0).ToVector2();

                    /// check collision with DRAGGABLE entities only
                    foreach (Entity entity in this.Owner.Scene.EntityManager.FindAllByTag(GameConstants.TAGDRAGGABLE))
                    {
                        Collider2D collider = entity.FindComponent <Collider2D>(false);
                        if (collider != null)
                        {
                            if (collider.Contain(this.WorldPosition))
                            {
                                RigidBody2D rigidBody = entity.FindComponent <RigidBody2D>();
                                if (rigidBody != null)
                                {
                                    if (rigidBody.PhysicBodyType == WaveEngine.Common.Physics2D.RigidBodyType2D.Dynamic)
                                    {
                                        this.ConnectedEntity = entity;

                                        //Create Joint
                                        this.mouseJoint = new MouseJoint2D()
                                        {
                                            Target = this.WorldPosition,
                                        };

                                        this.ConnectedEntity.AddComponent(mouseJoint);

                                        var audioService = WaveServices.GetService <AudioService>();
                                        audioService.Play(Audio.Sfx.Rubber_wav);

                                        break;
                                    }
                                }
                            }
                        }
                    }
                }

                // Touch just released, remove joint
                if (this.touchState.Count == 0 && this.mouseJoint != null)
                {
                    if (!this.ConnectedEntity.IsDisposed)
                    {
                        this.ConnectedEntity.RemoveComponent(this.mouseJoint);

                        var playerComponent = this.ConnectedEntity.FindComponent <PlayerComponent>();
                        playerComponent.Launch();
                    }

                    this.mouseJoint = null;
                }

                // update touch positions
                if (this.mouseJoint != null)
                {
                    this.TouchPosition = this.touchState[0].Position;
                    this.vsm.ToVirtualPosition(ref this.TouchPosition);

                    Ray r;
                    activeCamera2D.CalculateRay(ref this.TouchPosition, out r);
                    this.WorldPosition     = r.IntersectionZPlane(0).ToVector2();
                    this.mouseJoint.Target = this.WorldPosition;
                }
            }
        }
Пример #5
0
        /// <summary>
        /// Update Method
        /// </summary>
        /// <param name="gameTime">Current Game Time</param>
        protected override void Update(TimeSpan gameTime)
        {
            WaveServices.Layout.PerformLayout();

            switch (_scene.GameState)
            {
            case GameStateEnum.Automatic:
                if (_timer == null)
                {
                    _soundManager.PlaySound(SimpleSoundService.SoundType.Timer, 1f, true);

                    _timer = WaveServices.TimerFactory.CreateTimer("TimeToWait", TimeSpan.FromSeconds(1f), () =>
                    {
                        _time--;
                        _scene.TimerLeftTextBlock.Text = _time + " s";
                    });
                }
                else if (_time <= 0)
                {
                    WaveServices.TimerFactory.RemoveTimer("TimeToWait");
                    _scene.TimerLeftTextBlock.Text = "- s";

                    if (_timer2 == null)
                    {
                        _soundManager.StopAllSounds();

                        _revoluteJoint.EnableMotor = true;

                        _timer2 = WaveServices.TimerFactory.CreateTimer("TimeToAceletare", TimeSpan.FromSeconds(1f), () => { _aceleratingTime--; });
                    }

                    if (_aceleratingTime <= 0)
                    {
                        WaveServices.TimerFactory.RemoveTimer("TimeToAceletare");

                        _revoluteJoint.EnableMotor = false;
                    }
                }

                break;

            case GameStateEnum.Manual:
                WaveServices.TimerFactory.RemoveAllTimers();
                _timer  = null;
                _timer2 = null;
                _scene.TimerLeftTextBlock.Text = "- s";
                _revoluteJoint.EnableMotor     = false;

                _touchState = WaveServices.Input.TouchPanelState;
                if (_touchState.IsConnected)
                {
                    // Checks Mouse Left Button Click and anyone entity linked
                    if (_touchState.Count > 0 && _mouseJoint == null)
                    {
                        foreach (var touch in _touchState)
                        {
                            // Updates Mouse Position
                            _touchPosition = touch.Position;

                            // Adjust the position to the Viewport.
                            _scene.VirtualScreenManager.ToVirtualPosition(ref _touchPosition);

                            // Collider Test
                            var collider = _needleImage.FindComponent <CircleCollider2D>();
                            if (collider != null && collider.Contain(_touchPosition))
                            {
                                var rigidBody = _needleImage.FindComponent <RigidBody2D>();
                                if (rigidBody != null)
                                {
                                    _touchingId = touch.Id;

                                    // Create Mouse Joint
                                    _mouseJoint = new MouseJoint2D()
                                    {
                                        Target = _touchPosition
                                    };
                                    _needleImage.AddComponent(_mouseJoint);

                                    // We break after collide because no more than one touch can be Joint to entity.
                                    break;
                                }
                            }
                        }
                    }

                    // If joint exists then update joint anchor position.
                    // If touchReleased Then touchFound = false; so Remove the Joint to conserve physics.
                    if (_mouseJoint != null)
                    {
                        TouchLocation touchLocation;

                        if (_touchState.TryGetTouch(_touchingId, out touchLocation))
                        {
                            _touchPosition = touchLocation.Position;
                            _scene.VirtualScreenManager.ToVirtualPosition(ref _touchPosition);

                            _mouseJoint.Target = _touchPosition;
                        }
                        else
                        {
                            if (!_needleImage.IsDisposed)
                            {
                                _needleImage.RemoveComponent <MouseJoint2D>();
                            }

                            _mouseJoint = null;
                        }
                    }
                }

                break;
            }

            // Collision with pieces when the needle stops.
            float angularVelocity = _needleImageRigidBody2D.AngularVelocity;

            if (!_stopRepeat && angularVelocity > -0.005f && angularVelocity < 0.005f)
            {
                _soundManager.StopAllSounds();

                Movement movement = RouletteHelper.GetMovementFromRotation(_needleImage.FindComponent <Transform2D>().Rotation);

                movement.Number          = _movementsCount++;
                movement.NumberOfPlayers = _scene.NumberOfPlayers;

                _noMovementsTextBlock.IsVisible = false;
                _movementsTextBlockText.Text   += "\n" + movement.ToString();

                if (_scene.GameState == GameStateEnum.Automatic)
                {
                    ResetAutomaticTimers();
                }

                _soundManager.PlaySound(SimpleSoundService.SoundType.Pick);

                _stopRepeat = true;
            }

            // When needle start to ride, it's time to another movement
            if (angularVelocity > 0.01f || angularVelocity < -0.01f)
            {
                _stopRepeat = false;
            }

            if (angularVelocity > 2f || angularVelocity < -2f)
            {
                if (_sound == null || _sound.State == SoundState.Stopped)
                {
                    _sound = _soundManager.PlaySound(SimpleSoundService.SoundType.Rotating);
                }
            }
        }