public bool CanSetBlock(PlaceableBlock block, Vector3Int coords) { BoundsInt blockBounds = block.Bounds; // who would've guessed :D for (int x = blockBounds.xMin; x < blockBounds.xMin + blockBounds.size.x; ++x) { for (int y = blockBounds.yMin; y < blockBounds.yMin + blockBounds.size.y; ++y) { var blockTileAt = block.TileAt(x, y); if (blockTileAt == null || _tileset.GetByTile(blockTileAt).TileType == TileType.None) { continue; } var tileTargetCoords = coords; tileTargetCoords.x += x; tileTargetCoords.y += y; var tileData = GetTileDataAt(tileTargetCoords); if (tileData != null && tileData.BlocksPlacement) { return(false); } } } return(true); }
public bool TrySetBlock(PlaceableBlock block, Vector3Int coords) { if (CanSetBlock(block, coords)) { var targetBounds = block.Bounds; var size = targetBounds.size; targetBounds.xMin = coords.x; targetBounds.yMin = coords.y; targetBounds.size = size; var auxTiles = new TileBase[block.Tiles.Length]; Array.Copy(block.Tiles, auxTiles, block.Tiles.Length); for (int r = 0; r < size.y; ++r) { for (int c = 0; c < size.x; ++c) { var tile = _tilemap.GetTile(new Vector3Int(c + coords.x, r + coords.y, 0)); if (tile != null && auxTiles[r * size.x + c] == null) { auxTiles[r * size.x + c] = tile; } } } _tilemap.SetTilesBlock(targetBounds, auxTiles); return(true); } return(false); }
void Update() { renderer.enabled = isVisible; if (!isStatic) { // Find nearest placeable spot float minSqrDist = Mathf.Infinity; PlaceableBlock nearSpot = null; for (int i = 0; i < placeableSpots.Count; i++) { float sqrDist = (transform.position - placeableSpots[i].transform.position).sqrMagnitude; if (sqrDist < minSqrDist && placeableSpots[i].attachedWall != null) { minSqrDist = sqrDist; nearSpot = placeableSpots[i]; } } // Place in nearest spot if possible. if (minSqrDist <= Mathf.Pow(placeRange, 2)) { carried = false; isStatic = false; transform.parent = null; transform.position = nearSpot.transform.position; transform.rotation = nearSpot.transform.rotation; nearSpot.UsePath(); } } }
void CreateBlock(Vector3Int coords) { var pos = _map.WorldFromCoords(coords, centered: false); _tempBlockInstance = Instantiate(_blockPrefab); _tempBlockInstance.RefreshBounds(); var xfm = _tempBlockInstance.transform; xfm.position = pos; bool validPos = _map.CanSetBlock(_tempBlockInstance, coords); _tempBlockInstance.SetTint(validPos ? _validBlockTint : _invalidBlockTint); }
void CheckScrolls(Vector3Int coords) { var scroll = GetScrollAt(coords); if (scroll != null && scroll.gameObject.activeInHierarchy) { if (_destructiveCancel) { _scrolls.Remove(scroll); Destroy(scroll.gameObject); } else { _selectedScroll = scroll; scroll.gameObject.SetActive(false); } _blockPrefab = scroll.BlockData; CreateBlock(coords); _player.SetTint(new Color(1, 1, 1, 0.4f)); _gameState = GameState.Placement; } }
// Update is called once per frame void Update() { switch (_gameState) { case GameState.WaitInfo: { if (_infoReady && _gameInput.Confirmed) { _infoCanvas.gameObject.SetActive(false); _gameState = GameState.WaitPlayerInput; } break; } case GameState.Ended: { if (_endReady && _gameInput.Confirmed) { if (_gameResult == GameResult.Won) { PlayState.Instance.NextScene(); } else if (_gameResult == GameResult.Lost) { PlayState.Instance.LoadCurrentLevel(); } } break; } case GameState.WaitPlayerInput: { Vector3Int playerCoords = _map.CoordsFromWorld(_player.transform.position); if (_gameInput.Direction != MoveDirection.None) { Vector3Int coordOffset = _offsets[(int)_gameInput.Direction]; Vector3Int targetCoords = playerCoords + coordOffset; if (_map.EntityCanMoveTo(targetCoords)) { _gameState = GameState.WaitPlayerAction; Vector3 targetWorld = _map.WorldFromCoords(targetCoords, centered: true); StartCoroutine(MoveTo(targetWorld)); } } else if (!_destructiveCancel && _gameInput.Confirmed) { CheckScrolls(playerCoords); } break; } case GameState.WaitPlayerAction: { if (_playerActionFinished) { _playerActionFinished = false; _gameResult = EvaluateVictory(); if (_gameResult == GameResult.Running) { _gameState = GameState.Simulating; } } break; } case GameState.Placement: { if (_tempBlockInstance == null) { Debug.LogError($"WAT"); } else { Vector3Int blockCoords = _map.CoordsFromWorld(_tempBlockInstance.transform.position); bool validPos = _map.CanSetBlock(_tempBlockInstance, blockCoords); if (_gameInput.Direction != MoveDirection.None && !_placementDelay) { Vector3Int coordOffset = _offsets[(int)_gameInput.Direction]; Vector3Int targetCoords = blockCoords + coordOffset; BoundsInt bounds = _tempBlockInstance.Bounds; int w = bounds.size.x; int h = bounds.size.y; bounds.xMin = targetCoords.x; bounds.yMin = targetCoords.y; bounds.xMax = targetCoords.x + w; bounds.yMax = targetCoords.y + h; targetCoords = _map.ClampBlockToFitBounds(bounds); validPos = _map.CanSetBlock(_tempBlockInstance, targetCoords); StartCoroutine(MoveBlock(targetCoords)); } else if (_gameInput.Rotation != RotateDirection.None) { _tempBlockInstance.Rotate(_gameInput.Rotation); validPos = _map.CanSetBlock(_tempBlockInstance, blockCoords); } else if (_gameInput.Confirmed) { validPos = _map.TrySetBlock(_tempBlockInstance, blockCoords); if (validPos) { if (!_destructiveCancel && _selectedScroll != null) { _scrolls.Remove(_selectedScroll); Destroy(_selectedScroll.gameObject); _selectedScroll = null; } Destroy(_tempBlockInstance.gameObject); _tempBlockInstance = null; RefreshVisibility(ignoreHiddenCheck: true); _player.SetTint(Color.white); if (NoPossiblePaths()) { _gameResult = GameResult.Lost; } else { _gameState = GameState.Simulating; } } } else if (_gameInput.Cancelled) { if (!_destructiveCancel && _selectedScroll != null) { _selectedScroll.gameObject.SetActive(true); } Destroy(_tempBlockInstance.gameObject); _tempBlockInstance = null; _player.SetTint(Color.white); if (NoPossiblePaths()) { _gameResult = GameResult.Lost; } else { _gameState = GameState.Simulating; } } if (_tempBlockInstance != null) { _tempBlockInstance.SetTint(validPos ? _validBlockTint : _invalidBlockTint); } } break; } case GameState.Simulating: { // Stuff moves around _turnCount++; _playerActionFinished = false; _gameState = GameState.WaitPlayerInput; break; } } _gameInput.Reset(); if (_gameState != GameState.Ended && _gameResult != GameResult.Running) { _gameState = GameState.Ended; _endReady = false; StartCoroutine(OnLevelEnd()); } }