Création d'un petit party-game anonyme de Build + Bagar sous Unity
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.InputSystem;
  4. public class BlockSpawner : MonoBehaviour
  5. {
  6. public Inventory inventory;
  7. public Shader previewShader;
  8. private Shader _initialShader;
  9. #pragma warning disable 0649
  10. [SerializeField] private UI_Inventory uiInventory;
  11. #pragma warning restore 0649
  12. private GameObject _blockInstance;
  13. private Quaternion _lastRotationState;
  14. public float spawnDistance = 3f;
  15. public bool _blockRotating = false;
  16. private bool _isPreview;
  17. public void Awake()
  18. {
  19. _isPreview = false;
  20. inventory = new Inventory();
  21. uiInventory.SetInventory(inventory);
  22. }
  23. public void OnPlaceBlock ()
  24. {
  25. if (!_isPreview) return;
  26. // todo remove in inventory
  27. _blockInstance.transform.SetParent(null);
  28. SwitchToDefaultRender();
  29. _blockInstance = null;
  30. DisablePreviewMode();
  31. }
  32. public void OnTogglePreviewMode(InputValue value)
  33. {
  34. float valueAsFloat = value.Get<float>();
  35. if (valueAsFloat == 1f) EnablePreviewMode();
  36. else DisablePreviewMode();
  37. }
  38. public void OnScroll(InputValue value)
  39. {
  40. DisablePreviewMode();
  41. float valueAsFloat = value.Get<float>();
  42. if (valueAsFloat > 0) inventory.SelectPreviousItem();
  43. if (valueAsFloat < 0) inventory.SelectNextItem();
  44. }
  45. public void EnablePreviewMode()
  46. {
  47. Item selectedItem = inventory.GetSelectedItem();
  48. if (selectedItem == null) return;
  49. Vector3 spawnPos = Vector3.forward * spawnDistance;
  50. _blockInstance = Instantiate(selectedItem.Prefab, spawnPos, Quaternion.identity);
  51. _blockInstance.transform.SetParent(this.transform, false);
  52. _blockInstance.transform.rotation = _lastRotationState;
  53. _isPreview = true;
  54. SwitchToMeshRender();
  55. }
  56. public void DisablePreviewMode()
  57. {
  58. if (_blockInstance != null) Destroy(_blockInstance);
  59. _isPreview = false;
  60. }
  61. private void SwitchToMeshRender()
  62. {
  63. MeshRenderer vRenderer = _blockInstance.GetComponent<MeshRenderer>();
  64. Material vMaterial = vRenderer.material;
  65. _initialShader = vMaterial.shader;
  66. vMaterial.shader = previewShader;
  67. }
  68. private void SwitchToDefaultRender()
  69. {
  70. MeshRenderer vRenderer = _blockInstance.GetComponent<MeshRenderer>();
  71. Material vMaterial = vRenderer.material;
  72. vMaterial.shader = _initialShader;
  73. }
  74. public void OnEnterRotateBlock(InputValue value)
  75. {
  76. float valueAsFloat = value.Get<float>();
  77. Debug.Log(valueAsFloat);
  78. _blockRotating = true;
  79. }
  80. public void OnExitRotateBlock(InputValue value)
  81. {
  82. float valueAsFloat = value.Get<float>();
  83. Debug.Log(valueAsFloat);
  84. _blockRotating = false;
  85. }
  86. public void OnRotateBlock(InputValue value)
  87. {
  88. if (_isPreview && _blockRotating)
  89. {
  90. Vector2 iMouseMovement = value.Get<Vector2>();
  91. Transform vTransform = _blockInstance.transform;
  92. //vTransform.Rotate(iMouseMovement.y / 10, iMouseMovement.x / 10, 0);
  93. vTransform.RotateAround(vTransform.position, transform.right, iMouseMovement.y / 10.0f);
  94. vTransform.RotateAround(vTransform.position, transform.up, iMouseMovement.x / 10.0f);
  95. //vTransform.eulerAngles += vTransform.worldToLocalMatrix.MultiplyVector(new Vector3(iMouseMovement.y / 10, iMouseMovement.x / 10, 0));
  96. //vTransform.Rotate(iMouseMovement.y/10, iMouseMovement.x/10, 0);
  97. _lastRotationState = vTransform.rotation;
  98. }
  99. }
  100. }