first commit
This commit is contained in:
112
Assets/Scripts/ScaleTest/MainScaleZone.cs
Normal file
112
Assets/Scripts/ScaleTest/MainScaleZone.cs
Normal file
@@ -0,0 +1,112 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using TMPro;
|
||||
|
||||
public class MainScaleZone : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private GameObject _mainScaleObject;
|
||||
[SerializeField] private Collider _mainScaleTrigger;
|
||||
[SerializeField] private TextMeshProUGUI _resultText;
|
||||
[SerializeField] private float _showResultTime = 2f;
|
||||
private bool _isChecking = false;
|
||||
private bool _canCheck = false;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_mainScaleObject.SetActive(false);
|
||||
_resultText.gameObject.SetActive(false);
|
||||
SetTag(false);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (Input.GetMouseButtonDown(1))
|
||||
{
|
||||
if (_mainScaleObject.activeSelf)
|
||||
{
|
||||
_mainScaleObject.SetActive(false);
|
||||
SetTag(false);
|
||||
SetPriorityForAllBalls(null);
|
||||
_canCheck = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
_mainScaleObject.SetActive(true);
|
||||
SetTag(true);
|
||||
SetPriorityForAllBalls(_mainScaleTrigger.transform);
|
||||
_canCheck = false;
|
||||
StartCoroutine(EnableCheckDelay());
|
||||
}
|
||||
}
|
||||
|
||||
if (!_mainScaleObject.activeSelf || _isChecking || !_canCheck) return;
|
||||
|
||||
Collider[] cols = Physics.OverlapSphere(
|
||||
_mainScaleTrigger.bounds.center,
|
||||
_mainScaleTrigger.bounds.extents.magnitude
|
||||
);
|
||||
foreach (var col in cols)
|
||||
{
|
||||
DraggableObject ball = col.GetComponent<DraggableObject>();
|
||||
if (ball == null) continue;
|
||||
if (ball.isDragging) continue;
|
||||
Rigidbody rb = col.GetComponent<Rigidbody>();
|
||||
if (rb != null && rb.velocity.magnitude > 0.1f) continue;
|
||||
_isChecking = true;
|
||||
SetTag(false);
|
||||
if (ball.isHeavy)
|
||||
StartCoroutine(ShowResultAndRestart("³ðíî!"));
|
||||
else
|
||||
StartCoroutine(ShowResultAndContinue("Ñïðîáóé ùå"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void SetPriorityForAllBalls(Transform target)
|
||||
{
|
||||
DraggableObject[] balls = FindObjectsOfType<DraggableObject>();
|
||||
foreach (var ball in balls)
|
||||
ball.priorityTarget = target;
|
||||
}
|
||||
|
||||
private IEnumerator EnableCheckDelay()
|
||||
{
|
||||
yield return new WaitForSeconds(2f);
|
||||
_canCheck = true;
|
||||
}
|
||||
|
||||
private IEnumerator ShowResultAndRestart(string text)
|
||||
{
|
||||
NewScaleZone scaleZone = FindObjectOfType<NewScaleZone>();
|
||||
scaleZone?.ShowSuccess();
|
||||
_resultText.text = text;
|
||||
_resultText.gameObject.SetActive(true);
|
||||
yield return new WaitForSeconds(_showResultTime);
|
||||
_mainScaleObject.SetActive(false);
|
||||
_resultText.gameObject.SetActive(false);
|
||||
SetPriorityForAllBalls(null);
|
||||
FindObjectOfType<WeighingManager>()?.FullRestart();
|
||||
scaleZone?.RestartAttempts();
|
||||
_isChecking = false;
|
||||
}
|
||||
|
||||
private IEnumerator ShowResultAndContinue(string text)
|
||||
{
|
||||
_resultText.text = text;
|
||||
_resultText.gameObject.SetActive(true);
|
||||
yield return new WaitForSeconds(_showResultTime);
|
||||
_mainScaleObject.SetActive(false);
|
||||
_resultText.gameObject.SetActive(false);
|
||||
SetPriorityForAllBalls(null);
|
||||
FindObjectOfType<WeighingManager>()?.ResetAllBalls();
|
||||
FindObjectOfType<NewScaleZone>()?.RegisterAttempt();
|
||||
_isChecking = false;
|
||||
}
|
||||
|
||||
private void SetTag(bool active)
|
||||
{
|
||||
if (_mainScaleTrigger != null)
|
||||
_mainScaleTrigger.gameObject.tag = active ? "MainScale" : "Untagged";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user