| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.Models; |
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL.ECS7.InternalComponents |
| | 8 | | { |
| | 9 | | public static class InternalSceneBoundsCheckExtensions |
| | 10 | | { |
| | 11 | | public static void SetPosition(this IInternalECSComponent<InternalSceneBoundsCheck> sbcInternalComponent, |
| | 12 | | IParcelScene scene, IDCLEntity entity, Vector3 newEntityPosition, bool createComponentIfMissing = true) |
| | 13 | | { |
| 28 | 14 | | var model = sbcInternalComponent.GetFor(scene, entity.entityId)?.model; |
| | 15 | |
|
| | 16 | | InternalSceneBoundsCheck finalModel; |
| 28 | 17 | | if (model == null) |
| | 18 | | { |
| 25 | 19 | | if (!createComponentIfMissing) |
| 3 | 20 | | return; |
| | 21 | |
|
| 22 | 22 | | finalModel = new InternalSceneBoundsCheck(new Bounds()); |
| | 23 | | } |
| | 24 | | else |
| | 25 | | { |
| 3 | 26 | | finalModel = model.Value; |
| | 27 | | } |
| | 28 | |
|
| 25 | 29 | | finalModel.entityPosition = newEntityPosition; |
| | 30 | |
|
| 25 | 31 | | sbcInternalComponent.PutFor(scene, entity.entityId, finalModel); |
| | 32 | |
|
| | 33 | | // Update children position in their SBCComponent as well |
| 25 | 34 | | IList<long> childrenId = entity.childrenId; |
| 50 | 35 | | for (int i = 0; i < childrenId.Count; i++) |
| | 36 | | { |
| 0 | 37 | | if (scene.entities.TryGetValue(childrenId[i], out IDCLEntity childEntity)) |
| | 38 | | { |
| 0 | 39 | | sbcInternalComponent.SetPosition(scene, childEntity, childEntity.gameObject.transform.position, crea |
| | 40 | | } |
| | 41 | | } |
| 25 | 42 | | } |
| | 43 | |
|
| | 44 | | public static void OnTransformScaleRotationChanged(this IInternalECSComponent<InternalSceneBoundsCheck> sbcInter |
| | 45 | | IParcelScene scene, IDCLEntity entity) |
| | 46 | | { |
| 10 | 47 | | var model = sbcInternalComponent.GetFor(scene, entity)?.model ?? new InternalSceneBoundsCheck(new Bounds()); |
| | 48 | |
|
| | 49 | | // Mesh bounds need to be recalculated |
| 10 | 50 | | model.meshesDirty = true; |
| | 51 | |
|
| 10 | 52 | | sbcInternalComponent.PutFor(scene, entity, model); |
| | 53 | |
|
| | 54 | | // Update children 'meshesDirty' in their SBCComponent as well |
| 10 | 55 | | IList<long> childrenId = entity.childrenId; |
| 20 | 56 | | for (int i = 0; i < childrenId.Count; i++) |
| | 57 | | { |
| 0 | 58 | | if (scene.entities.TryGetValue(childrenId[i], out IDCLEntity childEntity)) |
| | 59 | | { |
| 0 | 60 | | sbcInternalComponent.OnTransformScaleRotationChanged(scene, childEntity); |
| | 61 | | } |
| | 62 | | } |
| 10 | 63 | | } |
| | 64 | |
|
| | 65 | | public static void SetRenderers(this IInternalECSComponent<InternalSceneBoundsCheck> sbcInternalComponent, |
| | 66 | | IParcelScene scene, IDCLEntity entity, IList<Renderer> newRenderersCollection) |
| | 67 | | { |
| 0 | 68 | | var model = sbcInternalComponent.GetFor(scene, entity)?.model ?? new InternalSceneBoundsCheck(new Bounds()); |
| 0 | 69 | | model.renderers = newRenderersCollection; |
| 0 | 70 | | model.meshesDirty = true; |
| | 71 | |
|
| 0 | 72 | | sbcInternalComponent.PutFor(scene, entity, model); |
| 0 | 73 | | } |
| | 74 | |
|
| | 75 | | public static void SetPhysicsColliders(this IInternalECSComponent<InternalSceneBoundsCheck> sbcInternalComponent |
| | 76 | | IParcelScene scene, IDCLEntity entity, KeyValueSet<Collider, uint> newCollidersCollection) |
| | 77 | | { |
| 0 | 78 | | var model = sbcInternalComponent.GetFor(scene, entity)?.model ?? new InternalSceneBoundsCheck(new Bounds()); |
| 0 | 79 | | model.physicsColliders = newCollidersCollection; |
| 0 | 80 | | model.meshesDirty = true; |
| | 81 | |
|
| 0 | 82 | | sbcInternalComponent.PutFor(scene, entity, model); |
| 0 | 83 | | } |
| | 84 | |
|
| | 85 | | public static void SetPointerColliders(this IInternalECSComponent<InternalSceneBoundsCheck> sbcInternalComponent |
| | 86 | | IParcelScene scene, IDCLEntity entity, KeyValueSet<Collider, uint> newCollidersCollection) |
| | 87 | | { |
| 0 | 88 | | var model = sbcInternalComponent.GetFor(scene, entity)?.model ?? new InternalSceneBoundsCheck(new Bounds()); |
| 0 | 89 | | model.pointerColliders = newCollidersCollection; |
| 0 | 90 | | model.meshesDirty = true; |
| | 91 | |
|
| 0 | 92 | | sbcInternalComponent.PutFor(scene, entity, model); |
| 0 | 93 | | } |
| | 94 | |
|
| | 95 | | public static void RegisterOnSceneBoundsStateChangeCallback(this IInternalECSComponent<InternalSceneBoundsCheck> |
| | 96 | | IParcelScene scene, IDCLEntity entity, Action<bool> callback) |
| | 97 | | { |
| 11 | 98 | | var model = sbcInternalComponent.GetFor(scene, entity)?.model ?? new InternalSceneBoundsCheck(new Bounds()); |
| 11 | 99 | | model.OnSceneBoundsStateChange += callback; |
| | 100 | |
|
| 11 | 101 | | sbcInternalComponent.PutFor(scene, entity, model); |
| 11 | 102 | | } |
| | 103 | |
|
| | 104 | | public static void RemoveOnSceneBoundsStateChangeCallback(this IInternalECSComponent<InternalSceneBoundsCheck> s |
| | 105 | | IParcelScene scene, IDCLEntity entity, Action<bool> callback) |
| | 106 | | { |
| 12 | 107 | | var model = sbcInternalComponent.GetFor(scene, entity)?.model; |
| | 108 | |
|
| 12 | 109 | | if (model == null) |
| 12 | 110 | | return; |
| | 111 | |
|
| 0 | 112 | | InternalSceneBoundsCheck finalModel = model.Value; |
| 0 | 113 | | finalModel.OnSceneBoundsStateChange -= callback; |
| | 114 | |
|
| 0 | 115 | | sbcInternalComponent.PutFor(scene, entity, finalModel); |
| 0 | 116 | | } |
| | 117 | |
|
| | 118 | | public static bool IsFullyDefaulted(this IInternalECSComponent<InternalSceneBoundsCheck> sbcInternalComponent, |
| | 119 | | IParcelScene scene, IDCLEntity entity) |
| | 120 | | { |
| 0 | 121 | | var model = sbcInternalComponent.GetFor(scene, entity)?.model; |
| | 122 | |
|
| 0 | 123 | | return model == null || (model.Value.entityPosition == Vector3.zero |
| | 124 | | && model.Value.entityLocalMeshBounds.size == Vector3.zero |
| | 125 | | && model.Value.OnSceneBoundsStateChange == null); |
| | 126 | | } |
| | 127 | | } |
| | 128 | | } |