Beispiel #1
0
    private BubbleCollision BubbleMarching(Vector2 start, Vector2 dir, float step)
    {
        BubbleCollision result  = null;
        var             current = start;

        if (dir == Vector2.zero)
        {
            return(null);
        }

        while (true)
        {
            current += dir * step;

            if (current.magnitude > 1000)
            {
                return(new BubbleCollision
                {
                    isSticking = false,
                    grid = Local2Grid(current)
                });
            }

            if (CollisionTest(current, dir, out result))
            {
                if (Local2Grid(start) == Local2Grid(Grid2Local(result.grid)))
                {
                    Debug.Log("!!!");
                }

                return(result);
            }
        }
    }
Beispiel #2
0
        void OnBubbleCollision(BubbleCollision e)
        {
            if (e.Bubble != _lastBubble)
            {
                return;
            }

            ReloadGun();
        }
Beispiel #3
0
    private IEnumerator ShootImpl(Vector2 dir, Bubble bubble)
    {
        canShoot = false;

        var waypoints = new List <Vector2>();

        var chained_bubbles  = new List <Bubble>();
        var dropping_bubbles = new List <Bubble>();

        BubbleCollision collision = BubbleMarching(bubble.transform.localPosition, dir, bubbleRadius);

        var current_waypoint = (Vector2)bubble.transform.localPosition;

        waypoints.Add(Grid2Local(collision.grid));

        while (!collision.isSticking)
        {
            dir              = Vector2.Reflect((waypoints.Last() - current_waypoint).normalized, collision.collidingPointNormal);
            collision        = BubbleMarching(waypoints.Last(), dir, bubbleRadius);
            current_waypoint = waypoints.Last();
            waypoints.Add(Grid2Local(collision.grid));
        }

        if (!collision.isSticking)
        {
            var start = bubble.transform.localPosition;

            foreach (var end in waypoints)
            {
                Debug.DrawLine(transform.position + start, transform.position + (Vector3)end, Color.cyan, 5.0f);
                start = end;
            }

            canShoot = true;

            yield break;
        }
        else
        {
            if (collision.grid.y < boardHeightInBubbleRows)
            {
                slots[Grid2Index(collision.grid)].bubble     = bubble;
                slots[Grid2Index(collision.grid)].generation = currentBubbleGeneration;

                var chained_bubble_slots = CollectChainedBubbleSlots((int)collision.grid.x, (int)collision.grid.y, bubble.type);

                if (chained_bubble_slots.Count >= bubbleChainThreshold)
                {
                    foreach (var slot in chained_bubble_slots)
                    {
                        chained_bubbles.Add(slot.bubble);
                        slot.bubble = null;
                    }

                    var dropping_bubble_slots = CollectDroppingBubbleSlots(++currentBubbleGeneration);
                    foreach (var slot in dropping_bubble_slots)
                    {
                        dropping_bubbles.Add(slot.bubble);
                        slot.bubble = null;
                    }
                }
            }
            else
            {
                Debug.Log("You lose!");
            }
        }

        foreach (var current_end in waypoints)
        {
            yield return(StartCoroutine(MoveTo(bubble.transform, current_end, bubbleShootingSpeed)));
        }

        foreach (var cb in chained_bubbles)
        {
            StartCoroutine(Explode(cb));
        }

        foreach (var db in dropping_bubbles)
        {
            StartCoroutine(Drop(db));
        }

        canShoot = true;

        yield break;
    }
Beispiel #4
0
    private bool CollisionTest(Vector3 local, Vector2 dir, out BubbleCollision collision)
    {
        var grid_coordinate = Local2Grid(local);

        collision = null;

        Vector2[] neighbourGridOffsets = ((int)grid_coordinate.y & 1) == 0 ?
                                         neighbourGridOffsetsForEvenRow : neighbourGridOffsetsForOddRow;

        var potential_collisions = new List <KeyValuePair <Vector2, float> >();
        var p2 = new List <KeyValuePair <Vector2, float> >();

        foreach (var offset in neighbourGridOffsets)
        {
            var neighbour_grid_coord  = grid_coordinate + offset;
            var neighbour_local_coord = Grid2Local(neighbour_grid_coord);
            var distance_between_bubble_and_neighbour = Vector2.Distance(local, neighbour_local_coord);

            if (distance_between_bubble_and_neighbour < bubbleRadius * 2 * collideThreshold)
            {
                potential_collisions.Add(new KeyValuePair <Vector2, float>(neighbour_grid_coord,
                                                                           distance_between_bubble_and_neighbour));
                p2.Add(new KeyValuePair <Vector2, float>(neighbour_local_coord, distance_between_bubble_and_neighbour));
            }
        }

        var prioritized_potential_colliders_grid = potential_collisions.OrderBy(t => t.Value).Select(t => t.Key).ToArray();
        var p22 = p2.OrderBy(t => t.Value).Select(t => t.Key).ToArray();

        foreach (var potential_collider_grid in prioritized_potential_colliders_grid)
        {
            var is_colliding_with_upper_wall = potential_collider_grid.y < 0.0f;
            var is_colliding_with_left_wall  = potential_collider_grid.x < 0.0f && dir.x < 0.0f;
            var is_colliding_with_right_wall = potential_collider_grid.x >= bubblesPerRow && dir.x > 0.0f;
            var is_colliding_with_side_walls = is_colliding_with_left_wall || is_colliding_with_right_wall;

            var slot_index = Grid2Index(potential_collider_grid);

            if (is_colliding_with_side_walls)
            {
                // Colliding with a wall...
                collision = new BubbleCollision
                {
                    isSticking           = false,
                    grid                 = grid_coordinate,
                    collidingPointNormal = is_colliding_with_left_wall ? Vector2.right : Vector2.left
                };

                return(true);
            }
            else if (is_colliding_with_upper_wall || IsSlotIndexValid(slot_index) && slots[slot_index].bubble != null)
            {
                // Colliding with another bubble...
                collision = new BubbleCollision
                {
                    isSticking           = true,
                    grid                 = grid_coordinate,
                    collidingPointNormal = Vector3.zero
                };

                return(true);
            }
        }

        return(false);
    }
