public void OnClick() { if (this.count == 0) { return; } if (controller.isHoldingPart) { return; } if (slot == Slot.body) { RobotType type; if (Enum.TryParse <RobotType>(itemName.text, out type)) { inventory.bodies[type]--; GameObject body = Instantiate(list.botBodyPrefab); body.GetComponent <BotBody>().robotType = type; body.GetComponent <BotBody>().isBroken = false; body.GetComponent <SpriteRenderer>().sprite = body.GetComponent <BotBody>().GetSprite(); list.BuildBodyList(inventory.bodies); controller.TakePart(body); list.InTrigger(false); } } else if (slot == Slot.head) { HeadType type; if (Enum.TryParse <HeadType>(itemName.text, out type)) { inventory.heads[type]--; GameObject head = Instantiate(list.botHeadPrefab); head.GetComponent <BotHead>().headType = type; head.GetComponent <BotHead>().isBroken = false; head.GetComponent <SpriteRenderer>().sprite = head.GetComponent <BotHead>().GetSprite(); list.BuildHeadList(inventory.heads); controller.TakePart(head); list.InTrigger(false); } } else if (slot == Slot.leftArm || slot == Slot.rightArm) { ArmType type; if (Enum.TryParse <ArmType>(itemName.text, out type)) { inventory.arms[type]--; GameObject arm = Instantiate(list.armPrefab); arm.GetComponent <BotArm>().armType = type; arm.GetComponent <BotArm>().isBroken = false; arm.GetComponent <SpriteRenderer>().sprite = arm.GetComponent <BotArm>().GetSprite(); list.BuildArmsList(inventory.arms); controller.TakePart(arm); list.InTrigger(false); } } else if (slot == Slot.leftLeg || slot == Slot.rightLeg) { LegType type; if (Enum.TryParse <LegType>(itemName.text, out type)) { inventory.legs[type]--; GameObject leg = Instantiate(list.legPrefab); leg.GetComponent <BotLeg>().legType = type; leg.GetComponent <BotLeg>().isBroken = false; leg.GetComponent <SpriteRenderer>().sprite = leg.GetComponent <BotLeg>().GetSprite(); list.BuildLegsList(inventory.legs); controller.TakePart(leg); list.InTrigger(false); } } }
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; } } }