| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.Models; |
| | 3 | | using System.Linq; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | public static class ECSTransformUtils |
| | 7 | | { |
| | 8 | | public readonly struct OrphanEntity |
| | 9 | | { |
| | 10 | | public readonly IParcelScene scene; |
| | 11 | | public readonly IDCLEntity entity; |
| | 12 | | public readonly long parentId; |
| | 13 | |
|
| | 14 | | public OrphanEntity(IParcelScene scene, IDCLEntity entity, long parentId) |
| | 15 | | { |
| 12 | 16 | | this.scene = scene; |
| 12 | 17 | | this.entity = entity; |
| 12 | 18 | | this.parentId = parentId; |
| 12 | 19 | | } |
| | 20 | | } |
| | 21 | |
|
| | 22 | | public static KeyValueSet<IDCLEntity, OrphanEntity> orphanEntities = null; |
| | 23 | |
|
| | 24 | | public static bool TrySetParent(IParcelScene scene, IDCLEntity child, long parentId) |
| | 25 | | { |
| 7 | 26 | | return TrySetParent(scene, child, parentId, out IDCLEntity parent); |
| | 27 | | } |
| | 28 | |
|
| | 29 | | public static bool TrySetParent(IParcelScene scene, IDCLEntity child, long parentId, out IDCLEntity parent) |
| | 30 | | { |
| 13 | 31 | | if (parentId == SpecialEntityId.SCENE_ROOT_ENTITY) |
| | 32 | | { |
| 9 | 33 | | child.gameObject.transform.SetParent(GetRootEntityTransform(scene), false); |
| 9 | 34 | | parent = null; |
| 9 | 35 | | return true; |
| | 36 | | } |
| | 37 | |
|
| 4 | 38 | | if (scene.entities.TryGetValue(parentId, out parent)) |
| | 39 | | { |
| 3 | 40 | | child.gameObject.transform.SetParent(parent.gameObject.transform, false); |
| 3 | 41 | | return true; |
| | 42 | | } |
| | 43 | |
|
| 1 | 44 | | return false; |
| | 45 | | } |
| | 46 | |
|
| | 47 | | public static bool IsCircularParenting(IParcelScene scene, IDCLEntity entity, long parentId) |
| | 48 | | { |
| 20 | 49 | | if (parentId == SpecialEntityId.SCENE_ROOT_ENTITY) |
| | 50 | | { |
| 4 | 51 | | return false; |
| | 52 | | } |
| | 53 | |
|
| 16 | 54 | | if (parentId == entity.entityId) |
| | 55 | | { |
| 1 | 56 | | return true; |
| | 57 | | } |
| | 58 | |
|
| | 59 | | do |
| | 60 | | { |
| 32 | 61 | | if (!scene.entities.TryGetValue(parentId, out IDCLEntity parent)) |
| | 62 | | break; |
| | 63 | |
|
| 29 | 64 | | if (entity.entityId == parentId) |
| | 65 | | { |
| 5 | 66 | | return true; |
| | 67 | | } |
| | 68 | |
|
| 24 | 69 | | parentId = parent.parentId; |
| | 70 | | } |
| 24 | 71 | | while (parentId != SpecialEntityId.SCENE_ROOT_ENTITY); |
| | 72 | |
|
| 10 | 73 | | return false; |
| | 74 | | } |
| | 75 | |
|
| | 76 | | public static Transform GetRootEntityTransform(IParcelScene scene) |
| | 77 | | { |
| 9 | 78 | | return scene.GetSceneTransform(); |
| | 79 | | } |
| | 80 | |
|
| | 81 | | public static bool IsInsideSceneBoundaries(IParcelScene scene, Vector2Int position) |
| | 82 | | { |
| 0 | 83 | | if (scene.isPersistent) |
| | 84 | | { |
| 0 | 85 | | return true; |
| | 86 | | } |
| | 87 | |
|
| 0 | 88 | | return scene.sceneData.parcels.Contains(position); |
| | 89 | | } |
| | 90 | | } |