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