123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.InputSystem;
-
- public class BlockSpawner : MonoBehaviour
- {
-
- private void Start()
- {
- _playerInput = GetComponent<PlayerInput>();
- }
-
- [SerializeField]
- private GameObject _blockToPlace;
- public GameObject BlockToPlace
- {
- get
- {
- return _blockToPlace;
- }
- set
- {
- DisablePreviewMode();
- _blockToPlace = value;
- }
- }
- PlayerInput _playerInput;
-
- public float spawnDistance = 3f;
-
- public Shader previewShader;
- private GameObject _blockInstance;
-
-
- private bool _isPreview = false;
- private Shader _initialShader;
-
- public bool _blockRotating = false;
- private Quaternion _lastRotationState;
-
- public void OnPlaceBlock()
- {
- if (!_isPreview) return;
- _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 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;
- }
- }
-
- private void EnablePreviewMode()
- {
- if (_blockToPlace == null) return;
-
- Vector3 spawnPos = Vector3.forward * spawnDistance;
-
- _blockInstance = Instantiate(_blockToPlace, spawnPos, Quaternion.identity);
- _blockInstance.transform.SetParent(this.transform, false);
- _blockInstance.transform.rotation = _lastRotationState;
- _isPreview = true;
- SwitchToMeshRender();
- }
-
- private 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;
- }
-
- }
|