generated from VR-Sexe/Unity3DTemplate
174 lines
5.5 KiB
C#
174 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;
|
|
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);
|
|
sphere.GetComponent<Interactable>().enabled = false;
|
|
sphere.GetComponent<SteamVR_Skeleton_Poser>().enabled = false;
|
|
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, Valve.VR.InteractionSystem.Hand hand)
|
|
{
|
|
SpawnSphere(sphere.index);
|
|
MovableFurniture grab = hand.GetComponent<FurnitureMover>().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();
|
|
hand.GetComponent<FurnitureMover>().Grabbing = script;
|
|
}
|
|
|
|
public void OnReleaseSphere(CatalogElement sphere, Valve.VR.InteractionSystem.Hand hand)
|
|
{
|
|
hand.GetComponent<FurnitureMover>().Grabbing = null;
|
|
}
|
|
|
|
|
|
}
|