public void Bounce(Box boxTo)
        {
            if (state != State.IDLE)
            {
                return;
            }

            // set start and end positions
            endBox = boxTo;

            // solving parabola path
            float startY = startBox.top + Y_OFFSET;
            float endY   = endBox.top + Y_OFFSET;
            float height = Mathf.Max(startY, endY);

            // make height max of adjacent boxes when moving diagonally
            if (startBox.col != endBox.col && startBox.row != endBox.row)
            {
                height = Mathf.Max(height, TerrainArea.instance.GetBox(startBox.col, endBox.row).top, TerrainArea.instance.GetBox(endBox.col, startBox.row).top);
            }
            height += BOUNCE_HEIGHT;

            Parabola.Create(startY, height, endY, out paraA, out paraB, out paraC);

            state = State.BOUNCING;
            time  = 0;

            // duration affected by distance to end box
            Vector2 startPos = new Vector2(startBox.col, startBox.row);
            Vector2 endPos   = new Vector2(endBox.col, endBox.row);
            float   dist     = Vector2.Distance(startPos, endPos);

            duration = Mathf.Max(1, dist) * BOUNCE_BASE_DURATION;
        }
Esempio n. 2
0
        public void Launch(Vector3 targetPosition, float height, float duration)
        {
            startPosition       = transform.localPosition;
            this.targetPosition = targetPosition;
            Parabola.Create(startPosition.y, height, targetPosition.y, out paraA, out paraB, out paraC);

            time          = 0;
            this.duration = duration;
        }
            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);
                    }
                }
            }
Esempio n. 4
0
        // Update is called once per frame
        void Update()
        {
            UpdateTranslation();
            RotateTowardsPlayer();

            time += Time.deltaTime;
            if (time >= Game.instance.tankLaunchPeriod)
            {
                // launching cannonball
                time -= Game.instance.tankLaunchPeriod;

                // start and end positions
                Vector3    start     = transform.localPosition;
                Vector2Int playerBox = TerrainArea.instance.BoxFromLocalPosition(Player.instance.transform.localPosition);
                Vector3    end       = TerrainArea.instance.LocalPositionFromBox(playerBox.x, playerBox.y, TerrainArea.instance.GetBox(playerBox.x, playerBox.y).top + Cannonball.RADIUS);
                float      distance  = (new Vector2(end.z - start.z, end.x - start.x)).magnitude;
                float      duration  = distance / Cannonball.SPEED;
                if (duration < .0001f)
                {
                    duration = 1;
                }

                // binary searching to determine height of cannonball arc
                float low = Mathf.Max(start.y, end.y);
                float high = low * 2;
                float paraA, paraB, paraC;

                // look for height of arc that won't hit boxes
                while (true)
                {
                    Parabola.Create(start.y, high, end.y, out paraA, out paraB, out paraC);
                    if (!Cannonball.CheckBoxCollision(start, end, 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 > Game.instance.playerParabolaPrecision)
                {
                    float mid = (low + high) / 2;
                    Parabola.Create(start.y, mid, end.y, out paraA, out paraB, out paraC);
                    if (Cannonball.CheckBoxCollision(start, end, paraA, paraB, paraC))
                    {
                        // not high enough
                        low = mid;
                    }
                    else
                    {
                        // too high
                        high = mid;
                    }
                }

                // launch with calculated height
                float      height     = (low + high) / 2;
                Cannonball cannonball = Cannonball.Create(transform.parent, start);
                cannonball.Launch(end, height, duration);

                // set cannon rotation
                SetCannonRotation(Mathf.Atan2(Parabola.Solve(paraA, paraB, paraC, .1f) - Parabola.Solve(paraA, paraB, paraC, 0), .1f) * Mathf.Rad2Deg);
            }
        }
Esempio n. 5
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)
                        );
                }
            }