123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.InputSystem;
-
- public class BlockSpawner : MonoBehaviour
- {
- public Inventory inventory;
-
- public float spawnDistance = 3f;
-
- #pragma warning disable 0649
- [SerializeField] private UI_Inventory uiInventory;
- #pragma warning restore 0649
- private GameObject _blockInstance;
-
- 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);
- _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);
- _isPreview = true;
- }
-
- public void DisablePreviewMode()
- {
- if (_blockInstance != null) Destroy(_blockInstance);
- _isPreview = false;
- }
- }
|