Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public struct CharacterMaskedEmoteComponent

private int currentAnimationTag;

public float PlayingEmoteDuration => CurrentEmoteReference?.avatarClip
? CurrentEmoteReference.avatarClip.length * (CurrentEmoteReference.animatorComp != null ? CurrentEmoteReference.animatorComp.speed : 1f)
: 0f;

public readonly int CurrentAnimationTag => currentAnimationTag;

public readonly bool IsPlaying
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,8 @@ private void ConsumeEmoteIntent([Data] float dt, Entity entity,
ReportHub.LogError(ReportCategory.EMOTE, $"Emote name:{emoteId} cant be played.");
else
{
uint durationMs = !isLooping ? (uint)(emoteComponent.PlayingEmoteDuration * 1000) : 0;
// The duration comes from the masked emote's own clip; a looping emote has no end to report.
uint durationMs = !isLooping ? (uint)(masked.PlayingEmoteDuration * 1000) : 0;
World.Add(entity, new EmotePendingToBroadcast { EmoteId = emoteId, DurationMs = durationMs, Mask = mask});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,14 @@ private void CancelMaskedEmotes(ref CharacterMaskedEmoteComponent masked)

AvatarBase view = mainPlayerAvatarBaseProxy.Object!;

// Both branches below tear down an emote that stopped by itself. A non-looping clip ran to its
// end and has nothing left to resume, so its urn is cleared too; a looping one lost its animator
// state to something else and keeps the urn to be resumed.

// Legacy-blender path: tear down once the blender signals natural completion.
if (view.HasMaskedLegacyEmoteFinished)
{
TryStopMaskedEmote(ref masked);
TryStopMaskedEmote(ref masked, permanent: !masked.EmoteLoop);
return;
}

Expand All @@ -105,7 +109,7 @@ private void CancelMaskedEmotes(ref CharacterMaskedEmoteComponent masked)
bool isOnAnotherTag = currentTag != AnimationHashes.MASKED_EMOTE && currentTag != AnimationHashes.MASKED_EMOTE_LOOP;

if (isOnAnotherTag)
TryStopMaskedEmote(ref masked);
TryStopMaskedEmote(ref masked, permanent: !masked.EmoteLoop);
}

[Query]
Expand Down Expand Up @@ -180,7 +184,8 @@ private void ConsumeMaskedEmoteIntent([Data] float dt, Entity entity,
ReportHub.LogError(ReportCategory.EMOTE, $"Emote name:{emoteId} cant be played.");
else
{
uint durationMs = !isLooping ? (uint)(emoteComponent.PlayingEmoteDuration * 1000) : 0;
// The duration comes from the masked emote's own clip; a looping emote has no end to report.
uint durationMs = !isLooping ? (uint)(masked.PlayingEmoteDuration * 1000) : 0;
globalWorld.Add(globalPlayerEntity, new EmotePendingToBroadcast { EmoteId = emoteId, DurationMs = durationMs, Mask = emoteIntent.Mask });
}

Expand All @@ -203,16 +208,25 @@ private void UpdateMaskedEmoteVisibility(ref CharacterMaskedEmoteComponent maske
bool shouldPlay = isInScene && !fullBodyIsPlaying && !isGliding;

if (shouldPlay && masked.CurrentEmoteReference == null)
ReplayMaskedEmote(ref masked, ec);
ReplayMaskedEmote(ref masked);
else if (!shouldPlay && masked.CurrentEmoteReference != null)
TryStopMaskedEmote(ref masked);
}

private void ReplayMaskedEmote(ref CharacterMaskedEmoteComponent masked, CharacterEmoteComponent emoteComponent)
private void ReplayMaskedEmote(ref CharacterMaskedEmoteComponent masked)
{
if (!emoteStorage.TryGetElement(masked.EmoteUrn.Shorten(), out IEmote emote)) return;
if (emote.IsLoading) return;

// Replaying resumes a looping emote that was suspended. A non-looping emote has already
// delivered its single playback, so drop the urn instead of restarting it on every frame
// that the play conditions hold.
if (!emote.IsLooping())
{
masked.Reset();
return;
}

if (!globalWorld.TryGet(globalPlayerEntity, out AvatarShapeComponent avatarShape)) return;

BodyShape bodyShape = avatarShape.BodyShape;
Expand All @@ -231,17 +245,15 @@ private void ReplayMaskedEmote(ref CharacterMaskedEmoteComponent masked, Charact

IAvatarView avatarBase = mainPlayerAvatarBaseProxy.Object!;

bool isLooping = emote.IsLooping();

if (!emotePlayer.PlayMasked(mainAsset, audioClip, isLooping, true, in avatarBase, ref masked))
if (!emotePlayer.PlayMasked(mainAsset, audioClip, isLooping: true, isSpatial: true, in avatarBase, ref masked))
return;

// Reset stored tag so CancelMaskedEmotes doesn't fire on the next frame
// before UpdateMaskedEmoteTags has a chance to set the real animator state.
masked.SetAnimationTag(0);

uint durationMs = !isLooping ? (uint)(emoteComponent.PlayingEmoteDuration * 1000) : 0;
globalWorld.Add(globalPlayerEntity, new EmotePendingToBroadcast { EmoteId = masked.EmoteUrn, DurationMs = durationMs, Mask = masked.Mask });
// Only looping emotes are replayed, and a looping emote has no end to report.
globalWorld.Add(globalPlayerEntity, new EmotePendingToBroadcast { EmoteId = masked.EmoteUrn, Mask = masked.Mask });
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using DCL.ECSComponents;
using NUnit.Framework;
using UnityEngine;
using Utility.Animations;

namespace DCL.AvatarRendering.Emotes.Tests
{
public class CharacterMaskedEmoteComponentShould
{
private const float CLIP_LENGTH = 2f;

private GameObject gameObject = null!;
private EmoteReferences emoteReferences = null!;
private AnimationClip clip = null!;

[SetUp]
public void SetUp()
{
gameObject = new GameObject(nameof(CharacterMaskedEmoteComponentShould));
emoteReferences = gameObject.AddComponent<EmoteReferences>();

clip = new AnimationClip { name = "TestClip" };
clip.SetCurve(string.Empty, typeof(Transform), "localPosition.x", AnimationCurve.Linear(0, 0, CLIP_LENGTH, 1));
}

[TearDown]
public void TearDown()
{
if (gameObject != null) Object.DestroyImmediate(gameObject);
if (clip != null) Object.DestroyImmediate(clip);
}

[Test]
public void PlayingEmoteDuration_ReturnsZero_WhenNoReferenceIsSet()
{
var masked = new CharacterMaskedEmoteComponent();

Assert.AreEqual(0f, masked.PlayingEmoteDuration, 0.001f);
}

[Test]
public void PlayingEmoteDuration_ReturnsZero_WhenReferenceHasNoAvatarClip()
{
emoteReferences.Initialize(null, null, null, null, 0, legacy: false);

var masked = new CharacterMaskedEmoteComponent { CurrentEmoteReference = emoteReferences };

Assert.AreEqual(0f, masked.PlayingEmoteDuration, 0.001f);
}

[Test]
public void PlayingEmoteDuration_ReturnsAvatarClipLength()
{
emoteReferences.Initialize(clip, null, null, null, 0, legacy: false);

var masked = new CharacterMaskedEmoteComponent { CurrentEmoteReference = emoteReferences };

Assert.AreEqual(CLIP_LENGTH, masked.PlayingEmoteDuration, 0.001f,
"The broadcast duration of a masked emote comes from the masked clip itself, not from the full body emote.");
}

[Test]
public void PlayingEmoteDuration_ScalesWithAnimatorSpeed()
{
Animator animator = gameObject.AddComponent<Animator>();
animator.speed = 2f;
emoteReferences.Initialize(clip, null, animator, null, 0, legacy: false);

var masked = new CharacterMaskedEmoteComponent { CurrentEmoteReference = emoteReferences };

Assert.AreEqual(CLIP_LENGTH * 2f, masked.PlayingEmoteDuration, 0.001f);
}

[Test]
public void IsPlaying_ReturnsFalse_WhenNoReferenceIsSet()
{
var masked = new CharacterMaskedEmoteComponent();
masked.SetAnimationTag(AnimationHashes.MASKED_EMOTE);

Assert.IsFalse(masked.IsPlaying);
}

[Test]
public void IsPlaying_ReturnsTrue_WhenAnimatorInMaskedEmoteLoopTag()
{
emoteReferences.Initialize(clip, null, null, null, 0, legacy: false);

var masked = new CharacterMaskedEmoteComponent { CurrentEmoteReference = emoteReferences };
masked.SetAnimationTag(AnimationHashes.MASKED_EMOTE_LOOP);

Assert.IsTrue(masked.IsPlaying);
}

[Test]
public void Reset_ClearsTheEmoteUrn()
{
var masked = new CharacterMaskedEmoteComponent
{
EmoteUrn = "urn:decentraland:off-chain:scene-emote:test-scene-hash-false",
EmoteLoop = true,
CurrentEmoteReference = emoteReferences,
Mask = AvatarEmoteMask.AemUpperBody,
};

masked.Reset();

Assert.IsTrue(masked.EmoteUrn.IsNullOrEmpty(),
"Clearing the urn is what stops a finished emote from being replayed.");
Assert.IsNull(masked.CurrentEmoteReference);
Assert.IsFalse(masked.EmoteLoop);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using Arch.Core;
using CommunicationData.URLHelpers;
using DCL.AvatarRendering.AvatarShape.Components;
using DCL.AvatarRendering.AvatarShape.UnityInterface;
using DCL.AvatarRendering.Emotes.Play;
using DCL.AvatarRendering.Loading.Assets;
using DCL.AvatarRendering.Loading.Components;
using DCL.ECSComponents;
using DCL.Multiplayer.Emotes;
using DCL.Utilities;
using ECS.StreamableLoading.Common.Components;
using ECS.TestSuite;
using NSubstitute;
using NUnit.Framework;
using SceneRunner.Scene;
using UnityEngine;
using Object = UnityEngine.Object;

namespace DCL.AvatarRendering.Emotes.Tests
{
public class SceneMaskedEmoteSystemShould : UnitySystemTestBase<SceneMaskedEmoteSystem>
{
private const string EMOTE_URN_FORMAT = "urn:decentraland:off-chain:scene-emote:test-scene-bafkreiemotehash-{0}";

private World globalWorld = null!;
private Entity globalPlayerEntity;
private IEmoteStorage emoteStorage = null!;
private GameObject poolRoot = null!;
private GameObject audioSourcePrefab = null!;
private GameObject avatarBaseGameObject = null!;

[SetUp]
public void SetUp()
{
poolRoot = new GameObject("ROOT_POOL_CONTAINER");
audioSourcePrefab = new GameObject("EmoteAudioSource");
AudioSource audioSource = audioSourcePrefab.AddComponent<AudioSource>();
var emotePlayer = new EmotePlayer(audioSource, ScriptableObject.CreateInstance<EmoteMaskCatalog>(), legacyAnimationsEnabled: true);
Comment thread
pravusjif marked this conversation as resolved.
Outdated

globalWorld = World.Create();
globalPlayerEntity = globalWorld.Create(new AvatarShapeComponent { BodyShape = BodyShape.MALE });

avatarBaseGameObject = new GameObject(nameof(AvatarBase));
var avatarBaseProxy = new ObjectProxy<AvatarBase>();
avatarBaseProxy.SetObject(avatarBaseGameObject.AddComponent<AvatarBase>());

emoteStorage = Substitute.For<IEmoteStorage>();

// The player stands in the scene that triggered the emote, so the play conditions are met.
ISceneStateProvider sceneStateProvider = Substitute.For<ISceneStateProvider>();
sceneStateProvider.IsCurrent.Returns(true);

system = new SceneMaskedEmoteSystem(world, globalWorld, globalPlayerEntity, avatarBaseProxy,
emotePlayer, emoteStorage, Substitute.For<IEmotesMessageBus>(), sceneStateProvider);
}

protected override void OnTearDown()
{
globalWorld.Dispose();
Object.DestroyImmediate(avatarBaseGameObject);
Object.DestroyImmediate(audioSourcePrefab);
Object.DestroyImmediate(poolRoot);
}
Comment thread
pravusjif marked this conversation as resolved.

[Test]
public void DiscardNonLoopingEmoteThatAlreadyPlayed()
{
Entity entity = CreateSuspendedMaskedEmote(loop: false);

system!.Update(0);

CharacterMaskedEmoteComponent masked = world.Get<CharacterMaskedEmoteComponent>(entity);

Assert.IsTrue(masked.EmoteUrn.IsNullOrEmpty(),
"A one-shot masked emote that already played must not stay resumable: while its urn is set, every frame that meets the play conditions starts the emote again.");
Assert.IsNull(masked.CurrentEmoteReference);
Assert.IsFalse(globalWorld.Has<EmotePendingToBroadcast>(globalPlayerEntity),
"Nothing was played, so nothing must be broadcast.");
}

[Test]
public void KeepLoopingEmoteResumable()
{
Entity entity = CreateSuspendedMaskedEmote(loop: true);

system!.Update(0);

CharacterMaskedEmoteComponent masked = world.Get<CharacterMaskedEmoteComponent>(entity);

Assert.IsFalse(masked.EmoteUrn.IsNullOrEmpty(),
"A looping masked emote is resumed once the play conditions are met again, so its urn must survive being suspended.");
}

/// <summary>
/// A masked emote in the state a non-permanent stop leaves behind: the animation is torn down
/// but the urn is kept so that the emote can be resumed.
/// </summary>
private Entity CreateSuspendedMaskedEmote(bool loop)
{
IEmote emote = Substitute.For<IEmote>();
emote.IsLoading.Returns(false);
emote.IsLooping().Returns(loop);

// Empty results: the asset is not resident, so playback stops short of touching the avatar view.
emote.AssetResults.Returns(new StreamableLoadingResult<AttachmentRegularAsset>?[BodyShape.COUNT]);

emoteStorage.TryGetElement(Arg.Any<URN>(), out Arg.Any<IEmote>())
.Returns(call =>
{
call[1] = emote;
return true;
});

return world.Create(new CharacterMaskedEmoteComponent
{
EmoteUrn = string.Format(EMOTE_URN_FORMAT, loop.ToString().ToLower()),
Mask = AvatarEmoteMask.AemUpperBody,
});
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading