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

125 lines
3.5 KiB
C#

using System.Collections;
using UnityEngine;
using Valve.VR.InteractionSystem;
public class CatalogElement : Throwable
{
public GameObject largeFurnitureModel;
public int index;
private GameObject smallFurnitureModel;
private Vector3 returnPosition;
// Start is called before the first frame update
void Start()
{
returnPosition = transform.localPosition;
}
// Update is called once per frame
void Update()
{
}
protected override void OnAttachedToHand(Hand hand)
{
//Debug.Log("<b>[SteamVR Interaction]</b> Pickup: " + hand.GetGrabStarting().ToString());
hadInterpolation = this.rigidbody.interpolation;
attached = true;
onPickUp.Invoke();
hand.HoverLock(null);
rigidbody.interpolation = RigidbodyInterpolation.None;
attachTime = Time.time;
attachPosition = transform.position;
attachRotation = transform.rotation;
Catalog.Instance.OnGrabSphere(this);
//hand.GetComponent<FurnitureMover>().Grabbing = largeFurnitureModel.GetComponent<MovableFurniture>();
}
//-------------------------------------------------
protected override void OnDetachedFromHand(Hand hand)
{
attached = false;
onDetachFromHand.Invoke();
hand.HoverUnlock(null);
rigidbody.interpolation = hadInterpolation;
Catalog.Instance.OnReleaseSphere(this);
Destroy(gameObject);
}
public void SetFurniture(GameObject furniture, float objectScale)
{
largeFurnitureModel = furniture;
smallFurnitureModel = Instantiate(furniture);
var throwables = smallFurnitureModel.GetComponentsInChildren<Throwable>();
foreach (Throwable throwable in throwables)
{
Destroy(throwable);
};
var rigidbodies = smallFurnitureModel.GetComponentsInChildren<Rigidbody>();
foreach (Rigidbody rigidbody in rigidbodies)
{
Destroy(rigidbody);
};
smallFurnitureModel.transform.parent = transform;
Destroy(smallFurnitureModel.GetComponent<MovableFurniture>());
Furniture objectBounds = smallFurnitureModel.AddComponent<Furniture>();
objectBounds.CalculateBounds();
var colldiers = smallFurnitureModel.GetComponentsInChildren<Collider>();
foreach (Collider collider in colldiers)
{
Destroy(collider);
};
float rescale = (objectScale / 1.4f) / objectBounds.combinedBounds.size.magnitude;
smallFurnitureModel.transform.localScale *= rescale;
smallFurnitureModel.transform.localPosition = Vector3.down * 0.2f;
StartCoroutine(KeepFurnitureAligned());
}
public void ClearFurniture()
{
Destroy(smallFurnitureModel);
}
private IEnumerator ReturnSphere()
{
float time = 0;
Vector3 startPosition = transform.localPosition;
while (time <= 1)
{
transform.localPosition = Vector3.Lerp(startPosition, returnPosition, time);
time += Time.deltaTime * 4;
yield return null;
}
yield break;
}
private IEnumerator KeepFurnitureAligned()
{
while (true)
{
smallFurnitureModel.transform.eulerAngles = new Vector3(0, smallFurnitureModel.transform.eulerAngles.y - (20 * Time.deltaTime), 0);
yield return null;
}
}
private void OnPointerEnter()
{
Debug.Log("enter");
}
}