public void PublishInactiveConsumerComponent() { ActiveCollisionsContainer.EventData eventData = new ActiveCollisionsContainer.EventData(); GameObject oneContainer; CollisionNotifier.EventData oneData = CollisionNotifierHelper.GetEventData(out oneContainer); ActiveCollisionConsumerMock oneConsumer = oneContainer.AddComponent <ActiveCollisionConsumerMock>(); GameObject twoContainer; CollisionNotifier.EventData twoData = CollisionNotifierHelper.GetEventData(out twoContainer); ActiveCollisionConsumerMock twoConsumer = twoContainer.AddComponent <ActiveCollisionConsumerMock>(); twoConsumer.enabled = false; eventData.ActiveCollisions.Add(oneData); eventData.ActiveCollisions.Add(twoData); subject.SetActiveCollisions(eventData); Assert.IsFalse(oneConsumer.received); Assert.IsFalse(twoConsumer.received); subject.Publish(); Assert.IsTrue(oneConsumer.received); Assert.IsFalse(twoConsumer.received); Object.DestroyImmediate(oneContainer); Object.DestroyImmediate(twoContainer); }
public void Publish() { ActiveCollisionsContainer.EventData eventData = new ActiveCollisionsContainer.EventData(); GameObject oneContainer; CollisionNotifier.EventData oneData = CollisionNotifierHelper.GetEventData(out oneContainer); ActiveCollisionConsumerMock oneConsumer = oneContainer.AddComponent <ActiveCollisionConsumerMock>(); GameObject twoContainer; CollisionNotifier.EventData twoData = CollisionNotifierHelper.GetEventData(out twoContainer); ActiveCollisionConsumerMock twoConsumer = twoContainer.AddComponent <ActiveCollisionConsumerMock>(); eventData.ActiveCollisions.Add(oneData); eventData.ActiveCollisions.Add(twoData); subject.SetActiveCollisions(eventData); Assert.IsFalse(oneConsumer.received); Assert.IsFalse(twoConsumer.received); subject.Publish(); Assert.IsTrue(oneConsumer.received); Assert.IsTrue(twoConsumer.received); Assert.AreEqual("{ SourceContainer = [null] | PublisherContainer = New Game Object (UnityEngine.GameObject) }", subject.Payload.ToString()); Object.DestroyImmediate(oneContainer); Object.DestroyImmediate(twoContainer); }
public void SliceEmptyList() { UnityEventListenerMock slicedMock = new UnityEventListenerMock(); subject.Sliced.AddListener(slicedMock.Listen); UnityEventListenerMock remainedMock = new UnityEventListenerMock(); subject.Remained.AddListener(remainedMock.Listen); List <CollisionNotifier.EventData> collisionList = new List <CollisionNotifier.EventData>(); ActiveCollisionsContainer.EventData eventData = new ActiveCollisionsContainer.EventData().Set(collisionList); subject.StartIndex = 0; subject.Length = 1; Assert.AreEqual("", ActiveCollisionsHelper.GetNamesOfActiveCollisions(eventData)); Assert.IsFalse(slicedMock.Received); Assert.IsFalse(remainedMock.Received); ActiveCollisionsContainer.EventData remainedList; ActiveCollisionsContainer.EventData slicedList = subject.Slice(eventData, out remainedList); Assert.IsTrue(slicedMock.Received); Assert.IsTrue(remainedMock.Received); Assert.AreEqual("", ActiveCollisionsHelper.GetNamesOfActiveCollisions(slicedList)); Assert.AreEqual("", ActiveCollisionsHelper.GetNamesOfActiveCollisions(remainedList)); }
/// <summary> /// Slices the collision collection. /// </summary> /// <param name="originalList">The original collision collection.</param> /// <returns>The sliced collection.</returns> public virtual ActiveCollisionsContainer.EventData Slice(ActiveCollisionsContainer.EventData originalList) { SlicedList.Clear(); RemainingList.Clear(); if (!isActiveAndEnabled) { return(SlicedList); } int collectionCount = originalList.ActiveCollisions.Count; int actualStartIndex = GetStartIndex(StartIndex, collectionCount); int actualLength = GetRangeLength(actualStartIndex, (int)Length, collectionCount); for (int index = actualStartIndex; index < actualStartIndex + actualLength; index++) { SlicedList.ActiveCollisions.Add(originalList.ActiveCollisions[index]); } Sliced?.Invoke(SlicedList); foreach (CollisionNotifier.EventData originalCollision in originalList.ActiveCollisions) { if (!SlicedList.ActiveCollisions.Contains(originalCollision)) { RemainingList.ActiveCollisions.Add(originalCollision); } } Remained?.Invoke(RemainingList); return(SlicedList); }
protected virtual ActiveCollisionsContainer.EventData CreateActiveCollisionsEventData(GameObject forwardSource, Collision collision = null, Collider collider = null) { collider = (collider == null ? forwardSource.GetComponentInChildren <Collider>() : collider); CollisionNotifier.EventData collisionPayload = new CollisionNotifier.EventData(); collisionPayload.Set(forwardSource.TryGetComponent <Component>(), collider.isTrigger, collision, collider); ActiveCollisionsContainer.EventData activeCollisionPayload = new ActiveCollisionsContainer.EventData(); List <CollisionNotifier.EventData> collisionList = new List <CollisionNotifier.EventData>(); collisionList.Add(collisionPayload); activeCollisionPayload.Set(collisionList); return(activeCollisionPayload); }
public void Receive() { UnityEventListenerMock emittedMock = new UnityEventListenerMock(); subject.Emitted.AddListener(emittedMock.Listen); ActiveCollisionsContainer.EventData digest = new ActiveCollisionsContainer.EventData(); Assert.IsFalse(emittedMock.Received); subject.Receive(digest); Assert.AreEqual(digest, subject.Payload); Assert.IsTrue(emittedMock.Received); }
public void SetActiveCollisionsNoData() { Assert.AreEqual(0, subject.ActiveCollisions.Count); subject.SetActiveCollisions(null); Assert.AreEqual(0, subject.ActiveCollisions.Count); ActiveCollisionsContainer.EventData eventData = new ActiveCollisionsContainer.EventData(); eventData.activeCollisions = null; subject.SetActiveCollisions(eventData); Assert.AreEqual(0, subject.ActiveCollisions.Count); }
public void SliceInactiveComponent() { UnityEventListenerMock slicedMock = new UnityEventListenerMock(); subject.Sliced.AddListener(slicedMock.Listen); UnityEventListenerMock remainedMock = new UnityEventListenerMock(); subject.Remained.AddListener(remainedMock.Listen); List <CollisionNotifier.EventData> collisionList = new List <CollisionNotifier.EventData>(); GameObject oneContainer; CollisionNotifier.EventData oneData = CollisionNotifierHelper.GetEventData(out oneContainer); oneContainer.name = "one"; collisionList.Add(oneData); GameObject twoContainer; CollisionNotifier.EventData twoData = CollisionNotifierHelper.GetEventData(out twoContainer); twoContainer.name = "two"; collisionList.Add(twoData); GameObject threeContainer; CollisionNotifier.EventData threeData = CollisionNotifierHelper.GetEventData(out threeContainer); threeContainer.name = "three"; collisionList.Add(threeData); ActiveCollisionsContainer.EventData eventData = new ActiveCollisionsContainer.EventData().Set(collisionList); subject.StartIndex = 0; subject.Length = 1; subject.enabled = false; Assert.AreEqual("one,two,three", ActiveCollisionsHelper.GetNamesOfActiveCollisions(eventData)); Assert.IsFalse(slicedMock.Received); Assert.IsFalse(remainedMock.Received); ActiveCollisionsContainer.EventData remainedList; ActiveCollisionsContainer.EventData slicedList = subject.Slice(eventData, out remainedList); Assert.IsFalse(slicedMock.Received); Assert.IsFalse(remainedMock.Received); Assert.AreEqual("", ActiveCollisionsHelper.GetNamesOfActiveCollisions(slicedList)); Assert.AreEqual("one,two,three", ActiveCollisionsHelper.GetNamesOfActiveCollisions(remainedList)); Object.DestroyImmediate(oneContainer); Object.DestroyImmediate(twoContainer); Object.DestroyImmediate(threeContainer); }
/// <summary> /// Reverses the given collision collection. /// </summary> /// <param name="originalList">The original collision collection.</param> /// <returns>The reversed collision collection.</returns> public virtual ActiveCollisionsContainer.EventData Reverse(ActiveCollisionsContainer.EventData originalList) { if (!isActiveAndEnabled) { return(originalList); } reversedList.Set(originalList); reversedList.activeCollisions.Reverse(); Reversed?.Invoke(reversedList); return(reversedList); }
/// <summary> /// Sorts the given collision collection by the collisions that are nearest to the source <see cref="Transform"/>. /// </summary> /// <param name="originalList">The original collision collection.</param> /// <returns>The sorted collision collection.</returns> public virtual ActiveCollisionsContainer.EventData Sort(ActiveCollisionsContainer.EventData originalList) { if (!isActiveAndEnabled || Source == null) { return(originalList); } SortedList.Set(originalList); Comparer.SourcePosition = Source.transform.position; SortedList.ActiveCollisions.Sort(Comparison); Sorted?.Invoke(SortedList); return(SortedList); }
public static string GetNamesOfActiveCollisions(ActiveCollisionsContainer.EventData list, string separator = ",") { string returnString = ""; for (int i = 0; i < list.activeCollisions.Count; i++) { CollisionNotifier.EventData data = list.activeCollisions[i]; returnString += data.collider.name; if (i < list.activeCollisions.Count - 1) { returnString += separator; } } return(returnString); }
public void ReceiveInactiveComponent() { UnityEventListenerMock emittedMock = new UnityEventListenerMock(); subject.Emitted.AddListener(emittedMock.Listen); ActiveCollisionsContainer.EventData digest = new ActiveCollisionsContainer.EventData(); subject.enabled = false; Assert.IsNull(subject.Payload); Assert.IsFalse(emittedMock.Received); subject.Receive(digest); Assert.IsNull(subject.Payload); Assert.IsFalse(emittedMock.Received); }
public void ReverseEmptyList() { UnityEventListenerMock reversedMock = new UnityEventListenerMock(); subject.Reversed.AddListener(reversedMock.Listen); List <CollisionNotifier.EventData> collisionList = new List <CollisionNotifier.EventData>(); ActiveCollisionsContainer.EventData eventData = new ActiveCollisionsContainer.EventData().Set(collisionList); Assert.AreEqual("", ActiveCollisionsHelper.GetNamesOfActiveCollisions(eventData)); Assert.IsFalse(reversedMock.Received); ActiveCollisionsContainer.EventData reversedList = subject.Reverse(eventData); Assert.IsTrue(reversedMock.Received); Assert.AreEqual("", ActiveCollisionsHelper.GetNamesOfActiveCollisions(reversedList)); }
public void SetActiveCollisions() { ActiveCollisionsContainer.EventData eventData = new ActiveCollisionsContainer.EventData(); GameObject oneContainer; CollisionNotifier.EventData oneData = CollisionNotifierHelper.GetEventData(out oneContainer); GameObject twoContainer; CollisionNotifier.EventData twoData = CollisionNotifierHelper.GetEventData(out twoContainer); eventData.ActiveCollisions.Add(oneData); eventData.ActiveCollisions.Add(twoData); Assert.AreEqual(0, subject.Payload.ActiveCollisions.Count); subject.SetActiveCollisions(eventData); Assert.AreEqual(2, subject.Payload.ActiveCollisions.Count); Object.DestroyImmediate(oneContainer); Object.DestroyImmediate(twoContainer); }
public void SortInactiveGameObject() { UnityEventListenerMock sortedMock = new UnityEventListenerMock(); subject.Sorted.AddListener(sortedMock.Listen); List <CollisionNotifier.EventData> collisionList = new List <CollisionNotifier.EventData>(); GameObject oneContainer; CollisionNotifier.EventData oneData = CollisionNotifierHelper.GetEventData(out oneContainer, Vector3.one * 5f); oneContainer.name = "one"; collisionList.Add(oneData); GameObject twoContainer; CollisionNotifier.EventData twoData = CollisionNotifierHelper.GetEventData(out twoContainer, Vector3.one * 2f); twoContainer.name = "two"; collisionList.Add(twoData); GameObject threeContainer; CollisionNotifier.EventData threeData = CollisionNotifierHelper.GetEventData(out threeContainer, Vector3.one * 3f); threeContainer.name = "three"; collisionList.Add(threeData); ActiveCollisionsContainer.EventData eventData = new ActiveCollisionsContainer.EventData().Set(collisionList); subject.Source = containingObject; subject.gameObject.SetActive(false); Assert.AreEqual("one,two,three", ActiveCollisionsHelper.GetNamesOfActiveCollisions(eventData)); Assert.IsFalse(sortedMock.Received); ActiveCollisionsContainer.EventData sortedList = subject.Sort(eventData); Assert.IsFalse(sortedMock.Received); Assert.AreEqual("one,two,three", ActiveCollisionsHelper.GetNamesOfActiveCollisions(sortedList)); Object.DestroyImmediate(oneContainer); Object.DestroyImmediate(twoContainer); Object.DestroyImmediate(threeContainer); }
public void SortEmptyList() { UnityEventListenerMock sortedMock = new UnityEventListenerMock(); subject.Sorted.AddListener(sortedMock.Listen); List <CollisionNotifier.EventData> collisionList = new List <CollisionNotifier.EventData>(); ActiveCollisionsContainer.EventData eventData = new ActiveCollisionsContainer.EventData().Set(collisionList); subject.Source = containingObject; Assert.AreEqual("", ActiveCollisionsHelper.GetNamesOfActiveCollisions(eventData)); Assert.IsFalse(sortedMock.Received); ActiveCollisionsContainer.EventData sortedList = subject.Sort(eventData); Assert.IsTrue(sortedMock.Received); Assert.AreEqual("", ActiveCollisionsHelper.GetNamesOfActiveCollisions(sortedList)); }
public void PublishWithRegisteredConsumerContainer() { ActiveCollisionsContainer.EventData eventData = new ActiveCollisionsContainer.EventData(); GameObject oneContainer; CollisionNotifier.EventData oneData = CollisionNotifierHelper.GetEventData(out oneContainer); ActiveCollisionConsumerMock oneConsumer = oneContainer.AddComponent <ActiveCollisionConsumerMock>(); GameObject twoContainer; CollisionNotifier.EventData twoData = CollisionNotifierHelper.GetEventData(out twoContainer); ActiveCollisionConsumerMock twoConsumer = twoContainer.AddComponent <ActiveCollisionConsumerMock>(); eventData.ActiveCollisions.Add(oneData); eventData.ActiveCollisions.Add(twoData); subject.SetActiveCollisions(eventData); ActiveCollisionRegisteredConsumerContainerMock reigsteredConsumerContainer = containingObject.AddComponent <ActiveCollisionRegisteredConsumerContainerMock>(); subject.RegisteredConsumerContainer = reigsteredConsumerContainer; Assert.AreEqual(0, reigsteredConsumerContainer.ConsumerCount); Assert.AreEqual(0, reigsteredConsumerContainer.IgnoredConsumerCount); subject.Publish(); Assert.AreEqual(2, reigsteredConsumerContainer.ConsumerCount); Assert.AreEqual(2, reigsteredConsumerContainer.IgnoredConsumerCount); subject.UnregisterRegisteredConsumer(twoConsumer); Assert.AreEqual(1, reigsteredConsumerContainer.ConsumerCount); Assert.AreEqual(1, reigsteredConsumerContainer.IgnoredConsumerCount); subject.UnregisterRegisteredConsumer(oneConsumer); Assert.AreEqual(0, reigsteredConsumerContainer.ConsumerCount); Assert.AreEqual(0, reigsteredConsumerContainer.IgnoredConsumerCount); Object.DestroyImmediate(oneContainer); Object.DestroyImmediate(twoContainer); }
/// <summary> /// Sorts the given collision collection by the collisions that are nearest to the source <see cref="Transform"/>. /// </summary> /// <param name="originalList">The original collision collection.</param> /// <returns>The sorted collision collection.</returns> public virtual ActiveCollisionsContainer.EventData Sort(ActiveCollisionsContainer.EventData originalList) { if (!isActiveAndEnabled || source == null) { return(originalList); } SortedList.Set(originalList); SortedList.activeCollisions.Sort( (data1, data2) => { float distance1 = Vector3.Distance(data1.collider.GetContainingTransform().position, source.transform.position); float distance2 = Vector3.Distance(data2.collider.GetContainingTransform().position, source.transform.position); return(distance1.CompareTo(distance2)); } ); Sorted?.Invoke(SortedList); return(SortedList); }
public void Reverse() { UnityEventListenerMock reversedMock = new UnityEventListenerMock(); subject.Reversed.AddListener(reversedMock.Listen); List <CollisionNotifier.EventData> collisionList = new List <CollisionNotifier.EventData>(); GameObject oneContainer; CollisionNotifier.EventData oneData = CollisionNotifierHelper.GetEventData(out oneContainer); oneContainer.name = "one"; collisionList.Add(oneData); GameObject twoContainer; CollisionNotifier.EventData twoData = CollisionNotifierHelper.GetEventData(out twoContainer); twoContainer.name = "two"; collisionList.Add(twoData); GameObject threeContainer; CollisionNotifier.EventData threeData = CollisionNotifierHelper.GetEventData(out threeContainer); threeContainer.name = "three"; collisionList.Add(threeData); ActiveCollisionsContainer.EventData eventData = new ActiveCollisionsContainer.EventData().Set(collisionList); Assert.AreEqual("one,two,three", ActiveCollisionsHelper.GetNamesOfActiveCollisions(eventData)); Assert.IsFalse(reversedMock.Received); ActiveCollisionsContainer.EventData reversedList = subject.Reverse(eventData); Assert.IsTrue(reversedMock.Received); Assert.AreEqual("three,two,one", ActiveCollisionsHelper.GetNamesOfActiveCollisions(reversedList)); Object.DestroyImmediate(oneContainer); Object.DestroyImmediate(twoContainer); Object.DestroyImmediate(threeContainer); }
/// <summary> /// Slices the collision collection. /// </summary> /// <param name="originalList">The original collision collection.</param> /// <returns>The sliced collection.</returns> public virtual ActiveCollisionsContainer.EventData Slice(ActiveCollisionsContainer.EventData originalList) { SlicedList.Clear(); RemainingList.Clear(); if (!isActiveAndEnabled) { return(SlicedList); } uint collectionCount = (uint)originalList.activeCollisions.Count; uint actualStartIndex = GetStartIndex(startIndex, collectionCount); uint actualLength = GetRangeLength(actualStartIndex, length, collectionCount); SlicedList.activeCollisions.AddRange(originalList.activeCollisions.GetRange((int)actualStartIndex, (int)actualLength)); Sliced?.Invoke(SlicedList); RemainingList.activeCollisions.AddRange(originalList.activeCollisions.Except(SlicedList.activeCollisions)); Remained?.Invoke(RemainingList); return(SlicedList); }
/// <summary> /// Slices the collision collection. /// </summary> /// <param name="originalList">The original collision collection.</param> public virtual void DoSlice(ActiveCollisionsContainer.EventData originalList) { Slice(originalList); }
/// <summary> /// Slices the collision collection. /// </summary> /// <param name="originalList">The original collision collection.</param> /// <param name="remaining">The collection of remaining elements that were not included in the sliced collection.</param> /// <returns>The sliced collection.</returns> public virtual ActiveCollisionsContainer.EventData Slice(ActiveCollisionsContainer.EventData originalList, out ActiveCollisionsContainer.EventData remaining) { ActiveCollisionsContainer.EventData returnList = Slice(originalList); remaining = (isActiveAndEnabled ? RemainingList : originalList); return(returnList); }
public void LengthIsZero() { UnityEventListenerMock slicedMock = new UnityEventListenerMock(); subject.Sliced.AddListener(slicedMock.Listen); UnityEventListenerMock remainedMock = new UnityEventListenerMock(); subject.Remained.AddListener(remainedMock.Listen); List <CollisionNotifier.EventData> collisionList = new List <CollisionNotifier.EventData>(); GameObject oneContainer; CollisionNotifier.EventData oneData = CollisionNotifierHelper.GetEventData(out oneContainer); oneContainer.name = "one"; collisionList.Add(oneData); GameObject twoContainer; CollisionNotifier.EventData twoData = CollisionNotifierHelper.GetEventData(out twoContainer); twoContainer.name = "two"; collisionList.Add(twoData); GameObject threeContainer; CollisionNotifier.EventData threeData = CollisionNotifierHelper.GetEventData(out threeContainer); threeContainer.name = "three"; collisionList.Add(threeData); GameObject fourContainer; CollisionNotifier.EventData fourData = CollisionNotifierHelper.GetEventData(out fourContainer); fourContainer.name = "four"; collisionList.Add(fourData); GameObject fiveContainer; CollisionNotifier.EventData fiveData = CollisionNotifierHelper.GetEventData(out fiveContainer); fiveContainer.name = "five"; collisionList.Add(fiveData); GameObject sixContainer; CollisionNotifier.EventData sixData = CollisionNotifierHelper.GetEventData(out sixContainer); sixContainer.name = "six"; collisionList.Add(sixData); ActiveCollisionsContainer.EventData eventData = new ActiveCollisionsContainer.EventData().Set(collisionList); subject.StartIndex = 0; subject.Length = 0; ActiveCollisionsContainer.EventData remainedList; ActiveCollisionsContainer.EventData slicedList = subject.Slice(eventData, out remainedList); Assert.IsTrue(slicedMock.Received); Assert.IsTrue(remainedMock.Received); Assert.AreEqual("", ActiveCollisionsHelper.GetNamesOfActiveCollisions(slicedList)); Assert.AreEqual("one,two,three,four,five,six", ActiveCollisionsHelper.GetNamesOfActiveCollisions(remainedList)); Object.DestroyImmediate(oneContainer); Object.DestroyImmediate(twoContainer); Object.DestroyImmediate(threeContainer); Object.DestroyImmediate(fourContainer); Object.DestroyImmediate(fiveContainer); Object.DestroyImmediate(sixContainer); }