Exemplo n.º 1
0
    private int respawnTime = 15; // 15 seconds

    void OnTriggerEnter2D(Collider2D collider)
    {
        if (collider.gameObject.tag == "Player")
        {
            /* Code for PickUp items
             * These items have no duration and no opposites.
             * Their effects are immediate - so no need to do anything intensive.
             * */
            if (powerUp == PowerUpType.Sugar)
            {
                int slimeAmt = collider.gameObject.GetComponent <PlayerScript>().slime - 30;
                if (slimeAmt < 0)
                {
                    slimeAmt = 0;
                }
                collider.gameObject.GetComponent <PlayerScript>().slime = slimeAmt;
            }
            else if (powerUp == PowerUpType.Flower)
            {
                int slimeAmt = collider.gameObject.GetComponent <PlayerScript>().slime + 30;
                if (slimeAmt > PlayerScript.SLIME_MAX)
                {
                    slimeAmt = PlayerScript.SLIME_MAX;
                }
                collider.gameObject.GetComponent <PlayerScript>().slime = slimeAmt;
            }
            else
            {
                /* Code for power ups with duration */
                PowerUpEffectScript script = null;

                /* Only Salt and Water are opposites right now. */
                if (powerUp == PowerUpType.Salt || powerUp == PowerUpType.Water)
                {
                    PowerUpType opposite;
                    if (powerUp == PowerUpType.Salt)
                    {
                        opposite = PowerUpType.Water;
                    }
                    else
                    {
                        opposite = PowerUpType.Salt;
                    }

                    if ((script = (PowerUpEffectScript)collider.gameObject.GetComponent(opposite.ToString() + "Script")) != null)
                    {
                        //If power up is opposite of power up on player. - removes power up and does not put new power up on
                        script.StopCoroutine(script.currentCoroutine);
                        script.removeEffect();
                        Destroy(script);
                    }
                }

                if (script == null) //If it is null then it means an opposite was not found or not looked for.
                {
                    if ((script = (PowerUpEffectScript)collider.gameObject.GetComponent(powerUp.ToString() + "Script")) != null)
                    {
                        //If same power up is already on player - removes the power up first
                        script.StopCoroutine(script.currentCoroutine);
                        script.removeEffect();
                        Destroy(script);
                    }
                    //puts new power up on
                    collider.gameObject.AddComponent(Type.GetType(powerUp.ToString() + "Script"));
                }
            }
            setActive(false);
            StartCoroutine(respawn());
        }
    }
Exemplo n.º 2
0
    void OnTriggerEnter2D(Collider2D collider)
    {
        if (collider.gameObject.tag == "Player")
        {
            PlayerScript player = collider.gameObject.GetComponent <PlayerScript> ();
            if (player.powerUpEffects [powerUp.ToString()] == 0)
            {
                //If the effect is 0... do not eat the power up at all. Ignore it.
                return;
            }

            /* Code for PickUp items
             * These items have no duration and no opposites.
             * Their effects are immediate - so no need to do anything intensive.
             * */
            if (powerUp == PowerUpType.Sugar)
            {
                int slimeAmt = player.getSlime() - (int)(SUGAR_EFFECT * player.powerUpEffects ["Sugar"]);
                if (slimeAmt < 0)
                {
                    slimeAmt = 0;
                }
                player.changeSlimeBar(slimeAmt);
            }
            else if (powerUp == PowerUpType.Flower)
            {
                int slimeAmt = player.getSlime() + (int)(FLOWER_EFFECT * player.powerUpEffects ["Flower"]);
                if (slimeAmt > player.SLIME_MAX)
                {
                    slimeAmt = player.SLIME_MAX;
                }
                player.changeSlimeBar(slimeAmt);
            }
            else
            {
                /* Code for power ups with duration */
                PowerUpEffectScript script = null;

                /* Only Salt and Water are opposites right now. */
                if (powerUp == PowerUpType.Salt || powerUp == PowerUpType.Water)
                {
                    PowerUpType opposite;
                    if (powerUp == PowerUpType.Salt)
                    {
                        opposite = PowerUpType.Water;
                    }
                    else
                    {
                        opposite = PowerUpType.Salt;
                    }

                    if ((script = (PowerUpEffectScript)collider.gameObject.GetComponent(opposite.ToString() + "Script")) != null)
                    {
                        //If power up is opposite of power up on player. - removes power up and does not put new power up on
                        script.StopCoroutine(script.currentCoroutine);
                        script.removeEffect();
                        Destroy(script);
                    }
                }

                if (script == null)                   //If it is null then it means an opposite was not found or not looked for.
                {
                    if ((script = (PowerUpEffectScript)collider.gameObject.GetComponent(powerUp.ToString() + "Script")) != null)
                    {
                        //If same power up is already on player - removes the power up first
                        script.StopCoroutine(script.currentCoroutine);
                        script.removeEffect();
                        Destroy(script);
                    }
                    //puts new power up on
                    PowerUpEffectScript s      = (PowerUpEffectScript)collider.gameObject.AddComponent(Type.GetType(powerUp.ToString() + "Script"));
                    GameObject          prefab = Instantiate(displayPrefab);
                    prefab.GetComponentInChildren <UnityEngine.UI.RawImage> ().texture = images [(int)powerUp].texture;
                    s.setPrefab(prefab);
                }
            }
            setActive(false);
            StartCoroutine(respawn());
        }
    }