private JointScript CreateJointObject(Joint joint, GameObject parent) { Shape shape = PartLoader.GetJoint(joint); GameObject gameObject = shape.Instantiate(parent.transform); JointScript jointScript = gameObject.AddComponent <JointScript>(); jointScript.shape = shape; jointScript.Id = joint.Id; jointScript.Controller = joint.Controller; // todo: this is not gud jointScript.SetColor(joint.Color); if (!GameController.Instance.potatoMode) { shape.ApplyTextures(gameObject); } jointScript.SetBlueprintPosition(joint.PosA); jointScript.SetBlueprintRotation(joint.XaxisA, joint.ZaxisA); jointScript.DoRotationPositionOffset(joint.XaxisA, joint.ZaxisA); // required for joints return(jointScript); }
public static Shape GetJoint(Joint joint) { var shape = loadedShapes.GetOrAdd(joint.ShapeId, (key) => Shape.CreateBlank(joint)); return(shape); }
internal static Shape CreateBlank(Data.Joint joint) { throw new NotImplementedException($"Could not find part/block for {joint.ShapeId}"); }
/// <summary> /// create child - joint references so both know what they are connected to /// </summary> /// <param name="blueprintScript"></param> /// <param name="blueprintData"></param> private void ArrangeJointReferencesUsingData(BlueprintScript blueprintScript, BlueprintData blueprintData) { // child -> joint is a bit of a pain because scriptchild knows nothing of joints[] int childIndex = 0; IEnumerable <Child> flatDataChildList = blueprintData.Bodies.SelectMany(body => body.Childs).Select(child => child); List <ChildScript> flatLiveChildList = blueprintScript.Bodies.SelectMany(body => body.Childs).Select(child => child).ToList(); IEnumerable <Joint> flatDataJointList = blueprintData.Joints; List <JointScript> flatLiveJointsList = blueprintScript.Joints; if (blueprintData.Joints == null || blueprintData.Joints.Count == 0) { Debug.Log($"no joints found, skipping reference step"); return; } foreach (Child childData in flatDataChildList) { if (childData.Joints?.Count > 0) { ChildScript childScript = flatLiveChildList[childIndex]; childScript.connectedJoints = new List <JointScript>(); foreach (JointReference jointRef in childData.Joints) { JointScript jointScript = flatLiveJointsList.FirstOrDefault(joint => joint.Id == jointRef.Id); if (jointScript == default) { Debug.LogWarning($"child.joints: reference to missing joint!"); continue; } childScript.connectedJoints.Add(jointScript); // connect that joint to this child if it has the data to connect to this child: Joint jointData = flatDataJointList.FirstOrDefault(joint => joint.Id == jointRef.Id); if (jointData.ChildA == childScript.shapeIdx) { jointScript.childA = childScript; } if (jointData.ChildB == childScript.shapeIdx) { jointScript.childB = childScript; } } } childIndex++; } //Debug.Log($"Verifying joint-child connection references"); // verify connections, perspective: joint int jointIndex = 0; foreach (Joint jointData in flatDataJointList) { JointScript jointScript = flatLiveJointsList[jointIndex]; if (jointData.ChildA == -1 || jointData.ChildA >= flatLiveChildList.Count) { if (jointScript.childA == null) { Debug.LogError($"Invalid Joints! no joint.childA information. Attempting fix"); // todo: attempt fix: find child(s) that is connected to this jointscript, 1 child: childA, 2 childs: check childB || error } else { Debug.LogWarning($"joint.childA had invalid information! resolved via child.joints!"); } } else { ChildScript childScriptA = flatLiveChildList[jointData.ChildA]; if (childScriptA.connectedJoints == null) { childScriptA.connectedJoints = new List <JointScript>(); } if (!childScriptA.connectedJoints.Contains(jointScript)) { Debug.LogWarning($"child.joints had missing information. resolving with joint.childA info!"); childScriptA.connectedJoints.Add(jointScript); } } if (jointData.ChildB == -1 || jointData.ChildB >= flatLiveChildList.Count) { if (jointScript.childB == null) { // this is allowed. a joint doesn't have to have a childB // todo: attempt fix: find child(s) that is connected to this jointscript, 1 child: childA, 2 childs: check childB || ok } else { Debug.LogWarning($"joint.childB had invalid information! resolved via child.joints!"); // 'recovered' in prev step } } else { ChildScript childScriptB = flatLiveChildList[jointData.ChildB]; if (childScriptB.connectedJoints == null) { childScriptB.connectedJoints = new List <JointScript>(); } if (!childScriptB.connectedJoints.Contains(jointScript)) { Debug.LogWarning($"child.joints had missing information. resolving with joint.childA info!"); childScriptB.connectedJoints.Add(jointScript); } } jointIndex++; } // todo: to be complete there should be another verify run on all childs to check if connected joints actually reference them back. }