Пример #1
0
 private void ModelShipChangedEventHandler(object sender, ShipChangedEventArgs e)
 {
     if (!e.Killed)
     {
         if (e.ShipUpdated is Invader)
         {
             Invader invader = e.ShipUpdated as Invader;
             if (!_invaders.ContainsKey(invader))
             {
                 FrameworkElement invaderControl = InvadersHelper.InvaderControlFactory(invader, Scale);
                 _invaders[invader] = invaderControl;
                 _sprites.Add(invaderControl);
             }
             else
             {
                 FrameworkElement invaderControl = _invaders[invader];
                 InvadersHelper.MoveElement(invaderControl, invader.Location.X, Scale, invader.Location.Y * Scale);
                 InvadersHelper.ResizeElement(invaderControl, invader.Size.Width * Scale, invader.Size.Height * Scale);
             }
         }
         else if (e.ShipUpdated is Player)
         {
             if (_playerFlashing)
             {
                 AnimatedImage playerImage = _playControl as AnimatedImage;
                 _playerFlashing = false;
             }
             if (_playControl == null)
             {
                 FrameworkElement playerControl = InvadersHelper.PlayerControlFactory(player, Scale);
                 _sprites.Add(playerControl);
             }
         }
     }
 }
Пример #2
0
        private void ModelShotMovedEventHandler(object sender, Model.Event_Arguments.ShotMovedEventArgs e)
        {
            if (!e.Disappeared)
            {
                if (!_shots.ContainsKey(e.Shot))
                {
                    Rectangle shot = InvadersHelper.ShotFactory(new Rect(e.Shot.Location, e.Shot.ShotSize), e.PlayerShot);
                    _shots.Add(e.Shot, shot);
                    _sprites.Add(shot);
                    InvadersHelper.ResizeElement(shot, shot.Width * Scale, shot.Height * Scale);
                    InvadersHelper.RepositionElement(shot, e.Shot.Location.X * Scale, e.Shot.Location.Y * Scale);
                }
                else
                {
                    InvadersHelper.ResizeElement(_shots[e.Shot], e.Shot.ShotSize.Width * Scale, e.Shot.ShotSize.Height * Scale);
                    InvadersHelper.RepositionElement(_shots[e.Shot], e.Shot.Location.X * Scale, e.Shot.Location.Y * Scale);
                }
            }

            else
            {
                if (_shots.ContainsKey(e.Shot))
                {
                    _sprites.Remove(_shots[e.Shot]);
                    _shots.Remove(e.Shot);
                }
            }
        }
 private void CreatePlayer(Player player)
 {
     if (_playerControl == null)
     {
         _playerControl = InvadersHelper.PlayerControlFactory(player, Scale);
         _sprites.Add(_playerControl);
     }
     else
     {
         InvadersHelper.MoveElementOnCanvas(_playerControl, player.Location.X * Scale, player.Location.Y * Scale);
         InvadersHelper.ResizeElement(_playerControl, player.Size.Width * Scale, player.Size.Height * Scale);
     }
 }
 private void CreateOrMoveInvader(Invader invader)
 {
     if (!_invaders.ContainsKey(invader))
     {
         FrameworkElement invaderControl = InvadersHelper.InvaderControlFactory(invader, Scale);
         _invaders[invader] = invaderControl;
         _sprites.Add(invaderControl);
     }
     else
     {
         FrameworkElement invaderControl = _invaders[invader];
         InvadersHelper.MoveElementOnCanvas(invaderControl, invader.Location.X * Scale, invader.Location.Y * Scale);
         InvadersHelper.ResizeElement(invaderControl, invader.Size.Width * Scale, invader.Size.Height * Scale);
     }
 }
