// Called externall, we want to tile this specified object
    public void TileAnObject(GameObject gObj)
    {
        // Remove IncomingAsteroid if it exists. This script is for newly instantiated asteroids only
        IncomingAsteroid incoming = gObj.GetComponent <IncomingAsteroid> ();

        if (incoming)
        {
            Collider2D coll = gObj.GetComponent <Collider2D> ();
            if (coll)
            {
                coll.isTrigger = false;
            }
            Destroy(incoming);
            gObj.layer = LayerMask.NameToLayer("Asteroid");
            gObj.tag   = "Asteroid";
        }

        // Instance around our grid 8 additional times, fixing with a fixedJoint
        GameObject parentObj = new GameObject();

        if (gObj.tag == "Asteroid")
        {
            Asteroid asteroidScript = gObj.GetComponent <Asteroid> ();
            if (asteroidScript != null)
            {
                parentObj.name = asteroidScript.size == 1 ? "AsteroidsSmall" :
                                 asteroidScript.size == 2 ? "AsteroidsMedium" : "AsteroidsLarge";
                parentObj.tag = parentObj.name;
            }
            else
            {
                parentObj.name = gObj.name;
            }
        }
        else
        {
            parentObj.name = gObj.name;
        }
        gObj.name = System.Guid.NewGuid().ToString();
        gObj.AddComponent <ObjectTilingCtrl> ().isOriginal = true;

        // Get shield sync information
        //		Setting these variables here so we can re-use them later without using more memory
        Transform       shieldTf;
        GameObject      childObj;
        AsteroidShields shieldScript;

        if (gObj.tag == "Asteroid")
        {
            shieldTf = gObj.transform.GetChild(0);
            if (shieldTf != null)
            {
                childObj     = shieldTf.gameObject;
                shieldScript = childObj.GetComponent <AsteroidShields> ();
                if (shieldScript)
                {
                    shieldScript.SyncSheilds();
                }
            }
        }

        foreach (KeyValuePair <string, Vector2> bound in boundaries)
        {
            GameObject newObj = TileItHere(bound.Value, gObj);
            if (newObj != null)
            {
                newObj.transform.SetParent(parentObj.transform);
                if (gObj.tag == "Asteroid")
                {
                    // Sync up shields...
                    shieldTf = newObj.transform.GetChild(0);
                    if (shieldTf != null)
                    {
                        childObj     = shieldTf.gameObject;
                        shieldScript = childObj.GetComponent <AsteroidShields> ();
                        if (shieldScript)
                        {
                            shieldScript.SyncSheilds();
                        }
                    }
                }
            }
        }
        // Configure some layers for original objects so
        // we can control how non-tiled objects interact
        gObj.transform.SetParent(parentObj.transform);
        if (gObj.tag == "Asteroid")
        {
            gObj.layer = LayerMask.NameToLayer("Original Asteroid");
        }
        else
        {
            gObj.layer = LayerMask.NameToLayer("Original Player");
        }
    }
Beispiel #2
0
    // Disable OR enable things on pause or unpause
    void SetComponentStats(bool state = true)
    {
        pausePanel.SetActive(!state);
        // If we're pausing, clear the rigidbody's dictionary so we can store all the latest velocities for un-pausing
        if (!state)
        {
            previousVelocities = new Dictionary <Rigidbody2D, Vector2> ();
        }
        objectsToDisable = new List <GameObject> (GameObject.FindGameObjectsWithTag("Asteroid"));
        objectsToDisable.AddRange(GameObject.FindGameObjectsWithTag("IncomingAsteroid"));
        foreach (GameObject asteroid in objectsToDisable)
        {
            rb               = asteroid.GetComponent <Rigidbody2D> ();
            coll             = asteroid.GetComponent <Collider2D> ();
            asteroidScript   = asteroid.GetComponent <Asteroid> ();
            incomingAsteroid = asteroid.GetComponent <IncomingAsteroid> ();
            if (rb)
            {
                if (state == true)
                {
                    rb.WakeUp();
                    if (previousVelocities.ContainsKey(rb))
                    {
                        rb.velocity = previousVelocities [rb];
                    }
                }
                else
                {
                    previousVelocities [rb] = rb.velocity;
                    rb.Sleep();
                }
            }
            if (coll)
            {
                coll.enabled = state;
            }

            if (asteroidScript)
            {
                asteroidScript.enabled = state;
            }

            if (incomingAsteroid)
            {
                incomingAsteroid.enabled = state;
            }
        }

        // TODO: could be cached as players should always be visible
        objectsToDisable = new List <GameObject> (GameObject.FindGameObjectsWithTag("Player"));

        foreach (GameObject player in objectsToDisable)
        {
            rb             = player.GetComponent <Rigidbody2D> ();
            coll           = player.GetComponent <Collider2D> ();
            fireShotScript = player.GetComponent <FireShots> ();
            moveScript     = player.GetComponent <SpaceShipMovement> ();
            if (rb)
            {
                if (state == true)
                {
                    rb.WakeUp();
                    if (previousVelocities.ContainsKey(rb))
                    {
                        rb.velocity = previousVelocities [rb];
                    }
                }
                else
                {
                    previousVelocities [rb] = rb.velocity;
                    rb.Sleep();
                }
            }
            if (coll)
            {
                coll.enabled = state;
            }

            if (fireShotScript)
            {
                fireShotScript.enabled = state;
            }

            if (moveScript)
            {
                moveScript.enabled = state;
            }
        }

        objectsToDisable = new List <GameObject> (GameObject.FindGameObjectsWithTag("Laser"));
        foreach (GameObject laser in objectsToDisable)
        {
            rb   = laser.GetComponent <Rigidbody2D> ();
            coll = laser.GetComponent <Collider2D> ();
            if (rb)
            {
                if (state == true)
                {
                    rb.WakeUp();
                    if (previousVelocities.ContainsKey(rb))
                    {
                        rb.velocity = previousVelocities [rb];
                    }
                }
                else
                {
                    previousVelocities [rb] = rb.velocity;
                    rb.Sleep();
                }
            }
            if (coll)
            {
                coll.enabled = state;
            }
        }
    }