Modal – 2-5-1 {5-4-21}
using System.Collections; using System.Collections.Generic; using UnityEngine; using ArgosTweenSpacePuppy; public class Spoke_Handler : MonoBehaviour { public bool setLastHeld = false; public bool isBeingHeld = false; public bool bLOCKED = false; public float maxFJForce = 1000f; private float reset_Timer = 2f; private bool bReseting = false; private Vector3 targetPos; private Quaternion targetRot; // +x,+y,+z,-x,-y,-z public List<Spoke_AllShape> spoke_AS = new List<Spoke_AllShape>(); public Rigidbody rb; private AF_Instance af_Instance; public SphereCollider sphereCollider; public AF_Network_Grabable netGrabbable; public Touch_Lighting_and_Sound tls; public GameObject Target_Bee; public Spoke_AllShape SAS1 { get; private set; } public Spoke_AllShape SAS2 { get; private set; } //private GameObject AllShape_1; //private GameObject AllShape_2; private GameObject objectToStay; private GameObject objectToMove; private Spoke_Handler spokeHandler_Object_to_Move; private Spoke_Handler spokeHandler_Object_to_Stay; private float tAccum_ST = 0f; private float tDurr_ST = 1.5f; private float tweenVAL = 0f; private Vector3[] axisFLIPPER = { new Vector3 { x = 1, y = 0, z = 0 }, new Vector3 { x = 0, y = 1, z = 0 }, new Vector3 { x = 0, y = 0, z = 1 }, new Vector3 { x = -1, y = 0, z = 0 }, new Vector3 { x = 0, y =-1, z = 0 }, new Vector3 { x = 0, y = 0, z = -1}, }; public float CollisionSpring = 3000; public float CollisionSlerp = 500; public void Awake() { af_Instance = AF_Instance.Instance; tls = GetComponent<Touch_Lighting_and_Sound>(); //TAG Spokes to access target transforms for (int i = 0; i<spoke_AS.Count; i++) { spoke_AS[i].SetParent_AllShape(this, i); tls.initializeColors_in_Spoke(spoke_AS[i]); } } void Start() { SetColliders_Size(Spoke_AllShape.ColliderSize.LARGE); } public void SetColliders_Size(Spoke_AllShape.ColliderSize colliderSize) { for (int i = 0; i < spoke_AS.Count; i++) { spoke_AS[i].Set_Collider_Size(colliderSize); } } public void Set_Target_Transform(Transform targ) { Vector3 center = (objectToMove.transform.position + objectToStay.transform.position) / 2f; int idx1 = spokeHandler_Object_to_Move.Closest_Fin(center); int idx2 = spokeHandler_Object_to_Stay.Closest_Fin(center); Vector3 vin = spokeHandler_Object_to_Stay.spoke_AS[idx2].trans_to.vIN[idx1]; Vector3 vup = spokeHandler_Object_to_Stay.spoke_AS[idx2].trans_to.vUP[idx1]; Vector3 vInTrans = objectToStay.transform.TransformDirection(vin); Vector3 vUpTrans = objectToStay.transform.TransformDirection(vup); targetPos = targ.position; targetRot = Quaternion.LookRotation(vInTrans, vUpTrans); //Flip it - for minimal rotation to bonding FIN if (Vector3.Dot(vUpTrans, objectToMove.transform.up) < 0 || Vector3.Dot(vInTrans, objectToMove.transform.forward) < 0) { Quaternion qRota = Quaternion.AngleAxis(180, axisFLIPPER[idx1]); targetRot *= qRota; } spokeHandler_Object_to_Stay.Target_Bee.transform.position = targetPos; spokeHandler_Object_to_Stay.Target_Bee.transform.rotation = targetRot; } public void SpokeTriggered(int idx, Spoke_AllShape sourceSpoke) { Spoke_Handler SH1 = spoke_AS[idx].myALL_SHAPE; Transform targ1 = spoke_AS[idx].targTransform; Spoke_Handler SH2 = sourceSpoke.myALL_SHAPE; Transform targ2 = sourceSpoke.targTransform; Determine_Target_Movement(SH1, targ1, SH2, targ2); } public void Determine_Target_Movement(Spoke_Handler AS1, Transform targ1, Spoke_Handler AS2, Transform targ2) { //Two calls - only take this one if (!AS1.bLOCKED && !AS2.bLOCKED && !bReseting /*|| (AS1.GetComponent<Spoke_Handler>().isBeingHeld)*/) { AS1.bLOCKED = true; AS2.bLOCKED = true; objectToMove = AS1.gameObject; objectToStay = AS2.gameObject; spokeHandler_Object_to_Move = objectToMove.GetComponent<Spoke_Handler>(); spokeHandler_Object_to_Stay = objectToStay.GetComponent<Spoke_Handler>(); spokeHandler_Object_to_Move.netGrabbable.DropItem(null, true, true); spokeHandler_Object_to_Stay.netGrabbable.DropItem(null, true, true); Set_Target_Transform(targ2); tAccum_ST = 0.0f; Debug.Log("Start COROUTINE:" + Time.time.ToString("F3")); StartCoroutine(Move_to_Connect()); } } private IEnumerator Move_to_Connect() { spokeHandler_Object_to_Move.SetAllActive_Colliders(false, false); spokeHandler_Object_to_Stay.SetAllActive_Colliders(false, false); int i = 0; //Sanity check for infinite loops float linProg = tAccum_ST / tDurr_ST; while (i < 180 && linProg < 1) { tAccum_ST += Time.deltaTime; linProg = tAccum_ST / tDurr_ST; Mathf.Clamp(tAccum_ST, 0, tDurr_ST); tweenVAL = EaseMethods.ExpoEaseInOut(tAccum_ST, 0, 1, tDurr_ST); Move_AllShape_Into_Place(tweenVAL); yield return true; i++; } tAccum_ST = tDurr_ST; tweenVAL = 1; Move_AllShape_Into_Place(tweenVAL); //DETERMINE OTHER CONNECTIONS HERE - TODO Set_Occupied_and_Form_Bond_These_Spokes(); Debug.Log("StopCoroutine(Move_to_Connect)" + Time.time.ToString("F3")); StopCoroutine("Move_to_Connect"); } private void Move_AllShape_Into_Place(float val) { objectToMove.transform.position = Vector3.Lerp(objectToMove.transform.position, spokeHandler_Object_to_Stay.Target_Bee.transform.position, val); objectToMove.transform.rotation = Quaternion.Slerp(objectToMove.transform.rotation, spokeHandler_Object_to_Stay.Target_Bee.transform.rotation, val); } private void Set_Occupied_and_Form_Bond_These_Spokes() { Vector3 center = (objectToMove.transform.position + objectToStay.transform.position) / 2f; int idx1 = spokeHandler_Object_to_Move.Closest_Fin(center); int idx2 = spokeHandler_Object_to_Stay.Closest_Fin(center); //FixedJoint fj = objectToStay.AddComponent<FixedJoint>(); //fj.autoConfigureConnectedAnchor = true; //fj.connectedBody = objectToMove.GetComponent<Rigidbody>(); ConfigurableJoint joint = objectToStay.AddComponent<ConfigurableJoint>(); joint.autoConfigureConnectedAnchor = true; joint.connectedBody = objectToMove.GetComponent<Rigidbody>(); joint.rotationDriveMode = RotationDriveMode.Slerp; JointDrive slerpDrive = joint.slerpDrive; slerpDrive.positionSpring = 600; JointDrive xDrive = joint.xDrive; xDrive.positionSpring = 2500; JointDrive yDrive = joint.yDrive; yDrive.positionSpring = 2500; JointDrive zDrive = joint.zDrive; zDrive.positionSpring = 2500; joint.rotationDriveMode = RotationDriveMode.Slerp; joint.xMotion = ConfigurableJointMotion.Limited; joint.yMotion = ConfigurableJointMotion.Limited; joint.zMotion = ConfigurableJointMotion.Limited; joint.angularXMotion = ConfigurableJointMotion.Limited; joint.angularYMotion = ConfigurableJointMotion.Limited; joint.angularZMotion = ConfigurableJointMotion.Limited; SoftJointLimit sjl = joint.linearLimit; sjl.limit = 15f; SoftJointLimitSpring sjlsp = joint.linearLimitSpring; sjlsp.spring = 3000; sjlsp.damper = 10f; // Set X,Y, and Z drive to our values // Set X,Y, and Z drive to our values setPositionSpring(joint, CollisionSpring, 10f); // Slerp drive used for rotation setSlerpDrive(joint, CollisionSlerp, 10f); spokeHandler_Object_to_Move.setOccupied(idx1, true, spokeHandler_Object_to_Stay.spoke_AS[idx2], joint); spokeHandler_Object_to_Stay.setOccupied(idx2, true, spokeHandler_Object_to_Move.spoke_AS[idx1], joint); spokeHandler_Object_to_Move.SetAllActive_Colliders(true, true); spokeHandler_Object_to_Stay.SetAllActive_Colliders(true, true); if (spokeHandler_Object_to_Move && spokeHandler_Object_to_Stay) { af_Instance.Add_Bond(spokeHandler_Object_to_Move, spokeHandler_Object_to_Stay, idx1, idx2); } spokeHandler_Object_to_Move.bLOCKED = false; spokeHandler_Object_to_Stay.bLOCKED = false; } public void setOccupied(int spokeIDX, bool bOccupied, Spoke_AllShape connectedSpoke, ConfigurableJoint cj) { spoke_AS[spokeIDX].bOccupied = bOccupied; tls.Light_Occupied(spoke_AS[spokeIDX], bOccupied); spoke_AS[spokeIDX].connectedSpoke = connectedSpoke; spoke_AS[spokeIDX].cj = cj; } void setPositionSpring(ConfigurableJoint connectedJoint, float spring, float damper) { if (connectedJoint == null) { return; } JointDrive xDrive = connectedJoint.xDrive; xDrive.positionSpring = spring; xDrive.positionDamper = damper; connectedJoint.xDrive = xDrive; JointDrive yDrive = connectedJoint.yDrive; yDrive.positionSpring = spring; yDrive.positionDamper = damper; connectedJoint.yDrive = yDrive; JointDrive zDrive = connectedJoint.zDrive; zDrive.positionSpring = spring; zDrive.positionDamper = damper; connectedJoint.zDrive = zDrive; } void setSlerpDrive(ConfigurableJoint connectedJoint, float slerp, float damper) { if (connectedJoint) { JointDrive slerpDrive = connectedJoint.slerpDrive; slerpDrive.positionSpring = slerp; slerpDrive.positionDamper = damper; connectedJoint.slerpDrive = slerpDrive; } } public void SetAllActive_Colliders(bool bOn, bool bSphereCollider) { sphereCollider.enabled = bSphereCollider; rb.isKinematic = false; //MORE CONSIDERATIONS HERE - Possible timer to relieve sim load for (int i = 0; i < spoke_AS.Count; i++) { if (spoke_AS[i].bOccupied) { spoke_AS[i].enableCollider(false); } else { spoke_AS[i].enableCollider(bOn); } } } public int Closest_Fin(Vector3 testPos) { int idxMin = 0; float minDistance = 1000000f; float dist; for (int i = 0; i < spoke_AS.Count; i++) { dist = (spoke_AS[i].fin_LOC.position - testPos).sqrMagnitude; if(dist<minDistance) { idxMin = i; minDistance = dist; } } return idxMin; } public void SetAllColliders(bool bOn) { sphereCollider.enabled = bOn; rb.isKinematic = true; //MORE CONSIDERATIONS HERE for (int i = 0; i < spoke_AS.Count; i++) { spoke_AS[i].bOccupied = false; spoke_AS[i].enableCollider(bOn); } } public void Reset_Spokes_On_Grab() { for (int i = 0; i < spoke_AS.Count; i++) { if (spoke_AS[i].bOccupied) { spoke_AS[i].setOccupied(false); } } } public void Disconect_AllShape() { for(int i = 0; i<spoke_AS.Count; i++) { spoke_AS[i].Remove_Configurable_Joint(); spoke_AS[i].setOccupied(false); } StartCoroutine(Wait_To_Reset_Spokes()); } IEnumerator Wait_To_Reset_Spokes() { bReseting = true; yield return new WaitForSeconds(reset_Timer); for (int i = 0; i < spoke_AS.Count; i++) { spoke_AS[i].Set_Collider_Size(Spoke_AllShape.ColliderSize.LARGE); spoke_AS[i].enableCollider(true); } bReseting = false; } //public void Remove_FJs() //{ // FixedJoint[] fj = GetComponents<FixedJoint>(); // if (fj.Length > 0) // { // for (int i = 0; i < fj.Length; i++) // { // if (Vector3.Magnitude(fj[i].currentForce) > maxFJForce) // { // Debug.Log("Breaking Bond"); // int idx = -1; // AF_Instance.AF_Bond AFB = af_Instance.Find_Bond(fj[i], ref idx); // if (AFB != null) // { // bReseting = true; // Debug.Log("AF_Instance.AllShape_Bond ASB"); // AFB.spokeHandler_Object_Moved.spoke_AS[AFB.fin_Moved_IDX].bOccupied = false; // AFB.spokeHandler_Object_Stayed.spoke_AS[AFB.fin_Stayed_IDX].bOccupied = false; // Spoke_Handler s1 = AFB.spokeHandler_Object_Moved; // Spoke_Handler s2 = AFB.spokeHandler_Object_Stayed; // StartCoroutine(Wait_To_Reset_Spokes(s1, s2)); // //ASB.spokeHandler_Object_Moved.SetAllActive_Colliders(true, true); // //ASB.spokeHandler_Object_Stayed.SetAllActive_Colliders(true, true); // af_Instance.Remove_Bond(idx); // } // fj[i].connectedBody = null; // Destroy(fj[i]); // } // } // } //} void Update() { //FixedJoint[] fj = GetComponents<FixedJoint>(); //if (fj.Length > 0) //{ // for (int i = 0; i < fj.Length; i++) // { // if (Vector3.Magnitude(fj[i].currentForce) > maxFJForce) // { // Debug.Log("Breaking Bond"); // int idx = -1; // AF_Instance.AF_Bond AFB = af_Instance.Find_Bond(fj[i], ref idx); // if (AFB != null) // { // bReseting = true; // Debug.Log("AF_Instance.AllShape_Bond ASB"); // AFB.spokeHandler_Object_Moved.spoke_AS[AFB.fin_Moved_IDX].bOccupied = false; // AFB.spokeHandler_Object_Stayed.spoke_AS[AFB.fin_Stayed_IDX].bOccupied = false; // Spoke_Handler s1 = AFB.spokeHandler_Object_Moved; // Spoke_Handler s2 = AFB.spokeHandler_Object_Stayed; // StartCoroutine(Wait_To_Reset_Spokes(s1, s2)); // //ASB.spokeHandler_Object_Moved.SetAllActive_Colliders(true, true); // //ASB.spokeHandler_Object_Stayed.SetAllActive_Colliders(true, true); // af_Instance.Remove_Bond(idx); // } // fj[i].connectedBody = null; // Destroy(fj[i]); // } // } //} if (setLastHeld) { af_Instance.last_Held_AllShape = this.gameObject; setLastHeld = false; } } }
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine; public class Spoke_AllShape : MonoBehaviour { public enum ColliderSize { LARGE, SMALL, } private float colRadius; private Vector3 colCenter; public Spoke_Handler myALL_SHAPE { get; private set; } private int myIDx; private Collider myCollider; public Trans_To trans_to { get; private set; }//holds destination orientation info public Transform targTransform { get; private set; } public bool bOccupied = false; public Spoke_AllShape connectedSpoke = null; public ConfigurableJoint cj = null; public GameObject fin; private Material fin_Mat; public Transform fin_LOC; private int frameCounter; private Color albedo_Normal; private Color emissive_Normal; private Color albedo_Occupied; private Color emissive_Occupied; private void Awake() { fin_Mat = fin.GetComponent<Renderer>().material; fin_Mat.EnableKeyword("_EMISSION"); myCollider = GetComponent<SphereCollider>(); trans_to = GetComponentInChildren<Trans_To>(); targTransform = trans_to.gameObject.transform; colRadius = ((SphereCollider)myCollider).radius; colCenter = ((SphereCollider)myCollider).center; } void Start() { for(int i = 0; i < myALL_SHAPE.spoke_AS.Count; i++) { if (myCollider != myALL_SHAPE.spoke_AS[i].myCollider) { Physics.IgnoreCollision(myCollider, myALL_SHAPE.spoke_AS[i].myCollider, true); } } } public void SetBaseColors(Color an, Color en, Color ao, Color eo) { albedo_Normal = an; emissive_Normal = en; albedo_Occupied = ao; emissive_Occupied = eo; } public void Set_Collider_Size(ColliderSize size) { if(size == ColliderSize.LARGE) { ((SphereCollider)myCollider).radius = colRadius * 1.5f; ((SphereCollider)myCollider).center = colCenter * 1.5f; } else { ((SphereCollider)myCollider).radius = colRadius; ((SphereCollider)myCollider).center = colCenter; } } public void SetColors(Color albedo, Color emissive) { fin_Mat.SetColor("_Color", albedo); fin_Mat.SetColor("_EmissionColor", emissive); } public void SetParent_AllShape(Spoke_Handler spAS, int iDx) { myALL_SHAPE = spAS; myIDx = iDx; } public void enableCollider(bool bOn) { myCollider.enabled = bOn; } public void Remove_Configurable_Joint() { if (cj != null) { cj.connectedBody = null; Destroy(cj); } } public void setOccupied(bool bOccupy) { if(bOccupy == false) { if (connectedSpoke != null) { connectedSpoke.bOccupied = false; connectedSpoke.myCollider.enabled = true; connectedSpoke = null; } } bOccupied = bOccupy; //myCollider.enabled = !bOccupy; } private void Update() { if (++frameCounter > 20) { frameCounter = 0; if (bOccupied) { SetColors(albedo_Occupied, emissive_Occupied); } else { SetColors(albedo_Normal, emissive_Normal); } } } private void OnTriggerEnter(Collider coll) { Spoke_AllShape sas = coll.GetComponent<Spoke_AllShape>(); Debug.Log("From - " + gameObject.name + "received Trigger enter from: " + coll.gameObject.name); if (sas.myALL_SHAPE == myALL_SHAPE) { return; } Debug.Log("CHECK OCCUPIED"); if (sas != null && !bOccupied && !sas.bOccupied && !myALL_SHAPE.bLOCKED && !sas.myALL_SHAPE.bLOCKED) { myALL_SHAPE.SetAllActive_Colliders(false,false); sas.myALL_SHAPE.SetAllActive_Colliders(false, false); myALL_SHAPE.SetColliders_Size(ColliderSize.SMALL); sas.myALL_SHAPE.SetColliders_Size(ColliderSize.SMALL); myALL_SHAPE.SpokeTriggered(myIDx, sas); } } }//Debug.Log("From - " + gameObject.name + "received Trigger enter from: " + coll.gameObject.name + "Sending to spoke handler");