using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; namespace BNG { /// /// Contains various functionality used in the Demo Scene such as Switching Hands and Locomotion /// public class DemoScript : MonoBehaviour { public Text LabelToUpdate; /// /// Used in the demo scene to shoot various objects /// public ProjectileLauncher DemoLauncher; /// /// Max number of objects to launch from DemoLauncher. Old objects will be destroyed to make room for new. /// public int MaxLaunchedObjects = 5; List launchedObjects; /// /// Used in demo scene /// public Text JoystickText; /// /// Used in demo scene to spawn ammo clips /// public GameObject AmmoObject; /// /// Holds all of the grabbable objects in the scene /// public Transform ItemsHolder; Dictionary _initalGrabbables; // Strictly used in demo scene Rigidbody cubeRigid; Rigidbody cubeRigid1; Rigidbody cubeRigid2; Rigidbody cubeRigid3; // Start is called before the first frame update void Start() { launchedObjects = new List(); VRUtils.Instance.Log("Output text here by using VRUtils.Log(\"Message Here\");"); VRUtils.Instance.Log("Click the Menu button to toggle this menu."); // Set up initial grabbables so we can reset them later if(ItemsHolder) { _initalGrabbables = new Dictionary(); var allGrabs = ItemsHolder.GetComponentsInChildren(); foreach(var grab in allGrabs) { _initalGrabbables.Add(grab, new PosRot() { Position = grab.transform.position, Rotation = grab.transform.rotation }); } } // Spinning Cubes in Demo Scene initGravityCubes(); } // Some example controls useful for testing void Update() { // Spin Cubes around rotateGravityCubes(); } public void UpdateSliderText(float sliderValue) { if (LabelToUpdate != null) { LabelToUpdate.text = "Power : " + (int)sliderValue + "%"; } // Scale Launcher based on slider value if(DemoLauncher) { DemoLauncher.SetForce(DemoLauncher.GetInitialProjectileForce() * (sliderValue / 100)); } } public void UpdateJoystickText(float leverX, float leverY) { if (JoystickText != null) { JoystickText.text = "X : " + (int)leverX + "\nY: " + (int)leverY; } } public void ResetGrabbables() { foreach (var kvp in _initalGrabbables) { // Only reset high level grabbables that aren't being held if(kvp.Key != null && !kvp.Key.BeingHeld && kvp.Key.transform.parent == ItemsHolder) { kvp.Key.transform.position = kvp.Value.Position; kvp.Key.transform.rotation = kvp.Value.Rotation; Rigidbody rb = kvp.Key.GetComponent(); if(rb) { rb.velocity = Vector3.zero; rb.angularVelocity = Vector3.zero; } } } } List demoClips; public void GrabAmmo(Grabber grabber) { if(demoClips == null) { demoClips = new List(); } if(demoClips.Count > 0 && demoClips[0] == null) { demoClips.RemoveAt(0); } if(AmmoObject != null) { // Make room for new clip. This ensures the demo doesn't ge bogged down if(demoClips.Count > 4 && demoClips[0] != null && demoClips[0].transform.parent == null) { GameObject.Destroy(demoClips[0].gameObject); } GameObject ammo = Instantiate(AmmoObject, grabber.transform.position, grabber.transform.rotation) as GameObject; Grabbable g = ammo.GetComponent(); // Disable rings for performance GrabbableRingHelper grh = ammo.GetComponentInChildren(); Destroy(grh); RingHelper r = ammo.GetComponentInChildren(); Destroy(r.gameObject); // Offset to hand ammo.transform.parent = grabber.transform; ammo.transform.localPosition = -g.GrabPositionOffset; ammo.transform.parent = null; if(g != null) { demoClips.Add(g); } grabber.GrabGrabbable(g); } } public void ShootLauncher() { if(launchedObjects == null) { launchedObjects = new List(); } // Went over max. Destroy oldest launch object if(launchedObjects.Count > MaxLaunchedObjects) { launchedObjects.Remove(launchedObjects[0]); GameObject.Destroy(launchedObjects[0]); } launchedObjects.Add(DemoLauncher.ShootProjectile(DemoLauncher.ProjectileForce)); } void initGravityCubes() { // Makes cubes spin in example scene if(GameObject.Find("GravityCube 1")) { cubeRigid = GameObject.Find("GravityCube 1").GetComponent(); } if (GameObject.Find("GravityCube 2")) { cubeRigid1 = GameObject.Find("GravityCube 2").GetComponent(); } if (GameObject.Find("GravityCube 3")) { cubeRigid2 = GameObject.Find("GravityCube 3").GetComponent(); } if (GameObject.Find("GravityCube 4")) { cubeRigid3 = GameObject.Find("GravityCube 4").GetComponent(); } } // Cache for performance Vector3 rotateX = new Vector3(0.2f, 0, 0); Vector3 rotateY = new Vector3(0, 0.2f, 0); Vector3 rotateZ = new Vector3(0, 0, 0.2f); Vector3 rotateXYX = new Vector3(0.2f, 0.2f, 0.2f); void rotateGravityCubes() { if (cubeRigid) { cubeRigid.angularVelocity = rotateX; } if (cubeRigid1) { cubeRigid1.angularVelocity = rotateY; } if (cubeRigid2) { cubeRigid2.angularVelocity = rotateZ; } if (cubeRigid3) { cubeRigid3.angularVelocity = rotateXYX; } } } public class PosRot { public Vector3 Position; public Quaternion Rotation; } }