| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.ECS7.InternalComponents; |
| | 3 | | using DCL.ECSComponents; |
| | 4 | | using DCL.ECSRuntime; |
| | 5 | | using DCL.Models; |
| | 6 | | using DG.Tweening; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using UnityEngine; |
| | 9 | | using static DCL.ECSComponents.EasingFunction; |
| | 10 | | using static DG.Tweening.Ease; |
| | 11 | |
|
| | 12 | | public class ECSTweenHandler : IECSComponentHandler<PBTween> |
| | 13 | | { |
| 1 | 14 | | private static readonly Dictionary<EasingFunction, Ease> easingFunctionsMap = new Dictionary<EasingFunction,Ease>() |
| | 15 | | { |
| | 16 | | [EfLinear] = Linear, |
| | 17 | | [EfEaseinsine] = InSine, |
| | 18 | | [EfEaseoutsine] = OutSine, |
| | 19 | | [EfEasesine] = InOutSine, |
| | 20 | | [EfEaseinquad] = InQuad, |
| | 21 | | [EfEaseoutquad] = OutQuad, |
| | 22 | | [EfEasequad] = InOutQuad, |
| | 23 | | [EfEaseinexpo] = InExpo, |
| | 24 | | [EfEaseoutexpo] = OutExpo, |
| | 25 | | [EfEaseexpo] = InOutExpo, |
| | 26 | | [EfEaseinelastic] = InElastic, |
| | 27 | | [EfEaseoutelastic] = OutElastic, |
| | 28 | | [EfEaseelastic] = InOutElastic, |
| | 29 | | [EfEaseinbounce] = InBounce, |
| | 30 | | [EfEaseoutbounce] = OutBounce, |
| | 31 | | [EfEasebounce] = InOutBounce, |
| | 32 | | [EfEaseincubic] = InCubic, |
| | 33 | | [EfEaseoutcubic] = OutCubic, |
| | 34 | | [EfEasecubic] = InOutCubic, |
| | 35 | | [EfEaseinquart] = InQuart, |
| | 36 | | [EfEaseoutquart] = OutQuart, |
| | 37 | | [EfEasequart] = InOutQuart, |
| | 38 | | [EfEaseinquint] = InQuint, |
| | 39 | | [EfEaseoutquint] = OutQuint, |
| | 40 | | [EfEasequint] = InOutQuint, |
| | 41 | | [EfEaseincirc] = InCirc, |
| | 42 | | [EfEaseoutcirc] = OutCirc, |
| | 43 | | [EfEasecirc] = InOutCirc, |
| | 44 | | [EfEaseinback] = InBack, |
| | 45 | | [EfEaseoutback] = OutBack, |
| | 46 | | [EfEaseback] = InOutBack |
| | 47 | | }; |
| | 48 | |
|
| | 49 | | private readonly IInternalECSComponent<InternalTween> internalTweenComponent; |
| | 50 | | private readonly IInternalECSComponent<InternalSceneBoundsCheck> sbcInternalComponent; |
| | 51 | | private Tweener currentTweener; |
| | 52 | |
|
| 14 | 53 | | public ECSTweenHandler(IInternalECSComponent<InternalTween> internalTweenComponent, IInternalECSComponent<InternalSc |
| | 54 | | { |
| 14 | 55 | | this.internalTweenComponent = internalTweenComponent; |
| 14 | 56 | | this.sbcInternalComponent = sbcInternalComponent; |
| 14 | 57 | | } |
| | 58 | |
|
| 0 | 59 | | public void OnComponentCreated(IParcelScene scene, IDCLEntity entity) { } |
| | 60 | |
|
| | 61 | | public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity) |
| | 62 | | { |
| 15 | 63 | | currentTweener.Kill(false); |
| 15 | 64 | | internalTweenComponent.RemoveFor(scene, entity, new InternalTween() |
| | 65 | | { |
| | 66 | | removed = true |
| | 67 | | }); |
| | 68 | |
|
| | 69 | | // SBC Internal Component is reset when the Transform component is removed, not here. |
| 15 | 70 | | } |
| | 71 | |
|
| | 72 | | public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBTween model) |
| | 73 | | { |
| 18 | 74 | | if (model.ModeCase == PBTween.ModeOneofCase.None) |
| 0 | 75 | | return; |
| | 76 | |
|
| | 77 | | // by default it's playing |
| 18 | 78 | | bool isPlaying = !model.HasPlaying || model.Playing; |
| | 79 | |
|
| 18 | 80 | | var internalComponentModel = internalTweenComponent.GetFor(scene, entity)?.model ?? new InternalTween(); |
| 18 | 81 | | if (!AreSameModels(model, internalComponentModel.lastModel)) |
| | 82 | | { |
| 16 | 83 | | Transform entityTransform = entity.gameObject.transform; |
| 16 | 84 | | float durationInSeconds = model.Duration / 1000; |
| 16 | 85 | | currentTweener = internalComponentModel.tweener; |
| | 86 | |
|
| | 87 | | // There may be a tween running for the entity transform, even with internalComponentModel.tweener |
| | 88 | | // as null, e.g: during preview mode hot-reload. |
| 16 | 89 | | var transformTweens = DOTween.TweensByTarget(entityTransform, true); |
| 16 | 90 | | transformTweens?[0].Rewind(false); |
| | 91 | |
|
| 16 | 92 | | internalComponentModel.transform = entityTransform; |
| 16 | 93 | | internalComponentModel.currentTime = model.CurrentTime; |
| | 94 | |
|
| 16 | 95 | | if (!easingFunctionsMap.TryGetValue(model.EasingFunction, out Ease ease)) |
| 0 | 96 | | ease = Ease.Linear; |
| | 97 | |
|
| 16 | 98 | | switch (model.ModeCase) |
| | 99 | | { |
| | 100 | | case PBTween.ModeOneofCase.Rotate: |
| 3 | 101 | | currentTweener = SetupRotationTween(scene, entity, |
| | 102 | | ProtoConvertUtils.PBQuaternionToUnityQuaternion(model.Rotate.Start), |
| | 103 | | ProtoConvertUtils.PBQuaternionToUnityQuaternion(model.Rotate.End), |
| | 104 | | durationInSeconds, ease); |
| 3 | 105 | | break; |
| | 106 | | case PBTween.ModeOneofCase.Scale: |
| 5 | 107 | | currentTweener = SetupScaleTween(scene, entity, |
| | 108 | | ProtoConvertUtils.PBVectorToUnityVector(model.Scale.Start), |
| | 109 | | ProtoConvertUtils.PBVectorToUnityVector(model.Scale.End), |
| | 110 | | durationInSeconds, ease); |
| 5 | 111 | | break; |
| | 112 | | case PBTween.ModeOneofCase.Move: |
| | 113 | | default: |
| 8 | 114 | | currentTweener = SetupPositionTween(scene, entity, |
| | 115 | | ProtoConvertUtils.PBVectorToUnityVector(model.Move.Start), |
| | 116 | | ProtoConvertUtils.PBVectorToUnityVector(model.Move.End), |
| | 117 | | durationInSeconds, ease, model.Move.HasFaceDirection && model.Move.FaceDirection); |
| | 118 | | break; |
| | 119 | | } |
| | 120 | |
|
| 16 | 121 | | currentTweener.Goto(model.CurrentTime * durationInSeconds, isPlaying); |
| 16 | 122 | | internalComponentModel.tweener = currentTweener; |
| 16 | 123 | | internalComponentModel.tweenMode = model.ModeCase; |
| | 124 | | } |
| 2 | 125 | | else if (internalComponentModel.playing == isPlaying) |
| | 126 | | { |
| 0 | 127 | | return; |
| | 128 | | } |
| | 129 | |
|
| 18 | 130 | | internalComponentModel.playing = isPlaying; |
| | 131 | |
|
| 18 | 132 | | if (isPlaying) |
| 17 | 133 | | internalComponentModel.tweener.Play(); |
| | 134 | | else |
| 1 | 135 | | internalComponentModel.tweener.Pause(); |
| | 136 | |
|
| 18 | 137 | | internalComponentModel.lastModel = model; |
| 18 | 138 | | internalTweenComponent.PutFor(scene, entity, internalComponentModel); |
| 18 | 139 | | } |
| | 140 | |
|
| | 141 | | private static bool AreSameModels(PBTween modelA, PBTween modelB) |
| | 142 | | { |
| 18 | 143 | | if (modelB == null || modelA == null) |
| 14 | 144 | | return false; |
| | 145 | |
|
| 4 | 146 | | if (modelB.ModeCase != modelA.ModeCase |
| | 147 | | || modelB.EasingFunction != modelA.EasingFunction |
| | 148 | | || !modelB.CurrentTime.Equals(modelA.CurrentTime) |
| | 149 | | || !modelB.Duration.Equals(modelA.Duration)) |
| 2 | 150 | | return false; |
| | 151 | |
|
| 2 | 152 | | return modelA.ModeCase switch |
| | 153 | | { |
| 0 | 154 | | PBTween.ModeOneofCase.Scale => modelB.Scale.Start.Equals(modelA.Scale.Start) && modelB.Scale.End.Equa |
| 0 | 155 | | PBTween.ModeOneofCase.Rotate => modelB.Rotate.Start.Equals(modelA.Rotate.Start) && modelB.Rotate.End. |
| 2 | 156 | | PBTween.ModeOneofCase.Move => modelB.Move.Start.Equals(modelA.Move.Start) && modelB.Move.End.Equals(m |
| 0 | 157 | | PBTween.ModeOneofCase.None => modelB.Move.Start.Equals(modelA.Move.Start) && modelB.Move.End.Equals(m |
| 0 | 158 | | _ => modelB.Move.Start.Equals(modelA.Move.Start) && modelB.Move.End.Equals(modelA.Move.End) |
| | 159 | | }; |
| | 160 | | } |
| | 161 | |
|
| | 162 | | private Tweener SetupRotationTween(IParcelScene scene, IDCLEntity entity, Quaternion startRotation, |
| | 163 | | Quaternion endRotation, float durationInSeconds, Ease ease) |
| | 164 | | { |
| 3 | 165 | | var entityTransform = entity.gameObject.transform; |
| 3 | 166 | | entityTransform.localRotation = startRotation; |
| 3 | 167 | | var tweener = entityTransform.DOLocalRotateQuaternion(endRotation, durationInSeconds).SetEase(ease).SetAutoKill( |
| | 168 | |
|
| 3 | 169 | | sbcInternalComponent.OnTransformScaleRotationChanged(scene, entity); |
| | 170 | |
|
| 3 | 171 | | return tweener; |
| | 172 | | } |
| | 173 | |
|
| | 174 | | private Tweener SetupScaleTween(IParcelScene scene, IDCLEntity entity, Vector3 startScale, |
| | 175 | | Vector3 endScale, float durationInSeconds, Ease ease) |
| | 176 | | { |
| 5 | 177 | | var entityTransform = entity.gameObject.transform; |
| 5 | 178 | | entityTransform.localScale = startScale; |
| 5 | 179 | | var tweener = entityTransform.DOScale(endScale, durationInSeconds).SetEase(ease).SetAutoKill(false); |
| | 180 | |
|
| 5 | 181 | | sbcInternalComponent.OnTransformScaleRotationChanged(scene, entity); |
| | 182 | |
|
| 5 | 183 | | return tweener; |
| | 184 | | } |
| | 185 | |
|
| | 186 | | private Tweener SetupPositionTween(IParcelScene scene, IDCLEntity entity, Vector3 startPosition, |
| | 187 | | Vector3 endPosition, float durationInSeconds, Ease ease, bool faceDirection) |
| | 188 | | { |
| 8 | 189 | | var entityTransform = entity.gameObject.transform; |
| | 190 | |
|
| 8 | 191 | | if (faceDirection) |
| 2 | 192 | | entityTransform.forward = (endPosition - startPosition).normalized; |
| | 193 | |
|
| 8 | 194 | | entityTransform.localPosition = startPosition; |
| 8 | 195 | | var tweener = entityTransform.DOLocalMove(endPosition, durationInSeconds).SetEase(ease).SetAutoKill(false); |
| | 196 | |
|
| 8 | 197 | | sbcInternalComponent.SetPosition(scene, entity, entityTransform.position); |
| | 198 | |
|
| 8 | 199 | | return tweener; |
| | 200 | | } |
| | 201 | | } |