Exemple #1
0
        // replaces zap at location i j with zap
        public IEnumerator DestroyAndReplaceZap(int i, int j, Zap zap, float transitionTime, AutoTurretBullet bullet)
        {
            // get previous zap and all data from it into new zap
            Zap previousZap = GetZap(i, j);

            zap.IsDangerousZap = previousZap.IsDangerousZap;
            zap.SetWidth(m_ZapWidth);
            zap.SetHeight(m_ZapHeight);
            zap.SetOffsetDistance(m_OffsetDistance);
            zap.Row = i;
            zap.Col = j;

            // remove previous zap from dangerous zaps and destroy it
            if (previousZap.IsDangerousZap)
            {
                m_DangerousZaps.Remove(previousZap);
            }

            // Scale new zap until size of old zap and then delete old zap
            Vector3 startNewZapScale = new Vector3(0, zap.transform.localScale.y, zap.transform.localScale.z);
            float   currTime         = 0;

            while (currTime < transitionTime)
            {
                // if the zap gets broken while it is lerping
                if (zap == null)
                {
                    break;
                }

                currTime += Time.deltaTime;
                zap.transform.localScale          = Vector3.Lerp(startNewZapScale, previousZap.transform.localScale, currTime / transitionTime);
                zap.m_SpriteRenderer.sortingOrder = previousZap.m_SpriteRenderer.sortingOrder + 1;
                yield return(null);
            }

            // destroy old zap and set new zap as zap in the new place of old zap in zap grid
            Destroy(previousZap.gameObject);

            /// make sure zap wasnt destroyed while lerping size
            if (zap != null)
            {
                m_ZapGrid[i][j] = zap;
            }

            Destroy(bullet.gameObject);
        }
Exemple #2
0
        public void Init()
        {
            // pre fill the zap grid before shuffling.
            m_ZapGrid = new List <List <Zap> >();
            for (int i = 0; i < m_Rows + 1; i++)
            {
                List <Zap> zapRowToFill = new List <Zap>();

                if (i >= m_Rows)
                {
                    for (int j = 0; j < m_Cols; j++)
                    {
                        zapRowToFill.Add(m_EndZapPrefab);
                    }
                }
                else
                {
                    // spawn all required zaps for this row
                    int totalForcedPrefabsAdded = 0;
                    for (int j = 0; j < m_ZapPrefabsForced.Count; j++)
                    {
                        int numOfPrefabToSpawn = m_ZapPrefabsForced[j];
                        Zap prefabToSpawn      = m_ZapPrefabs[j];
                        for (int k = 0; k < numOfPrefabToSpawn; k++)
                        {
                            zapRowToFill.Add(prefabToSpawn);
                            totalForcedPrefabsAdded++;
                        }
                    }

                    for (int j = totalForcedPrefabsAdded; j < m_Cols; j++)
                    {
                        // spawn zap
                        Zap zapPrefab = GetRandomZapPrefab();
                        zapRowToFill.Add(zapPrefab);
                    }

                    // shuffle the zaps in each row
                    for (int k = 0; k < zapRowToFill.Count; k++)
                    {
                        Zap temp        = zapRowToFill[k];
                        int randomIndex = (int)Random.Range(0, zapRowToFill.Count - 1);
                        zapRowToFill[k]           = zapRowToFill[randomIndex];
                        zapRowToFill[randomIndex] = temp;
                    }
                }

                m_ZapGrid.Add(zapRowToFill);
            }

            // Do one time calculations for grid before spawning zaps.
            Vector3 topLeftInWorldSpace = Camera.main.ScreenToWorldPoint(
                new Vector3(0, 1.5f * ScreenUtilities.GetDistanceInWS(1.0f), 0));
            Vector3 origin = topLeftInWorldSpace;

            origin.z = 1.0f;
            this.transform.position = origin;
            m_ZapWidth = CalculateZapWidth(m_EndZapPrefab, m_Cols);

            for (int i = 0; i < m_Rows + 1; i++)
            {
                Vector3 spawnPos = Vector3.zero;
                for (int j = 0; j < m_Cols; j++)
                {
                    // set position accordingly relative to previous zap.
                    if (j > 0)
                    {
                        Zap prevZap = m_ZapGrid[i][j - 1];
                        spawnPos = prevZap.transform.position + new Vector3(prevZap.Width, 0, 0);
                    }
                    else // spawn start zap in row
                    {
                        spawnPos = origin + new Vector3(0, i * m_RowGapDistance, 0);
                    }

                    Zap zapPrefab = m_ZapGrid[i][j];
                    if (zapPrefab != null)
                    {
                        Zap zap = (Zap)Instantiate(zapPrefab, this.transform);
                        if (zap.IsDangerousZap)
                        {
                            m_DangerousZaps.Add(zap);
                        }
                        if (zap.m_HasPoints && zap.m_Points > 0)
                        {
                            m_LucrativeZaps.Add(zap);
                        }
                        zap.transform.position = spawnPos;
                        zap.SetWidth(m_ZapWidth);
                        zap.SetHeight(m_ZapHeight);
                        zap.SetOffsetDistance(m_OffsetDistance);
                        zap.Row         = i;
                        zap.Col         = j;
                        m_ZapGrid[i][j] = zap;

                        // spawn things randomly on zap
                        // SpawnRandomObstacle(i, j, m_ChanceOfObstacle);
                    }
                }
            }

            // randomly determine if there is zap money on this zap
            for (int i = 0; i < m_MaxZapMoneys; i++)
            {
                float randomPercentChance = Random.Range(0.0f, 1.0f);
                if (randomPercentChance <= m_ZapMoneyProbability)
                {
                    Zap randomZap = GetRandomZap();
                    if (randomZap != null && !randomZap.GetIsOccupied())
                    {
                        ZapMoney zapMoney = Instantiate(
                            m_ZapMoneyPrefab,
                            randomZap.GetOffsetPosition(),
                            Quaternion.identity,
                            this.transform);
                        randomZap.SetOccupied(true);
                    }
                }
            }
        }