public void PlaceObjects() { var gridState = _options.gridState; for (int i = 0; i < gridState.Length; i++) { var prefab = GetPrefab(i); var position = GridUtils.GetPositionByIndex(i, _options.rows, _options.cellSize); var go = Object.Instantiate(prefab, position, Quaternion.identity, _options.parent); var scale = go.transform.localScale; go.transform.localScale = new Vector3(scale.x * _options.cellSize, scale.y, scale.z * _options.cellSize); go.name += $"(index: {i})"; } }
public void Execute(int index) { if (_gridState[index] == GridStateConstants.START) { return; } var(x, y) = GridUtils.GetCoordinates(index, _rows); var up = y + 1; var down = y - 1; var left = x - 1; var right = x + 1; if (Mathf.Tan(up * left + right - down) > _sensitivity) { _gridState[index] = GridStateConstants.BLOCKED; } }
private void FloodGate() { for (var index = _cols; index < _gridState.Length - _cols; index++) { if (_floodState[index] != GridStateConstants.FLOODED) { continue; } var(x, y) = GridUtils.GetCoordinates(index, _rows); var topLeft = GridUtils.GetIndex(x - 1, y + 1, _rows); var topRight = GridUtils.GetIndex(x + 1, y - 1, _rows); var botLeft = GridUtils.GetIndex(x - 1, y - 1, _rows); var botRight = GridUtils.GetIndex(x + 1, y - 1, _rows); int targetIndex; if (_gridState[topLeft] == GridStateConstants.FREE || _gridState[botLeft] == GridStateConstants.FREE) { targetIndex = x - 1; // left cell } else if (_gridState[topRight] == GridStateConstants.FREE || _gridState[botRight] == GridStateConstants.FREE) { targetIndex = x + 1; // right cell } else { continue; } var(targetX, targetY) = GridUtils.GetCoordinates(targetIndex, _rows); if (GridUtils.HasOutOfBoundsNeighbour(targetX, targetY, _rows, _cols)) { continue; } //UnityEngine.Debug.Log($"index: {index} has {freeNeighbours} free neighbours, delta: {freeNeighbourIdx - index}"); _gridState[targetIndex] = GridStateConstants.FREE; } }