Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections.Generic;
- public class InteractionSystem : MonoBehaviour
- {
- 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;
- private InventorySystem inventorySystem;
- private PlayerMovement playerMovement;
- private bool isPrecisionDrop = false;
- private Dictionary<GameObject, int> originalLayers = new Dictionary<GameObject, int>();
- private float lastPreviewUpdateTime;
- private bool isRotating = false;
- private Vector3 currentRotation;
- private List<GameObject> previewObjects = new List<GameObject>();
- private void Start()
- {
- inventorySystem = InventorySystem.Instance;
- playerMovement = GetComponent<PlayerMovement>();
- UpdateItemVisibility();
- }
- private void Update()
- {
- if (isPrecisionDrop)
- {
- UpdatePlacementPreview();
- HandleRotation();
- }
- else
- {
- ClearPreviewObjects();
- }
- }
- public void TryPickUpItem(InteractableObject interactable)
- {
- Item item = interactable.item;
- bool isLeftHand = inventorySystem.rightHandItem != null && inventorySystem.leftHandItem == null;
- if (item.isTwoHanded)
- {
- if (inventorySystem.leftHandItem == null && inventorySystem.rightHandItem == null)
- {
- EquipItem(item, true, true);
- }
- else
- {
- Debug.Log("Cannot pick up two-handed item. Hands are not empty.");
- }
- }
- else if (inventorySystem.rightHandItem == null || isLeftHand)
- {
- EquipItem(item, isLeftHand, false);
- }
- else
- {
- Debug.Log("Cannot pick up item. Both hands are full.");
- }
- }
- 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);
- }
- }
- 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);
- Destroy(previewObj.GetComponent<Rigidbody>());
- Destroy(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.blue;
- outline.OutlineWidth = 5f;
- previewObjects.Add(previewObj);
- previewObj.SetActive(true); // Ensure the preview object is active
- }
- bool canPlace = !Physics.Raycast(previewPosition, Vector3.down, placementHeightOffset + 0.05f, placementLayerMask);
- foreach (var obj in previewObjects)
- {
- obj.GetComponent<Outline>().OutlineColor = canPlace ? Color.blue : Color.blue;
- }
- }
- else
- {
- ClearPreviewObjects();
- }
- }
- private void HandleRotation()
- {
- if (Input.GetKeyDown(KeyCode.R))
- {
- isRotating = !isRotating;
- if (isRotating)
- {
- currentRotation = previewObjects[0].transform.rotation.eulerAngles;
- UIManager.Instance.ShowInventoryPrompt(null);
- UIManager.Instance.UpdatePromptText("Rotation mode enabled. Use Q/E to rotate horizontally, Z/C to rotate vertically.");
- UIManager.Instance.ShowOkayButton();
- }
- else
- {
- UIManager.Instance.ShowInventoryPrompt(null);
- UIManager.Instance.UpdatePromptText("Rotation mode disabled.");
- UIManager.Instance.ShowOkayButton();
- }
- }
- if (isRotating && previewObjects.Count > 0)
- {
- if (Input.GetKeyDown(KeyCode.Q))
- {
- currentRotation.y -= rotationIncrement;
- }
- else if (Input.GetKeyDown(KeyCode.E))
- {
- currentRotation.y += rotationIncrement;
- }
- else if (Input.GetKeyDown(KeyCode.Z))
- {
- currentRotation.x -= rotationIncrement;
- }
- else if (Input.GetKeyDown(KeyCode.C))
- {
- currentRotation.x += rotationIncrement;
- }
- foreach (var obj in previewObjects)
- {
- obj.transform.rotation = Quaternion.Euler(currentRotation);
- }
- }
- }
- public void DropItem(bool isLeftHand)
- {
- Item itemToDrop = isLeftHand ? inventorySystem.leftHandItem : inventorySystem.rightHandItem;
- if (itemToDrop != null)
- {
- GameObject itemObject = itemToDrop.gameObject;
- itemObject.transform.SetParent(null);
- Vector3 dropPosition;
- if (isPrecisionDrop && previewObjects.Count > 0)
- {
- int index = isLeftHand ? 0 : (previewObjects.Count > 1 ? 1 : 0);
- dropPosition = previewObjects[index].transform.position;
- itemObject.transform.position = dropPosition;
- itemObject.transform.rotation = previewObjects[index].transform.rotation;
- }
- else
- {
- dropPosition = transform.position + transform.forward * 1f + transform.right * Random.Range(-0.5f, 0.5f);
- itemObject.transform.position = dropPosition;
- 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);
- RestoreOriginalLayer(itemObject);
- InteractableObject interactable = itemObject.GetComponent<InteractableObject>();
- if (interactable != null)
- {
- interactable.enabled = true;
- }
- playerMovement.UpdateCarryingAnimations();
- }
- ClearPreviewObjects();
- if (inventorySystem.leftHandItem == null && inventorySystem.rightHandItem == null)
- {
- isPrecisionDrop = false;
- isRotating = false;
- }
- UpdateItemVisibility();
- }
- public void DropBothItems()
- {
- DropItem(true);
- DropItem(false);
- }
- private void ChangeItemLayer(GameObject itemObject, LayerMask newLayer)
- {
- if (!originalLayers.ContainsKey(itemObject))
- {
- originalLayers[itemObject] = itemObject.layer;
- }
- itemObject.layer = (int)Mathf.Log(newLayer.value, 2);
- }
- private void RestoreOriginalLayer(GameObject itemObject)
- {
- if (originalLayers.ContainsKey(itemObject))
- {
- itemObject.layer = originalLayers[itemObject];
- originalLayers.Remove(itemObject);
- }
- }
- public void TogglePrecisionDrop()
- {
- isPrecisionDrop = !isPrecisionDrop;
- UpdateItemVisibility();
- UIManager.Instance.ShowInventoryPrompt(null);
- UIManager.Instance.UpdatePromptText(isPrecisionDrop ? "Precision drop enabled" : "Precision drop disabled");
- UIManager.Instance.ShowOkayButton();
- }
- private void ClearPreviewObjects()
- {
- foreach (var obj in previewObjects)
- {
- Destroy(obj);
- }
- previewObjects.Clear();
- }
- public bool IsPrecisionDropEnabled()
- {
- return isPrecisionDrop;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement