| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.ECSRuntime; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Models; |
| | 5 | | using UnityEngine; |
| | 6 | |
|
| | 7 | | namespace DCL.ECSComponents |
| | 8 | | { |
| | 9 | | public class ECSTransformHandler : IECSComponentHandler<ECSTransform> |
| | 10 | | { |
| | 11 | | private readonly IWorldState worldState; |
| | 12 | | private readonly BaseVariable<Vector3> playerTeleportVariable; |
| | 13 | |
|
| 29 | 14 | | public ECSTransformHandler(IWorldState worldState, BaseVariable<Vector3> playerTeleportVariable) |
| | 15 | | { |
| 29 | 16 | | ECSTransformUtils.orphanEntities = new KeyValueSet<IDCLEntity, ECSTransformUtils.OrphanEntity>(100); |
| 29 | 17 | | this.worldState = worldState; |
| 29 | 18 | | this.playerTeleportVariable = playerTeleportVariable; |
| 29 | 19 | | } |
| | 20 | |
|
| 0 | 21 | | public void OnComponentCreated(IParcelScene scene, IDCLEntity entity) { } |
| | 22 | |
|
| | 23 | | public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity) |
| | 24 | | { |
| 2 | 25 | | ECSTransformUtils.orphanEntities.Remove(entity); |
| | 26 | |
|
| | 27 | | // reset transform and re-parent to the scene |
| 2 | 28 | | entity.gameObject.transform.ResetLocalTRS(); |
| 2 | 29 | | ECSTransformUtils.TrySetParent(scene, entity, SpecialEntityId.SCENE_ROOT_ENTITY); |
| | 30 | |
|
| | 31 | | // if entity has any parent |
| 2 | 32 | | if (entity.parentId != SpecialEntityId.SCENE_ROOT_ENTITY) |
| | 33 | | { |
| | 34 | | // remove as child |
| 0 | 35 | | if (scene.entities.TryGetValue(entity.parentId, out IDCLEntity parent)) |
| | 36 | | { |
| 0 | 37 | | parent.childrenId.Remove(entity.entityId); |
| | 38 | | } |
| | 39 | |
|
| 0 | 40 | | entity.parentId = SpecialEntityId.SCENE_ROOT_ENTITY; |
| | 41 | | } |
| | 42 | |
|
| | 43 | | // if entity has any children |
| 2 | 44 | | int childrenCount = entity.childrenId.Count; |
| 2 | 45 | | if (childrenCount > 0) |
| | 46 | | { |
| 4 | 47 | | for (int i = childrenCount - 1; i >= 0; i--) |
| | 48 | | { |
| 1 | 49 | | long childId = entity.childrenId[i]; |
| | 50 | |
|
| 1 | 51 | | if (!scene.entities.TryGetValue(childId, out IDCLEntity child)) |
| | 52 | | continue; |
| | 53 | |
|
| | 54 | | // re-parent child to the scene |
| 1 | 55 | | ECSTransformUtils.TrySetParent(scene, child, SpecialEntityId.SCENE_ROOT_ENTITY); |
| | 56 | |
|
| | 57 | | // add child as orphan |
| 1 | 58 | | ECSTransformUtils.orphanEntities[child] = new ECSTransformUtils.OrphanEntity(scene, child, child.par |
| | 59 | | } |
| 1 | 60 | | entity.childrenId.Clear(); |
| | 61 | | } |
| 2 | 62 | | } |
| | 63 | |
|
| | 64 | | public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, ECSTransform model) |
| | 65 | | { |
| | 66 | | // From SDK `PLAYER_ENTITY` entity's transform can be modified to |
| | 67 | | // move character across the scene |
| 17 | 68 | | if (entity.entityId == SpecialEntityId.PLAYER_ENTITY) |
| | 69 | | { |
| 3 | 70 | | TryMoveCharacter(scene, model.position, worldState, playerTeleportVariable); |
| 3 | 71 | | return; |
| | 72 | | } |
| | 73 | |
|
| 14 | 74 | | Transform transform = entity.gameObject.transform; |
| 14 | 75 | | transform.localPosition = model.position; |
| 14 | 76 | | transform.localRotation = model.rotation; |
| 14 | 77 | | transform.localScale = model.scale; |
| | 78 | |
|
| 14 | 79 | | if (entity.parentId != model.parentId) |
| | 80 | | { |
| 9 | 81 | | ProcessNewParent(scene, entity, model.parentId); |
| | 82 | | } |
| 14 | 83 | | } |
| | 84 | |
|
| | 85 | | private static void ProcessNewParent(IParcelScene scene, IDCLEntity entity, long parentId) |
| | 86 | | { |
| | 87 | | //check for cyclic parenting |
| 9 | 88 | | if (ECSTransformUtils.IsCircularParenting(scene, entity, parentId)) |
| | 89 | | { |
| 1 | 90 | | Debug.LogError($"cyclic parenting found for entity {entity.entityId} " + |
| | 91 | | $"parenting to {parentId} at scene {scene.sceneData.id} ({scene.sceneData.basePosition})" |
| 1 | 92 | | return; |
| | 93 | | } |
| | 94 | |
|
| | 95 | | // remove as child of previous parent |
| 8 | 96 | | if (entity.parentId != SpecialEntityId.SCENE_ROOT_ENTITY) |
| | 97 | | { |
| 2 | 98 | | if (scene.entities.TryGetValue(entity.parentId, out IDCLEntity parent)) |
| | 99 | | { |
| 1 | 100 | | parent.childrenId.Remove(entity.entityId); |
| | 101 | | } |
| | 102 | | } |
| | 103 | |
|
| 8 | 104 | | entity.parentId = parentId; |
| | 105 | |
|
| | 106 | | // add as orphan so system can parent it |
| 8 | 107 | | ECSTransformUtils.orphanEntities[entity] = new ECSTransformUtils.OrphanEntity(scene, entity, parentId); |
| 8 | 108 | | } |
| | 109 | |
|
| | 110 | | private static bool TryMoveCharacter(IParcelScene scene, Vector3 localPosition, |
| | 111 | | IWorldState worldState, BaseVariable<Vector3> playerTeleportVariable) |
| | 112 | | { |
| | 113 | | // If player is not at the scene that triggered this event |
| | 114 | | // we'll ignore it |
| 3 | 115 | | if (scene.sceneData.id != worldState.GetCurrentSceneId()) |
| | 116 | | { |
| 1 | 117 | | return false; |
| | 118 | | } |
| | 119 | |
|
| 2 | 120 | | Vector2Int targetCoords = scene.sceneData.basePosition + Utils.WorldToGridPosition(localPosition); |
| | 121 | |
|
| | 122 | | // If target coordinates are outside the scene we'll ignore it |
| 2 | 123 | | if (!ECSTransformUtils.IsInsideSceneBoundaries(scene, targetCoords)) |
| | 124 | | { |
| 1 | 125 | | return false; |
| | 126 | | } |
| | 127 | |
|
| 1 | 128 | | playerTeleportVariable.Set(Utils.GridToWorldPosition(scene.sceneData.basePosition.x, scene.sceneData.basePos |
| | 129 | | + localPosition); |
| 1 | 130 | | return true; |
| | 131 | | } |
| | 132 | | } |
| | 133 | | } |