public void PurchaseProduct(Transform product)
    {
        if (product.CompareTag("Product"))
        {
            StockItem stockItem = product.GetComponent <StockItem>();

            if (stockItem)
            {
                if (UnityEngine.SceneManagement.SceneManager.GetActiveScene().name != "MainMenu")
                {
                    gameManager.AddScore(mapManager.GetStockTypePrice(stockItem.GetStockType()));
                }

                if (gameInfo)
                {
                    gameInfo.AddProductSold(stockItem.GetStockType());
                }

                PushPriceToaster(mapManager.GetStockTypePrice(stockItem.GetStockType()));

                // Delete sold product
                product.SetParent(null);
                Destroy(product.gameObject);
            }
            else
            {
                Debug.LogError("Product is missing the StockItem component!", stockItem);
            }
        }
    }
    private void OnCollisionStay(Collision collision)
    {
        foreach (ContactPoint contactPoint in collision.contacts)
        {
            Transform other = contactPoint.otherCollider.transform.root;

            int result = 0;

            StockItem stockItem = other.GetComponent <StockItem>();

            if (other.CompareTag("Product") || other.CompareTag("StockCrate"))
            {
                // Claim the product before any other shelf can >:D
                if (stockItem.IsClaimed())
                {
                    return;
                }
                else
                {
                    stockItem.ClaimItem(gameObject);
                }

                result = AddStock(stockItem.GetStockType());
            }

            Debug.Log(result);

            if (result == 0)
            {
                Destroy(other.gameObject);
            }
            else
            {
                // Unclaim item
                stockItem.UnclaimItem(gameObject);

                // Get the damn item out of this shelf
                if (allowPickupF)
                {
                    other.transform.position = boundsOfShelf.center + (transform.forward * 0.5f);
                }
                else if (allowPickupB)
                {
                    other.transform.position = boundsOfShelf.center + (transform.forward * -0.5f);
                }

                else if (allowPickupL)
                {
                    other.transform.position = boundsOfShelf.center + (transform.forward * -0.5f);
                }
                else
                {
                    other.transform.position = boundsOfShelf.center + (transform.right * 0.5f);
                }
            }
        }
    }
    private void OnCollisionEnter(Collision collision)
    {
        ContactPoint contact = collision.GetContact(0);

        GameObject other = contact.otherCollider.transform.root.gameObject;

        //Debug.Log(collision.relativeVelocity.magnitude);

        // Add products to the shelf if it hits it hard enough
        if (collision.relativeVelocity.magnitude < COLLISION_SENSITIVITY)
        {
            return;
        }

        StockItem stockItem = other.GetComponent <StockItem>();
        int       result    = 0;

        if (other.CompareTag("Product") || other.CompareTag("StockCrate"))
        {
            // Claim the product before any other shelf can >:D
            if (stockItem.IsClaimed())
            {
                return;
            }
            else
            {
                stockItem.ClaimItem(gameObject);
            }

            result = AddStock(stockItem.GetStockType());
        }

        Debug.Log(result);

        if (result == 0)
        {
            Destroy(other);
        }
        else
        {
            stockItem.UnclaimItem(gameObject);
        }
    }
Ejemplo n.º 4
0
    protected virtual void OnCollisionEnter_UE(Collision collision)
    {
        ContactPoint contact = collision.GetContact(0);

        GameObject other = contact.otherCollider.transform.root.gameObject;

        //Debug.Log(collision.relativeVelocity.magnitude);

        // Add products to the shelf if it hits it hard enough
        if (collision.relativeVelocity.magnitude < stateMachine.collisionSensitivity)
        {
            return;
        }

        StockItem stock = other.GetComponent <StockItem>();

        switch (other.tag)
        {
        case "Product":
        {
            // Double check if StockItem exists
            stock = other.GetComponent <StockItem>();
            if (stock)
            {
                // Check if stock matches the current wanted product
                if (stock.GetStockType() == stateMachine.currentWantedProduct)
                {
                    // Equip cought product
                    stateMachine.EquipItem(other.transform);
                }
            }
            break;
        }

        case "StockCrate":
        {
            // Double check if StockCrate exists
            StockCrate crate = stock as StockCrate;
            if (crate)
            {
                // Claim crate
                crate.ClaimItem(stateMachine.gameObject);

                /* Check if stock in crate matches the current wanted
                 * product and if there is enough in the crate to take one
                 */
                if (crate.GetStockType() == stateMachine.currentWantedProduct && crate.GetQuantity() >= 1)
                {
                    // Deduct one stock from the crate
                    crate.SetQuantity(crate.GetQuantity() - 1);

                    // Make new stock and add to customer
                    GameObject newStock = Object.Instantiate(stateMachine.mapManager.GetStockTypePrefab(stateMachine.currentWantedProduct)) as GameObject;

                    // Equip the new stock
                    stateMachine.EquipItem(newStock.transform);
                }

                // Unclaim crate
                crate.UnclaimItem(stateMachine.gameObject);
            }
            break;
        }
        }
    }