void timer_Tick(object sender, object e)
        {
            _model.Twinkle();
            if (!Paused)
            {
                _model.Update();
                foreach (FrameworkElement starControl in _fadedStars.Keys.ToList())
                {
                    TimeSpan timeSinceRemoved = DateTime.Now - _fadedStars[starControl];
                    if (timeSinceRemoved > TimeSpan.FromMilliseconds(1000))
                    {
                        _fadedStars.Remove(starControl);
                        _sprites.Remove(starControl);
                    }
                }

                foreach (FrameworkElement control in _shotInvaders.Keys.ToList())
                {
                    TimeSpan timeSinceLastShot = DateTime.Now - _shotInvaders[control];
                    if (timeSinceLastShot > TimeSpan.FromMilliseconds(2000))
                    {
                        _sprites.Remove(control);
                        _shotInvaders.Remove(control);
                    }
                }
                OnPropertyChanged("GameOver");
                OnPropertyChanged("Score");
                // No longer necessary:
                // OnPropertyChanged("Lives");
            }
        }
        void TimerTickEventHandler(object sender, object e)
        {
            if (_lastPaused != Paused)
            {
                OnPropertyChanged("Paused");
                _lastPaused = Paused;
            }
            if (!Paused)
            {
                if (_leftAction.HasValue && _rightAction.HasValue)
                {
                    _model.MovePlayer(_leftAction > _rightAction ? Direction.Left : Direction.Right);
                }
                else if (_leftAction.HasValue)
                {
                    _model.MovePlayer(Direction.Left);
                }
                else if (_rightAction.HasValue)
                {
                    _model.MovePlayer(Direction.Right);
                }
            }

            _model.Update(Paused);

            if (Score != _model.Score)
            {
                Score = _model.Score;
                OnPropertyChanged("Score");
            }

            if (_model.Lives >= 0)
            {
                while (_lives.Count > _model.Lives)
                {
                    _lives.RemoveAt(0);
                }
                while (_lives.Count < _model.Lives)
                {
                    _lives.Add(new object());
                }
            }

            foreach (FrameworkElement control in _shotInvaders.Keys.ToList())
            {
                DateTime elapsed = _shotInvaders[control];
                if (DateTime.Now - elapsed > TimeSpan.FromSeconds(.5))
                {
                    _sprites.Remove(control);
                    _shotInvaders.Remove(control);
                }
            }

            if (_model.GameOver)
            {
                OnPropertyChanged("GameOver");
                _timer.Stop();
            }
        }
        private void TimerTickEventhandler(object sender, object e)
        {
            if (_lastPaused != Paused)
            {
                _lastPaused = Paused;
                OnPropertyChanged("Paused");
            }
            if (!Paused)
            {
                if (_leftAction.HasValue && _righrAction.HasValue)                           // jesli jednoczesnie zrobil 2 akcje wybierz pozniejsze zdarzenie
                {
                    if (DateTime.Compare((DateTime)_leftAction, (DateTime)_righrAction) > 0) //jesli akcja 'w prawo' byla wczesniej idz w lewo
                    {
                        _model.MovePlayer(Direction.Left);
                    }
                    else // jesli pozniej w prawo
                    {
                        _model.MovePlayer(Direction.Right);
                    }
                }
                if (_leftAction.HasValue)//ruch w lewo
                {
                    _model.MovePlayer(Direction.Left);
                }
                if (_righrAction.HasValue)//ruch w prawo
                {
                    _model.MovePlayer(Direction.Right);
                }
            }

            _model.Update(Paused);

            if (Score != _model.Score)//sprawdzamy score
            {
                Score = _model.Score;
                OnPropertyChanged("Score");
            }

            if (_model.Lives >= 0)// aktualizacja _lives gracza
            {
                while (_model.Lives > _lives.Count)
                {
                    _lives.Add(new object());
                }
                while (_model.Lives < _lives.Count)
                {
                    _lives.RemoveAt(0);
                }
            }

            foreach (FrameworkElement control in _shotInvaders.Keys.ToList())//jesli invader zostal zniszczony, to po animacji niszczenia(0.5s) jest usuwany po 0.5s
            {
                if (DateTime.Now - _shotInvaders[control] > TimeSpan.FromSeconds(0.5))
                {
                    _sprites.Remove(control);
                    _shotInvaders.Remove(control);
                }
            }

            if (_model.GameOver)//jesli gra sie skonczyla zatrzymujemy stoper i zglaszamy GameOver w viewModel
            {
                _timer.Stop();
                OnPropertyChanged("GameOver");
            }
        }
