Skip to content

Commit

Permalink
fix: character trigger area + avatar modifier area (#2859)
Browse files Browse the repository at this point in the history
  • Loading branch information
fcolarich authored Nov 29, 2024
1 parent 67740e0 commit d1b0c15
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using DCL.AvatarRendering.AvatarShape.UnityInterface;
using DCL.Character.Components;
using DCL.CharacterCamera;
using DCL.ECSComponents;
using ECS.Abstract;
using System.Runtime.CompilerServices;
using UnityEngine;
Expand Down Expand Up @@ -45,7 +46,7 @@ private void AddPlayerCachedVisibilityComponent([Data] in CameraComponent camera

[Query]
[All(typeof(AvatarShapeComponent))]
[None(typeof(AvatarCachedVisibilityComponent), typeof(PlayerComponent))]
[None(typeof(AvatarCachedVisibilityComponent), typeof(PlayerComponent), typeof(PBAvatarShape))]
private void AddOthersCachedVisibilityComponent(in Entity entity, ref AvatarShapeComponent avatarShape)
{
bool shouldBeHidden = avatarShape.HiddenByModifierArea;
Expand All @@ -67,14 +68,13 @@ private void UpdateNonPlayerAvatarVisibilityOnCameraDistance(in AvatarCustomSkin
}

[Query]
[None(typeof(PlayerComponent))]
private void UpdateAvatarsVisibilityState(ref AvatarShapeComponent avatarShape, ref AvatarCachedVisibilityComponent avatarCachedVisibility)
{
bool shouldBeHidden = avatarShape.HiddenByModifierArea;
UpdateVisibilityState(ref avatarShape, ref avatarCachedVisibility, shouldBeHidden);
}

private void UpdateVisibilityState( ref AvatarShapeComponent avatarShape, ref AvatarCachedVisibilityComponent avatarCachedVisibility, bool shouldBeHidden)
private void UpdateVisibilityState(ref AvatarShapeComponent avatarShape, ref AvatarCachedVisibilityComponent avatarCachedVisibility, bool shouldBeHidden)
{
if (avatarCachedVisibility.IsVisible == shouldBeHidden)
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,24 @@ namespace DCL.CharacterTriggerArea.Components
public struct CharacterTriggerAreaComponent : IDirtyMarker
{
private static readonly IReadOnlyCollection<Transform> EMPTY_COLLECTION = Array.Empty<Transform>();
internal CharacterTriggerArea? monoBehaviour;
public CharacterTriggerArea? monoBehaviour { get; private set; }

private readonly bool targetOnlyMainPlayer;
private bool hasMonoBehaviour;

public Vector3 AreaSize { get; private set; }
public bool IsDirty { get; set; }

public readonly IReadOnlyCollection<Transform> EnteredAvatarsToBeProcessed => monoBehaviour != null
? monoBehaviour.EnteredAvatarsToBeProcessed
public readonly IReadOnlyCollection<Transform> EnteredAvatarsToBeProcessed => hasMonoBehaviour
? monoBehaviour!.EnteredAvatarsToBeProcessed
: EMPTY_COLLECTION;

public readonly IReadOnlyCollection<Transform> ExitedAvatarsToBeProcessed => monoBehaviour != null
? monoBehaviour.ExitedAvatarsToBeProcessed
public readonly IReadOnlyCollection<Transform> ExitedAvatarsToBeProcessed => hasMonoBehaviour
? monoBehaviour!.ExitedAvatarsToBeProcessed
: EMPTY_COLLECTION;

public readonly IReadOnlyCollection<Transform> CurrentAvatarsInside => monoBehaviour != null
? monoBehaviour.CurrentAvatarsInside
public readonly IReadOnlyCollection<Transform> CurrentAvatarsInside => hasMonoBehaviour
? monoBehaviour!.CurrentAvatarsInside
: EMPTY_COLLECTION;

public CharacterTriggerAreaComponent(Vector3 areaSize, bool targetOnlyMainPlayer = false, CharacterTriggerArea? monoBehaviour = null)
Expand All @@ -33,25 +37,34 @@ public CharacterTriggerAreaComponent(Vector3 areaSize, bool targetOnlyMainPlayer
this.targetOnlyMainPlayer = targetOnlyMainPlayer;

this.monoBehaviour = monoBehaviour;
hasMonoBehaviour = monoBehaviour != null;

IsDirty = true;
}

public void TryAssignArea(IComponentPool<CharacterTriggerArea> pool, Transform mainPlayerTransform)
public void TryAssignArea(IComponentPool<CharacterTriggerArea> pool, Transform mainPlayerTransform, TransformComponent transformComponent)
{
if (IsDirty == false) return;
if (hasMonoBehaviour && !monoBehaviour!.BoxCollider.enabled)
monoBehaviour.BoxCollider.enabled = true;

if (IsDirty == false) return;
IsDirty = false;

if (monoBehaviour == null)
if (!hasMonoBehaviour)
{
monoBehaviour = pool.Get();
SetMonoBehaviour(pool.Get());

if (targetOnlyMainPlayer)
monoBehaviour!.TargetTransform = mainPlayerTransform;
monoBehaviour.TargetTransform = mainPlayerTransform;

Transform triggerAreaTransform = monoBehaviour.transform;
triggerAreaTransform.SetParent(transformComponent.Transform);
triggerAreaTransform.localPosition = Vector3.zero;
triggerAreaTransform.localRotation = Quaternion.identity;
}

monoBehaviour!.BoxCollider.size = AreaSize;
monoBehaviour.BoxCollider.enabled = true;
}

public void UpdateAreaSize(Vector3 size)
Expand All @@ -60,30 +73,13 @@ public void UpdateAreaSize(Vector3 size)
IsDirty = true;
}

public readonly void TryUpdateTransform(ref TransformComponent transformComponent)
{
if (monoBehaviour == null)
return;

Transform triggerAreaTransform = monoBehaviour.transform;

if (transformComponent.Cached.WorldPosition != triggerAreaTransform.position)
triggerAreaTransform.position = transformComponent.Cached.WorldPosition;

if (transformComponent.Cached.WorldRotation != triggerAreaTransform.rotation)
triggerAreaTransform.rotation = transformComponent.Cached.WorldRotation;

if (!monoBehaviour.BoxCollider.enabled)
monoBehaviour.BoxCollider.enabled = true;
}

public void TryRelease(IComponentPool<CharacterTriggerArea> pool)
{
if (monoBehaviour != null)
{
pool.Release(monoBehaviour);
monoBehaviour = null;
}
if (!hasMonoBehaviour) return;

pool.Release(monoBehaviour!);
monoBehaviour = null;
hasMonoBehaviour = false;
}

public void TryClear() => monoBehaviour?.Clear();
Expand All @@ -96,15 +92,19 @@ public void TryClearExitedAvatarsToBeProcessed() =>

public bool TryDispose(ISceneStateProvider sceneStateProvider)
{
if (!sceneStateProvider.IsCurrent && monoBehaviour != null)
if (!sceneStateProvider.IsCurrent && hasMonoBehaviour)
{
monoBehaviour.Dispose();
monoBehaviour!.Dispose();
return true;
}

return false;
}

public bool IsDirty { get; set; }
internal void SetMonoBehaviour(CharacterTriggerArea newMonoBehaviour)
{
monoBehaviour = newMonoBehaviour;
hasMonoBehaviour = newMonoBehaviour != null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ private void UpdateCharacterTriggerArea(ref TransformComponent transformComponen
if (triggerAreaComponent.TryDispose(sceneStateProvider))
return;

triggerAreaComponent.TryAssignArea(poolRegistry, mainPlayerTransform);
triggerAreaComponent.TryUpdateTransform(ref transformComponent);
triggerAreaComponent.TryAssignArea(poolRegistry, mainPlayerTransform, transformComponent);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public void Teardown()
Object.DestroyImmediate(entityTransformComponent.Transform.gameObject);
Object.DestroyImmediate(fakeMainPlayerGO);
Object.DestroyImmediate(fakeMainPlayerAvatarGO);
Object.DestroyImmediate(characterTriggerArea.gameObject);
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ namespace DCL.SDKComponents.AvatarModifierArea.Systems
[LogCategory(ReportCategory.CHARACTER_TRIGGER_AREA)]
public partial class AvatarModifierAreaHandlerSystem : BaseUnityLoopSystem, IFinalizeWorldSystem
{
private static readonly QueryDescription AVATAR_BASE_QUERY = new QueryDescription().WithAll<AvatarBase>();
private readonly World globalWorld;
private readonly FindAvatarQuery findAvatarQuery;
private readonly ISceneRestrictionBusController sceneRestrictionBusController;
Expand Down Expand Up @@ -167,10 +166,12 @@ private void HideAvatar(Transform avatarTransform, HashSet<string> excludedIds)

private class FindAvatarQuery
{
private static readonly QueryDescription AVATAR_BASE_QUERY = new QueryDescription().WithAll<AvatarBase>();

private readonly World globalWorld;
private readonly ForEach cachedFindEntity;

private Entity foundedEntityOrNull = Entity.Null;
private Entity foundEntityOrNull = Entity.Null;
private Transform? requiredTransform;

public FindAvatarQuery(World globalWorld)
Expand All @@ -181,20 +182,20 @@ public FindAvatarQuery(World globalWorld)

public LightResult<Entity> AvatarWithTransform(Transform avatarTransform)
{
foundedEntityOrNull = Entity.Null;
foundEntityOrNull = Entity.Null;
requiredTransform = avatarTransform;
globalWorld.Query(in AVATAR_BASE_QUERY, cachedFindEntity);

return foundedEntityOrNull == Entity.Null
return foundEntityOrNull == Entity.Null
? LightResult<Entity>.FAILURE
: new LightResult<Entity>(foundedEntityOrNull);
: new LightResult<Entity>(foundEntityOrNull);
}

private void FindEntity(Entity entity)
{
if (foundedEntityOrNull != Entity.Null) return;
if (foundEntityOrNull != Entity.Null) return;
if (globalWorld.Get<AvatarBase>(entity).transform.parent != requiredTransform) return;
foundedEntityOrNull = entity;
foundEntityOrNull = entity;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public void ToggleHidingFlagCorrectly()
// "Enter" trigger area
characterTriggerArea.OnTriggerEnter(fakeAvatarShapeCollider);
CharacterTriggerAreaComponent component = world.Get<CharacterTriggerAreaComponent>(triggerAreaEntity);
component.monoBehaviour = characterTriggerArea;
component.SetMonoBehaviour(characterTriggerArea);
world.Set(triggerAreaEntity, component);

system.Update(0f);
Expand Down Expand Up @@ -317,7 +317,7 @@ public void HandleExcludedIdsUpdateCorrectly()
characterTriggerArea.OnTriggerEnter(fakeAvatar2ShapeCollider);

CharacterTriggerAreaComponent component = world.Get<CharacterTriggerAreaComponent>(triggerAreaEntity);
component.monoBehaviour = characterTriggerArea;
component.SetMonoBehaviour(characterTriggerArea);
world.Set(triggerAreaEntity, component);

system.Update(0);
Expand Down Expand Up @@ -351,8 +351,6 @@ public void HandleExcludedIdsUpdateCorrectly()
Object.DestroyImmediate(fakeAvatar2BaseGO);
}

// TODO: leeaving scene ???

[Test]
public void HandleComponentRemoveCorrectly()
{
Expand Down Expand Up @@ -384,7 +382,7 @@ public void HandleComponentRemoveCorrectly()
// "Enter" trigger area
characterTriggerArea.OnTriggerEnter(fakeAvatarShapeCollider);
CharacterTriggerAreaComponent component = world.Get<CharacterTriggerAreaComponent>(triggerAreaEntity);
component.monoBehaviour = characterTriggerArea;
component.SetMonoBehaviour(characterTriggerArea);
world.Set(triggerAreaEntity, component);

system.Update(0);
Expand Down Expand Up @@ -432,7 +430,7 @@ public void HandleEntityDestructionCorrectly()
// "Enter" trigger area
characterTriggerArea.OnTriggerEnter(fakeAvatarShapeCollider);
CharacterTriggerAreaComponent component = world.Get<CharacterTriggerAreaComponent>(triggerAreaEntity);
component.monoBehaviour = characterTriggerArea;
component.SetMonoBehaviour(characterTriggerArea);
world.Set(triggerAreaEntity, component);

system.Update(0);
Expand Down

0 comments on commit d1b0c15

Please sign in to comment.