gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
привет, помоги с рефакторингом кода для unity public class WheelCollision :…
- User
привет, помоги с рефакторингом кода для unity public class WheelCollision : MonoBehaviour { private const string Cone = "Cone"; private const string RoadMarkings = "RoadMarkings"; private const string BorderTrigger = "BorderTrigger";
codeprivate WheelController wheelController; [SerializeField] private WheelType wheelType; private bool isCollidingBorder; private bool isCollidingBeyondRestrictionsFrontWheel; private bool isCollidingBeyondRestrictionsRearWheel; private BorderTrigger borderTrigger; private ViolationTriggerBeyondRestrictions violationTriggerBeyondRestrictions; private void Start() { wheelController = GetComponent<WheelController>(); } private void Update() { if (!isCollidingBorder) { borderTrigger?.EndContact(); } isCollidingBorder = false; switch (wheelType) { case WheelType.FrontWheel: if (!isCollidingBeyondRestrictionsFrontWheel) { violationTriggerBeyondRestrictions?.EndContact(wheelType); violationTriggerBeyondRestrictions = null; } isCollidingBeyondRestrictionsFrontWheel = false; break; case WheelType.RearWheel: if (!isCollidingBeyondRestrictionsRearWheel) { violationTriggerBeyondRestrictions?.EndContact(wheelType); violationTriggerBeyondRestrictions = null; } isCollidingBeyondRestrictionsRearWheel = false; break; } if (wheelController.HitCollider.CompareTag(Cone)) { if (wheelController.HitCollider.TryGetComponent(out Cone cone)) { cone.KnockedCone(); } } if (wheelController.HitCollider.CompareTag(RoadMarkings)) { if (wheelController.HitCollider.TryGetComponent(out LeavingRoadMarkingsTrigger leavingRoadMarkings)) { leavingRoadMarkings.LeavingRoadMarkings(wheelController.HitPoint); } } if (wheelController.HitCollider.CompareTag(BorderTrigger)) { if (wheelController.HitCollider.TryGetComponent(out BorderTrigger borderTrigger)) { isCollidingBorder = true; this.borderTrigger = borderTrigger; borderTrigger.TriggerBorderEnter(); } } if (wheelController.HitCollider.CompareTag(BorderTrigger)) { if (wheelController.HitCollider.TryGetComponent(out ViolationTriggerBeyondRestrictions violationTriggerBeyondRestrictions)) { switch (wheelType) { case WheelType.FrontWheel: isCollidingBeyondRestrictionsFrontWheel = true; break; case WheelType.RearWheel: isCollidingBeyondRestrictionsRearWheel = true; break; } this.violationTriggerBeyondRestrictions = violationTriggerBeyondRestrictions; violationTriggerBeyondRestrictions.HittingObstacle(wheelController.HitNormal, wheelType); } } } - Assistant
- User
помоги еще с этим кодом public class ViolationTriggerBeyondRestrictions : SerializedMonoBehaviour { [field: SerializeField] public ITrafficViolation TrafficViolation { get; private set; }
code - Assistant
Вот рефакторированный код:
csharppublic class ViolationTriggerBeyondRestrictions : SerializedMonoBehaviour { [field: SerializeField] public ITrafficViolation TrafficViolation { get; private set; } [Inject] private IViolationsTransmitter transmitter; private Dictionary<WheelType, bool> isContact; private void Start() { isContact = new Dictionary<WheelType, bool>(); isContact[WheelType.FrontWheel] = false; isContact[WheelType.RearWheel] = false; } public void EndContact(WheelType wheelType) { isContact[wheelType] = false; } public void HittingObstacle(Vector3 contacts, WheelType wheelType) { if (contacts.normalized == Vector3.up && !isContact[wheelType]) { isContact[wheelType] = true; OnViolationTriggered(TrafficViolation); } } private void OnViolationTriggered(ITrafficViolation violation) { transmitter.TriggerViolation(violation); } }