public void Execute(Entity _, int index, [ReadOnly] ref LocalToWorld localToWorld, ref TankFireCountdown countdown,
                                ref Rotation rotation)
            {
                // launching cannonball
                countdown.SecondsUntilFire -= DeltaTime;
                if (countdown.SecondsUntilFire <= 0)
                {
                    countdown.SecondsUntilFire = math.fmod(countdown.SecondsUntilFire, FirePeriod) + FirePeriod;
                    // start and end positions
                    int2   playerBoxCoords = TerrainSystem.PositionToBlockCoords(PlayerPosition.x, PlayerPosition.z, TerrainSize);
                    int    playerBoxIndex  = TerrainSystem.BlockCoordsToIndex(playerBoxCoords, TerrainSize);
                    float  playerBoxHeight = BlockHeights[playerBoxIndex];
                    float3 startPos        = math.mul(localToWorld.Value, new float4(0, 0, 0, 1)).xyz;
                    float3 endPos          = TerrainSystem.BlockCoordsToPosition(playerBoxCoords, playerBoxHeight, TerrainSize);
                    endPos.y += CANNONBALL_RADIUS + 0.01f; // extra 0.01 to avoid false positive collisions at endPos
                    float distance = math.distance(endPos.xz, startPos.xz);
                    float duration = distance / CANNONBALL_SPEED;
                    if (duration < 0.0001f)
                    {
                        duration = 1.0f;
                    }

                    // binary searching to determine height of cannonball arc
                    float low = math.max(startPos.y, endPos.y);
                    float high = low * 2;
                    float paraA, paraB, paraC;

                    // look for height of arc that won't hit boxes
                    while (true)
                    {
                        Parabola.Create(startPos.y, high, endPos.y, out paraA, out paraB, out paraC);
                        if (!PathHitsTerrain(BlockBoundingBoxes, TerrainSize, startPos, endPos, paraA, paraB, paraC))
                        {
                            // high enough
                            break;
                        }

                        // not high enough.  Double value
                        low   = high;
                        high *= 2;
                        // failsafe
                        if (high > 9999)
                        {
                            return; // skip launch
                        }
                    }
                    // do binary searches to narrow down
                    while (high - low > CANNONBALL_PARABOLA_PRECISION)
                    {
                        float mid = (low + high) / 2;
                        Parabola.Create(startPos.y, mid, endPos.y, out paraA, out paraB, out paraC);
                        if (PathHitsTerrain(BlockBoundingBoxes, TerrainSize, startPos, endPos, paraA, paraB, paraC))
                        {
                            // not high enough
                            low = mid;
                        }
                        else
                        {
                            // too high
                            high = mid;
                        }
                    }

                    // launch with calculated height
                    float height = (low + high) / 2;
                    Parabola.Create(startPos.y, height, endPos.y, out paraA, out paraB, out paraC);

                    Entity ballEntity = CommandBuffer.Instantiate(index, CannonballPrefab);
                    CommandBuffer.SetComponent(index, ballEntity, new Translation {
                        Value = startPos
                    });
                    CommandBuffer.SetComponent(index, ballEntity, new ArcState
                    {
                        Parabola  = new float3(paraA, paraB, paraC),
                        StartTime = ElapsedTime,
                        Duration  = duration,
                        StartPos  = startPos,
                        EndPos    = endPos,
                    });

                    // set cannon rotation
                    {
                        const float t             = 0.01f;
                        float       altitudeAngle = -math.atan2(
                            Parabola.Solve(paraA, paraB, paraC, t) - Parabola.Solve(paraA, paraB, paraC, 0.0f),
                            t * distance);
                        rotation.Value = quaternion.Euler(altitudeAngle, 0, 0);
                    }
                }
            }
Exemplo n.º 2
0
            public void Execute(ref Translation position, ref ArcState arc)
            {
                float t = ElapsedTime - arc.StartTime;

                if (t > arc.Duration)
                {
                    // Compute the next arc info
                    int2 playerCoords = TerrainSystem.PositionToBlockCoords(position.Value.x, position.Value.z, TerrainSize);
                    int2 dir          = math.clamp(MouseBoxCoords - playerCoords,
                                                   new int2(-1, -1), new int2(1, 1));
                    int2 destCoords = playerCoords + dir;
                    // If destination is outside the terrain bounds, don't move.
                    if (destCoords.x < 0 || destCoords.x >= TerrainSize.x ||
                        destCoords.y < 0 || destCoords.y >= TerrainSize.y)
                    {
                        destCoords = playerCoords;
                    }
                    int playerBlockIndex = TerrainSystem.BlockCoordsToIndex(playerCoords, TerrainSize);
                    int destBlockIndex   = TerrainSystem.BlockCoordsToIndex(destCoords, TerrainSize);
                    // If destination is occupied, don't move.
                    if (IsBlockOccupied[destBlockIndex] != 0)
                    {
                        destCoords     = playerCoords;
                        destBlockIndex = playerBlockIndex;
                    }

                    float playerBlockHeight = BlockHeights[playerBlockIndex];
                    float destBlockHeight   = BlockHeights[destBlockIndex];
                    float parabolaHeight    = math.max(playerBlockHeight, destBlockHeight);
                    if (dir.x != 0 && dir.y != 0)
                    {
                        // If jumping diagonally, the parabola height must take into account the directly
                        // adjacent blocks as well.
                        int2 adjacentCoordsX = playerCoords + new int2(dir.x, 0);
                        if (adjacentCoordsX.x >= 0 && adjacentCoordsX.x < TerrainSize.x)
                        {
                            int   adjacentIndexX  = TerrainSystem.BlockCoordsToIndex(adjacentCoordsX, TerrainSize);
                            float adjacentHeightX = BlockHeights[adjacentIndexX];
                            parabolaHeight = math.max(parabolaHeight, adjacentHeightX);
                        }
                        int2 adjacentCoordsZ = playerCoords + new int2(0, dir.y);
                        if (adjacentCoordsZ.y >= 0 && adjacentCoordsZ.y < TerrainSize.y)
                        {
                            int   adjacentIndexZ  = TerrainSystem.BlockCoordsToIndex(adjacentCoordsZ, TerrainSize);
                            float adjacentHeightZ = BlockHeights[adjacentIndexZ];
                            parabolaHeight = math.max(parabolaHeight, adjacentHeightZ);
                        }
                    }
                    parabolaHeight += BOUNCE_HEIGHT;

                    Parabola.Create(playerBlockHeight, parabolaHeight, destBlockHeight,
                                    out arc.Parabola.x, out arc.Parabola.y, out arc.Parabola.z);
                    arc.StartPos =
                        TerrainSystem.BlockCoordsToPosition(playerCoords, playerBlockHeight, TerrainSize);
                    arc.EndPos =
                        TerrainSystem.BlockCoordsToPosition(destCoords, destBlockHeight, TerrainSize);
                    arc.StartTime = ElapsedTime;
                    float distance = math.distance(playerCoords, destCoords);
                    arc.Duration = math.max(1, distance) * BOUNCE_BASE_DURATION;
                }
                else
                {
                    // Get the current height along the parabola
                    float s = t / arc.Duration;
                    position.Value = new float3(
                        math.lerp(arc.StartPos.x, arc.EndPos.x, s),
                        Parabola.Solve(arc.Parabola.x, arc.Parabola.y, arc.Parabola.z, s),
                        math.lerp(arc.StartPos.z, arc.EndPos.z, s)
                        );
                }
            }