Ejemplo n.º 1
0
 void OnTriggerExit2D(Collider2D col)
 {
     if (isActiveAndEnabled)
     {
         currentTrigger = null;
     }
 }
Ejemplo n.º 2
0
 void OnTriggerEnter2D(Collider2D col)
 {
     if (isActiveAndEnabled)
     {
         Trigger2D c = col.gameObject.GetComponent <Trigger2D>();
         if (c != null)
         {
             currentTrigger = c;
             isTriggerNew   = true;
         }
     }
 }
Ejemplo n.º 3
0
    void Awake()
    {
        //create two child gameobjects
        GameObject left = new GameObject(name + " trigger left");

        left.transform.SetParent(transform, false);
        BoxCollider2D colliderLeft = left.AddComponent <BoxCollider2D>();

        colliderLeft.offset    = new Vector2(-0.25f, 0.0f);
        colliderLeft.size      = new Vector2(0.5f, 1.0f);
        colliderLeft.isTrigger = true;
        Trigger2D leftTrigger = left.AddComponent <Trigger2D>();

        leftTrigger.onEnter.AddListener(delegate {
            foreach (Track t in affectedTracks)
            {
                t.onGoLeft(fadeTime);
            }
        });

        GameObject right = new GameObject(name + " trigger right");

        right.transform.SetParent(transform, false);
        BoxCollider2D colliderRight = right.AddComponent <BoxCollider2D>();

        colliderRight.offset    = new Vector2(0.25f, 0.0f);
        colliderRight.size      = new Vector2(0.5f, 1.0f);
        colliderRight.isTrigger = true;
        Trigger2D rightTrigger = right.AddComponent <Trigger2D>();

        rightTrigger.onEnter.AddListener(delegate {
            foreach (Track t in affectedTracks)
            {
                t.onGoRight(fadeTime);
            }
        });
    }
    void TriggerBehaviourTeleport(GameObject entity, Collidable2D collidable)
    {
        Trigger2D trigger = collidable.currentTrigger;

        //Transform vars
        float teleportDestinationX = entity.transform.position.x;
        float teleportDestinationY = entity.transform.position.y;
        int   directionX           = trigger.transform.position.x.CompareTo(trigger.teleportDestination.x);
        int   directionY           = trigger.transform.position.y.CompareTo(trigger.teleportDestination.y);

        //Grapic vars
        bool       doTeleportGraphic       = trigger.TeleportEffect != null;
        Vector2    teleportOutGraphicPoint = new Vector2(teleportDestinationX, teleportDestinationY);
        GameObject currentTeleportGraphic;
        bool       teleportGraphicFlipX = false;
        bool       teleportGraphicFlipY = false;

        //Determine Graphics and Destination from TeleportBehaviour
        switch (trigger.teleportBehaviour)
        {
        case Trigger2D.TeleportBehaviour.TeleportX:
            teleportDestinationX      = trigger.teleportDestination.x;
            teleportOutGraphicPoint.x = trigger.transform.position.x + (Mathf.Abs(trigger.transform.position.x) * -0.16f * directionX);
            teleportGraphicFlipY      = true;
            break;

        case Trigger2D.TeleportBehaviour.TeleportY:
            teleportDestinationY      = trigger.teleportDestination.y;
            teleportOutGraphicPoint.y = trigger.transform.position.y + (Mathf.Abs(trigger.transform.position.y) * -0.16f * directionY);
            teleportGraphicFlipX      = true;
            break;

        case Trigger2D.TeleportBehaviour.TeleportXY:
            teleportDestinationX      = trigger.teleportDestination.x;
            teleportDestinationY      = trigger.teleportDestination.y;
            teleportOutGraphicPoint.x = trigger.transform.position.x + (Mathf.Abs(trigger.transform.position.x) * -0.16f * directionX);
            teleportOutGraphicPoint.y = trigger.transform.position.y + (Mathf.Abs(trigger.transform.position.y) * -0.16f * directionY);
            teleportGraphicFlipX      = true;
            teleportGraphicFlipY      = true;
            break;

        default:
            return;
        }

        //Draw Pre-Graphic
        if (doTeleportGraphic)
        {
            currentTeleportGraphic = GameObject.Instantiate(trigger.TeleportEffect,
                                                            new Vector3(teleportOutGraphicPoint.x,
                                                                        teleportOutGraphicPoint.y,
                                                                        entity.transform.position.z),
                                                            trigger.TeleportEffect.transform.rotation) as GameObject;
            if (teleportGraphicFlipY)
            {
                currentTeleportGraphic.GetComponent <SpriteRenderer> ().flipY = directionX > 0;
            }
            if (teleportGraphicFlipX)
            {
                currentTeleportGraphic.GetComponent <SpriteRenderer> ().flipX = directionY > 0;
            }
            ;
        }

        //Teleport entity
        entity.transform.position = (new Vector3(
                                         teleportDestinationX,
                                         teleportDestinationY,
                                         entity.transform.position.z
                                         ));

        //Draw Post-Graphic
        if (doTeleportGraphic)
        {
            currentTeleportGraphic = GameObject.Instantiate(trigger.TeleportEffect,
                                                            new Vector3(teleportDestinationX,
                                                                        teleportDestinationY,
                                                                        entity.transform.position.z),
                                                            trigger.TeleportEffect.transform.rotation) as GameObject;
            if (teleportGraphicFlipY)
            {
                currentTeleportGraphic.GetComponent <SpriteRenderer> ().flipY = directionX < 0;
            }
            if (teleportGraphicFlipX)
            {
                currentTeleportGraphic.GetComponent <SpriteRenderer> ().flipX = directionY < 0;
            }
            ;
        }
    }