void ModelShotMovedEventHandler(object sender, ShotMovedEventArgs e) { if (!e.Disappeared) { if (!_shots.ContainsKey(e.Shot)) { FrameworkElement shotControl = InvadersHelper.ShotControlFactory(e.Shot, Scale); _shots[e.Shot] = shotControl; _sprites.Add(shotControl); } else { FrameworkElement shotControl = _shots[e.Shot]; InvadersHelper.MoveElementOnCanvas(shotControl, e.Shot.Location.X * Scale, e.Shot.Location.Y * Scale); } } else { if (_shots.ContainsKey(e.Shot)) { FrameworkElement shotControl = _shots[e.Shot]; _sprites.Remove(shotControl); _shots.Remove(e.Shot); } } }
private void ModelStarChangedEventHandler(object sender, StarChangedEventArgs e) { if (e.Disappeared && _stars.ContainsKey(e.Point)) { FrameworkElement control = _stars[e.Point]; _sprites.Remove(control); } else { if (!_stars.ContainsKey(e.Point)) { FrameworkElement starControl = InvadersHelper.StarControlFactory(Scale); InvadersHelper.SendToBack(starControl); _stars.Add(e.Point, starControl); _sprites.Add(starControl); InvadersHelper.SetCanvasLocation(starControl, e.Point.X, e.Point.Y, Scale); } else { // Create a shooting star FrameworkElement starControl = _stars[e.Point]; InvadersHelper.MoveElementOnCanvas(starControl, InvadersModel.PlayAreaSize.Width, InvadersModel.PlayAreaSize.Height, Scale); } } }
void ModelStarChangedEventHandler(object sender, StarChangedEventArgs e) { if (e.Disappeared && _stars.ContainsKey(e.Point)) { FrameworkElement control = _stars[e.Point]; _sprites.Remove(control); } else { if (!_stars.ContainsKey(e.Point)) { FrameworkElement control = InvadersHelper.StarControlFactory(e.Point, Scale); _stars.Add(e.Point, control); _sprites.Add(control); } else { //Stars typically won't change locations, so this else clause probably won't //get hit—but you can use it to add shooting stars if you //want.Look up the star's control in _stars and use a //helper method to move it to the new location. //Random random = new Random(); } } }
private void ModelShotMovedEventHandler(object sender, ShotMovedEventArgs e) { if (!e.Disappeared) { if (!_shots.ContainsKey(e.Shot)) { FrameworkElement shotControl = InvadersHelper.ShotFactory(Shot.ShotSize.Width, Shot.ShotSize.Height, Scale, _timer.Interval); InvadersHelper.SetCanvasLocation(shotControl, e.Shot.Location.X, e.Shot.Location.Y, Scale); _shots.Add(e.Shot, shotControl); _sprites.Add(shotControl); } else { FrameworkElement shotControl = _shots[e.Shot]; InvadersHelper.MoveElementOnCanvas(shotControl, e.Shot.Location.X, e.Shot.Location.Y, Scale); } } else if (_shots.ContainsKey(e.Shot)) { FrameworkElement shotControl = _shots[e.Shot]; _sprites.Remove(shotControl); } }
public void PopulateSprites() { RecreateScanLines(); foreach (Point star in _model._stars) { StarControl newStar = InvadersHelper.StarControlFactory(star, Scale) as StarControl; _starDictionary.Add(star, newStar); // Obsolete: // InvadersHelper.SetCanvasLocation(newStar, star.X, star.Y, Scale); _sprites.Add(newStar); } }
private void RecreateScanLines() { foreach (FrameworkElement scanLine in _scanLines) { if (_sprites.Contains(scanLine)) { _sprites.Remove(scanLine); } } _scanLines.Clear(); for (int y = 0; y < 300; y += 2) { FrameworkElement scanLine = InvadersHelper.ScanLineFactory(y, 400, Scale); _scanLines.Add(scanLine); _sprites.Add(scanLine); } }
private void RecreateScanLines() { foreach (FrameworkElement scanLine in _scanLines) { if (_sprites.Contains(scanLine)) { _sprites.Remove(scanLine); } } _scanLines.Clear(); // should make all rescaling values relative values, not set numbers for (int y = 0; y < 300; y += 2) { FrameworkElement scanLine = InvadersHelper.ScanLineFactory(y, 400, Scale); _scanLines.Add(scanLine); _sprites.Add(scanLine); } }
void ModelStarChangedEventHandler(object sender, StarChangedEventArgs e) { if (e.Disappeared && _stars.ContainsKey(e.Point)) { FrameworkElement starControl = _stars[e.Point]; _sprites.Remove(starControl); } else { if (!_stars.ContainsKey(e.Point)) { FrameworkElement starControl = InvadersHelper.StarControlFactory(e.Point, Scale); _stars[e.Point] = starControl; _sprites.Add(starControl); } else { FrameworkElement starControl = _stars[e.Point]; InvadersHelper.SetCanvasLocation(starControl, e.Point.X * Scale, e.Point.Y * Scale); } } }
private void StarChangedHandler(object sender, StarChangedEventArgs e) { Point star = e.StarThatChanged; if (e.Removed) { if (_starDictionary.ContainsKey(e.StarThatChanged)) { StarControl starControl = _starDictionary[e.StarThatChanged]; _starDictionary.Remove(e.StarThatChanged); _fadedStars.Add(starControl, DateTime.Now); starControl.FadeOut(); //// not sure where to put this resize element method //InvadersHelper.ResizeElement(starControl, e.StarThatChanged.X * Scale, e.StarThatChanged.Y * Scale, // e.StarThatChanged.X * Scale, e.StarThatChanged.Y * Scale, Scale); } } else { StarControl newStar; if (_starDictionary.ContainsKey(e.StarThatChanged)) { newStar = _starDictionary[e.StarThatChanged]; } else { newStar = InvadersHelper.StarControlFactory(star, Scale) as StarControl; _starDictionary[e.StarThatChanged] = newStar; newStar.FadeIn(); _sprites.Add(newStar); } //InvadersHelper.SetCanvasLocation(newStar, e.StarThatChanged.X, e.StarThatChanged.Y, Scale); //// not sure where to put this resize element method //InvadersHelper.ResizeElement(newStar, e.StarThatChanged.X * Scale, e.StarThatChanged.Y * Scale, // e.StarThatChanged.X * Scale, e.StarThatChanged.Y * Scale, Scale); } }
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 ShotMovedHandler(object sender, ShotMovedEventArgs e) { Shot shot = e.Shot; if (e.Removed) { AnimatedImage shotControl; if (_shots.ContainsKey(e.Shot)) { shotControl = _shots[e.Shot]; _sprites.Remove(shotControl); _shots.Remove(e.Shot); } } else { AnimatedImage shotControl; if (!e.IsBomb) { List <string> shotImages = new List <string>() { "Assets/shot1.png", "Assets/shot2.png", "Assets/shot3.png", "Assets/shot4.png", }; if (!_shots.ContainsKey(e.Shot)) { shotControl = InvadersHelper.ShotControlFactory(shot, shotImages, Scale) as AnimatedImage; _shots[e.Shot] = shotControl; //InvadersHelper.ResizeElement(shotControl, Shot.ShotSize.Width * Scale, Shot.ShotSize.Height * Scale); _sprites.Add(shotControl); } else { shotControl = _shots[e.Shot]; //InvadersHelper.ResizeElement(shotControl, Shot.ShotSize.Width * Scale, Shot.ShotSize.Height * Scale); InvadersHelper.MoveElementOnCanvas(shotControl, e.Shot.Location.X * Scale, e.Shot.Location.Y * Scale); } } else { List <string> bombImages = new List <string>() { "Assets/bomb1.png", "Assets/bomb2.png", "Assets/bomb3.png", "Assets/bomb4.png", }; if (!_shots.ContainsKey(e.Shot)) { shotControl = InvadersHelper.ShotControlFactory(shot, bombImages, Scale) as AnimatedImage; _shots[e.Shot] = shotControl; _sprites.Add(shotControl); } else { shotControl = _shots[e.Shot]; InvadersHelper.MoveElementOnCanvas(shotControl, e.Shot.Location.X * Scale, e.Shot.Location.Y * Scale); } } } }
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); } } }
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; } } }