using System.Collections; using System.Reflection; using BumpCombat.Combat; using BumpCombat.Constants; using BumpCombat.Player; using BumpCombat.Progression; using NUnit.Framework; using UnityEngine; using UnityEngine.TestTools; namespace BumpCombat.PlayModeTests { public sealed class GameplayConstantsConsumptionPlayModeTests { private static readonly BindingFlags NonPublicInstance = BindingFlags.Instance | BindingFlags.NonPublic; [UnityTest] public IEnumerator ExperienceOrb_UsesChangedPickupRadius() { ItemConstants constants = GameplayConstants.Current.Items; float originalPickupRadius = constants.ExperienceOrbPickupRadius; float originalMagnetSpeed = constants.ExperienceOrbMagnetSpeed; GameObject playerObject = new("Constants Orb Player"); GameObject orbObject = new("Constants Orb"); try { playerObject.AddComponent(); playerObject.AddComponent(); Rigidbody2D orbBody = orbObject.AddComponent(); orbObject.AddComponent(); ExperienceOrb orb = orbObject.AddComponent(); orb.Initialize(1); playerObject.transform.position = Vector3.zero; orbBody.position = new Vector2(0.3f, 0f); orbBody.gravityScale = 0f; constants.ExperienceOrbPickupRadius = 0.2f; constants.ExperienceOrbMagnetSpeed = 0f; yield return null; // ExperienceOrb resolves its target through the scene-wide // lookup in Start. Bind this fixture back to its own player // after Start so another scene player cannot make the radius // assertion depend on test order. SetPrivate(orb, "experienceSystem", playerObject.GetComponent()); SetPrivate(orb, "player", playerObject.transform); yield return new WaitForFixedUpdate(); Assert.That(orb, Is.Not.Null); Assert.That(GetPrivate(orb, "pickedUp"), Is.False); constants.ExperienceOrbPickupRadius = 0.4f; yield return new WaitForFixedUpdate(); Assert.That(GetPrivate(orb, "pickedUp"), Is.True); } finally { constants.ExperienceOrbPickupRadius = originalPickupRadius; constants.ExperienceOrbMagnetSpeed = originalMagnetSpeed; if (playerObject != null) { Object.Destroy(playerObject); } if (orbObject != null) { Object.Destroy(orbObject); } } } [UnityTest] public IEnumerator CombatFeedback_UsesChangedHeavyEffectLimit() { CombatFeedbackSettings constants = GameplayConstants.Current.CombatFeedback; int originalLimit = constants.MaxConcurrentHeavyEffects; GameObject feedbackObject = new("Constants Feedback Host"); GameObject targetObject = new("Constants Feedback Target"); try { constants.MaxConcurrentHeavyEffects = 2; feedbackObject.AddComponent(); CombatFeedback feedback = feedbackObject.AddComponent(); yield return null; for (int i = 0; i < 8; i++) { CombatEvents.RaiseEnemyStagger(targetObject, 1, 1, true); CombatEvents.RaiseLaunchResisted(targetObject); } Assert.That(CombatFeedback.MaxConcurrentHeavyEffects, Is.EqualTo(2)); Assert.That(feedback.ActiveHeavyEffectCount, Is.GreaterThan(0)); Assert.That(feedback.ActiveHeavyEffectCount, Is.LessThanOrEqualTo(2)); } finally { constants.MaxConcurrentHeavyEffects = originalLimit; if (targetObject != null) { Object.Destroy(targetObject); } if (feedbackObject != null) { Object.Destroy(feedbackObject); } } } private static T GetPrivate(object target, string fieldName) { FieldInfo field = target.GetType().GetField(fieldName, NonPublicInstance); Assert.That(field, Is.Not.Null, fieldName); return (T)field.GetValue(target); } private static void SetPrivate(object target, string fieldName, object value) { FieldInfo field = target.GetType().GetField(fieldName, NonPublicInstance); Assert.That(field, Is.Not.Null, fieldName); field.SetValue(target, value); } } }