Exemplo n.º 1
0
        private void ShipChangedHandler(object sender, ShipChangedEventArgs e)
        {
            if (!e.Killed) // if ship is not killed
            {
                if (e.ShipUpdated is Invader)
                {
                    Invader       invader = e.ShipUpdated as Invader;
                    AnimatedImage invaderControl;

                    // set up preliminary data to create or alter invader sprites
                    List <string> invaderNames = new List <string>();
                    for (int i = 1; i < 5; i++)
                    {
                        string invaderName = "Assets/" + invader._invaderType.ToString() + i + ".png";
                        invaderNames.Add(invaderName);
                    }

                    if (!_invaders.ContainsKey(e.ShipUpdated as Invader))
                    {
                        invaderControl = InvadersHelper.InvaderControlFactory(invader, invaderNames, Scale) as AnimatedImage;
                        //_invaders.Add(invader, invaderControl);
                        _invaders[invader] = invaderControl;
                        _sprites.Add(invaderControl);
                    }
                    else
                    {
                        invaderControl = _invaders[e.ShipUpdated as Invader];
                        InvadersHelper.MoveElementOnCanvas(invaderControl, e.X * Scale, e.Y * Scale);
                        InvadersHelper.ResizeElement(invaderControl, invader.Size.Width * Scale, invader.Size.Height * Scale);
                    }
                }
                else // if ship is Player
                {
                    Player player = e.ShipUpdated as Player;

                    if (_playerFlashing)
                    {
                        playerControl.StopFlashing();
                        _playerFlashing = false;
                    }

                    if (playerControl == null)
                    {
                        List <string> playerImage = new List <string>();
                        playerImage.Add("Assets/player.png");
                        playerControl = InvadersHelper.PlayerControlFactory(player, playerImage, Scale) as AnimatedImage;
                        _sprites.Add(playerControl);
                    }
                    else
                    {
                        InvadersHelper.MoveElementOnCanvas(playerControl, e.X * Scale, e.Y * Scale);
                        InvadersHelper.ResizeElement(playerControl, player.Size.Width * Scale, player.Size.Height * Scale);
                    }
                }
            }
            else // if Ship is killed
            {
                if (e.ShipUpdated is Invader) // if ship is Invader
                {
                    AnimatedImage invaderControl;
                    Invader       invader = e.ShipUpdated as Invader;

                    if (_invaders.ContainsKey(invader as Invader))
                    {
                        invaderControl = _invaders[e.ShipUpdated as Invader];
                        _invaders.Remove(invader as Invader);
                        _shotInvaders.Add(invaderControl, DateTime.Now);
                        invaderControl.FadeOut();
                        //// not sure where to put this resize element method
                        //InvadersHelper.ResizeElement(invaderControl, invader.Size.Width * Scale, invader.Size.Height * Scale,
                        //    invader.Location.X * Scale, invader.Location.Y * Scale, Scale);
                    }
                }
                else // if ship is Player
                {
                    playerControl.StartFlashing();
                    _playerFlashing = true;
                    //// not sure where to put this resize element method
                    //InvadersHelper.ResizeElement(playerControl, e.ShipUpdated.Size.Width * Scale,
                    //    e.ShipUpdated.Size.Height * Scale, e.ShipUpdated.Location.X * Scale, e.ShipUpdated.Location.Y * Scale, Scale);
                }
            }
        }
Exemplo n.º 2
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;
                }
            }
        }
        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.InvaderFactory((int)invader.InvaderType,
                                                                                        invader.Size.Width,
                                                                                        invader.Size.Height,
                                                                                        Scale,
                                                                                        _timer.Interval);
                        _sprites.Add(invaderControl);
                        _invaders.Add(invader, invaderControl);
                        InvadersHelper.SetCanvasLocation(invaderControl,
                                                         invader.Location.X,
                                                         invader.Location.Y,
                                                         Scale);
                    }
                    else
                    {
                        FrameworkElement invaderControl = _invaders[invader];
                        InvadersHelper.MoveElementOnCanvas(invaderControl,
                                                           invader.Location.X,
                                                           invader.Location.Y,
                                                           Scale);
                        InvadersHelper.ResizeElement((AnimatedImage)invaderControl,
                                                     invader.Size.Width,
                                                     invader.Size.Height,
                                                     Scale);
                    }
                }
                else if (e.ShipUpdated is Player)
                {
                    Player player = e.ShipUpdated as Player;

                    if (_playerFlashing && !_model.PlayerDying)
                    {
                        _playerFlashing = false;
                        AnimatedImage playerControl = (AnimatedImage)_playerControl;
                        playerControl.StopFlashing();
                    }

                    if (_playerControl == null)
                    {
                        _playerControl = InvadersHelper.PlayerFactory(player.Size.Width,
                                                                      player.Size.Height,
                                                                      Scale,
                                                                      _timer.Interval);
                        _sprites.Add(_playerControl);
                        InvadersHelper.SetCanvasLocation(_playerControl,
                                                         player.Location.X,
                                                         player.Location.Y,
                                                         Scale);
                    }
                    else
                    {
                        InvadersHelper.MoveElementOnCanvas(_playerControl,
                                                           player.Location.X,
                                                           player.Location.Y,
                                                           Scale);
                        InvadersHelper.ResizeElement((AnimatedImage)_playerControl,
                                                     player.Size.Width,
                                                     player.Size.Height,
                                                     Scale);
                    }
                }
            }
            else
            {
                // Invader killed
                if (e.ShipUpdated is Invader)
                {
                    Invader invader = e.ShipUpdated as Invader;

                    if (_invaders.ContainsKey(invader))
                    {
                        AnimatedImage invaderControl = (AnimatedImage)_invaders[invader];

                        invaderControl.InvaderShot(_timeLimitInvadersFadeoutSeconds);
                        _shotInvaders.Add(invaderControl, DateTime.Now);
                        _invaders.Remove(invader);
                    }
                }
                // Player killed
                else if (e.ShipUpdated is Player)
                {
                    Player        player        = e.ShipUpdated as Player;
                    AnimatedImage playerControl = (AnimatedImage)_playerControl;
                    playerControl.StartFlashing();
                    _playerFlashing = true;
                }
            }
        }