Пример #5
0
 private void RecreateScanLines()
 {
     foreach (FrameworkElement scanLine in _scanLines)
     {
         if (_sprites.Contains(scanLine))
         {
             _sprites.Remove(scanLine);
         }
     }
     _scanLines.Clear();
     for (int y = 0; y < 300; y += 4)
     {
         FrameworkElement scanLine = InvadersHelper.ScanLineFactory();
         _scanLines.Add(scanLine);
         _sprites.Add(scanLine);
         InvadersHelper.ResizeElement(scanLine, 400 * Scale, 1 * Scale);
         InvadersHelper.RepositionElement(scanLine, 0, y * Scale);
     }
 }
Пример #6
0
        void ModelShipChangedEventHandler(object sender, ShipChangedEventArgs e)
        {
            if (!e.Killed)
            {
                if (e.ShipUpdated is Invader)
                {
                    Invader invader = e.ShipUpdated as Invader;
                    if (!_invaders.ContainsKey(invader))
                    {
                        FrameworkElement invaderControl = InvadersHelper.InvaderControlFactory(invader, Scale);
                        _invaders[invader] = invaderControl;
                        _sprites.Add(invaderControl);
                    }
                    else
                    {
                        FrameworkElement invaderControl = _invaders[invader];
                        InvadersHelper.MoveElementOnCanvas(invaderControl, invader.Location.X * Scale,
                                                           invader.Location.Y * Scale);
                        InvadersHelper.ResizeElement(invaderControl, invader.Size.Width * Scale,
                                                     invader.Size.Height * Scale);
                    }
                }
                else if (e.ShipUpdated is Player)
                {
                    if (_playerFlashing)
                    {
                        _playerFlashing = false;
                        AnimatedImage control = _playerControl as AnimatedImage;
                        if (control != null)
                        {
                            control.StopFlashing();
                        }
                    }

                    Player player = e.ShipUpdated as Player;
                    if (_playerControl == null)
                    {
                        _playerControl = InvadersHelper.PlayerControlFactory(player, Scale);
                        _sprites.Add(_playerControl);
                    }
                    else
                    {
                        InvadersHelper.MoveElementOnCanvas(_playerControl, player.Location.X * Scale,
                                                           player.Location.Y * Scale);
                        InvadersHelper.ResizeElement(_playerControl, player.Size.Width * Scale,
                                                     player.Size.Height * Scale);
                    }
                }
            }
            else
            {
                if (e.ShipUpdated is Invader)
                {
                    Invader invader = e.ShipUpdated as Invader;
                    if (!_invaders.ContainsKey(invader))
                    {
                        return;
                    }
                    AnimatedImage invaderControl = _invaders[invader] as AnimatedImage;
                    if (invaderControl != null)
                    {
                        invaderControl.InvaderShot();
                        _shotInvaders[invaderControl] = DateTime.Now;
                        _invaders.Remove(invader);
                    }
                }
                else if (e.ShipUpdated is Player)
                {
                    AnimatedImage control = _playerControl as AnimatedImage;
                    if (control != null)
                    {
                        control.StartFlashing();
                    }
                    _playerFlashing = true;
                }
            }
        }
