private void Start() { planMap.SetPlaceListener(this); _cellCount = MapAreaSize.x * MapAreaSize.y; leaveButton.AddListener(LeaveGame); clearButton.AddListener(OnClearButtonPressed); randomButton.AddListener(PlaceShipsRandomly); continueButton.AddListener(CompletePlacement); BeginShipPlacement(); void OnClearButtonPressed() { _shipsNotDragged.Clear(); ResetPlacementMap(); } void PlaceShipsRandomly() { ResetPlacementMap(); if (_shipsNotDragged.Count == 0) { _shipsNotDragged = _pool.Keys.ToList(); } // Avoid ships that the player dragged into the map. foreach (var placement in _placements.Where(placement => !_shipsNotDragged.Contains(placement.shipId))) { planMap.SetShip(placement.ship, placement.Coordinate); RegisterShipToCells(placement.shipId, placement.ship, placement.Coordinate); placementMap.PlaceShip(placement.shipId, placement.ship, placement.Coordinate); } // Place the remaining ships randomly foreach (int shipId in _shipsNotDragged) { var uncheckedCells = new List <int>(); for (var i = 0; i < _cellCount; i++) { uncheckedCells.Add(i); } var isPlaced = false; while (!isPlaced) { if (uncheckedCells.Count == 0) { break; } int cell = uncheckedCells[Random.Range(0, uncheckedCells.Count)]; uncheckedCells.Remove(cell); isPlaced = PlaceShip(_pool[shipId], default, CellIndexToCoordinate(cell, MapAreaSize.x), false,
private void Start() { clearButton.AddListener(ResetThePool); randomButton.AddListener(ClearThePool); foreach (var coordinate in tilemap.cellBounds.allPositionsWithin) { if (!tilemap.HasTile(coordinate)) { continue; } if (_cache.ContainsKey(coordinate)) { _cache[coordinate] = tilemap.GetTile(coordinate); } else { _cache.Add(coordinate, tilemap.GetTile(coordinate)); } } }
private void Start() { opponentMap.SetClickListener(this); opponentTurnHighlighter.SetClickListener(this); opponentStatusMapTurnHighlighter.SetClickListener(this); foreach (var placement in placementMap.GetPlacements()) { userMap.SetShip(placement.ship, placement.Coordinate); } statusData.State = BeginBattle; leaveButton.AddListener(LeaveGame); fireButton.AddListener(FireShots); fireButton.SetInteractable(false); _state = _client.GetRoomState(); _player = _state.players[_client.GetSessionId()].sessionId; foreach (string key in _state.players.Keys) { if (key != _client.GetSessionId()) { _enemy = _state.players[key].sessionId; break; } } RegisterToStateEvents(); OnGamePhaseChanged(_state.phase); void RegisterToStateEvents() { _state.OnChange += OnStateChanged; _state.players[_player].shots.OnChange += OnPlayerShotsChanged; _state.players[_enemy].ships.OnChange += OnEnemyShipsChanged; _state.players[_enemy].shots.OnChange += OnEnemyShotsChanged; } }