Exemple #1
0
    private IEnumerator LayDownCorutine(LayDownCorutineArgs args)
    {
        // Get real index [this is copied code, maybe use a function]
        if (args.fromIndex < 0)
        {
            args.fromIndex = _spots.Count - 1;
        }
        args.fromIndex = Mathf.Clamp(args.fromIndex, 0, _spots.Count);

        // Generate transform data
        List <TransformData> transformDatas = style.GetCardsTransformData(_spots.Count);

        // Start from the args.fromIndex, then gradually go to the cards on its side. Set the
        // transform of this cards by getting it from the deck style and the smoothly move them
        // using the smooth movements component
        int sideIndex = 0;

        while (sideIndex <= _spots.Count - 1)
        {
            if (sideIndex == 0)
            {
                // Initial card
                ApplyDeckStyleTransformToCard(args.fromIndex, transformDatas[args.fromIndex]);
            }
            else
            {
                if (args.fromIndex - sideIndex >= 0)
                {
                    // Card before
                    ApplyDeckStyleTransformToCard(args.fromIndex - sideIndex, transformDatas[args.fromIndex - sideIndex]);
                }
                if (args.fromIndex + sideIndex <= _spots.Count - 1)
                {
                    // Card after
                    ApplyDeckStyleTransformToCard(args.fromIndex + sideIndex, transformDatas[args.fromIndex + sideIndex]);
                }
            }

            sideIndex++;

            if (args.gradually)
            {
                yield return(new WaitForSeconds(_timeForGradually));
            }
        }

        // Set layer order (from the card component) again (or the z pos will be 0)
        // [TODO: DO NOT REPEAT]
        for (int i = 0; i < _spots.Count; i++)
        {
            // Set layer order (from the card component)
            TabletopCard tc = _spots[i].card.GetComponentInChildren <TabletopCard>();
            if (tc != null)
            {
                tc.SetLayerOrder(i);
            }
        }

        yield return(null);
    }
Exemple #2
0
    /// <summary>
    /// Laies down cards.
    /// </summary>
    /// <param name="fromIndex">First card to lay down. If <c>-1</c> the first card is the top one.</param>
    /// <param name="gradually">If set to <c>false</c> all cards are positioned at the same time.</param>
    public void LayDown(int fromIndex = 0, bool gradually = true)
    {
        UpdateDropAreas();

        // Stop if there is no deck style
        if (style == null)
        {
            Debug.Log("Cannot lay down cards of \"" + gameObject.name + "\": deck style missing.");
            return;
        }

        // Set correct z index before anything else
        for (int i = 0; i < _spots.Count; i++)
        {
            // Set layer order (from the card component)
            TabletopCard tc = _spots[i].card.GetComponentInChildren <TabletopCard>();
            if (tc != null)
            {
                tc.SetLayerOrder(i);
            }
        }

        // Apply transform
        LayDownCorutineArgs args;

        args.fromIndex = fromIndex;
        args.gradually = gradually;
        StopCoroutine("LayDownCorutine");
        StartCoroutine("LayDownCorutine", args);
    }
Exemple #3
0
    void Update()
    {
        SetPositionAsMousePosition();

        // Grab a card
        if (_card == null && Input.GetMouseButtonDown(0))
        {
            RaycastHit2D[] hits = Physics2D.RaycastAll(transform.position, Vector2.zero);

            foreach (RaycastHit2D hit in hits)
            {
                // Check ray collision
                GameObject collided = hit.collider.gameObject;

                // If it's a card
                if (collided.GetComponent <TabletopCard>() != null)
                {
                    _card = collided;

                    // Reset rotation
                    _card.GetComponent <SmoothAnimations>().SmoothRotate(Quaternion.identity);

                    break;
                }
            }
        }

        // If holding a card, set the card position as finger position
        if (_card != null && Input.GetMouseButton(0))
        {
            _card.transform.position = transform.position;

            // You can also flip it
            if (Input.GetKeyDown("f"))
            {
                TabletopCard tc = _card.GetComponent <TabletopCard>();
                tc.faceUp = !tc.faceUp;
            }
        }

        // Release card
        if (_card != null && Input.GetMouseButtonUp(0))
        {
            // Remove from original deck
            TabletopCard tc = _card.GetComponent <TabletopCard>();
            if (tc != null)
            {
                GameObject originalDeck = tc.deck;
                if (originalDeck != null)
                {
                    originalDeck.GetComponent <TabletopDeck>().RemoveCard(_card);
                }
            }

            // Cast a ray
            RaycastHit2D[] hits = Physics2D.RaycastAll(transform.position, Vector2.zero);

            foreach (RaycastHit2D hit in hits)
            {
                // Check ray collision
                GameObject collided = hit.collider.gameObject;

                if (collided.name == "Drop_Area")
                {
                    TabletopDeck newDeck = collided.transform.parent.GetComponent <TabletopDeck>();
                    newDeck.AddCard(_card, collided);

                    break;
                }
            }

            _card = null;
        }

        // If not holding a card, flip a hovered card
        if (_card == null && Input.GetKeyDown("f"))
        {
            // Cast a ray
            RaycastHit2D[] hits = Physics2D.RaycastAll(transform.position, Vector2.zero);

            foreach (RaycastHit2D hit in hits)
            {
                // Check ray collision
                TabletopCard c = hit.collider.gameObject.GetComponent <TabletopCard>();

                // If it's a card
                if (c != null)
                {
                    c.faceUp = !c.faceUp;

                    break;
                }
            }
        }
    }