Phillipes_Fablab/Assets/Scripts/Catalog.cs
Axel Galand 4b5b691e6b +
2023-01-18 16:23:15 +01:00

140 lines
4.4 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEditor;
using UnityEngine;
using UnityEngine.ProBuilder.Shapes;
using UnityEngine.XR;
using Valve.VR.InteractionSystem;
public class Catalog : MonoBehaviour
{
public static Catalog Instance;
private Player player = null;
public int angle = 180;
public float distance = .5f;
public float objectScale = .1f;
private GameObject spherePrefab;
private GameObject currentSphere;
public List<GameObject> Furnitures;
private int maxShownObjects;
// Start is called before the first frame update
void Start()
{
Instance = this;
maxShownObjects = Furnitures.Count;
spherePrefab = transform.Find("Sphere").gameObject;
player = Player.instance;
if (player == null)
{
Debug.LogError("<b>[SteamVR Interaction]</b> Teleport: No Player instance found in map.", this);
Destroy(this.gameObject);
return;
}
ShowCatalog();
}
// Update is called once per frame
void Update()
{
}
private void ShowCatalog()
{
gameObject.transform.SetParent(player.hands[0].transform);
gameObject.transform.localPosition = Vector3.zero;
float radians = angle * Mathf.Deg2Rad;
float angleIncrement = radians / (maxShownObjects - 1);
for (int i = 0; i < maxShownObjects; i++)
{
var j = i * angleIncrement - radians / 2;
var sphere = Instantiate(spherePrefab);
sphere.transform.parent = transform;
sphere.transform.localScale = Vector3.one * objectScale;
sphere.transform.localPosition = new Vector3(Mathf.Sin(j), 0, Mathf.Cos(j)) * distance;
sphere.SetActive(true);
GameObject furniture = Instantiate(Furnitures[i]);
furniture.transform.parent = sphere.transform;
Furniture objectBounds = furniture.GetComponent<Furniture>();
objectBounds.CalculateBounds();
float rescale = (objectScale / 1.4f) / objectBounds.combinedBounds.size.magnitude;
furniture.transform.localScale *= rescale;
furniture.transform.localPosition = Vector3.down * 0.2f;
}
StartCoroutine(CatalogCoroutine());
}
private IEnumerator CatalogCoroutine()
{
var hand = player.hands;
while (true)
{
transform.rotation = Quaternion.LookRotation(transform.position - player.hmdTransform.position);
transform.rotation = new Quaternion(0, transform.rotation.y, 0, transform.rotation.w);
yield return null;
}
}
public void GrabObject(GameObject furniture, Valve.VR.InteractionSystem.Hand hand)
{
var sphere = Instantiate(spherePrefab);
sphere.transform.localScale = Vector3.one * objectScale;
GameObject furrnitureBall = Instantiate(furniture);
furrnitureBall.transform.parent = sphere.transform;
Furniture objectBounds = furrnitureBall.GetComponent<Furniture>();
if (!objectBounds)
{
objectBounds = furrnitureBall.AddComponent<Furniture>();
}
objectBounds.CalculateBounds();
float rescale = (objectScale / 1.4f) / objectBounds.combinedBounds.size.magnitude;
furrnitureBall.transform.localScale *= rescale;
furrnitureBall.transform.localPosition = Vector3.down * 0.2f;
sphere.SetActive(true);
//hand.GetGrabStarting
hand.AttachObject(sphere, GrabTypes.Trigger);
currentSphere = sphere;
}
public void ReleaseObject(GameObject furniture, Valve.VR.InteractionSystem.Hand hand)
{
hand.DetachObject(currentSphere);
currentSphere = null;
}
private void OnDrawGizmosSelected()
{
maxShownObjects = Furnitures.Count;
float radians = angle * Mathf.Deg2Rad;
float angleIncrement = radians / (maxShownObjects - 1);
for (float i = 0 / 2; i < maxShownObjects; i++)
{
var j = i * angleIncrement - radians / 2;
Gizmos.DrawWireSphere(transform.position + new Vector3(Mathf.Sin(j), 0, Mathf.Cos(j)) * distance, objectScale / 2);
}
}
public void OnGrabSphere(CatalogElement sphere)
{
}
public void OnReleaseSphere(CatalogElement sphere, Valve.VR.InteractionSystem.Hand hand)
{
hand.DetachObject(sphere.gameObject);
}
}