public virtual void SetShip(Ship shipToSet) { if (!shipToSet.IsSet()) { shipToSet = Ship.CreateNewShip( shipToSet.StartPosition, shipToSet.ShipOrientation, shipToSet.Id, shipToSet.Size ); } Ship ship; int shipIndex; try { ship = Ships.Single(x => x.Id == shipToSet.Id); shipIndex = Ships.IndexOf(ship); } catch (InvalidOperationException) { throw new ArgumentException("No such ship Id"); } if (ship.IsSet()) { DeleteShipFromField(ship.Id); } if (!CanShipBeSet(shipToSet)) { if (ship.IsSet()) { AddShipOnField(ship); } throw new ArgumentException("Ship can not be set."); } Ships[shipIndex] = shipToSet; if (!IsShipListRespectsConfig(Ships)) { Ships[shipIndex] = ship; throw new ArgumentException("Ship violates configuration"); } AddShipOnField(shipToSet); }
public void DeleteShipFromField(int shipId) { int index; try { index = Ships.IndexOf(Ships.Single(x => x.Id == shipId)); } catch (InvalidOperationException) { throw new ArgumentException("Ship Id not found"); } if (!Ships[shipId].IsSet()) { throw new ArgumentNullException("Ship is not set"); } foreach (var cell in Ships[index].Cells) { Cells[cell.Position.Y, cell.Position.X] = CellStatus.NotSet; } }
/// <summary> /// Determines Collision between an enemy in the block and a missile /// </summary> /// <param name="m">Colliding missile</param> public bool Collision(Missile m) { if (m.Position.x > Position.x + Size.Width) { return(false); } else if (m.Position.y > Position.y + Size.Height) { return(false); } else if (Position.x > m.Position.x + m.imageWidth) { return(false); } else if (Position.y > m.Position.y + m.imageHeight) { return(false); } else { int index = -1; foreach (SpaceShip s in Ships) { if (s.Collision(m)) { index = Ships.IndexOf(s); } } if (index != -1) { RemoveEnemy(index); return(true); } } return(false); }