Example #4
0
        private void TimerTickEventHandler(object sender, EventArgs e)
        {
            if (_lastPaused != Paused)
            {
                OnPropertyChanged("Paused");
                _lastPaused = Paused;
            }

            if (!Paused)
            {
                if (_leftAction.HasValue && _rightAction.HasValue)
                {
                    if (_leftAction.Value > _rightAction.Value)
                    {
                        _model.MovePlayer(Direction.Left);
                    }
                    else
                    {
                        _model.MovePlayer(Direction.Right);
                    }
                }
                else
                {
                    if (_leftAction.HasValue)
                    {
                        _model.MovePlayer(Direction.Left);
                    }
                    if (_rightAction.HasValue)
                    {
                        _model.MovePlayer(Direction.Right);
                    }
                }
            }

            _model.Update();

            if (Score != _model.Score)
            {
                Score = _model.Score;
                OnPropertyChanged("Score");
            }

            if (Wave != _model.Wave)
            {
                Wave = _model.Wave;
                OnPropertyChanged("Wave");
            }

            if (_model.Lives != _lives.Count)
            {
                int difference = _model.Lives - _lives.Count;

                if (difference > 0)
                {
                    for (int i = 0; i < difference; i++)
                    {
                        _lives.Add(new object());
                    }
                }

                else
                {
                    for (int i = 0; i < Math.Abs(difference); i++)
                    {
                        _lives.RemoveAt(0);
                    }
                }
            }

            foreach (FrameworkElement control in _shotInvaders.Keys.ToList())
            {
                if (DateTime.Now - _shotInvaders[control] > TimeSpan.FromSeconds(0.5))
                {
                    _sprites.Remove(control);
                    _shotInvaders.Remove(control);
                }
            }

            if (GameOver)
            {
                _lives.Clear();
                OnPropertyChanged("GameOver");
                _timer.Stop();
            }
        }
        private void TimerTickEventHandler(object sender, EventArgs e)
        {
            if (_lastPaused != Paused)
            {
                OnPropertyChanged(nameof(Paused));
                _lastPaused = Paused;
            }

            if (!Paused)
            {
                if (_leftAction.HasValue && !_rightAction.HasValue)
                {
                    _model.MovePlayer(Direction.Left);
                }
                else if (!_leftAction.HasValue && _rightAction.HasValue)
                {
                    _model.MovePlayer(Direction.Right);
                }
                else if (_leftAction.HasValue && _rightAction.HasValue)
                {
                    _model.MovePlayer(_leftAction > _rightAction ? Direction.Left : Direction.Right);
                }
            }

            _model.Update(Paused);

            if (Score != _model.Score)
            {
                Score = _model.Score;
                OnPropertyChanged(nameof(Score));
            }

            if (_model.Lives > 0)
            {
                while (_lives.Count > _model.Lives)
                {
                    _lives.RemoveAt(0);
                }
                while (_lives.Count < _model.Lives)
                {
                    _lives.Add(new object());
                }
                OnPropertyChanged(nameof(LivesValue));
            }

            foreach (var control in _shotInvaders.Keys.ToList())
            {
                var elapsed = _shotInvaders[control];
                if (DateTime.Now - elapsed > TimeSpan.FromSeconds(0.5))
                {
                    _sprites.Remove(control);
                    _shotInvaders.Remove(control);
                }
            }

            if (_model.GameOver)
            {
                //Game over!
                OnPropertyChanged(nameof(GameOver));
                _timer.Stop();
            }
        }
        private void TimerTickEventHandler(object sender, object e)
        {
            if (_lastPaused != Paused)
            {
                _lastPaused = Paused;
                OnPropertyChanged("Paused");
            }
            if (!Paused)
            // Move player
            {
                if (_leftAction != null && _rightAction != null)
                {
                    if (_leftAction > _rightAction)
                    {
                        if (_fireAction != null && _fireAction > _leftAction)
                        {
                            _model.FireShot();
                        }
                        else
                        {
                            _model.MovePlayer(Direction.Left);
                        }
                    }
                    else
                    {
                        if (_fireAction != null && _fireAction > _leftAction)
                        {
                            _model.FireShot();
                        }
                        else
                        {
                            _model.MovePlayer(Direction.Right);
                        }
                    }
                }
                else if (_leftAction != null)
                {
                    _model.MovePlayer(Direction.Left);
                }
                else if (_rightAction != null)
                {
                    _model.MovePlayer(Direction.Right);
                }
                else if (_fireAction != null)
                {
                    _model.FireShot();
                }
            }

            _model.Update(Paused);


            // Update score
            if (Score != _model.Score)
            {
                Score = _model.Score;
                OnPropertyChanged("Score");
            }

            // Update lives
            if (_lives.Count > _model.Lives)
            {
                _lives.RemoveAt(_lives.Count - 1);
                if (_lives.Count == 0)
                {
                    EndGame();
                }
            }
            else if (_lives.Count < _model.Lives)
            {
                _lives.Add(new object());
            }
            OnPropertyChanged("Lives");

            // Remove dead invaders who have finished fading out
            foreach (FrameworkElement control in _shotInvaders.Keys.ToList())
            {
                DateTime invaderDiedTime      = _shotInvaders[control];
                TimeSpan timeSinceInvaderDied = DateTime.Now - invaderDiedTime;
                if (timeSinceInvaderDied > _timeLimitInvadersFadeoutSeconds)
                {
                    _sprites.Remove(control);
                    _shotInvaders.Remove(control);
                }
            }

            if (GameOver)
            {
                OnPropertyChanged("GameOver");
                _timer.Stop();
            }
        }