示例#1
0
    private void OnTriggerEnter2D(Collider2D collision)
    {
        // (meme) solution to mouse hitting trigger twice while turning
        if (m_state == MouseState.Turn)
        {
            return;
        }

        // not sure if we want this:
        // Increment this direction's box to dissuade backtracking the maze
        if (!trigger_map.ContainsKey(collision.name))
        {
            trigger_map.Add(collision.name, 1);
        }
        else
        {
            trigger_map[collision.name]++;
        }

        DialogueTrigger dt = collision.gameObject.GetComponent <DialogueTrigger>();

        if (dt != null)
        {
            if (!dt.activated)
            {
                dt.Activate();
                m_state     = MouseState.InDialogue;
                rb.velocity = Vector2.zero;
            }
            return;
        }

        ChangeLevelTrigger changer = collision.gameObject.GetComponent <ChangeLevelTrigger>();

        if (changer != null)
        {
            victory_sound.PlayThis();
            changer.loadNextLevel();
            m_state     = MouseState.Wait;
            rb.velocity = Vector2.zero;
            return;
        }

        // make the mouse choose a new path semi-randomly
        // raycast forward, left, and right to find the 3 directions he can go
        List <RaycastHit2D> collider_hits = new List <RaycastHit2D>();

        // check in front of mouse, and to left and right of mouse
        Vector2[]    raycast_directions = { transform.up, -1.0f * transform.right, transform.right };
        RaycastHit2D hit;
        float        trigger_size = collision.GetComponent <BoxCollider2D>().size.y;
        float        rand_total   = 0f;

        foreach (Vector2 raycast_dir in raycast_directions)
        {
            // raycast from side of trigger box outwards
            hit = Physics2D.Raycast(collision.transform.position + (Vector3)raycast_dir * trigger_size, raycast_dir);
            if (hit.collider.tag == "MazeTrigger")
            {
                collider_hits.Add(hit);
                if (!trigger_map.ContainsKey(hit.collider.name))
                {
                    rand_total += 1.0f;
                    trigger_map.Add(hit.collider.name, 1);
                }
                else
                {
                    rand_total += 1.0f / trigger_map[hit.collider.name];
                }
            }
        }

        if (collider_hits.Count == 0)
        {
            // have to turn around
            rb.velocity = new Vector2(0, 0);
            transform.Rotate(Vector3.forward * 180);
            direction *= -1.0f;
            return;
        }


        Random rnd = new Random();

        float place = Random.Range(0.0f, rand_total);

        // FOR DEBUGGING PLEASE REMEMBER TO REMOVE
        //place = 0;

        float        cur_place   = 0.0f;
        RaycastHit2D next_target = collider_hits[0];

        for (int i = 0; i < collider_hits.Count; ++i)
        {
            cur_place += 1.0f / trigger_map[collider_hits[i].collider.name];
            if (place < cur_place)
            {
                next_target = collider_hits[i];
                break;
            }
        }

        Vector2 next_direction = (next_target.transform.position - collision.transform.position).normalized;

        if (direction != next_direction)
        {
            // we are going to need to turn

            // update the direction that we are going in
            direction = (next_target.transform.position - collision.transform.position);
            direction.Normalize();

            if (Vector2.Dot(direction, Vector2.up) == 0)
            {
                snap_float = next_target.transform.position.y;
            }
            else
            {
                snap_float = next_target.transform.position.x;
            }


            Invoke("swap_to_turn", turn_delay);
        }
    }