コード例 #1
0
    /// <summary>
    /// Create grass edge items
    /// </summary>
    private void CreateGrassObstacles()
    {
        // TODO: Handle possible special obstacles

        // Compute the maximum number of obstacles
        // [- m_activeLaneCount] accounts for a single-tile open path
        int itemCount = (m_activeLaneCount * m_activeTileCount) - m_activeLaneCount;

        // Prepare the pool
        if (m_grassObstacles == null)
        {
            m_grassObstacles = new AssetPool <Transform>(itemCount);
        }
        else
        {
            m_grassObstacles.ClearAllAssets();
            m_grassObstacles.Resize(itemCount);
        }

        float[] prob =
        {
            m_tallTreeObstacleProb,
            m_mediumTreeObstacleProb,
            m_shortTreeObstacleProb,
            m_treeTrunkObstacleProb,
            m_rockObstacleProb
        };

        // Populate the pool
        for (int i = 0; i < itemCount; ++i)
        {
            // Determine item type based on probability
            float       rand       = Random.value;
            float       cumulative = prob[0];
            MapItemType itemType   = MapItemType.Tree_Tall;
            for (int j = 0; rand > cumulative; ++j)
            {
                if (j + 1 == prob.Length)
                {
                    break;
                }
                cumulative += prob[j + 1];
                itemType    = (MapItemType)((int)itemType + 1);
            }

            // Create a new instance
            GameObject prefab  = m_mapResources.GetItemPrefab(itemType);
            GameObject newItem = Instantiate <GameObject>(prefab);
            newItem.transform.parent = m_grassObstaclesRoot;
            newItem.SetActive(false);
            m_grassObstacles.AddAsset(newItem.transform);
        }
    }
コード例 #2
0
    /// <summary>
    /// Creates shared assets
    /// </summary>
    private void CreateSharedAssets()
    {
        // Create coins

        // Prepare the pool
        if (m_coins == null)
        {
            // Create a new one if it hasn't been created yet
            m_coins = new AssetPool <Coin>(m_coinCountMax);
        }
        else
        {
            // Clear the pool if it already exists
            m_coins.ClearAllAssets();
            m_coins.Resize(m_coinCountMax);
        }
        // Populate the pool
        GameObject prefab = m_mapResources.GetCoinPrefab();

        for (int i = 0; i < m_coinCountMax; ++i)
        {
            GameObject coinObj    = Instantiate <GameObject>(prefab);
            Coin       coinScript = coinObj.AddComponentNoDupe <Coin>();
            coinObj.SetActive(false);
            coinObj.transform.parent = m_coinsRoot;
            coinScript.SetCoinRoot(m_coinsRoot);
            coinScript.Initialize();
            m_coins.AddAsset(coinScript);
        }

        // Create the eagle
        if (m_eagle == null)
        {
            prefab = m_mapResources.GetEaglePrefab();
            GameObject eagleObj = Instantiate <GameObject>(prefab);
            m_eagle = eagleObj.AddComponentNoDupe <Eagle>();
            eagleObj.transform.parent = m_sharedItemsRoot;
            eagleObj.SetActive(false);
        }

        // Create the water splash
        if (m_waterSplash == null)
        {
            prefab = m_mapResources.GetWaterSplashPrefab();
            GameObject waterSplashObj = Instantiate <GameObject>(prefab);
            m_waterSplash = waterSplashObj.GetComponent <ParticleSystem>();
            waterSplashObj.transform.parent = m_sharedItemsRoot;
            waterSplashObj.SetActive(false);
        }
    }