projet-massimo-pfe/PFE OmniVR/Assets/Scripts/CharacterUINavigation.cs
jimmy tremblay-Bernier 86fbe09b20 initial commit
2022-03-12 20:32:56 -05:00

132 lines
3.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;
public class CharacterUINavigation : MonoBehaviour
{
public LayerMask layerMask;
private LineRenderer line;
private Vector3[] points = new Vector3[2];
private readonly float defaultDistance = 10;
private bool isPressingAButton = false;
private bool isReleasingAButton = false;
private ButtonInteraction lastBtn;
private BasicProductUI lastProductUI;
void Start()
{
line = GetComponent<LineRenderer>();
points[0] = Vector3.zero;
points[1] = transform.position + new Vector3(0, 0, defaultDistance);
line.SetPositions(points);
}
void Update()
{
if (GameManager.Instance.GetIsUIMenuOpen())
{
line.enabled = true;
if (SteamVR_Actions.default_GrabPinch.GetStateUp(SteamVR_Input_Sources.RightHand))
isReleasingAButton = true;
else if (SteamVR_Actions.default_GrabPinch.GetStateDown(SteamVR_Input_Sources.RightHand))
isPressingAButton = true;
AlignLineRenderer();
}
else
{
line.enabled = false;
}
}
private void AlignLineRenderer()
{
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
float zDistance = defaultDistance;
if (Physics.Raycast(ray, out hit, layerMask))
{
GameObject go = hit.collider.gameObject;
if (go.layer == 5) // UI
{
TriggerUI(go, isPressingAButton, isReleasingAButton);
isPressingAButton = false;
isReleasingAButton = false;
}
else if (go.layer == 11)
{
TriggerProductUI(go, isPressingAButton, isReleasingAButton);
isPressingAButton = false;
isReleasingAButton = false;
}
line.startColor = Color.red;
line.endColor = Color.red;
zDistance = hit.distance;
}
else
{
if(lastBtn != null)
{
lastBtn.NormalTrigger();
lastBtn = null;
}
if(lastProductUI != null)
{
lastProductUI.NormalTrigger();
lastProductUI = null;
}
line.startColor = Color.green;
line.endColor = Color.green;
}
points[1] = new Vector3(0, 0, zDistance);
line.SetPositions(points);
line.material.color = line.startColor;
}
private void TriggerUI(GameObject go, bool isPressing = false, bool isReleasing = false)
{
lastBtn = go.GetComponent<ButtonInteraction>();
if (lastBtn != null)
{
if (isPressing)
lastBtn.PressTrigger();
if (isReleasing)
lastBtn.ClickTrigger();
else
lastBtn.HighlightTrigger();
}
}
private void TriggerProductUI(GameObject go, bool isPressing = false, bool isReleasing = false)
{
lastProductUI = go.GetComponent<BasicProductUI>();
StoreManager storeManager = GameObject.FindGameObjectWithTag("GameController").GetComponent<StoreManager>();
if (lastProductUI != null)
{
if (isPressing)
lastProductUI.PressTrigger();
if (isReleasing)
{
if (storeManager.BuyProduct(lastProductUI.id))
lastProductUI.ClickTrigger();
}
else
lastProductUI.HighlightTrigger();
}
}
}