Beispiel #1
0
    public void OnDragEnd(Vector3 mousePosition)
    {
        if (isFinalCube)
        {
            return;
        }

        MakeRigidBodyDraggable(false);

        // first we need to determine whether we are above another cube
        Ray ray = Camera.main.ScreenPointToRay(mousePosition);

        // because we are always also hitting ourselves, we need to find all colliding cubes
        RaycastHit[] hits = Physics.RaycastAll(ray);

        foreach (RaycastHit hit in hits)
        {
            Cubie c = hit.collider.GetComponent <Cubie>();
            if (c == this)
            {
                // we hit ourselves, don't do anything.
            }
            else if (c != null)
            {
                // we hit another cubie
                // is this other cubie of the same "level"?
                if (c.cubeLevel == this.cubeLevel)
                {
                    // we are on the level
                    // if we are already on the maximum level, we will spawn a cube in the next spawner

                    if (cubeLevel >= owner.maxLevel)
                    {
                        owner.nextSpawner.SpawnCube(cubeLevel + 1);
                        owner.RemoveCube(c);
                        Destroy(c.gameObject);
                        Instantiate(mergeEffect, transform.position + Vector3.back, Quaternion.identity);
                    }
                    else
                    {
                        // we will update the other cubie
                        c.IncreaseCubeLevel();
                        c.MakeRigidBodyDraggable(false);
                    }


                    // and we will destroy ourselves
                    owner.RemoveCube(this);
                    Destroy(gameObject);

                    // and register a succesful swipe
                    Simulation.Instance.Swipes += 1;
                    break;
                }
                else
                {
                    // go back to the top
                    transform.position = transform.parent.position;
                    MakeRigidBodyDraggable(false);
                }
            }
            else
            {
                // we hit nothing
                MakeRigidBodyDraggable(false);
            }
        }
    }