@@ -5,20 +5,57 @@ namespace LevelImposter.Builders;
55
66internal class PhysicsObjectBuilder : IElemBuilder
77{
8+ private bool _isCameraFixed ;
9+
810 public void OnBuild ( LIElement elem , GameObject obj )
911 {
10- /*
1112 if ( elem . type != "util-physics" )
1213 return ;
1314
14- Rigidbody2D rb = obj.AddComponent<Rigidbody2D>();
15- rb.mass = elem.properties.mass ?? 10.0f;
16- rb.drag = elem.properties.drag ?? 100.0f;
17- rb.freezeRotation = elem.properties.freezeRotation ?? true;
15+ // Add Rigidbody2D
16+ var rb = obj . AddComponent < Rigidbody2D > ( ) ;
17+ rb . mass = elem . properties . physicsMass ?? 10.0f ;
18+ rb . drag = elem . properties . physicsDrag ?? 100.0f ;
19+ rb . angularDrag = elem . properties . physicsAngularDrag ?? 100.0f ;
20+ rb . freezeRotation = elem . properties . physicsFreezeRotation ?? false ;
1821 rb . gravityScale = 0 ;
1922
20- obj.layer = (int)Layer.Default;
21- */
22- // TODO: Sync physics objects over network
23+ // Add Constraints
24+ var constraints = RigidbodyConstraints2D . None ;
25+ if ( elem . properties . physicsFreezeX ?? false )
26+ constraints |= RigidbodyConstraints2D . FreezePositionX ;
27+ if ( elem . properties . physicsFreezeY ?? false )
28+ constraints |= RigidbodyConstraints2D . FreezePositionY ;
29+ rb . constraints = constraints ;
30+
31+ // Create Physics Material
32+ var physicsMaterial = new PhysicsMaterial2D
33+ {
34+ bounciness = elem . properties . physicsBounciness ?? 0.6f ,
35+ friction = elem . properties . physicsFriction ?? 0.6f
36+ } ;
37+ rb . sharedMaterial = physicsMaterial ;
38+
39+ // Set Layer
40+ obj . layer = ( int ) Layer . Physics ;
41+
42+ // Add Physics Object Component
43+ obj . AddComponent < LIPhysicsObject > ( ) ;
44+
45+ // Fix Camera
46+ if ( _isCameraFixed )
47+ return ;
48+
49+ // Fix Camera to render physics objects
50+ var camera = Camera . main ;
51+ if ( camera != null )
52+ camera . cullingMask |= 1 << ( int ) Layer . Physics ;
53+
54+ // Fix Shadow camera to render physics objects
55+ var shadowCamera = camera ? . transform . Find ( "ShadowCamera" ) ? . GetComponent < Camera > ( ) ;
56+ if ( shadowCamera != null )
57+ shadowCamera . cullingMask |= 1 << ( int ) Layer . Physics ;
58+
59+ _isCameraFixed = true ;
2360 }
2461}
0 commit comments