123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.InputSystem;
-
- public class BlockSpawner : MonoBehaviour
- {
- public Inventory inventory;
- public Shader previewShader;
-
- private Shader _initialShader;
- #pragma warning disable 0649
- [SerializeField] private UI_Inventory uiInventory;
- #pragma warning restore 0649
- private GameObject _blockInstance;
- private Quaternion _lastRotationState;
-
-
- public float spawnDistance = 3f;
- public bool _blockRotating = false;
-
- private bool _isPreview;
-
-
-
- public void Awake()
- {
- _isPreview = false;
-
- inventory = new Inventory();
- uiInventory.SetInventory(inventory);
- }
-
- public void OnPlaceBlock ()
- {
- if (!_isPreview) return;
- // todo remove in inventory
- _blockInstance.transform.SetParent(null);
- SwitchToDefaultRender();
- _blockInstance = null;
- DisablePreviewMode();
- }
-
- public void OnTogglePreviewMode(InputValue value)
- {
- float valueAsFloat = value.Get<float>();
-
- if (valueAsFloat == 1f) EnablePreviewMode();
- else DisablePreviewMode();
- }
-
- public void OnScroll(InputValue value)
- {
- DisablePreviewMode();
-
- float valueAsFloat = value.Get<float>();
- if (valueAsFloat > 0) inventory.SelectPreviousItem();
- if (valueAsFloat < 0) inventory.SelectNextItem();
- }
-
- public void EnablePreviewMode()
- {
- Item selectedItem = inventory.GetSelectedItem();
-
- if (selectedItem == null) return;
-
- Vector3 spawnPos = Vector3.forward * spawnDistance;
- _blockInstance = Instantiate(selectedItem.Prefab, spawnPos, Quaternion.identity);
- _blockInstance.transform.SetParent(this.transform, false);
- _blockInstance.transform.rotation = _lastRotationState;
- _isPreview = true;
- SwitchToMeshRender();
- }
-
- public void DisablePreviewMode()
- {
- if (_blockInstance != null) Destroy(_blockInstance);
- _isPreview = false;
- }
-
-
- private void SwitchToMeshRender()
- {
- MeshRenderer vRenderer = _blockInstance.GetComponent<MeshRenderer>();
- Material vMaterial = vRenderer.material;
- _initialShader = vMaterial.shader;
- vMaterial.shader = previewShader;
- }
-
- private void SwitchToDefaultRender()
- {
- MeshRenderer vRenderer = _blockInstance.GetComponent<MeshRenderer>();
- Material vMaterial = vRenderer.material;
- vMaterial.shader = _initialShader;
- }
-
- public void OnEnterRotateBlock(InputValue value)
- {
- float valueAsFloat = value.Get<float>();
- Debug.Log(valueAsFloat);
- _blockRotating = true;
- }
-
- public void OnExitRotateBlock(InputValue value)
- {
- float valueAsFloat = value.Get<float>();
- Debug.Log(valueAsFloat);
- _blockRotating = false;
- }
-
- public void OnRotateBlock(InputValue value)
- {
- if (_isPreview && _blockRotating)
- {
- Vector2 iMouseMovement = value.Get<Vector2>();
-
- Transform vTransform = _blockInstance.transform;
- //vTransform.Rotate(iMouseMovement.y / 10, iMouseMovement.x / 10, 0);
- vTransform.RotateAround(vTransform.position, transform.right, iMouseMovement.y / 10.0f);
- vTransform.RotateAround(vTransform.position, transform.up, iMouseMovement.x / 10.0f);
- //vTransform.eulerAngles += vTransform.worldToLocalMatrix.MultiplyVector(new Vector3(iMouseMovement.y / 10, iMouseMovement.x / 10, 0));
- //vTransform.Rotate(iMouseMovement.y/10, iMouseMovement.x/10, 0);
- _lastRotationState = vTransform.rotation;
- }
- }
- }
|