private void nextWave() { // Increment the Wave property, clear the private _invaders collection, and then // create all of the Invader objects, giving each of them a Location field with // the correct coordinates. Try spacing them out so that they’re spaced 1.4 invader // lengths apart horizontally, and 1.4 invader heights vertically. Wave++; _invaders.Clear(); if (_mothership != null) { _mothership.Escape(); OnShipChanged(_mothership, false); _mothership = null; } RemoveAllShots(); _invaderDirection = Direction.Left; InvaderType invaderType; for (int row = 0; row < 6; row++) { switch (row) { case 0: invaderType = InvaderType.Spaceship; break; case 1: invaderType = InvaderType.Bug; break; case 2: invaderType = InvaderType.Saucer; break; case 3: invaderType = InvaderType.Satellite; break; default: invaderType = InvaderType.Star; break; } for (int column = 0; column < 11; column++) { Point location = new Point(column * Invader.InvaderSize.Width * 1.4, row * Invader.InvaderSize.Height * 1.4); _invaders.Add(new Invader(invaderType, location, _invaderScores[invaderType])); OnShipChanged(_invaders[_invaders.Count - 1], false); } } }
private void CheckForMothershipCollisions() { // We check to see if the mothership is null because it could have escaped off the edge // of the screen if (_mothership == null) return; List<Shot> playerShots = _playerShots.ToList(); foreach (Shot shot in playerShots) { Rect shotRect = new Rect(shot.Location.X, shot.Location.Y, Shot.ShotSize.Width, Shot.ShotSize.Height); if (RectsOverlap(_mothership.Area, shotRect)) { Mothership deadMothership = _mothership; _mothership = null; _playerShots.Remove(shot); OnShotMoved(shot, true); OnShipChanged(deadMothership, true); Score += deadMothership.Score; // An extra life is awarded for destroying a mothership if (Lives + 1 <= MaximumLives) Lives++; break; } } }
private void MoveInvaders() { TimeSpan timeSinceLastMove = DateTime.Now - _lastUpdated; double msBetweenMoves = Math.Max(7 - Wave, 1) * (2 * _invaders.Count()); if (timeSinceLastMove >= TimeSpan.FromMilliseconds(msBetweenMoves)) { _lastUpdated = DateTime.Now; if (_invaderDirection == Direction.Right) { var invadersTouchingRightBoundary = from invader in _invaders where invader.Area.Right > PlayAreaSize.Width - Invader.HorizontalPixelsPerMove * 2 select invader; if (invadersTouchingRightBoundary.Count() > 0) { _invaderDirection = Direction.Down; foreach (Invader invader in _invaders) { invader.Move(_invaderDirection); OnShipChanged(invader, false); } _justMovedDown = true; _invaderDirection = Direction.Left; } else { _justMovedDown = false; foreach (Invader invader in _invaders) { invader.Move(_invaderDirection); OnShipChanged(invader, false); } } } else if (_invaderDirection == Direction.Left) { var invadersTouchingLeftBoundary = from invader in _invaders where invader.Area.Left < Invader.HorizontalPixelsPerMove * 2 select invader; if (invadersTouchingLeftBoundary.Count() > 0) { _invaderDirection = Direction.Down; foreach (Invader invader in _invaders) { invader.Move(_invaderDirection); OnShipChanged(invader, false); } _justMovedDown = true; _invaderDirection = Direction.Right; } else { _justMovedDown = false; foreach (Invader invader in _invaders) { invader.Move(_invaderDirection); OnShipChanged(invader, false); } } } } TimeSpan timeSinceLastMothershipMove = DateTime.Now - _lastMothershipUpdate; double msBetweenMothershipMoves = Math.Max(7 - Wave, 1) * (_invaders.Count() / 2); if (_mothership != null && timeSinceLastMothershipMove > TimeSpan.FromMilliseconds(msBetweenMothershipMoves)) { _lastMothershipUpdate = DateTime.Now; if (_mothership.Area.Right > PlayAreaSize.Width - Mothership.HorizontalPixelsPerMove * 2) { _mothership.Escape(); OnShipChanged(_mothership, false); _mothership = null; } else { _mothership.Move(Direction.Right); OnShipChanged(_mothership, false); } } }
private void AddMothership() { _mothership = new Mothership(new Point(0, 0), _invaderScores[InvaderType.Mothership]); OnShipChanged(_mothership, false); }