|
| 1 | +using Unity.Collections; |
| 2 | +using UnityEngine; |
| 3 | +using UnityEngine.LowLevelPhysics2D; |
| 4 | +using UnityEngine.UIElements; |
| 5 | + |
| 6 | +public class ConveyorBelt : 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 | + |
| 15 | + private Button m_SpawnButton; |
| 16 | + private PhysicsBody m_ConveyorBeltBody; |
| 17 | + private PhysicsShape m_ConveyorBeltShape; |
| 18 | + |
| 19 | + private float m_ConveyorSpeed; |
| 20 | + private float m_ConveyorAngle; |
| 21 | + |
| 22 | + private void OnEnable() |
| 23 | + { |
| 24 | + m_SandboxManager = FindFirstObjectByType<SandboxManager>(); |
| 25 | + m_SceneManifest = FindFirstObjectByType<SceneManifest>(); |
| 26 | + m_UIDocument = GetComponent<UIDocument>(); |
| 27 | + |
| 28 | + m_CameraManipulator = FindFirstObjectByType<CameraManipulator>(); |
| 29 | + m_CameraManipulator.CameraSize = 12; |
| 30 | + m_CameraManipulator.CameraPosition = new Vector2(0f, 7f); |
| 31 | + |
| 32 | + // Set up the scene reset action. |
| 33 | + m_SandboxManager.SceneResetAction = SetupScene; |
| 34 | + |
| 35 | + // Set Overrides. |
| 36 | + m_SandboxManager.SetOverrideColorShapeState(false); |
| 37 | + |
| 38 | + m_PhysicsWorld = PhysicsWorld.defaultWorld; |
| 39 | + |
| 40 | + m_ConveyorSpeed = 20f; |
| 41 | + m_ConveyorAngle = 0f; |
| 42 | + |
| 43 | + SetupOptions(); |
| 44 | + |
| 45 | + SetupScene(); |
| 46 | + } |
| 47 | + |
| 48 | + private void OnDisable() |
| 49 | + { |
| 50 | + // Reset overrides. |
| 51 | + m_SandboxManager.ResetOverrideColorShapeState(); |
| 52 | + } |
| 53 | + |
| 54 | + private void SetupOptions() |
| 55 | + { |
| 56 | + var root = m_UIDocument.rootVisualElement; |
| 57 | + |
| 58 | + { |
| 59 | + // Menu Region (for camera manipulator). |
| 60 | + var menuRegion = root.Q<VisualElement>("menu-region"); |
| 61 | + menuRegion.RegisterCallback<PointerEnterEvent>(_ => ++m_CameraManipulator.OverlapUI); |
| 62 | + menuRegion.RegisterCallback<PointerLeaveEvent>(_ => --m_CameraManipulator.OverlapUI); |
| 63 | + |
| 64 | + // Conveyor Speed. |
| 65 | + var conveyorSpeed = root.Q<Slider>("conveyor-speed"); |
| 66 | + conveyorSpeed.value = m_ConveyorSpeed; |
| 67 | + conveyorSpeed.RegisterValueChangedCallback(evt => |
| 68 | + { |
| 69 | + m_ConveyorSpeed = evt.newValue; |
| 70 | + |
| 71 | + // Update the tangent speed. |
| 72 | + var surfaceMaterial = m_ConveyorBeltShape.surfaceMaterial; |
| 73 | + surfaceMaterial.tangentSpeed = m_ConveyorSpeed; |
| 74 | + m_ConveyorBeltShape.surfaceMaterial = surfaceMaterial; |
| 75 | + }); |
| 76 | + |
| 77 | + // Conveyor Angle. |
| 78 | + var conveyorAngle = root.Q<Slider>("conveyor-angle"); |
| 79 | + conveyorAngle.value = m_ConveyorAngle; |
| 80 | + conveyorAngle.RegisterValueChangedCallback(evt => |
| 81 | + { |
| 82 | + m_ConveyorAngle = evt.newValue; |
| 83 | + |
| 84 | + // Update the conveyor angle. |
| 85 | + m_ConveyorBeltBody.rotation = new PhysicsRotate(PhysicsMath.ToRadians(m_ConveyorAngle)); |
| 86 | + }); |
| 87 | + |
| 88 | + // Spawn. |
| 89 | + m_SpawnButton = root.Q<Button>("spawn"); |
| 90 | + m_SpawnButton.clicked += () => Spawn(10); |
| 91 | + |
| 92 | + // Reset Scene. |
| 93 | + var resetScene = root.Q<Button>("reset-scene"); |
| 94 | + resetScene.clicked += SetupScene; |
| 95 | + |
| 96 | + // Fetch the scene description. |
| 97 | + var sceneDescription = root.Q<Label>("scene-description"); |
| 98 | + sceneDescription.text = $"\"{m_SceneManifest.LoadedSceneName}\"\n{m_SceneManifest.LoadedSceneDescription}"; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private void SetupScene() |
| 103 | + { |
| 104 | + // Reset the scene state. |
| 105 | + m_SandboxManager.ResetSceneState(); |
| 106 | + |
| 107 | + var bodies = m_SandboxManager.Bodies; |
| 108 | + |
| 109 | + // Ground. |
| 110 | + { |
| 111 | + var body = m_PhysicsWorld.CreateBody(); |
| 112 | + bodies.Add(body); |
| 113 | + using var vertices = new NativeList<Vector2>(Allocator.Temp) |
| 114 | + { |
| 115 | + new(-20f, 0f), |
| 116 | + new(-20f, 23f), |
| 117 | + new(20f, 23f), |
| 118 | + new(20f, 0f) |
| 119 | + }; |
| 120 | + body.CreateChain(new ChainGeometry(vertices.AsArray()), PhysicsChainDefinition.defaultDefinition); |
| 121 | + } |
| 122 | + |
| 123 | + // Platform. |
| 124 | + { |
| 125 | + m_ConveyorBeltBody = m_PhysicsWorld.CreateBody(new PhysicsBodyDefinition { position = Vector2.up * 8f }); |
| 126 | + bodies.Add(m_ConveyorBeltBody); |
| 127 | + |
| 128 | + var geometry = PolygonGeometry.CreateBox(new Vector2(20f, 0.5f), 0.25f); |
| 129 | + var shapeDef = new PhysicsShapeDefinition { surfaceMaterial = new PhysicsShape.SurfaceMaterial { friction = 0.8f, tangentSpeed = 2f } }; |
| 130 | + m_ConveyorBeltShape = m_ConveyorBeltBody.CreateShape(geometry, shapeDef); |
| 131 | + } |
| 132 | + |
| 133 | + // Boxes. |
| 134 | + { |
| 135 | + Spawn(10); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + private void Spawn(int spawnCount) |
| 140 | + { |
| 141 | + var bodies = m_SandboxManager.Bodies; |
| 142 | + ref var random = ref m_SandboxManager.Random; |
| 143 | + |
| 144 | + for (var n = 0; n < spawnCount; ++n) |
| 145 | + { |
| 146 | + var spawnPosition = new Vector2(random.NextFloat(-5f, 5f), random.NextFloat(9f, 20f)); |
| 147 | + var scale = random.NextFloat(0.05f, 1f); |
| 148 | + var radius = random.NextFloat(0f, 0.2f); |
| 149 | + |
| 150 | + var body = m_PhysicsWorld.CreateBody(new PhysicsBodyDefinition { bodyType = RigidbodyType2D.Dynamic, position = spawnPosition }); |
| 151 | + bodies.Add(body); |
| 152 | + body.CreateShape(PolygonGeometry.CreateBox(Vector2.one * scale, radius)); |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments