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 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("[SteamVR Interaction] 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("[Catalog] 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(); element.SetFurniture(Furnitures[index], objectScale); element.index = index; } public void SetTrash(MovableFurniture Grabbing) { Grabbing.gameObject.SetActive(false); Trash.GetComponent().material.color = new Color(1, 0.5f, 0, 1); } public void LeaveTrash(MovableFurniture Grabbing) { Grabbing.gameObject.SetActive(true); /*CatalogElement element = Trash.GetComponent(); element.ClearFurniture();*/ Trash.GetComponent().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().material.color = new Color(1, 0, 0, 0.5f); Destroy(sphere.GetComponent()); Destroy(sphere.GetComponent()); sphere.AddComponent(); 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().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(); if (!furniture.GetComponent()) { script = furniture.AddComponent(); } script.CalculateBounds(); //script.OnPointerClickDown(); FurnitureMover.Instance.Grabbing = script; } public void OnReleaseSphere(CatalogElement sphere) { FurnitureMover.Instance.Grabbing = null; } }