| | 1 | | using System.Collections; |
| | 2 | | using DCL.Controllers; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Models; |
| | 5 | | using UnityEngine; |
| | 6 | | using Decentraland.Sdk.Ecs6; |
| | 7 | | using MainScripts.DCL.Components; |
| | 8 | |
|
| | 9 | | namespace DCL.Components |
| | 10 | | { |
| | 11 | | public class DCLTransform : IEntityComponent, IOutOfSceneBoundariesHandler |
| | 12 | | { |
| | 13 | | [System.Serializable] |
| | 14 | | public class Model : BaseModel |
| | 15 | | { |
| 53 | 16 | | public Vector3 position = Vector3.zero; |
| 53 | 17 | | public Quaternion rotation = Quaternion.identity; |
| 53 | 18 | | public Vector3 scale = Vector3.one; |
| | 19 | |
|
| | 20 | | public override BaseModel GetDataFromJSON(string json) |
| | 21 | | { |
| 229 | 22 | | DCLTransformUtils.DecodeTransform(json, ref model); |
| 229 | 23 | | return model; |
| | 24 | | } |
| | 25 | |
|
| | 26 | | public override BaseModel GetDataFromPb(ComponentBodyPayload pbModel) |
| | 27 | | { |
| 0 | 28 | | if (pbModel.PayloadCase != ComponentBodyPayload.PayloadOneofCase.Transform) |
| 0 | 29 | | return Utils.SafeUnimplemented<DCLTransform, Model>(expected: ComponentBodyPayload.PayloadOneofCase. |
| | 30 | |
|
| 0 | 31 | | var pb = new Model(); |
| 0 | 32 | | if (pbModel.Transform.Position != null) pb.position = pbModel.Transform.Position.AsUnityVector3(); |
| 0 | 33 | | if (pbModel.Transform.Scale != null) pb.scale = pbModel.Transform.Scale.AsUnityVector3(); |
| 0 | 34 | | if (pbModel.Transform.Rotation != null) pb.rotation = pbModel.Transform.Rotation.AsUnityQuaternion(); |
| | 35 | |
|
| 0 | 36 | | return pb; |
| | 37 | | } |
| | 38 | |
|
| | 39 | | } |
| | 40 | |
|
| 1 | 41 | | private static Model model = new (); |
| | 42 | |
|
| 133 | 43 | | public void Cleanup() { } |
| | 44 | |
|
| 15 | 45 | | public string componentName => "Transform"; |
| 171 | 46 | | public IParcelScene scene { get; private set; } |
| 1316 | 47 | | public IDCLEntity entity { get; private set; } |
| 0 | 48 | | public Transform GetTransform() => null; |
| | 49 | |
|
| | 50 | | public void Initialize(IParcelScene scene, IDCLEntity entity) |
| | 51 | | { |
| 171 | 52 | | this.scene = scene; |
| 171 | 53 | | this.entity = entity; |
| 171 | 54 | | } |
| | 55 | |
|
| | 56 | | public void UpdateFromJSON(string json) |
| | 57 | | { |
| 229 | 58 | | model.GetDataFromJSON(json); |
| 229 | 59 | | UpdateFromModel(model); |
| 229 | 60 | | } |
| | 61 | |
|
| | 62 | |
|
| | 63 | | public void UpdateFromPb(ComponentBodyPayload payload) |
| | 64 | | { |
| 0 | 65 | | Model newModel = (Model)model.GetDataFromPb(payload); |
| 0 | 66 | | UpdateFromModel(newModel); |
| 0 | 67 | | } |
| | 68 | |
|
| | 69 | | public void UpdateFromModel(BaseModel model) |
| | 70 | | { |
| 229 | 71 | | DCLTransform.model = model as Model; |
| | 72 | |
|
| | 73 | | // AvatarShape interpolation hack: we don't apply avatars position and rotation directly to the transform |
| | 74 | | // and those values are used for the interpolation. |
| 229 | 75 | | if (entity.OnTransformChange != null) |
| | 76 | | { |
| 0 | 77 | | entity.OnTransformChange.Invoke(DCLTransform.model.position, DCLTransform.model.rotation); |
| | 78 | | } |
| | 79 | | else |
| | 80 | | { |
| 229 | 81 | | entity.gameObject.transform.localPosition = DCLTransform.model.position; |
| 229 | 82 | | entity.gameObject.transform.localRotation = DCLTransform.model.rotation; |
| | 83 | | } |
| | 84 | |
|
| 229 | 85 | | entity.gameObject.transform.localScale = DCLTransform.model.scale; |
| 229 | 86 | | entity.gameObject.transform.CapGlobalValuesToMax(); |
| 229 | 87 | | } |
| | 88 | |
|
| 0 | 89 | | public IEnumerator ApplyChanges(BaseModel model) { return null; } |
| | 90 | |
|
| 0 | 91 | | public void RaiseOnAppliedChanges() { } |
| | 92 | |
|
| 0 | 93 | | public bool IsValid() => true; |
| 0 | 94 | | public BaseModel GetModel() => DCLTransform.model; |
| 0 | 95 | | public int GetClassId() => (int) CLASS_ID_COMPONENT.TRANSFORM; |
| 0 | 96 | | public void UpdateOutOfBoundariesState(bool enable) { } |
| | 97 | |
|
| | 98 | |
|
| | 99 | | } |
| | 100 | | } |