public void SpawnCoinAndPowerup(TrackSegment segment)
    {
        int         numCoinsSpawned = 0;
        const float increment       = 1.5f;
        float       currentWorldPos = 0.0f;
        int         currentLane     = segment.GetComponent <TrackSegment>().coinLane;

        float powerupChance = Mathf.Clamp01(Mathf.Floor(m_TimeSincePowerup) * 0.5f * 0.001f);

        while (currentWorldPos < segment.worldLength)
        {
            Vector3    pos;
            Quaternion rot;
            segment.GetPointAtInWorldUnit(currentWorldPos, out pos, out rot);

            pos = pos + ((currentLane - 1) * laneOffset * (rot * Vector3.right));

            GameObject toUse;
            if (Random.value < powerupChance)
            {
                int picked = Random.Range(0, consumableDatabase.consumbales.Length);

                //if the powerup can't be spawned, we don't reset the time since powerup to continue to have a high chance of picking one next track segment
                if (consumableDatabase.consumbales[picked].canBeSpawned)
                {
                    // Spawn a powerup instead.
                    m_TimeSincePowerup = 0.0f;
                    powerupChance      = 0.0f;

                    toUse = Instantiate(consumableDatabase.consumbales[picked].gameObject, pos, rot) as GameObject;
                    toUse.transform.SetParent(segment.transform, true);
                }
            }
            else
            {
                toUse = Coin.coinPool.Get(pos, rot);
                toUse.transform.SetParent(segment.collectibleTransform, true);
                numCoinsSpawned++;
            }
            currentWorldPos += increment;
        }
        numCoinsPerSection.Add(numCoinsSpawned);
    }
Ejemplo n.º 2
0
    public IEnumerator SpawnCoinAndPowerup(TrackSegment segment)
    {
        if (!m_IsTutorial)
        {
            const float increment       = 1.5f;
            float       currentWorldPos = 0.0f;
            int         currentLane     = Random.Range(0, 3);

            float powerupChance = Mathf.Clamp01(Mathf.Floor(m_TimeSincePowerup) * 0.5f * 0.001f);
            float premiumChance = Mathf.Clamp01(Mathf.Floor(m_TimeSinceLastPremium) * 0.5f * 0.0001f);

            while (currentWorldPos < segment.worldLength)
            {
                Vector3    pos;
                Quaternion rot;
                segment.GetPointAtInWorldUnit(currentWorldPos, out pos, out rot);


                bool laneValid  = true;
                int  testedLane = currentLane;
                while (Physics.CheckSphere(pos + ((testedLane - 1) * laneOffset * (rot * Vector3.right)), 0.4f, 1 << 9))
                {
                    testedLane = (testedLane + 1) % 3;
                    if (currentLane == testedLane)
                    {
                        // Couldn't find a valid lane.
                        laneValid = false;
                        break;
                    }
                }

                currentLane = testedLane;

                if (laneValid)
                {
                    pos = pos + ((currentLane - 1) * laneOffset * (rot * Vector3.right));


                    GameObject toUse = null;
                    if (Random.value < powerupChance)
                    {
                        int picked = Random.Range(0, consumableDatabase.consumbales.Length);

                        //if the powerup can't be spawned, we don't reset the time since powerup to continue to have a high chance of picking one next track segment
                        if (consumableDatabase.consumbales[picked].canBeSpawned)
                        {
                            // Spawn a powerup instead.
                            m_TimeSincePowerup = 0.0f;
                            powerupChance      = 0.0f;

                            AsyncOperationHandle op = Addressables.InstantiateAsync(consumableDatabase.consumbales[picked].gameObject.name, pos, rot);
                            yield return(op);

                            if (op.Result == null || !(op.Result is GameObject))
                            {
                                Debug.LogWarning(string.Format("Unable to load consumable {0}.", consumableDatabase.consumbales[picked].gameObject.name));
                                yield break;
                            }
                            toUse = op.Result as GameObject;
                            toUse.transform.SetParent(segment.transform, true);
                        }
                    }
                    else if (Random.value < premiumChance)
                    {
                        m_TimeSinceLastPremium = 0.0f;
                        premiumChance          = 0.0f;

                        AsyncOperationHandle op = Addressables.InstantiateAsync(currentTheme.premiumCollectible.name, pos, rot);
                        yield return(op);

                        if (op.Result == null || !(op.Result is GameObject))
                        {
                            Debug.LogWarning(string.Format("Unable to load collectable {0}.", currentTheme.premiumCollectible.name));
                            yield break;
                        }
                        toUse = op.Result as GameObject;
                        toUse.transform.SetParent(segment.transform, true);
                    }
                    else
                    {
                        toUse = Coin.coinPool.Get(pos, rot);
                        toUse.transform.SetParent(segment.collectibleTransform, true);
                    }

                    if (toUse != null)
                    {
                        //TODO : remove that hack related to #issue7
                        Vector3 oldPos = toUse.transform.position;
                        toUse.transform.position += Vector3.back;
                        toUse.transform.position  = oldPos;
                    }
                }

                currentWorldPos += increment;
            }
        }
    }
Ejemplo n.º 3
0
    public void SpawnCoinAndPowerup(TrackSegment segment)
    {
        const float increment       = 1.5f;
        float       currentWorldPos = 0.0f;
        int         currentLane     = Random.Range(0, 3);

        float powerupChance = Mathf.Clamp01(Mathf.Floor(m_TimeSincePowerup) * 0.5f * 0.001f);
        float premiumChance = Mathf.Clamp01(Mathf.Floor(m_TimeSinceLastPremium) * 0.5f * 0.0001f);

        while (currentWorldPos < segment.worldLength)
        {
            Vector3    pos;
            Quaternion rot;
            segment.GetPointAtInWorldUnit(currentWorldPos, out pos, out rot);


            bool laneValid  = true;
            int  testedLane = currentLane;
            while (Physics.CheckSphere(pos + ((testedLane - 1) * laneOffset * (rot * Vector3.right)), 0.4f, 1 << 9))
            {
                testedLane = (testedLane + 1) % 3;
                if (currentLane == testedLane)
                {
                    // Couldn't find a valid lane.
                    laneValid = false;
                    break;
                }
            }

            currentLane = testedLane;

            if (laneValid)
            {
                pos = pos + ((currentLane - 1) * laneOffset * (rot * Vector3.right));


                GameObject toUse;
                if (Random.value < powerupChance)
                {
                    int picked = Random.Range(0, consumableDatabase.consumbales.Length);

                    //if the powerup can't be spawned, we don't reset the time since powerup to continue to have a high chance of picking one next track segment
                    if (consumableDatabase.consumbales[picked].canBeSpawned)
                    {
                        // Spawn a powerup instead.
                        m_TimeSincePowerup = 0.0f;
                        powerupChance      = 0.0f;

                        toUse = Instantiate(consumableDatabase.consumbales[picked].gameObject, pos, rot) as GameObject;
                        toUse.transform.SetParent(segment.transform, true);
                    }
                }
                else if (Random.value < premiumChance)
                {
                    m_TimeSinceLastPremium = 0.0f;
                    premiumChance          = 0.0f;

                    toUse = Instantiate(currentTheme.premiumCollectible, pos, rot);
                    toUse.transform.SetParent(segment.transform, true);
                }
                else
                {
                    toUse = Coin.coinPool.Get(pos, rot);
                    toUse.transform.SetParent(segment.collectibleTransform, true);
                }
            }

            currentWorldPos += increment;
        }
    }