using System.Collections;
using TagFighter;
using TagFighter.Events;
using TagFighter.Resources;
using Testing.Actions;
using Testing.Line;
using Testing.TimeDialation;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.EventSystems;
public class Tester : MonoBehaviour
{
[SerializeField] Vector3 _forceToApply;
[SerializeField] Transform _affected;
[SerializeField] LayerMask _unitMovementLayerMask;
[SerializeField] ForceMode _forceMode;
[SerializeField] EventAggregator _eventAggregator;
[SerializeField] bool _shouldTestTags = false;
[SerializeField] bool _shouldTestPhysics = false;
[SerializeField] bool _shouldTestPawnConditions = false;
[SerializeField] bool _shouldRunAutoTests = false;
Renderer _r;
Rigidbody _rb;
NavMeshAgent _nma;
bool _pushed;
Vector3 _resetPosition;
protected void Start() {
if (_affected) {
_r = _affected.GetComponent<Renderer>();
_rb = _affected.GetComponent<Rigidbody>();
_nma = _affected.GetComponent<NavMeshAgent>();
_resetPosition = _affected.transform.position;
}
Reset();
}
protected void Update() {
if (_shouldTestTags) {
TestTags();
}
if (_shouldTestPhysics) {
TestPhysics();
}
if (_shouldTestPawnConditions) {
TestAddPawnCondition();
}
if (_shouldRunAutoTests) {
AutoTests();
}
}
void TestAddPawnCondition() {
if (Input.GetKeyDown(KeyCode.T)) {
var condition = _affected.gameObject.AddComponent<TagFighter.Effects.PawnCondition>();
condition.Apply();
}
}
void TestTags() {
if (Input.GetKeyDown(KeyCode.L)) {
var color = UnityEngine.Random.Range(0, 3);
switch (color) {
case 0: {
var modifier = new StatModifier<BlueTagUnit> {
Amount = (Unit<BlueTagUnit>)1
};
_affected.GetComponent<BlueTag>().AddCurrentModifier(modifier, System.Threading.CancellationToken.None);
break;
}
case 1: {
var modifier = new StatModifier<RedTagUnit> {
Amount = (Unit<RedTagUnit>)1
};
_affected.GetComponent<RedTag>().AddCurrentModifier(modifier, System.Threading.CancellationToken.None);
break;
}
case 2: {
var modifier = new StatModifier<GreenTagUnit> {
Amount = (Unit<GreenTagUnit>)1
};
_affected.GetComponent<GreenTag>().AddCurrentModifier(modifier, System.Threading.CancellationToken.None);
break;
}
}
}
}
void AutoTests() {
if (Input.GetKeyDown(KeyCode.T)) {
if (Input.GetKey(KeyCode.LeftShift)) {
Test();
}
}
}
void TestPhysics() {
if (Input.GetKeyDown(KeyCode.T)) {
if (!Input.GetKey(KeyCode.LeftShift)) {
_pushed = true;
}
}
if (Input.GetKeyDown(KeyCode.B) && !EventSystem.current.IsPointerOverGameObject()) {
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out var hit)) {
var objectHit = hit.transform;
if (_unitMovementLayerMask.IsLayerInMask(objectHit.gameObject.layer)) {
_nma.Warp(hit.point);
}
}
}
if (Input.GetKeyDown(KeyCode.R)) {
Reset();
}
if (Input.GetKeyDown(KeyCode.P)) {
print($"Velocity ({_rb.velocity}), Magnitude {_rb.velocity.magnitude}");
print($"world bounds {_r.bounds} local bounds {_r.localBounds}");
}
}
protected void FixedUpdate() {
if (_rb != null && !_rb.isKinematic && _nma.isOnNavMesh && _rb.velocity.sqrMagnitude < 0.25f) {
if (Physics.Raycast(_affected.transform.position, Vector3.down, _r.bounds.extents.y + 0.1f, _unitMovementLayerMask)) {
_rb.isKinematic = true;
_nma.updatePosition = true;
_nma.updateRotation = true;
_nma.Warp(_affected.transform.position);
}
//nma.enabled = true;
}
if (_pushed) {
// nma.enabled = false;
_nma.updatePosition = false;
_nma.updateRotation = false;
_rb.isKinematic = false;
_rb.AddForce(_forceToApply, _forceMode);
_pushed = false;
}
}
void Reset() {
if (_rb != null) {
_rb.isKinematic = true;
}
if (_nma) {
_nma.updatePosition = true;
_nma.updateRotation = true;
_nma.enabled = true;
_pushed = false;
}
if (_affected) {
_affected.transform.position = _resetPosition;
}
}
[SerializeField] Weaver _actionTesterWeaver;
[SerializeField] Transform _actionTesterTransform1;
[SerializeField] Transform _actionTesterTransform2;
void Test() {
TimeDialationTester timeDialationTester = new(_eventAggregator);
timeDialationTester.TestAll();
LineTester lineTester = new();
lineTester.TestAll();
ActionsTester actionsTester = new(_actionTesterWeaver, _actionTesterTransform1, _actionTesterTransform2);
actionsTester.TestAll();
}
}
namespace Testing.Actions
{
public class ActionsTester
{
Weaver _weaver;
Transform _transform1;
Transform _transform2;
public ActionsTester(Weaver weaver, Transform transform1, Transform transform2) {
_weaver = weaver;
_transform1 = transform1;
_transform2 = transform2;
}
public void TestAll() {
TestFollowActionConcreteEqual();
TestFollowActionInterfaceEqual();
TestActionInterfaceNotEqual();
TestMoveToActionConcreteEqual();
TestMoveToActionInterfaceEqual();
}
public void TestFollowActionConcreteEqual() {
var testName = System.Reflection.MethodBase.GetCurrentMethod().Name;
var action1 = new FollowAction(_weaver, _transform1);
var action2 = new FollowAction(_weaver, _transform1);
if (action1.IsSimilarAction(action2)) {
Debug.Log($"<color=green>PASSED: {testName}</color>");
}
else {
Debug.Log($"<color=red>FAILED: {testName}</color>");
}
}
public void TestFollowActionInterfaceEqual() {
var testName = System.Reflection.MethodBase.GetCurrentMethod().Name;
IAction action1 = new FollowAction(_weaver, _transform1);
IAction action2 = new FollowAction(_weaver, _transform1);
if (action1.IsSimilarAction(action2)) {
Debug.Log($"<color=green>PASSED: {testName}</color>");
}
else {
Debug.Log($"<color=red>FAILED: {testName}</color>");
}
}
public void TestActionInterfaceNotEqual() {
var testName = System.Reflection.MethodBase.GetCurrentMethod().Name;
IAction action1 = new FollowAction(_weaver, _transform1);
IAction action2 = new MoveToAction(_weaver, _transform1.position);
if (!action1.IsSimilarAction(action2)) {
Debug.Log($"<color=green>PASSED: {testName}</color>");
}
else {
Debug.Log($"<color=red>FAILED: {testName}</color>");
}
using System;
using System.Collections;
using CareBoo.Serially;
using TagFighter;
using TagFighter.Events;
using TagFighter.Resources;
using Testing.Actions;
using Testing.Line;
using Testing.Optics;
using Testing.TimeDialation;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.EventSystems;
public class Tester : MonoBehaviour
{
[SerializeField] Vector3 _forceToApply;
[SerializeField] Transform _affected;
[SerializeField] LayerMask _unitMovementLayerMask;
[SerializeField] ForceMode _forceMode;
[SerializeField] EventAggregator _eventAggregator;
[SerializeField] bool _shouldTestTags = false;
[SerializeField] bool _shouldTestPhysics = false;
[SerializeField] bool _shouldTestPawnConditions = false;
[SerializeField] bool _shouldRunAutoTests = false;
Renderer _r;
Rigidbody _rb;
NavMeshAgent _nma;
bool _pushed;
Vector3 _resetPosition;
protected void Start() {
if (_affected) {
_r = _affected.GetComponent<Renderer>();
_rb = _affected.GetComponent<Rigidbody>();
_nma = _affected.GetComponent<NavMeshAgent>();
_resetPosition = _affected.transform.position;
}
Reset();
}
protected void Update() {
if (_shouldTestTags) {
TestTags();
}
if (_shouldTestPhysics) {
TestPhysics();
}
if (_shouldTestPawnConditions) {
TestAddPawnCondition();
}
if (_shouldRunAutoTests) {
AutoTests();
}
}
void TestAddPawnCondition() {
if (Input.GetKeyDown(KeyCode.T)) {
var condition = _affected.gameObject.AddComponent<TagFighter.Effects.PawnCondition>();
condition.Apply();
}
}
void TestTags() {
if (Input.GetKeyDown(KeyCode.L)) {
var color = UnityEngine.Random.Range(0, 3);
switch (color) {
case 0: {
var modifier = new StatModifier<BlueTagUnit> {
Amount = (Unit<BlueTagUnit>)1
};
_affected.GetComponent<BlueTag>().AddCurrentModifier(modifier, System.Threading.CancellationToken.None);
break;
}
case 1: {
var modifier = new StatModifier<RedTagUnit> {
Amount = (Unit<RedTagUnit>)1
};
_affected.GetComponent<RedTag>().AddCurrentModifier(modifier, System.Threading.CancellationToken.None);
break;
}
case 2: {
var modifier = new StatModifier<GreenTagUnit> {
Amount = (Unit<GreenTagUnit>)1
};
_affected.GetComponent<GreenTag>().AddCurrentModifier(modifier, System.Threading.CancellationToken.None);
break;
}
}
}
}
void AutoTests() {
if (Input.GetKeyDown(KeyCode.T)) {
if (Input.GetKey(KeyCode.LeftShift)) {
Test();
}
}
}
void TestPhysics() {
if (Input.GetKeyDown(KeyCode.T)) {
if (!Input.GetKey(KeyCode.LeftShift)) {
_pushed = true;
}
}
if (Input.GetKeyDown(KeyCode.B) && !EventSystem.current.IsPointerOverGameObject()) {
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out var hit)) {
var objectHit = hit.transform;
if (_unitMovementLayerMask.IsLayerInMask(objectHit.gameObject.layer)) {
_nma.Warp(hit.point);
}
}
}
if (Input.GetKeyDown(KeyCode.R)) {
Reset();
}
if (Input.GetKeyDown(KeyCode.P)) {
print($"Velocity ({_rb.velocity}), Magnitude {_rb.velocity.magnitude}");
print($"world bounds {_r.bounds} local bounds {_r.localBounds}");
}
}
protected void FixedUpdate() {
if (_rb != null && !_rb.isKinematic && _nma.isOnNavMesh && _rb.velocity.sqrMagnitude < 0.25f) {
if (Physics.Raycast(_affected.transform.position, Vector3.down, _r.bounds.extents.y + 0.1f, _unitMovementLayerMask)) {
_rb.isKinematic = true;
_nma.updatePosition = true;
_nma.updateRotation = true;
_nma.Warp(_affected.transform.position);
}
//nma.enabled = true;
}
if (_pushed) {
// nma.enabled = false;
_nma.updatePosition = false;
_nma.updateRotation = false;
_rb.isKinematic = false;
_rb.AddForce(_forceToApply, _forceMode);
_pushed = false;
}
}
void Reset() {
if (_rb != null) {
_rb.isKinematic = true;
}
if (_nma) {
_nma.updatePosition = true;
_nma.updateRotation = true;
_nma.enabled = true;
_pushed = false;
}
if (_affected) {
_affected.transform.position = _resetPosition;
}
}
[ContextMenuItem("Test", "TestOptics")]
[SerializeField]
OpticsTester opticsTester;
[SerializeField] ActionsTester actionsTester;
void Test() {
//TimeDialationTester timeDialationTester = new(_eventAggregator);
//timeDialationTester.TestAll();
//LineTester lineTester = new();
//lineTester.TestAll();
//ActionsTester actionsTester = new(_actionTesterWeaver, _actionTesterTransform1, _actionTesterTransform2);
//actionsTester.TestAll();
opticsTester.TestAll();
}
void TestOptics() {
opticsTester.TestAll();
}
}
namespace Testing.Optics
{
using CareBoo.Serially;
using System;
using TagFighter.Effects;
using TagFighter.Effects.ResourceLocationAccessors.ContextRegisters;
using TagFighter.Optics;
using TagFighter.Resources;
public interface IOpticsVisitor
{
public Lens<Transform, int> VisitTransform(PawnAccessor pawnLensCreator);
public Lens<EffectContext, int> VisitContext(ContextAccessor contextLensCreator);
}
public class OpticsVisitor<TResource, TUnitType> : IOpticsVisitor
where TResource : Resource<TUnitType>
where TUnitType : IUnitType
{
public Lens<Transform, int> VisitTransform(PawnAccessor pawnLensCreator) {
return pawnLensCreator.CreateSpecific<Resource<TUnitType>, TUnitType>();
}
public Lens<EffectContext, int> VisitContext(ContextAccessor contextLensCreator) {
return contextLensCreator.CreateSpecific<Resource<TUnitType>, TUnitType>();
}
}
[ProvideSourceInfo][Serializable] public sealed class BlueTagVisitor : OpticsVisitor<BlueTag, BlueTagUnit> { };
[ProvideSourceInfo][Serializable] public sealed class RedTagVisitor : OpticsVisitor<RedTag, RedTagUnit> { };
[ProvideSourceInfo][Serializable] public sealed class GreenTagVisitor : OpticsVisitor<GreenTag, GreenTagUnit> { };
public interface IPawnAccessor
{
public Lens<Transform, int> Create();
public Lens<Transform, int> CreateSpecific<TResource, TUnitType>() where TResource : Resource<TUnitType> where TUnitType : IUnitType;
};
[Serializable]
public class PawnAccessor : IPawnAccessor
{
[SerializeReference, ShowSerializeReference]
public IOpticsVisitor ResourceLensVisitor;
[SerializeReference, ShowSerializeReference]
public IResourceStatLensCreator ResourceLensCreator; // Current/Capacity
public Lens<Transform, int> Create() {
return ResourceLensVisitor.VisitTransform(this);
}
public Lens<Transform, int> CreateSpecific<TResource, TUnitType>()
where TResource : Resource<TUnitType>
where TUnitType : IUnitType {
var resource = new Lens<Transform, TResource>(t => t.GetComponent<TResource>(), (t, r) => t);
var statLens = ResourceLensCreator.Create<TResource, TUnitType>();
var valueLens = new Lens<Unit<TUnitType>, int>(u => u.AsPrimitive(), (u, v) => u = (Unit<TUnitType>)v);
var lens = resource.Compose(statLens).Compose(valueLens);
return lens;
}
}
public interface IContextAccessor
{
public Lens<EffectContext, int> Create();
public Lens<EffectContext, int> CreateSpecific<TResource, TUnitType>() where TResource : Resource<TUnitType> where TUnitType : IUnitType;
}
public class ContextAccessor : IContextAccessor
{
// Resource/UnitType
[SerializeReference, ShowSerializeReference]
public IOpticsVisitor ResourceLensVisitor;
// Register
[SerializeReference, ShowSerializeReference]
public IRegisterTypeLensCreator RegisterTypeLensCreator;
public Lens<EffectContext, int> Create() {
return ResourceLensVisitor.VisitContext(this);
}
public Lens<EffectContext, int> CreateSpecific<TResource, TUnitType>()
where TResource : Resource<TUnitType>
where TUnitType : IUnitType {
var registerLens = RegisterTypeLensCreator.Create<TResource, TUnitType>();
var valueLens = new Lens<Unit<TUnitType>, int>(u => u.AsPrimitive(), (u, v) => u = (Unit<TUnitType>)v);
return registerLens.Compose(valueLens);
}
}
public interface IEffectInputLensCreator
{
public Lens<EffectContext, Unit<TUnitType>> LensContext<TResource, TUnitType>(IRegisterTypeLensCreator register)
where TResource : Resource<TUnitType>
where TUnitType : IUnitType;
public Lens<Transform, Unit<TUnitType>> CreateLensTransform<TResource, TUnitType>(IResourceStatLensCreator stat)
where TResource : Resource<TUnitType>
where TUnitType : IUnitType;
}
public class EffectInputLensCreator : IEffectInputLensCreator
{
public Lens<EffectContext, Unit<TUnitType>> LensContext<TResource, TUnitType>(IRegisterTypeLensCreator register)
where TResource : Resource<TUnitType>
where TUnitType : IUnitType {
return register.Create<TResource, TUnitType>();
}
public Lens<Transform, Unit<TUnitType>> CreateLensTransform<TResource, TUnitType>(IResourceStatLensCreator stat)
where TResource : Resource<TUnitType>
where TUnitType : IUnitType {
var transformLen = new Lens<Transform, TResource>(t => t.GetComponent<TResource>(), (t, r) => t);
var statLens = stat.Create<TResource, TUnitType>();
return transformLen.Compose(statLens);
}
}
public interface IRegisterTypeLensCreator
{
public Lens<EffectContext, Unit<TUnitType>> Create<TResource, TUnitType>()
where TResource : Resource<TUnitType>
where TUnitType : IUnitType;
}
public class CurrentRegisterTypeLensCreator : IRegisterTypeLensCreator
{
public Lens<EffectContext, Unit<TUnitType>> Create<TResource, TUnitType>()
where TResource : Resource<TUnitType>
where TUnitType : IUnitType {
return new Lens<EffectContext, Unit<TUnitType>>(
context => context.GetResource<TResource, TUnitType, Current>(),
(context, v) => {
context.SetResource<TResource, TUnitType, Current>(v);
return context;
}
);
}
}
[Serializable]
public class OpticsTester
{
[SerializeField] Transform _transform;
[SerializeReference, ShowSerializeReference] IPawnAccessor _accessor;
public void TestAll() {
Debug.Log(_accessor.Create().Get(_transform));
}
}
}
namespace Testing.Actions
{
[Serializable]
public class ActionsTester
{
[SerializeField] Weaver _weaver;
[SerializeField] Transform _transform1;
[SerializeField] Transform _transform2;
public void TestAll() {
TestFollowActionConcreteEqual();
TestFollowActionInterfaceEqual();
TestActionInterfaceNotEqual();
TestMoveToActionConcreteEqual();
TestMoveToActionInterfaceEqual();
}
public void TestFollowActionConcreteEqual() {
var testName = System.Reflection.MethodBase.GetCurrentMethod().Name;
var action1 = new FollowAction(_weaver, _transform1);
var action2 = new FollowAction(_weaver, _transform1);
if (action1.IsSimilarAction(action2)) {
Debug.Log($"<color=green>PASSED: {testName}</color>");
}
else {
Debug.Log($"<color=red>FAILED: {testName}</color>");
}
}
public void TestFollowActionInterfaceEqual() {
var testName = System.Reflection.MethodBase.GetCurrentMethod().Name;
IAction action1 = new FollowAction(_weaver, _transform1);
IAction action2 = new FollowAction(_weaver, _transform1);
if (action1.IsSimilarAction(action2)) {
Debug.Log($"<color=green>PASSED: {testName}</color>");
}
else {
Debug.Log($"<color=red>FAILED: {testName}</color>");
}
}
public void TestActionInterfaceNotEqual() {
var testName = System.Reflection.MethodBase.GetCurrentMethod().Name;
IAction action1 = new FollowAction(_weaver, _transform1);
IAction action2 = new MoveToAction(_weaver, _transform1.position);
if (!action1.IsSimilarAction(action2)) {
Debug.Log($"<color=green>PASSED: {testName}</color>");
}
else {
Debug.Log($"<color=red>FAILED: {testName}</color>");
}