1- using System ;
1+ using MSCLoader ;
2+ using System ;
23using System . Collections ;
34using System . Linq ;
45using TommoJProductions . ModApi . Attachable . CallBacks ;
@@ -89,6 +90,10 @@ public class PartSettings
8990 /// </summary>
9091 public AssembleType assembleType = AssembleType . static_rigidbodyDelete ;
9192 /// <summary>
93+ /// Represents '<see cref="AssembleType.joint"/>' settings.
94+ /// </summary>
95+ public AssemblyTypeJointSettings assemblyTypeJointSettings = new AssemblyTypeJointSettings ( ) { installPointRigidbodies = null } ;
96+ /// <summary>
9297 /// Represents the layer to send a part that is installed
9398 /// </summary>
9499 public LayerMasksEnum installedPartToLayer = LayerMasksEnum . Parts ;
@@ -119,6 +124,28 @@ public enum AssembleType
119124 /// </summary>
120125 joint
121126 }
127+ /// <summary>
128+ /// Represents settings for assembly type, joint
129+ /// </summary>
130+ public class AssemblyTypeJointSettings
131+ {
132+ /// <summary>
133+ /// Represents a list of install point rigidbodies for when using assemblyType:Joint
134+ /// </summary>
135+ public Rigidbody [ ] installPointRigidbodies ;
136+ /// <summary>
137+ /// Represents the break force to break this joint. NOTE: unbreakable joints are <see cref="float.PositiveInfinity"/>.
138+ /// </summary>
139+ public float breakForce = float . PositiveInfinity ;
140+ /// <summary>
141+ /// Inits new joint settings class and assigns rbs
142+ /// </summary>
143+ /// <param name="rigidbodies"></param>
144+ public AssemblyTypeJointSettings ( params Rigidbody [ ] rigidbodies )
145+ {
146+ installPointRigidbodies = rigidbodies ;
147+ }
148+ }
122149
123150 #endregion
124151
@@ -198,6 +225,10 @@ public int installPointIndex
198225 /// Represents the cached mass of the parts rigidbody.mass property.
199226 /// </summary>
200227 private float cachedMass ;
228+ /// <summary>
229+ /// Represents the fixed joint when using <see cref="AssembleType.joint"/>.
230+ /// </summary>
231+ private FixedJoint joint ;
201232
202233 #endregion
203234
@@ -219,6 +250,10 @@ void OnEnable()
219250 if ( installed )
220251 StartCoroutine ( partInstalled ( ) ) ;
221252 }
253+ void OnJointBreak ( float breakForce )
254+ {
255+ disassemble ( ) ;
256+ }
222257
223258 #endregion
224259
@@ -246,6 +281,7 @@ private IEnumerator partInstalled()
246281 mouseOverReset ( ) ;
247282 yield return null ;
248283 }
284+ installedRoutine = null ;
249285 }
250286 /// <summary>
251287 /// Represents the part in a trigger logic.
@@ -266,6 +302,7 @@ private IEnumerator partInTrigger(Collider other)
266302 }
267303 yield return null ;
268304 }
305+ triggerRoutine = null ;
269306 }
270307
271308 #endregion
@@ -295,17 +332,26 @@ void callback_onTriggerExit(Collider other, TriggerCallback callback_ref)
295332 {
296333 if ( installPointColliders . Contains ( callback_ref . callbackCollider ) )
297334 {
335+ ModClient . guiAssemble = false ;
336+ inTrigger = false ;
298337 if ( triggerRoutine != null )
299338 StopCoroutine ( triggerRoutine ) ;
300- inTrigger = false ;
301- ModClient . guiAssemble = false ;
302339 }
303340 }
341+ void callback_onJointBreak ( float breakForce , TriggerCallback callback_ref )
342+ {
343+ disassemble ( ) ;
344+ }
304345
305346 #endregion
306347
307348 #region Methods
308349
350+ private void vaildiatePart ( )
351+ {
352+ if ( partSettings . assembleType == AssembleType . joint && partSettings . assemblyTypeJointSettings . installPointRigidbodies . Length <= 0 )
353+ ModConsole . Print ( "NOTE: assembly type is 'joint' but no install point rigidbodies have been assigned!!! error!!!" ) ;
354+ }
309355 /// <summary>
310356 /// Initializes this part.
311357 /// </summary>
@@ -317,12 +363,14 @@ void callback_onTriggerExit(Collider other, TriggerCallback callback_ref)
317363 // Written, 08.09.2021
318364
319365 this . partSettings = partSettings ;
320-
366+ vaildiatePart ( ) ;
321367 triggers = triggerRefs ;
322368
323369 loadedSaveInfo = saveInfo ?? defaultSaveInfo ?? new PartSaveInfo ( ) ;
324370 installPointColliders = new Collider [ triggerRefs . Length ] ;
325371
372+ makePartPickable ( ! loadedSaveInfo . installed ) ;
373+
326374 if ( triggerRefs . Length > 0 )
327375 {
328376 TriggerCallback callback ;
@@ -334,6 +382,7 @@ void callback_onTriggerExit(Collider other, TriggerCallback callback_ref)
334382 callback = triggers [ i ] . triggerGameObject . AddComponent < TriggerCallback > ( ) ;
335383 callback . onTriggerExit += callback_onTriggerExit ;
336384 callback . onTriggerEnter += callback_onTriggerEnter ;
385+ //callback.onJointBreak += callback_onJointBreak;
337386 }
338387 if ( installed )
339388 assemble ( installPointColliders [ installPointIndex ] , false ) ;
@@ -356,7 +405,7 @@ public void assemble(Collider installPoint, bool playSound = true)
356405 inTrigger = false ;
357406 installPoint . enabled = false ;
358407 installPointIndex = Array . IndexOf ( installPointColliders , installPoint ) ;
359- makePartPickable ( false , partSettings . notInstalledPartToLayer ) ;
408+ makePartPickable ( false ) ;
360409 transform . parent = installPoint . transform ;
361410 transform . localPosition = Vector3 . zero ;
362411 transform . localEulerAngles = Vector3 . zero ;
@@ -372,6 +421,12 @@ public void assemble(Collider installPoint, bool playSound = true)
372421 case AssembleType . static_rigidibodySetKinematic :
373422 cachedRigidBody . isKinematic = true ;
374423 break ;
424+ case AssembleType . joint :
425+ joint = gameObject . AddComponent < FixedJoint > ( ) ;
426+ joint . connectedBody = partSettings . assemblyTypeJointSettings . installPointRigidbodies . Length > - 1 ? partSettings . assemblyTypeJointSettings . installPointRigidbodies [ installPointIndex ] : installPoint . transform . GetComponentInParent < Rigidbody > ( ) ;
427+ joint . breakForce = partSettings . assemblyTypeJointSettings . breakForce ;
428+ joint . breakTorque = joint . breakForce / 2 ;
429+ break ;
375430 }
376431 }
377432
@@ -395,7 +450,7 @@ public void disassemble(bool playSound = true)
395450 installed = false ;
396451 if ( mouseOver )
397452 mouseOverReset ( ) ;
398- makePartPickable ( true , partSettings . installedPartToLayer ) ;
453+ makePartPickable ( true ) ;
399454 transform . parent = null ;
400455 setActiveAttachedToTrigger ( true ) ;
401456
@@ -408,6 +463,9 @@ public void disassemble(bool playSound = true)
408463 case AssembleType . static_rigidibodySetKinematic :
409464 cachedRigidBody . isKinematic = false ;
410465 break ;
466+ case AssembleType . joint :
467+ Destroy ( joint ) ;
468+ break ;
411469 }
412470
413471 if ( playSound )
@@ -454,15 +512,15 @@ public PartSaveInfo getSaveInfo()
454512 /// </summary>
455513 /// <param name="inPickable">Make part pickable?</param>
456514 /// <param name="inLayer">Make part on different layer</param>
457- public void makePartPickable ( bool inPickable , LayerMasksEnum inLayer = LayerMasksEnum . Parts )
515+ public void makePartPickable ( bool inPickable )
458516 {
459517 // Written, 14.08.2018 | Modified, 10.09.2021
460518
461519 if ( inPickable )
462520 gameObject . tag = "PART" ;
463521 else
464522 gameObject . tag = "DontPickThis" ;
465- gameObject . layer = inLayer . layer ( ) ;
523+ gameObject . layer = ( inPickable ? partSettings . notInstalledPartToLayer : partSettings . installedPartToLayer ) . layer ( ) ;
466524 }
467525 /// <summary>
468526 /// ends (resets) a gui interaction.
0 commit comments