Phillipes_Fablab/Assets/Scripts/Catalog.cs
2023-01-26 14:35:38 +01:00

176 lines
5.5 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;
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;
private GameObject Trash;
// Start is called before the first frame update
void Start()
{
Instance = this;
maxShownObjects = Furnitures.Count;
spherePrefab = transform.Find("Sphere").gameObject;
Trash = transform.Find("Trash").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 SpawnSphere(int index)
{
float radians = angle * Mathf.Deg2Rad;
float angleIncrement = radians / (maxShownObjects - 1);
if (index > Furnitures.Count - 1)
{
Debug.LogError("<b>[Catalog]</b> Sphere index out of bounds", this);
return;
}
var j = index * 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);
CatalogElement element = sphere.GetComponent<CatalogElement>();
element.SetFurniture(Furnitures[index], objectScale);
element.index = index;
}
public void SetTrash(MovableFurniture Grabbing)
{
Grabbing.gameObject.SetActive(false);
Trash.GetComponent<MeshRenderer>().material.color = new Color(1, 0.5f, 0, 1);
}
public void LeaveTrash(MovableFurniture Grabbing)
{
Grabbing.gameObject.SetActive(true);
/*CatalogElement element = Trash.GetComponent<CatalogElement>();
element.ClearFurniture();*/
Trash.GetComponent<MeshRenderer>().material.color = new Color(1, 0, 0, 0.5f);
}
/*private void SpawnTrash()
{
var sphere = Instantiate(spherePrefab);
sphere.transform.parent = transform;
sphere.transform.localScale = Vector3.one * objectScale;
sphere.transform.localPosition = new Vector3(0, 1, 1) * distance;
sphere.GetComponent<MeshRenderer>().material.color = new Color(1, 0, 0, 0.5f);
Destroy(sphere.GetComponent<SteamVR_Skeleton_Poser>());
Destroy(sphere.GetComponent<Interactable>());
sphere.AddComponent<Trash>();
sphere.SetActive(true);
sphere.tag = "Trash";
sphere.layer = 10;
Trash = sphere;
}*/
private void ShowCatalog()
{
gameObject.transform.SetParent(player.hands[0].transform);
gameObject.transform.localPosition = Vector3.zero;
for (int i = 0; i < maxShownObjects; i++)
{
SpawnSphere(i);
}
//SpawnTrash();
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;
sphere.SetActive(true);
sphere.GetComponent<CatalogElement>().SetFurniture(furniture, objectScale);
hand.AttachObject(sphere, GrabTypes.Grip);
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)
{
SpawnSphere(sphere.index);
MovableFurniture grab = FurnitureMover.Instance.Grabbing;
if (grab != null)
{
return;
}
GameObject furniture = Instantiate(sphere.largeFurnitureModel);
MovableFurniture script = furniture.GetComponent<MovableFurniture>();
if (!furniture.GetComponent<MovableFurniture>())
{
script = furniture.AddComponent<MovableFurniture>();
}
script.CalculateBounds();
//script.OnPointerClickDown();
FurnitureMover.Instance.Grabbing = script;
}
public void OnReleaseSphere(CatalogElement sphere)
{
FurnitureMover.Instance.Grabbing = null;
}
}