コード例 #1
0
    public void Drop(Energy energy = null)
    {
        if (active)
        {
            col2D.gameObject.SetActive(true);
            if (collision == true)
            {
                Destroy(emptyCannon);
                drag = false;
                if (emptyCannon == null)
                {
                    if (TutorialClass.tutorial == true)
                    {
                        GameObject.Find("Tutorial1(Clone)").GetComponent <TutorialClass>().Next(this.transform.position);
                    }
                    energy.energy -= cost;
                    energy.UpdateUI();
                    if (type == "Battery")
                    {
                        energy.maxEnergy *= 2;
                    }
                    if (type == "Generator")
                    {
                        energy.energyAmount += 1;
                    }
                }
                else
                {
                    if (TutorialClass.tutorial2 == true)
                    {
                        GameObject.Find("Tutorial1(Clone)").GetComponent <TutorialClass2>().Next();
                    }
                }
            }
            else
            {
                Vector2       boxVec     = new Vector2();
                Vector2       newVec     = transform.position;
                int           steps      = 0;
                bool          fail       = false;
                BoxCollider2D col        = transform.Find("hitTestCannon").GetComponent <BoxCollider2D>();
                Collider2D[]  Box        = SpawnBoxCollision(transform.position, col);
                bool          isOnPlanet = false;
                for (var i = 0; i < Box.Length; i++)
                {
                    if (Box[i].tag == "Cannon")
                    {
                        GameObject o = Box[i].gameObject.transform.parent.gameObject;
                        if (o != this.gameObject && o != emptyCannon)
                        {
                            boxVec = o.transform.position;
                        }
                    }
                }

                while (collision == false)
                {
                    fail   = true;
                    newVec = Vector2.MoveTowards(newVec, boxVec, -0.01f);
                    Collider2D[] newBox = SpawnBoxCollision(newVec, col);

                    for (var i = 0; i < newBox.Length; i++)
                    {
                        if (newBox[i].tag == "Cannon")
                        {
                            GameObject o = newBox[i].gameObject.transform.parent.gameObject;
                            if (o != this.gameObject && o != emptyCannon)
                            {
                                fail = true;
                                break;
                            }
                        }
                        if (newBox[i].tag == "BigPlanet")
                        {
                            fail = false;
                        }
                        if (newBox[i].tag == "Platform")
                        {
                            fail = false;

                            goto End;
                        }
                    }
                    float stageLeft   = Game.stageLeft;
                    float stageRight  = Game.stageRight;
                    float stageTop    = Game.stageTop;
                    float stageBottom = Game.stageBottom;
                    if (newVec.x > stageRight)
                    {
                        break;
                    }
                    if (newVec.x < stageLeft)
                    {
                        break;
                    }
                    if (newVec.y < stageBottom)
                    {
                        break;
                    }
                    if (fail == false)
                    {
                        onCannon = false;
                        this.transform.position = newVec;
                        Destroy(emptyCannon);
                        drag      = false;
                        collision = true;
                        if (emptyCannon == null)
                        {
                            if (TutorialClass.tutorial == true)
                            {
                                GameObject.Find("Tutorial1(Clone)").GetComponent <TutorialClass>().Next(this.transform.position);
                            }
                            energy.energy -= cost;
                            energy.UpdateUI();
                            if (type == "Battery")
                            {
                                energy.maxEnergy *= 2;
                            }
                            if (type == "Generator")
                            {
                                energy.energyAmount += 1;
                            }
                        }
                        else
                        {
                            if (TutorialClass.tutorial2 == true)
                            {
                                GameObject.Find("Tutorial1(Clone)").GetComponent <TutorialClass2>().Next();
                            }
                        }
                        return;
                    }

                    steps++;
                    //Debug.Log(steps);
                    if (steps > 100)
                    {
                        break;
                    }
                }
End:
                if (emptyCannon != null)
                {
                    Cannon cannon = emptyCannon.GetComponent <Cannon>();
                    cannon.Active();

                    /*if (isOnPlanet == true)
                     * {
                     *  cannon.onPlanet = true;
                     * }
                     * else cannon.onPlatform = true;*/
                    cannon.onCannon = false;
                }
                Destroy(this.gameObject);
            }
        }
    }