Beispiel #5
0
    private IEnumerator ShootImpl(Vector2 dir, Bubble bubble)
    {
        canShoot = false;

        var waypoints = new List <Vector2>();

        var chained_bubbles  = new List <Bubble>();
        var dropping_bubbles = new List <Bubble>();

        BubbleCollision collision = BubbleMarching(bubble.transform.localPosition, dir, bubbleRadius);

        var current_waypoint = (Vector2)bubble.transform.localPosition;

        waypoints.Add(Grid2Local(collision.grid));

        //cập nhật lại collsiton nếu collsion từng gắn kết
        // không hiểu đoạn này lắm
        //nói chung là nếu ko có đoạn này thì sau khi cụm bóng bị xóa thì bắn bóng vào cụm này sẽ bug vướng bóng tại vị trị cụm đã bị xóa
        while (!collision.isSticking)
        {
            //gán dir = Vector2.Reflect
            //reflect trả về dội ngược lại của vector tùy theo collidingPointNormal là left, right hay 0
            dir              = Vector2.Reflect((waypoints.Last() - current_waypoint).normalized, collision.collidingPointNormal);
            collision        = BubbleMarching(waypoints.Last(), dir, bubbleRadius);
            current_waypoint = waypoints.Last();
            waypoints.Add(Grid2Local(collision.grid));
        }


        //xét board trứng nhỏ hơn 13
        if (collision.grid.y < boardHeightInBubbleRows)
        {
            slots[Grid2Index(collision.grid)].bubble     = bubble;
            slots[Grid2Index(collision.grid)].generation = currentBubbleGeneration;

            // Lấy những quả bóng cùng màu bị bắn trúng
            var chained_bubble_slots = CollectChainedBubbleSlots((int)collision.grid.x, (int)collision.grid.y, bubble.type);

            // nếu những quả bóng cùng màu lớn hơn 3
            if (chained_bubble_slots.Count >= bubbleChainThreshold)
            {
                var count = 0;
                var same  = 0;
                // lấy quả bóng cùng màu gán vào mảng chained_bubble
                foreach (var slot in chained_bubble_slots)
                {
                    chained_bubbles.Add(slot.bubble);
                    slot.bubble = null;
                    count++;
                }

                /*
                 *  lấy ra những quả bóng mà nằm trong cụm bóng gán vào biến dropping_bubble_slots rồi duyệt mảng rồi gán nó vào mảng dropping_bubbles
                 */
                var dropping_bubble_slots = CollectDroppingBubbleSlots(++currentBubbleGeneration);
                foreach (var slot in dropping_bubble_slots)
                {
                    dropping_bubbles.Add(slot.bubble);
                    slot.bubble = null;
                    same++;
                }
                var total = (count - 1) + same;
                scores += total;
            }
        }//board trứng lớn hơn 13 thì thua cuộc sẽ xử lý hiện ra bảng thua ở đây
        else
        {
            GameOverGUI.SetActive(true);
            Debug.Log("You lose!");
        }
        //if (collision.grid.y==0 )
        //{
        //    PlayGUI.SetActive(true);
        //    Debug.Log("You win!");
        //}

        //đường đi của quả trứng
        foreach (var current_end in waypoints)
        {
            yield return(StartCoroutine(MoveTo(bubble.transform, current_end, bubbleShootingSpeed)));
        }

        // gọi hàm hủy những quả bóng cùng màu trong mảng chained_bubbles
        foreach (var cb in chained_bubbles)
        {
            StartCoroutine(Explode(cb));
        }
        // gọi hàm hủy những quả trứng trong mảng dropping_bubbles
        foreach (var db in dropping_bubbles)
        {
            StartCoroutine(Drop(db));
        }

        canShoot = true;

        yield break;
    }