Пример #7
0
        private void ModelShipChangedEventHandler(object sender, Model.Event_Arguments.ShipChangedEventArgs e)
        {
            if (!e.Killed)
            {
                if (e.ShipUdated is Invader)
                {
                    Invader invader = e.ShipUdated as Invader;

                    if (!_invaders.ContainsKey(invader))
                    {
                        AnimatedImage animatedImage = InvadersHelper.InvadersControlFactory(invader.InvaderType, invaderAnimationInterval);
                        _invaders.Add(invader, animatedImage);
                        _sprites.Add(animatedImage);
                        InvadersHelper.RepositionElement(_invaders[invader], invader.Location.X * Scale, invader.Location.Y * Scale);
                        InvadersHelper.ResizeElement(animatedImage, invader.Size.Width * Scale, invader.Size.Height * Scale);
                    }
                    else
                    {
                        InvadersHelper.RepositionElement(_invaders[invader], invader.Location.X * Scale, invader.Location.Y * Scale);
                        InvadersHelper.ResizeElement(_invaders[invader], invader.Size.Width * Scale, invader.Size.Height * Scale);
                    }
                }

                else if (e.ShipUdated is Player)
                {
                    Player player = e.ShipUdated as Player;
                    if (_playerFlashing == true)
                    {
                        _playerControl.StopFlashing();
                        _playerFlashing = false;
                    }
                    if (_playerControl == null)
                    {
                        _playerControl = InvadersHelper.PlayerControlFactory(invaderAnimationInterval);
                        _sprites.Add(_playerControl);
                        InvadersHelper.RepositionElement(_playerControl, player.Location.X * Scale, player.Location.Y * Scale);
                        InvadersHelper.ResizeElement(_playerControl, player.Size.Width * Scale, player.Size.Height * Scale);
                    }
                    else
                    {
                        InvadersHelper.RepositionElement(_playerControl, player.Location.X * Scale, player.Location.Y * Scale);
                        InvadersHelper.ResizeElement(_playerControl, player.Size.Width * Scale, player.Size.Height * Scale);
                    }
                }
            }

            else
            {
                if (e.ShipUdated is Invader)
                {
                    if (e.ShipUdated != null)
                    {
                        if (_invaders.ContainsKey(e.ShipUdated as Invader))
                        {
                            (_invaders[e.ShipUdated as Invader] as AnimatedImage).InvaderShot();
                            _shotInvaders.Add(_invaders[e.ShipUdated as Invader], DateTime.Now);
                            _invaders.Remove(e.ShipUdated as Invader);
                        }
                    }
                }

                else if (e.ShipUdated is Player)
                {
                    _playerControl.StartFlashing();
                    _playerFlashing = true;
                }
            }
        }
Пример #8
0
        private void ModelShipChangedEventHandler(object sender, ShipChangedEventArgs e)
        {
            if (!e.Killed)
            {
                switch (e.ShipUpdated)
                {
                case Invader _:
                    var invader = e.ShipUpdated as Invader;
                    if (!_invaders.ContainsKey(invader))
                    {
                        var fe = InvadersHelper.InvaderControlFactory(invader, Scale);
                        _invaders.Add(invader, fe);
                        _sprites.Add(fe);
                    }
                    else
                    {
                        var fe = _invaders[invader];
                        InvadersHelper.MoveElementOnCanvas(fe,
                                                           invader.Location.X * Scale,
                                                           invader.Location.Y * Scale);
                        InvadersHelper.ResizeElement(fe,
                                                     invader.Size.Width * Scale,
                                                     invader.Size.Height * Scale);
                    }

                    break;

                case Player _:
                    if (_playerFlashing)
                    {
                        var playerControl = _playerControl as AnimatedImage;
                        playerControl?.StopFlashing();
                        _playerFlashing = false;
                    }

                    var player = e.ShipUpdated as Player;
                    if (_playerControl == null)
                    {
                        _playerControl = InvadersHelper.PlayerControlFactory(player, Scale);
                        _sprites.Add(_playerControl);
                    }
                    else
                    {
                        InvadersHelper.MoveElementOnCanvas(_playerControl,
                                                           player.Location.X * Scale,
                                                           player.Location.Y * Scale);
                        InvadersHelper.ResizeElement(_playerControl,
                                                     player.Size.Width * Scale,
                                                     player.Size.Height * Scale);
                    }

                    break;
                }
            }
            else
            {
                switch (e.ShipUpdated)
                {
                case Invader _:
                    var invader = e.ShipUpdated as Invader;
                    if (!_invaders.ContainsKey(invader))
                    {
                        return;
                    }
                    if (_invaders[invader] is AnimatedImage invaderControl)
                    {
                        invaderControl.InvaderShot();
                        _shotInvaders[invaderControl] = DateTime.Now;
                        _invaders.Remove(invader);
                    }

                    break;

                case Player _:
                    var playerControl = (AnimatedImage)_playerControl;
                    playerControl.StartFlashing();
                    _playerFlashing = true;
                    OnPlayerShot();
                    break;
                }
            }
        }