public void Move(DirectionMoving direction) { if (direction == DirectionMoving.Left) StepAngle(Directions.Left, HowMuchToMove); else if (direction == DirectionMoving.Right) StepAngle(Directions.Right, HowMuchToMove); }
//Moves and shoots public override void Alarm(string name) { if (name == "move") { #region Move //Get the rectangles for move checks: Rectangle invaders = GetInvadersRectangle(); Rectangle screen = new Rectangle(0, 0, SpaceInvaders.Device.Viewport.Width, SpaceInvaders.Device.Viewport.Height); if (direction == DirectionMoving.Left) { //Check if you can move to left (if there is space): if (invaders.Left - BaseInvader.HowMuchToMove >= screen.Left + BaseInvader.HowMuchToMove) { //If so, move: foreach (var invader in _invaders) { if (invader != null) invader.Move(direction); } PlayMoveSound(); } else { foreach (var invader in _invaders) { if (invader != null) invader.MoveDown(); } direction = DirectionMoving.Right; PlayMoveSound(); _moveTime -= (int)(_moveTime * 0.1); //Decrease time by 10% } } else if (direction == DirectionMoving.Right) { //Check if you can move to left (if there is space): if (invaders.Right + BaseInvader.HowMuchToMove <= screen.Right - BaseInvader.HowMuchToMove) { //If so, move: foreach (var invader in _invaders) { if (invader != null) invader.Move(direction); } PlayMoveSound(); } else { foreach (var invader in _invaders) { if (invader != null) invader.MoveDown(); } direction = DirectionMoving.Left; PlayMoveSound(); _moveTime -= (int)(_moveTime * 0.1); //Decrease time by 10% } } Alarms["move"].Restart(_moveTime); #endregion } if (name == "shoot") { #region Shoot if (!IsEmpty()) { //Get all the bottom invaders of all the columns List<BaseInvader> bottomRow = new List<BaseInvader>(); for (int i = 0; i < _columns; i++) //for each column { for (int j = _rows - 1; j >= 0; j--) //go from the bottom up, the first invader you find is the bottom one { if (_invaders[j, i] != null) { bottomRow.Add(_invaders[j, i]); break; } } } int shooterIndex = ObjectManager.Rand.Next(bottomRow.Count); bottomRow[shooterIndex].Shoot(); } Alarms["shoot"].Restart(ObjectManager.Rand.Next(_shootTimeBottom, _shootTimeTop)); #endregion } }