Beispiel #1
0
    // generate resources
    void generateResouces()
    {
        if (m_LevelManage.m_Levels[m_CurrentWay].m_TotalResouces < 0)
        {
            return;
        }
        if (m_CurrentGenerateTreeTimeStep < 0)
        {
            m_CurrentGenerateTreeTimeStep = m_LevelManage.m_Levels[m_CurrentWay].m_ReseoucesInterval;
            Debug.Log("generate resouces");
            m_LevelManage.m_Levels[m_CurrentWay].m_TotalResouces--;
            int        chanceWood = m_LevelManage.m_Levels[m_CurrentWay].m_ChanceWood;
            int        ran        = (int)Random.Range(0, 101);
            GameObject ob         = null;

            if (ran <= chanceWood)
            {
                ob = SpriteManage.CREATE_SPRITE(SpriteType.TREE);
            }
            else
            {
                ob = SpriteManage.CREATE_SPRITE(SpriteType.ROCK);
            }

            bool generated = false;
            // number of attem for item generate
            int attemp = 10;

            while (!generated && attemp > 0)
            {
                //generated = true;
                Vector2 pos       = SpriteManage.RandomPos();
                var     colliders = Physics2D.OverlapBoxAll(pos, ob.GetComponent <SpriteRenderer>().bounds.size, 0);
                if (colliders.Length == 0)
                {
                    generated = true;
                }
                else
                {
                    for (int i = 0; i < colliders.Length; i++)
                    {
                        Collider2D collider = colliders[i];
                        Debug.Log("collider tag: " + collider.tag);
                        Debug.Log("type: " + collider.GetType());
                        if (collider.GetType() == typeof(BoxCollider2D))
                        {
                            generated = false;
                        }
                        else if (collider.GetType() == typeof(CircleCollider2D))
                        {
                            if (collider.tag == "castle")
                            {
                                generated = true;
                            }
                        }
                    }
                }
                if (generated)

                {
                    ob.transform.position = pos;
                }
                attemp--;
            }
            Debug.Log("generated: " + generated);
            if (!generated)
            {
                SpriteManage.DestroyObject(ob);
            }
        }
    }