Example #1
0
    void Interact()
    {
        BoxCollider2D boxCollider2D = GetComponent <BoxCollider2D>();

        ContactFilter2D contactFilter2D = new ContactFilter2D();

        contactFilter2D.layerMask = playerMask;

        Collider2D[] colliders = new Collider2D[1];

        int collisionCount = boxCollider2D.OverlapCollider(contactFilter2D, colliders);

        if (collisionCount > 0)
        {
            Collider2D collider  = colliders[0];
            GameObject playerObj = collider.gameObject;

            PlayerController playerController = playerObj.GetComponent <PlayerController>();
            Inventory        inventory        = playerObj.GetComponent <Inventory>();

            switch (boxType)
            {
            case BoxType.head:
                if (controller.isHoldingPart)
                {
                    inventory.heads[controller.currentHeldObject.GetComponent <BotHead>().headType]++;
                    Destroy(controller.GivePart());
                }
                else
                {
                    assetList.BuildHeadList(inventory.heads);
                    assetList.gameObject.SetActive(assetList.Valid());
                }
                break;

            case BoxType.body:
                if (controller.isHoldingPart)
                {
                    inventory.bodies[controller.currentHeldObject.GetComponent <BotBody>().robotType]++;
                    Destroy(controller.GivePart());
                }
                else
                {
                    assetList.BuildBodyList(inventory.bodies);
                    assetList.gameObject.SetActive(assetList.Valid());
                }
                break;

            case BoxType.arms:
                if (controller.isHoldingPart)
                {
                    inventory.arms[controller.currentHeldObject.GetComponent <BotArm>().armType]++;
                    Destroy(controller.GivePart());
                }
                else
                {
                    assetList.BuildArmsList(inventory.arms);
                    assetList.gameObject.SetActive(assetList.Valid());
                }
                break;

            case BoxType.legs:
                if (controller.isHoldingPart)
                {
                    inventory.legs[controller.currentHeldObject.GetComponent <BotLeg>().legType]++;
                    Destroy(controller.GivePart());
                }
                else
                {
                    assetList.BuildLegsList(inventory.legs);
                    assetList.gameObject.SetActive(assetList.Valid());
                }
                break;

            case BoxType.recycle:
                // TODO
                if (playerController.isHoldingPart)
                {
                    //inventory.binItems.Add(playerController.currentHeldObject);
                    Scrap++;
                    Destroy(playerController.GivePart());
                }
                break;

            default:
                break;
            }
        }
    }