|
| 1 | +using System; |
| 2 | +using UnityEngine; |
| 3 | +using UnityEngine.LowLevelPhysics2D; |
| 4 | +using UnityEngine.UIElements; |
| 5 | + |
| 6 | +public class ModifyGeometry : MonoBehaviour |
| 7 | +{ |
| 8 | + private SandboxManager m_SandboxManager; |
| 9 | + private SceneManifest m_SceneManifest; |
| 10 | + private UIDocument m_UIDocument; |
| 11 | + private CameraManipulator m_CameraManipulator; |
| 12 | + |
| 13 | + private PhysicsWorld m_PhysicsWorld; |
| 14 | + private PhysicsBody m_ChangerBody; |
| 15 | + private PhysicsShape m_ChangerShape; |
| 16 | + |
| 17 | + private enum GeometryType |
| 18 | + { |
| 19 | + Circle, |
| 20 | + Capsule, |
| 21 | + Segment, |
| 22 | + Polygon |
| 23 | + } |
| 24 | + |
| 25 | + private RigidbodyType2D m_BodyType; |
| 26 | + private GeometryType m_GeometryType; |
| 27 | + private float m_GeometryScale; |
| 28 | + |
| 29 | + private void OnEnable() |
| 30 | + { |
| 31 | + m_SandboxManager = FindFirstObjectByType<SandboxManager>(); |
| 32 | + m_SceneManifest = FindFirstObjectByType<SceneManifest>(); |
| 33 | + m_UIDocument = GetComponent<UIDocument>(); |
| 34 | + |
| 35 | + m_CameraManipulator = FindFirstObjectByType<CameraManipulator>(); |
| 36 | + m_CameraManipulator.CameraSize = 6; |
| 37 | + m_CameraManipulator.CameraPosition = new Vector2(0f, 5f); |
| 38 | + |
| 39 | + // Set up the scene reset action. |
| 40 | + m_SandboxManager.SceneResetAction = SetupScene; |
| 41 | + |
| 42 | + // Set Overrides. |
| 43 | + m_SandboxManager.SetOverrideColorShapeState(false); |
| 44 | + |
| 45 | + m_PhysicsWorld = PhysicsWorld.defaultWorld; |
| 46 | + |
| 47 | + m_BodyType = RigidbodyType2D.Kinematic; |
| 48 | + m_GeometryType = GeometryType.Circle; |
| 49 | + m_GeometryScale = 1f; |
| 50 | + |
| 51 | + SetupOptions(); |
| 52 | + |
| 53 | + SetupScene(); |
| 54 | + } |
| 55 | + |
| 56 | + private void OnDisable() |
| 57 | + { |
| 58 | + // Reset overrides. |
| 59 | + m_SandboxManager.ResetOverrideColorShapeState(); |
| 60 | + } |
| 61 | + |
| 62 | + private void SetupOptions() |
| 63 | + { |
| 64 | + var root = m_UIDocument.rootVisualElement; |
| 65 | + |
| 66 | + { |
| 67 | + // Menu Region (for camera manipulator). |
| 68 | + var menuRegion = root.Q<VisualElement>("menu-region"); |
| 69 | + menuRegion.RegisterCallback<PointerEnterEvent>(_ => ++m_CameraManipulator.OverlapUI); |
| 70 | + menuRegion.RegisterCallback<PointerLeaveEvent>(_ => --m_CameraManipulator.OverlapUI); |
| 71 | + |
| 72 | + // Body Type. |
| 73 | + var bodyType = root.Q<EnumField>("body-type"); |
| 74 | + bodyType.value = m_BodyType; |
| 75 | + bodyType.RegisterValueChangedCallback(evt => |
| 76 | + { |
| 77 | + m_BodyType = (RigidbodyType2D)evt.newValue; |
| 78 | + m_ChangerBody.bodyType = m_BodyType; |
| 79 | + }); |
| 80 | + |
| 81 | + // Geometry Type. |
| 82 | + var geometryType = root.Q<EnumField>("geometry-type"); |
| 83 | + geometryType.value = m_GeometryType; |
| 84 | + geometryType.RegisterValueChangedCallback(evt => |
| 85 | + { |
| 86 | + m_GeometryType = (GeometryType)evt.newValue; |
| 87 | + UpdateShape(); |
| 88 | + }); |
| 89 | + |
| 90 | + // Geometry Scale. |
| 91 | + var geometryScale = root.Q<Slider>("geometry-scale"); |
| 92 | + geometryScale.value = m_GeometryScale; |
| 93 | + geometryScale.RegisterValueChangedCallback(evt => |
| 94 | + { |
| 95 | + m_GeometryScale = evt.newValue; |
| 96 | + UpdateShape(); |
| 97 | + }); |
| 98 | + |
| 99 | + // Reset Scene. |
| 100 | + var resetScene = root.Q<Button>("reset-scene"); |
| 101 | + resetScene.clicked += SetupScene; |
| 102 | + |
| 103 | + // Fetch the scene description. |
| 104 | + var sceneDescription = root.Q<Label>("scene-description"); |
| 105 | + sceneDescription.text = $"\"{m_SceneManifest.LoadedSceneName}\"\n{m_SceneManifest.LoadedSceneDescription}"; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + private void SetupScene() |
| 110 | + { |
| 111 | + // Reset the scene state. |
| 112 | + m_SandboxManager.ResetSceneState(); |
| 113 | + |
| 114 | + var bodies = m_SandboxManager.Bodies; |
| 115 | + |
| 116 | + // Ground. |
| 117 | + { |
| 118 | + var body = m_PhysicsWorld.CreateBody(); |
| 119 | + bodies.Add(body); |
| 120 | + body.CreateShape(PolygonGeometry.CreateBox(new Vector2(100f, 50f), 0f, Vector2.down * 25f)); |
| 121 | + } |
| 122 | + |
| 123 | + // Interact Shape. |
| 124 | + { |
| 125 | + var body = m_PhysicsWorld.CreateBody(new PhysicsBodyDefinition { bodyType = RigidbodyType2D.Dynamic, position = Vector2.up * 4f }); |
| 126 | + bodies.Add(body); |
| 127 | + body.CreateShape(PolygonGeometry.CreateBox(Vector2.one * 2f)); |
| 128 | + } |
| 129 | + |
| 130 | + // Shape Changer. |
| 131 | + { |
| 132 | + m_ChangerBody = m_PhysicsWorld.CreateBody(new PhysicsBodyDefinition { bodyType = m_BodyType, position = Vector2.up }); |
| 133 | + bodies.Add(m_ChangerBody); |
| 134 | + m_ChangerShape = m_ChangerBody.CreateShape(circleGeometry); |
| 135 | + |
| 136 | + // Update the geometry if this isn't the current default. |
| 137 | + if (m_GeometryType != GeometryType.Circle) |
| 138 | + UpdateShape(); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private void UpdateShape() |
| 143 | + { |
| 144 | + // Finish if the changer shape isn't valid. |
| 145 | + if (!m_ChangerShape.isValid) |
| 146 | + return; |
| 147 | + |
| 148 | + switch (m_GeometryType) |
| 149 | + { |
| 150 | + case GeometryType.Circle: |
| 151 | + m_ChangerShape.circleGeometry = circleGeometry; |
| 152 | + return; |
| 153 | + |
| 154 | + case GeometryType.Capsule: |
| 155 | + m_ChangerShape.capsuleGeometry = capsuleGeometry; |
| 156 | + return; |
| 157 | + |
| 158 | + case GeometryType.Segment: |
| 159 | + m_ChangerShape.segmentGeometry = segmentGeometry; |
| 160 | + return; |
| 161 | + |
| 162 | + case GeometryType.Polygon: |
| 163 | + m_ChangerShape.polygonGeometry = polygonGeometry; |
| 164 | + return; |
| 165 | + |
| 166 | + default: |
| 167 | + throw new ArgumentOutOfRangeException(); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + private CircleGeometry circleGeometry => new CircleGeometry { radius = 0.5f * m_GeometryScale }; |
| 172 | + private CapsuleGeometry capsuleGeometry => new CapsuleGeometry { center1 = Vector2.left * 0.5f * m_GeometryScale, center2 = Vector2.right * 0.5f * m_GeometryScale, radius = 0.5f * m_GeometryScale }; |
| 173 | + private SegmentGeometry segmentGeometry => new SegmentGeometry { point1 = Vector2.left * 0.5f * m_GeometryScale, point2 = Vector2.right * 0.5f * m_GeometryScale }; |
| 174 | + private PolygonGeometry polygonGeometry => PolygonGeometry.CreateBox(new Vector2(m_GeometryScale, 1.5f * m_GeometryScale)); |
| 175 | +} |
0 commit comments