Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections.Generic;
- public class InteractionSystem : MonoBehaviour
- {
- public static InteractionSystem Instance { get; private set; }
- public Transform leftObjectHolder;
- public Transform rightObjectHolder;
- public LayerMask pickupLayer;
- public float placementDistance = 5f;
- public LayerMask placementLayerMask;
- public float previewUpdateInterval = 0.1f;
- public float placementHeightOffset = 0.1f;
- public float rotationIncrement = 45f;
- public Transform backpackWearPosition;
- private InventorySystem inventorySystem;
- private PlayerMovement playerMovement;
- private UIManager uiManager;
- private bool isPrecisionDrop = false;
- private bool isRotating = false;
- private Vector3 currentRotation;
- private List<GameObject> previewObjects = new List<GameObject>();
- private float lastPreviewUpdateTime;
- private bool blockPickup = false;
- public Item currentBackpackItem;
- private void Start()
- {
- Instance = this;
- inventorySystem = InventorySystem.Instance;
- playerMovement = GetComponent<PlayerMovement>();
- uiManager = UIManager.Instance;
- UpdateItemVisibility();
- }
- private void Update()
- {
- if (Input.GetKeyDown(KeyCode.Tab))
- {
- if (uiManager.isInventoryOpen)
- {
- uiManager.CloseInventory();
- }
- else if (inventorySystem.IsBackpackEquipped)
- {
- uiManager.OpenInventory(inventorySystem.backpack);
- }
- }
- // Only allow these if the prompt is NOT open
- if (isPrecisionDrop && !uiManager.inventoryPrompt.activeSelf)
- {
- UpdatePlacementPreview();
- if (Input.GetKeyDown(KeyCode.R))
- {
- isRotating = !isRotating;
- uiManager.ShowRotationMessage(isRotating);
- if (isRotating && previewObjects.Count > 0)
- currentRotation = previewObjects[0].transform.rotation.eulerAngles;
- }
- if (isRotating && previewObjects.Count > 0)
- {
- if (Input.GetKey(KeyCode.Q))
- currentRotation.y -= rotationIncrement * Time.deltaTime;
- if (Input.GetKey(KeyCode.E))
- currentRotation.y += rotationIncrement * Time.deltaTime;
- if (Input.GetKey(KeyCode.Z))
- currentRotation.x -= rotationIncrement * Time.deltaTime;
- if (Input.GetKey(KeyCode.C))
- currentRotation.x += rotationIncrement * Time.deltaTime;
- foreach (var obj in previewObjects)
- obj.transform.rotation = Quaternion.Euler(currentRotation);
- }
- // F - Place both hands
- if (Input.GetKeyDown(KeyCode.F))
- {
- PlaceHandItem(true); // left
- PlaceHandItem(false); // right
- isPrecisionDrop = false;
- isRotating = false;
- ClearPreviewObjects();
- UpdateItemVisibility();
- }
- // H - Place left hand only
- else if (Input.GetKeyDown(KeyCode.H))
- {
- PlaceHandItem(true);
- UpdatePreviewAfterSingleHandDrop();
- }
- // G - Place right hand only
- else if (Input.GetKeyDown(KeyCode.G))
- {
- PlaceHandItem(false);
- UpdatePreviewAfterSingleHandDrop();
- }
- }
- else if (!isPrecisionDrop)
- {
- ClearPreviewObjects();
- }
- if (Input.GetKeyDown(KeyCode.E) && currentBackpackItem != null && !inventorySystem.IsBackpackEquipped)
- {
- WearBackpack();
- }
- }
- private void PlaceHandItem(bool isLeftHand)
- {
- Item itemToDrop = isLeftHand ? inventorySystem.leftHandItem : inventorySystem.rightHandItem;
- if (itemToDrop == null) return;
- int previewIndex = isLeftHand ? 0 : (previewObjects.Count > 1 ? 1 : 0);
- if (previewObjects.Count == 0) return;
- GameObject itemObject = itemToDrop.gameObject;
- itemObject.transform.SetParent(null);
- itemObject.transform.position = previewObjects[previewIndex].transform.position;
- itemObject.transform.rotation = previewObjects[previewIndex].transform.rotation;
- itemObject.SetActive(true);
- Rigidbody rb = itemObject.GetComponent<Rigidbody>();
- if (rb == null) rb = itemObject.AddComponent<Rigidbody>();
- rb.isKinematic = false;
- rb.useGravity = true;
- inventorySystem.UnequipItem(isLeftHand);
- InteractableObject interactable = itemObject.GetComponent<InteractableObject>();
- if (interactable != null) interactable.enabled = true;
- }
- // After dropping one hand, update preview for the remaining item (if any)
- private void UpdatePreviewAfterSingleHandDrop()
- {
- UpdateItemVisibility();
- ClearPreviewObjects();
- // If still holding an item, show preview for it
- if (inventorySystem.leftHandItem != null || inventorySystem.rightHandItem != null)
- {
- UpdatePlacementPreview();
- }
- else
- {
- isPrecisionDrop = false;
- isRotating = false;
- ClearPreviewObjects();
- UpdateItemVisibility();
- }
- }
- private void PlaceItems()
- {
- for (int i = 0; i < previewObjects.Count; i++)
- {
- Item itemToDrop = null;
- if (i == 0) itemToDrop = inventorySystem.leftHandItem;
- if (i == 1) itemToDrop = inventorySystem.rightHandItem;
- if (itemToDrop != null)
- {
- GameObject itemObject = itemToDrop.gameObject;
- itemObject.transform.SetParent(null);
- itemObject.transform.position = previewObjects[i].transform.position;
- itemObject.transform.rotation = previewObjects[i].transform.rotation;
- itemObject.SetActive(true);
- Rigidbody rb = itemObject.GetComponent<Rigidbody>();
- if (rb == null) rb = itemObject.AddComponent<Rigidbody>();
- rb.isKinematic = false;
- rb.useGravity = true;
- inventorySystem.UnequipItem(i == 0);
- InteractableObject interactable = itemObject.GetComponent<InteractableObject>();
- if (interactable != null) interactable.enabled = true;
- }
- }
- ClearPreviewObjects();
- isPrecisionDrop = false;
- isRotating = false;
- UpdateItemVisibility();
- // Optionally, show a "Precision drop disabled" message:
- // uiManager.ShowPrecisionDropMessage(false);
- blockPickup = true;
- StartCoroutine(AllowPickupAfterFrame());
- }
- private System.Collections.IEnumerator AllowPickupAfterFrame()
- {
- yield return null; // wait one frame
- blockPickup = false;
- }
- public void TogglePrecisionDrop()
- {
- if (!isPrecisionDrop &&
- inventorySystem.leftHandItem == null &&
- inventorySystem.rightHandItem == null)
- {
- Debug.Log("No items to place");
- return;
- }
- isPrecisionDrop = !isPrecisionDrop;
- isRotating = false;
- if (isPrecisionDrop)
- {
- uiManager.ShowPrecisionDropMessage(true);
- }
- else
- {
- uiManager.ShowPrecisionDropMessage(false);
- ClearPreviewObjects();
- }
- UpdateItemVisibility();
- }
- public void DropItem(bool isLeftHand)
- {
- Item itemToDrop = isLeftHand ? inventorySystem.leftHandItem : inventorySystem.rightHandItem;
- if (itemToDrop != null)
- {
- GameObject itemObject = itemToDrop.gameObject;
- itemObject.transform.SetParent(null);
- itemObject.transform.position = transform.position + transform.forward * 1f + transform.right * Random.Range(-0.5f, 0.5f);
- itemObject.transform.rotation = Random.rotation;
- itemObject.SetActive(true);
- Rigidbody rb = itemObject.GetComponent<Rigidbody>();
- if (rb == null) rb = itemObject.AddComponent<Rigidbody>();
- rb.isKinematic = false;
- rb.useGravity = true;
- rb.AddForce(Vector3.down * 2f, ForceMode.Impulse);
- inventorySystem.UnequipItem(isLeftHand);
- InteractableObject interactable = itemObject.GetComponent<InteractableObject>();
- if (interactable != null)
- {
- interactable.enabled = true;
- }
- playerMovement.UpdateCarryingAnimations();
- }
- UpdateItemVisibility();
- }
- public void DropBothItems()
- {
- DropItem(true);
- DropItem(false);
- }
- // === END PUBLIC METHODS ===
- private void UpdatePlacementPreview()
- {
- if (Time.time - lastPreviewUpdateTime < previewUpdateInterval) return;
- lastPreviewUpdateTime = Time.time;
- RaycastHit hit;
- if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, placementDistance, placementLayerMask))
- {
- List<Item> itemsToPlace = new List<Item>();
- if (inventorySystem.leftHandItem != null) itemsToPlace.Add(inventorySystem.leftHandItem);
- if (inventorySystem.rightHandItem != null) itemsToPlace.Add(inventorySystem.rightHandItem);
- ClearPreviewObjects();
- Vector3 previewPosition = hit.point + hit.normal * (0.05f + placementHeightOffset);
- Quaternion previewRotation = !isRotating ? Quaternion.LookRotation(hit.normal, Vector3.up) : Quaternion.Euler(currentRotation);
- for (int i = 0; i < itemsToPlace.Count; i++)
- {
- GameObject previewObj = Instantiate(itemsToPlace[i].gameObject);
- DestroyImmediate(previewObj.GetComponent<Rigidbody>());
- DestroyImmediate(previewObj.GetComponent<Collider>());
- previewObj.AddComponent<Outline>();
- previewObj.transform.position = previewPosition + Vector3.right * (i * 0.5f);
- previewObj.transform.rotation = previewRotation;
- Outline outline = previewObj.GetComponent<Outline>();
- outline.OutlineMode = Outline.Mode.OutlineAll;
- outline.OutlineColor = Color.green;
- outline.OutlineWidth = 5f;
- previewObjects.Add(previewObj);
- previewObj.SetActive(true);
- }
- }
- else
- {
- ClearPreviewObjects();
- }
- }
- private void ClearPreviewObjects()
- {
- foreach (var obj in previewObjects)
- {
- Destroy(obj);
- }
- previewObjects.Clear();
- }
- public void TryPickUpItem(InteractableObject interactable)
- {
- if (blockPickup) return;
- Item item = interactable.item;
- if (item.isBackpack && !inventorySystem.IsBackpackEquipped)
- {
- PickUpBackpack(item);
- }
- else
- {
- bool isLeftHand = inventorySystem.rightHandItem != null && inventorySystem.leftHandItem == null;
- EquipItem(item, isLeftHand, item.isTwoHanded);
- playerMovement.UpdateCarryingAnimations();
- // Explicitly ensure precision drop is OFF when picking up new items
- if (isPrecisionDrop)
- {
- isPrecisionDrop = false;
- uiManager.ShowPrecisionDropMessage(false);
- ClearPreviewObjects();
- }
- }
- }
- private void PickUpBackpack(Item backpackItem)
- {
- bool isLeftHand = inventorySystem.rightHandItem != null && inventorySystem.leftHandItem == null;
- EquipItem(backpackItem, isLeftHand, false);
- currentBackpackItem = backpackItem;
- uiManager.ShowBackpackPrompt(backpackItem);
- }
- public void WearBackpack()
- {
- if (currentBackpackItem != null)
- {
- bool wasLeftHand = inventorySystem.leftHandItem == currentBackpackItem;
- inventorySystem.UnequipItem(wasLeftHand);
- currentBackpackItem.transform.SetParent(backpackWearPosition);
- currentBackpackItem.transform.localPosition = Vector3.zero;
- currentBackpackItem.transform.localRotation = Quaternion.identity;
- Destroy(currentBackpackItem.GetComponent<Rigidbody>());
- MeshCollider meshCollider = currentBackpackItem.GetComponent<MeshCollider>();
- if (meshCollider != null) meshCollider.enabled = false;
- inventorySystem.EquipBackpack(currentBackpackItem);
- currentBackpackItem = null;
- uiManager.ClosePrompt();
- playerMovement.UpdateCarryingAnimations();
- uiManager.UpdateBackpackButtons(true);
- }
- }
- public void UnequipBackpack()
- {
- if (inventorySystem.IsBackpackEquipped)
- {
- Item backpack = inventorySystem.backpack;
- inventorySystem.UnequipBackpack();
- MeshCollider meshCollider = backpack.GetComponent<MeshCollider>();
- if (meshCollider != null) meshCollider.enabled = true;
- if (backpack.GetComponent<Rigidbody>() == null)
- {
- backpack.gameObject.AddComponent<Rigidbody>();
- }
- backpack.transform.SetParent(null);
- backpack.transform.position = transform.position + transform.forward * 1.5f;
- playerMovement.UpdateCarryingAnimations();
- uiManager.UpdateBackpackButtons(false);
- }
- }
- private void EquipItem(Item item, bool isLeftHand, bool isTwoHanded)
- {
- inventorySystem.EquipItem(item, isLeftHand, isTwoHanded);
- UpdateItemPosition(item.gameObject, isLeftHand);
- ChangeItemLayer(item.gameObject, pickupLayer);
- playerMovement.UpdateCarryingAnimations();
- }
- public void UpdateItemPosition(GameObject itemObject, bool isLeftHand)
- {
- Transform objectHolder = isLeftHand ? leftObjectHolder : rightObjectHolder;
- Item item = itemObject.GetComponent<Item>();
- Vector3 positionOffset = isLeftHand ? item.leftPositionOffset : item.rightPositionOffset;
- Vector3 rotationOffset = isLeftHand ? item.leftRotationOffset : item.rightRotationOffset;
- itemObject.transform.SetParent(objectHolder);
- itemObject.transform.localPosition = positionOffset;
- itemObject.transform.localRotation = Quaternion.Euler(rotationOffset);
- }
- private void UpdateItemVisibility()
- {
- if (inventorySystem.leftHandItem != null)
- {
- inventorySystem.leftHandItem.gameObject.SetActive(!isPrecisionDrop);
- }
- if (inventorySystem.rightHandItem != null)
- {
- inventorySystem.rightHandItem.gameObject.SetActive(!isPrecisionDrop);
- }
- }
- public void ChangeItemLayer(GameObject itemObject, LayerMask newLayer)
- {
- itemObject.layer = (int)Mathf.Log(newLayer.value, 2);
- }
- public bool IsPrecisionDropEnabled()
- {
- return isPrecisionDrop;
- }
- // Optional: expose rotation toggle if PlayerMovement needs it
- public void ToggleRotationMode()
- {
- if (isPrecisionDrop)
- {
- isRotating = !isRotating;
- uiManager.ShowRotationMessage(isRotating);
- if (isRotating && previewObjects.Count > 0)
- currentRotation = previewObjects[0].transform.rotation.eulerAngles;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment