Exemple #1
0
    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        var job = new WaterJob {
            translationArray         = GetComponentDataFromEntity <Translation>(true),
            canInWaterComponentArray = GetComponentDataFromEntity <CanInWaterComponent>(true)
        };
        var jobHandle = job.Schedule(this, inputDeps);

        return(jobHandle);
    }
Exemple #2
0
    private void Start()
    {
        _waterDisplay = GetComponent <WaterDisplay>();
        _colliders    = FindObjectsOfType <EdgeCollider2D>();
        var blobs         = _waterDisplay.InitialPositions;
        var waterBlobsNum = _waterDisplay.BlobsCount;

        velocities    = new NativeArray <float2>(waterBlobsNum, Allocator.Persistent);
        positions     = new NativeArray <float2>(waterBlobsNum, Allocator.Persistent);
        newVelocities = new NativeArray <float2>(waterBlobsNum, Allocator.Persistent);
        newPositions  = new NativeArray <float2>(waterBlobsNum, Allocator.Persistent);
        hasCollided   = new NativeArray <uint>(waterBlobsNum, Allocator.Persistent);

        cellSide = forceRadius;
        var cameraWidth  = Camera.main.orthographicSize * 2;
        var cameraHeight = cameraWidth / Camera.main.aspect;

        gridSize  = new Vector2(cameraWidth, cameraHeight);
        xCells    = (ushort)Mathf.CeilToInt(gridSize.x / cellSide);
        yCells    = (ushort)Mathf.CeilToInt(gridSize.y / cellSide);
        cellSpace = 10;
        grid      = new NativeArray <int>(xCells * yCells * cellSpace, Allocator.Persistent);

        for (var i = 0; i < xCells * yCells; i++)
        {
            grid[i * cellSpace] = 0;
        }

        for (var i = 0; i < positions.Length; i++)
        {
            positions[i]  = blobs[i];
            velocities[i] = 0;
        }

        var collidersPointsCount = 0;

        for (var c = 0; c < _colliders.Length; c++)
        {
            collidersPointsCount += _colliders[c].pointCount;
        }

        collidersPointsNum    = new NativeArray <int>(_colliders.Length, Allocator.Persistent);
        _oldColliderPositions = new NativeArray <float2>(_colliders.Length, Allocator.Persistent);
        collidersPoints       = new NativeArray <float2>(collidersPointsCount, Allocator.Persistent);
        var collidersPointsAccumulated = 0;

        for (var c = 0; c < _colliders.Length; c++)
        {
            var points = _colliders[c].pointCount;
            collidersPointsNum[c] = points;
            for (var i = 0; i < points; i++)
            {
                var collider = _colliders[c];
                var point    = collider.transform.TransformPoint(new Vector3(collider.points[i].x, collider.points[i].y, 0));
                collidersPoints[collidersPointsAccumulated + i] = new float2(point.x, point.y);
            }

            collidersPointsAccumulated += points;
        }

        for (var key = 0; key < positions.Length; key++)
        {
            // Place key in the grid
            var gridX = (int)math.floor((positions[key].x + gridSize.x / 2) / cellSide);
            if (gridX < 0)
            {
                gridX = 0;
            }
            else if (gridX > xCells - 1)
            {
                gridX = xCells - 1;
            }
            var gridY = (int)math.floor((positions[key].y + gridSize.y / 2) / cellSide);
            if (gridY < 0)
            {
                gridY = 0;
            }
            else if (gridY > yCells - 1)
            {
                gridY = yCells - 1;
            }
            var cellStart = (gridY * xCells + gridX) * cellSpace;
            var stored    = grid[cellStart];
            if (stored < cellSpace - 1)
            {
                grid[cellStart + 1 + stored] = key;
                grid[cellStart] = stored + 1;
            }
        }

        _sortJob = new SortJob {
            grid      = grid,
            xCells    = xCells,
            yCells    = yCells,
            halfXSize = gridSize.x / 2,
            halfYSize = gridSize.y / 2,
            cellSpace = cellSpace,
            cellSide  = cellSide
        };

        _waterJob = new WaterJob {
            gravity      = gravity,
            forceRadius  = forceRadius,
            waterForce   = waterForce,
            waterDamping = waterDamping,

            colliders      = collidersPointsNum,
            colliderPoints = collidersPoints,
            ballSize       = ballSize,

            hasCollided = hasCollided,

            gridSizeX = gridSize.x,
            gridSizeY = gridSize.y,

            grid      = grid,
            xCells    = xCells,
            yCells    = yCells,
            cellSpace = cellSpace,
            cellSide  = cellSide
        };
    }