Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sdk7 avatar modifier area dynamic excluded ids #6038

Merged
merged 5 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -56,7 +56,9 @@ public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBAva
excludedIds.Add(modelExcludeId.ToLower());
}
internalComponentModel.excludedIds = excludedIds;
internalComponentModel.avatarsInArea = new HashSet<GameObject>();

if (internalComponentModel.avatarsInArea?.Count == 0)
internalComponentModel.avatarsInArea = new HashSet<GameObject>();

internalAvatarModifierArea.PutFor(scene, entity, internalComponentModel);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using DCL.CRDT;
using DCL.ECS7.InternalComponents;
using DCL.ECSRuntime;
using NUnit.Framework;
using UnityEngine;
Expand Down Expand Up @@ -64,6 +65,33 @@ public void CreateInternalComponentCorrectly()
Assert.IsTrue(internalComponent.Value.model.excludedIds.Contains(excludedId2.ToLower()));
}

[Test]
public void KeepExistentInternalComponentAvatarsInArea()
{
var internalComponent = internalComponents.AvatarModifierAreaComponent.GetFor(scene, entity);
Assert.IsNull(internalComponent);

GameObject dummyGO = new GameObject("avatarInArea");
internalComponents.AvatarModifierAreaComponent.PutFor(scene, entity, new InternalAvatarModifierArea()
{
avatarsInArea = new HashSet<GameObject>(){ dummyGO }
});

var model = new PBAvatarModifierArea()
{
Area = new Decentraland.Common.Vector3() { X = 4f, Y = 2.5f, Z = 4f },
Modifiers = { AvatarModifierType.AmtHideAvatars },
};
componentHandler.OnComponentModelUpdated(scene, entity, model);

internalComponent = internalComponents.AvatarModifierAreaComponent.GetFor(scene, entity);
Assert.IsNotNull(internalComponent);
Assert.AreEqual(1, internalComponent.Value.model.avatarsInArea.Count);
Assert.IsTrue(internalComponent.Value.model.avatarsInArea.Contains(dummyGO));

GameObject.DestroyImmediate(dummyGO);
}

[Test]
[TestCase(AvatarModifierType.AmtHideAvatars)]
[TestCase(AvatarModifierType.AmtDisablePassports)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ public void Update()
var model = componentGroup[i].value.model;
Transform entityTransform = scene.entities[entityId].gameObject.transform;

if (model.excludedIds != null && model.excludedIds.Count > 0)
UpdateExcludedCollidersCollection(model.excludedIds);
UpdateExcludedCollidersCollection(model.excludedIds);

HashSet<GameObject> currentAvatarsInArea = ECSAvatarUtils.DetectAvatars(model.area, entityTransform.position,
entityTransform.rotation, excludedColliders);
Expand Down Expand Up @@ -91,9 +90,11 @@ private bool AreSetsEqual(HashSet<GameObject> set1, HashSet<GameObject> set2)

private void UpdateExcludedCollidersCollection(HashSet<string> excludedIds)
{
var ownPlayer = dataStore.ownPlayer.Get();
excludedColliders.Clear();

if (excludedIds == null || excludedIds.Count == 0) return;

var ownPlayer = dataStore.ownPlayer.Get();
foreach (string excludedId in excludedIds)
{
if (dataStore.otherPlayers.TryGetValue(excludedId, out Player player))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public void DetectOnlyAvatarsInArea()
Assert.AreEqual(2, internalComponentModel.Value.model.avatarsInArea.Count);
Assert.IsTrue(internalComponentModel.Value.model.avatarsInArea.Contains(fakeAvatar1));
Assert.IsTrue(internalComponentModel.Value.model.avatarsInArea.Contains(fakeAvatar2));

GameObject.DestroyImmediate(fakeAvatar1);
GameObject.DestroyImmediate(fakeAvatar2);
GameObject.DestroyImmediate(fakeAvatar3);
}

[Test]
Expand Down Expand Up @@ -135,6 +139,87 @@ void RemoveModifier(GameObject avatarGO)
systemUpdate();
Assert.IsTrue(removeCalled);
Assert.IsFalse(applyCalled);

GameObject.DestroyImmediate(fakeAvatar);
}

[Test]
public void ReactCorrectlyToExcludedIdsUpdates()
{
entity.gameObject.transform.position = new Vector3(8, 1, 8);
Vector3 entityPosition = entity.gameObject.transform.position;
Vector3 area = new Vector3(5f, 5f, 5f);

var fakeAvatar = CreateFakeAvatar();
fakeAvatar.transform.position = new Vector3(100, 100, 100);
Physics.SyncTransforms();

bool applyCalled = false;
bool removeCalled = false;
void ApplyModifier(GameObject avatarGO)
{
Assert.AreEqual(fakeAvatar, avatarGO);
applyCalled = true;
}

void RemoveModifier(GameObject avatarGO)
{
Assert.AreEqual(fakeAvatar, avatarGO);
removeCalled = true;
}

var model = new InternalAvatarModifierArea()
{
area = area,
OnAvatarEnter = ApplyModifier,
OnAvatarExit = RemoveModifier,
avatarsInArea = new HashSet<GameObject>(),
excludedIds = new HashSet<string>()
};
internalComponents.AvatarModifierAreaComponent.PutFor(scene, entity, model);

systemUpdate();
Assert.IsFalse(applyCalled);
Assert.IsFalse(removeCalled);

// move avatar inside area
fakeAvatar.transform.position = entityPosition + (area * 0.5f);
Physics.SyncTransforms();

systemUpdate();
Assert.IsTrue(applyCalled);
Assert.IsFalse(removeCalled);
applyCalled = false;
model = internalComponents.AvatarModifierAreaComponent.GetFor(scene, entity).Value.model;
Assert.AreEqual(1, model.avatarsInArea.Count);

// add current avatar user id to the excluded IDs collection
string targetUserId = "0x666";
dataStorePlayer.otherPlayers.Add(targetUserId, new Player()
{
id = targetUserId,
collider = fakeAvatar.GetComponentInChildren<BoxCollider>(),
});
model.excludedIds.Add(targetUserId);
internalComponents.AvatarModifierAreaComponent.PutFor(scene, entity, model);

systemUpdate();

Assert.IsTrue(removeCalled);
Assert.IsFalse(applyCalled);
removeCalled = false;
model = internalComponents.AvatarModifierAreaComponent.GetFor(scene, entity).Value.model;
Assert.AreEqual(0, model.avatarsInArea.Count);

// remove current avatar user id from the excluded IDs collection
model.excludedIds.Clear();
internalComponents.AvatarModifierAreaComponent.PutFor(scene, entity, model);

systemUpdate();
Assert.IsTrue(applyCalled);
Assert.IsFalse(removeCalled);

GameObject.DestroyImmediate(fakeAvatar);
}

private GameObject CreateFakeAvatar()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@
"GUID:e30cc2f9ee2c45df93dbb8f7f826792f",
"GUID:091c556278214ab49cfdc87549e3135c",
"GUID:f334064a9ed3462091d1b06f9e981366",
"GUID:59c9e1ae4a4a61c43b19957ad3bfbaf7"
"GUID:59c9e1ae4a4a61c43b19957ad3bfbaf7",
"GUID:c34e38f41494f834abff029ddf82af43"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand Down