2026-04-20 카트 잡기
This commit is contained in:
44
Assets/02_Scripts/GameTimer.cs
Normal file
44
Assets/02_Scripts/GameTimer.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public class GameTimer : MonoBehaviour
|
||||
{
|
||||
[SerializeField, Min(0f)] private float _timeLimit = 180f;
|
||||
[SerializeField] private bool _autoStart = true;
|
||||
|
||||
public event Action<float> OnTick;
|
||||
public event Action OnTimeUp;
|
||||
|
||||
private float _remaining;
|
||||
private bool _running;
|
||||
|
||||
public float Remaining => _remaining;
|
||||
public float TimeLimit => _timeLimit;
|
||||
public bool IsRunning => _running;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
_remaining = _timeLimit;
|
||||
OnTick?.Invoke(_remaining);
|
||||
if (_autoStart) StartTimer();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (!_running) return;
|
||||
|
||||
_remaining -= Time.deltaTime;
|
||||
if (_remaining <= 0f)
|
||||
{
|
||||
_remaining = 0f;
|
||||
_running = false;
|
||||
OnTick?.Invoke(_remaining);
|
||||
OnTimeUp?.Invoke();
|
||||
return;
|
||||
}
|
||||
OnTick?.Invoke(_remaining);
|
||||
}
|
||||
|
||||
public void StartTimer() => _running = true;
|
||||
public void StopTimer() => _running = false;
|
||||
}
|
||||
2
Assets/02_Scripts/GameTimer.cs.meta
Normal file
2
Assets/02_Scripts/GameTimer.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d623169466a09404e8f5773ee7e8fee7
|
||||
@@ -26,4 +26,13 @@ public void XRLeftControllerPrimaryButton(InputAction.CallbackContext ctx)
|
||||
if(ctx.started)
|
||||
XRLeftControllerPrimaryButton_Event?.Invoke();
|
||||
}
|
||||
|
||||
|
||||
#region 테스트용(키보드)
|
||||
public void XRLeftControllerPrimaryButton_Test_KeyNum1(InputAction.CallbackContext ctx)
|
||||
{
|
||||
if(ctx.started)
|
||||
XRLeftControllerPrimaryButton_Event?.Invoke();
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
@@ -1,10 +1,33 @@
|
||||
using UnityEngine;
|
||||
using VRShopping.UI;
|
||||
|
||||
public class PlayerController : MonoBehaviour
|
||||
{
|
||||
private Animator _anim;
|
||||
|
||||
//나중에 stateMachine으로 변경
|
||||
private bool _controlPositive = true;
|
||||
|
||||
[SerializeField] private ShoppingOrderView _shoppingOrderView;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_anim = GetComponent<Animator>();
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
//InputManager.Instance.XRLeftControllerPrimaryButton_Event += 장보기 목록 온오프 함수
|
||||
InputManager.Instance.XRLeftControllerPrimaryButton_Event += this.ToggleShoppingOrderList;
|
||||
}
|
||||
|
||||
public async void ToggleShoppingOrderList()
|
||||
{
|
||||
if(!_controlPositive) return;
|
||||
|
||||
Debug.Log("ToggleShoppingOrderList");
|
||||
|
||||
_controlPositive = false;
|
||||
await _shoppingOrderView.ToggleShoppingOrderList();
|
||||
_controlPositive = true;
|
||||
}
|
||||
}
|
||||
|
||||
123
Assets/02_Scripts/Player/RideController.cs
Normal file
123
Assets/02_Scripts/Player/RideController.cs
Normal file
@@ -0,0 +1,123 @@
|
||||
using System.Collections.Generic;
|
||||
using Unity.XR.CoreUtils;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Animations;
|
||||
using UnityEngine.XR.Interaction.Toolkit;
|
||||
using UnityEngine.XR.Interaction.Toolkit.Interactables;
|
||||
|
||||
public class RideController : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private Transform _itemRoot;
|
||||
[SerializeField, Min(0f)] private float _settleVelocity = 0.1f;
|
||||
[SerializeField, Min(0f)] private float _settleDuration = 0.3f;
|
||||
|
||||
private ParentConstraint _parentConstraint;
|
||||
private readonly Dictionary<Rigidbody, float> _settleTimer = new();
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_parentConstraint = GetComponent<ParentConstraint>();
|
||||
|
||||
if (_parentConstraint.sourceCount == 0 ||
|
||||
_parentConstraint.GetSource(0).sourceTransform == null)
|
||||
{
|
||||
var xrOrigin = FindFirstObjectByType<XROrigin>();
|
||||
if (xrOrigin == null)
|
||||
{
|
||||
Debug.LogWarning("RideController: 씬에서 XROrigin을 찾지 못했습니다.");
|
||||
return;
|
||||
}
|
||||
|
||||
var source = new ConstraintSource
|
||||
{
|
||||
sourceTransform = xrOrigin.transform,
|
||||
weight = 1f
|
||||
};
|
||||
|
||||
if (_parentConstraint.sourceCount == 0)
|
||||
_parentConstraint.AddSource(source);
|
||||
else
|
||||
_parentConstraint.SetSource(0, source);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void ActiveRide()
|
||||
{
|
||||
_parentConstraint.constraintActive = true;
|
||||
}
|
||||
|
||||
public void DeactiveRide()
|
||||
{
|
||||
_parentConstraint.constraintActive = false;
|
||||
}
|
||||
|
||||
public void OnDetectionStay(Collider other)
|
||||
{
|
||||
var rb = other.attachedRigidbody;
|
||||
if (rb == null || rb.isKinematic) return;
|
||||
if (!rb.TryGetComponent(out XRGrabInteractable grab)) return;
|
||||
|
||||
if (grab.isSelected)
|
||||
{
|
||||
_settleTimer.Remove(rb);
|
||||
return;
|
||||
}
|
||||
|
||||
if (rb.linearVelocity.magnitude > _settleVelocity)
|
||||
{
|
||||
_settleTimer[rb] = 0f;
|
||||
return;
|
||||
}
|
||||
|
||||
float elapsed = _settleTimer.GetValueOrDefault(rb, 0f) + Time.deltaTime;
|
||||
if (elapsed >= _settleDuration)
|
||||
{
|
||||
AttachToCart(grab, rb);
|
||||
_settleTimer.Remove(rb);
|
||||
}
|
||||
else
|
||||
{
|
||||
_settleTimer[rb] = elapsed;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnDetectionExit(Collider other)
|
||||
{
|
||||
var rb = other.attachedRigidbody;
|
||||
if (rb != null) _settleTimer.Remove(rb);
|
||||
}
|
||||
|
||||
private void AttachToCart(XRGrabInteractable grab, Rigidbody rb)
|
||||
{
|
||||
grab.transform.SetParent(_itemRoot);
|
||||
rb.linearVelocity = Vector3.zero;
|
||||
rb.angularVelocity = Vector3.zero;
|
||||
rb.isKinematic = true;
|
||||
|
||||
// 잡았다 놓을 때 XRGrabInteractable이 parent를 ItemRoot로 되돌리는 것 방지
|
||||
grab.retainTransformParent = false;
|
||||
|
||||
grab.selectEntered.AddListener(OnPickedUpFromCart);
|
||||
}
|
||||
|
||||
private void OnPickedUpFromCart(SelectEnterEventArgs args)
|
||||
{
|
||||
if (args.interactableObject is not XRGrabInteractable grab) return;
|
||||
|
||||
grab.transform.SetParent(null);
|
||||
|
||||
grab.selectEntered.RemoveListener(OnPickedUpFromCart);
|
||||
grab.selectExited.AddListener(OnReleasedAfterPickup);
|
||||
}
|
||||
|
||||
private void OnReleasedAfterPickup(SelectExitEventArgs args)
|
||||
{
|
||||
if (args.interactableObject is not XRGrabInteractable grab) return;
|
||||
|
||||
// XRGrabInteractable이 original(kinematic=true)로 복원한 것을 덮어씀
|
||||
if (grab.TryGetComponent(out Rigidbody rb)) rb.isKinematic = false;
|
||||
|
||||
grab.selectExited.RemoveListener(OnReleasedAfterPickup);
|
||||
}
|
||||
}
|
||||
2
Assets/02_Scripts/Player/RideController.cs.meta
Normal file
2
Assets/02_Scripts/Player/RideController.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5a30bf3ffde1d9f458179bc3de7798bc
|
||||
16
Assets/02_Scripts/Player/RideDetectionZone.cs
Normal file
16
Assets/02_Scripts/Player/RideDetectionZone.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class RideDetectionZone : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private RideController _ride;
|
||||
|
||||
private void OnTriggerStay(Collider other)
|
||||
{
|
||||
if (_ride != null) _ride.OnDetectionStay(other);
|
||||
}
|
||||
|
||||
private void OnTriggerExit(Collider other)
|
||||
{
|
||||
if (_ride != null) _ride.OnDetectionExit(other);
|
||||
}
|
||||
}
|
||||
2
Assets/02_Scripts/Player/RideDetectionZone.cs.meta
Normal file
2
Assets/02_Scripts/Player/RideDetectionZone.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b6318bd3f56cf2c49813bcb2d813a7ec
|
||||
34
Assets/02_Scripts/UI/GameTimerHud.cs
Normal file
34
Assets/02_Scripts/UI/GameTimerHud.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
namespace VRShopping.UI
|
||||
{
|
||||
public class GameTimerHud : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private GameTimer _timer;
|
||||
[SerializeField] private TMP_Text _text;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
if (_timer == null) return;
|
||||
_timer.OnTick += HandleTick;
|
||||
HandleTick(_timer.Remaining);
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
if (_timer == null) return;
|
||||
_timer.OnTick -= HandleTick;
|
||||
}
|
||||
|
||||
private void HandleTick(float remaining)
|
||||
{
|
||||
if (_text == null) return;
|
||||
|
||||
int total = Mathf.CeilToInt(remaining);
|
||||
int minutes = total / 60;
|
||||
int seconds = total % 60;
|
||||
_text.text = $"{minutes:00}:{seconds:00}";
|
||||
}
|
||||
}
|
||||
}
|
||||
2
Assets/02_Scripts/UI/GameTimerHud.cs.meta
Normal file
2
Assets/02_Scripts/UI/GameTimerHud.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7e4f8e4f5f50a734d96dd84839da9183
|
||||
@@ -8,6 +8,10 @@ public class ShoppingOrderView : MonoBehaviour
|
||||
[SerializeField] private ShoppingOrderList _orderList;
|
||||
[SerializeField] private ShoppingOrderRow _rowPrefab;
|
||||
[SerializeField] private Transform _rowContainer;
|
||||
[SerializeField] private Animator _anim;
|
||||
[SerializeField] private GameObject _paper;
|
||||
|
||||
private bool _visibleShoppingOrderList = false;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
@@ -29,5 +33,27 @@ public void Rebuild()
|
||||
row.Bind(entry);
|
||||
}
|
||||
}
|
||||
|
||||
public async Awaitable ToggleShoppingOrderList()
|
||||
{
|
||||
_visibleShoppingOrderList = !_visibleShoppingOrderList;
|
||||
await OnOffShoppingOrderList(_visibleShoppingOrderList);
|
||||
}
|
||||
|
||||
private async Awaitable OnOffShoppingOrderList(bool isOn)
|
||||
{
|
||||
if(isOn)
|
||||
{
|
||||
_paper.SetActive(true);
|
||||
_anim.SetTrigger("OnVisibleShoppingOrderListTrigger");
|
||||
}
|
||||
else
|
||||
{
|
||||
_anim.SetTrigger("OffVisibleShoppingOrderListTrigger");
|
||||
await Util.RunDelayed(1f,()=>{
|
||||
_paper.SetActive(false);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user