public void ElementPressedForAttack(ElementItem elementItem) { while (_userGridCells.Cast <ElementItem>().Any(item => item.GridElementType == GridElementType.Ship) && _computerGridCells.Cast <ElementItem>().Any(item => item.GridElementType == GridElementType.Ship)) { switch (elementItem.GridElementType) { case GridElementType.None: elementItem.GridElementType = GridElementType.Miss; _soundManager.PlaySfx(SfxType.Miss); _messageItemsController.LogGenerate( _computerGridCells[elementItem.Coordinates.X, elementItem.Coordinates.Y], OwnerType.User); StartCoroutine(ComputerAttack()); break; case GridElementType.Ship: elementItem.GridElementType = GridElementType.DestroyedShip; _soundManager.PlaySfx(SfxType.Explosion); _messageItemsController.LogGenerate( _computerGridCells[elementItem.Coordinates.X, elementItem.Coordinates.Y], OwnerType.User); _sessionDataManager.UserHitShipsCount++; break; case GridElementType.DestroyedShip: case GridElementType.Miss: return; } TryActivateShip(_shipsManager.ComputerShips); _gamePage.UserScore.text = "Hit: " + _sessionDataManager.UserHitShipsCount; _gamePage.ComputerScore.text = "Hit: " + _sessionDataManager.ComputerHitShipsCount; } _gamePage.WinnerText.text = _computerGridCells.Cast <ElementItem>().Any(item => item.GridElementType == GridElementType.Ship) ? "You LOSE" : "You WIN"; _gamePage.WinnerPanel.SetActive(true); }
private void ShipDragOnGrid(Coordinates nearestItemCoordinates, Ship ship) { if (ship.OwnerType != OwnerType.User) { return; } var coordinates = GetShipItemList(_userGrid, ship.ShipType, nearestItemCoordinates, ship.IsVertical); var gridCoordinates = (from ElementItem elementItem in _userGrid select elementItem.Coordinates).ToList(); var validShipCoordinates = coordinates.Item1; var shipItems = new List <ElementItem>(); if (validShipCoordinates.Count == ship.ElementsPositions.Length) { var accessCoordinates = coordinates.Item2.Where(shipCoordinate => gridCoordinates.Any(data => data.X == shipCoordinate.X && data.Y == shipCoordinate.Y)).ToList(); var accessItems = accessCoordinates.Select(t => _userGrid[t.X, t.Y]).ToArray(); for (var i = 0; i < ship.ElementsPositions.Length; i++) { shipItems.Add(_userGrid[validShipCoordinates[i].X, validShipCoordinates[i].Y]); } Debug.Log(shipItems.First()); ship.ShipItems = shipItems.ToArray(); if (ship.ShipItems.All(data => data.GridElementType == GridElementType.None) && accessItems.All(item => item.GridElementType == GridElementType.None)) { ship.ElementsPositions = (from ElementItem elementItem in ship.ShipItems select elementItem.transform.position) .ToArray(); var centerOfVectors = CenterOfVectors(ship.ElementsPositions); foreach (var shipItem in ship.ShipItems) { shipItem.GridElementType = GridElementType.Ship; } ship.ShipRectTransform.position = centerOfVectors; ship.IsSet = true; _soundManager.PlaySfx(SfxType.ShipPlaceSound); _tryActivateButton.Invoke(); } else { Debug.Log("Can't set ship"); ship.ShipItems = null; ship.IsSet = false; ship.ShipRectTransform.position = ship.DefaultPosition; } Debug.Log("Nearest item is " + nearestItemCoordinates); } else { ship.IsSet = false; ship.ShipRectTransform.position = ship.DefaultPosition; } }