public void RemoveInventroy(RuntimePlaceableInventoryData runtimePlaceableInventoryData) { Destroy(runtimePlaceableInventoryData.itemInstance); runtimePlaceableInventoryDataList.Remove(runtimePlaceableInventoryData); mapData.inventoryPlaceDataList.Remove(runtimePlaceableInventoryData.placeData); }
private void InteractBlock(Vector2 screenPos) { var currentInv = inventoryStorageList.Find(val => val.slotID == currentSelectID)?.inventory; var digSpeedBoost = 1f; if (currentInv is IDigBoost) { var digboost = currentInv as IDigBoost; digSpeedBoost = digboost.GetDigBoost(); } if (currentInv is IAttackable) { var attackable = currentInv as IAttackable; attackable.Attack(player); } digSpeedBoost = 1f / digSpeedBoost; var ray = playerCamera.ScreenPointToRay(screenPos); var isHit = Physics.Raycast(ray, out RaycastHit rayHit, 6, 1 << LayerMask.NameToLayer("Block") | 1 << LayerMask.NameToLayer("Item")); if (isHit) { var chunckPoint = rayHit.point - rayHit.normal * 0.5f; chunckPoint = new Vector3(Mathf.FloorToInt(chunckPoint.x), Mathf.FloorToInt(chunckPoint.y), Mathf.FloorToInt(chunckPoint.z)); var worldManager = rayHit.collider.transform.root.GetComponent <WorldManager> (); IDigable digableItem = null; RuntimePlaceableInventoryData itemData = null; var isBlock = false; var isItem = false; if (rayHit.collider.gameObject.layer == LayerMask.NameToLayer("Block")) { isBlock = true; var blockData = worldManager.GetBlockData((int)chunckPoint.y, (int)chunckPoint.x, (int)chunckPoint.z); digableItem = blockData as IDigable; } if (rayHit.collider.gameObject.layer == LayerMask.NameToLayer("Item")) { isItem = true; itemData = worldManager.GetItemData(rayHit.collider.transform.position); digableItem = itemData.inventory as IDigable; } if (digableItem != null) { //判断 当前挖的与上一帧挖的是否相同 if (interactPos == chunckPoint) { if (interactTime < digableItem.DigTime()) { interactTime += Time.deltaTime; } } else { interactPos = chunckPoint; interactTime = 0; } if (interactTime >= digableItem.DigTime() * digSpeedBoost && interactPos == chunckPoint) { if (isBlock) { worldManager.InteractBlock((int)chunckPoint.y, (int)chunckPoint.x, (int)chunckPoint.z); } else if (isItem) { worldManager.RemoveInventroy(itemData); } digableItem.DropInventory(chunckPoint); } //Update UI if (interactTime > 0.15f) { layout.digProgressBar.SetActive(true); layout.digProgressSlider.value = interactTime / (digableItem.DigTime() * digSpeedBoost); layout.digProgressText.text = $"{((interactTime / (digableItem.DigTime() * digSpeedBoost)) * 100).ToString("f1")} %"; SoundManager.Instance.PlayDigSound(digableItem.DigSound()); } else { layout.digProgressBar.SetActive(false); } } else { layout.digProgressBar.SetActive(false); } } }