B:BD[
3.286] → [
2.353:14862]
∅:D[
2.14862] → [
4.63794:65669]
B:BD[
4.63794] → [
4.63794:65669]
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>");
}
}
public void TestMoveToActionConcreteEqual() {
var testName = System.Reflection.MethodBase.GetCurrentMethod().Name;
var action1 = new MoveToAction(_weaver, _transform1.position);
var 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>");
}
}
public void TestMoveToActionInterfaceEqual() {
var testName = System.Reflection.MethodBase.GetCurrentMethod().Name;
IAction action1 = new MoveToAction(_weaver, _transform1.position);
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>");
}
}
}
}
namespace Testing.TimeDialation
{
public class TimeDialationTester
{
EventAggregator _eventAggregator;
public TimeDialationTester(EventAggregator eventAggregator) {
_eventAggregator = eventAggregator;
}
public void TestAll() {
TestTimeDialationSpeedUp();
TestTimeDialationSpeedDown();
TestTimeDialationSpeedPause();
TestTimeDialationSpeedReset();
}
public void TestTimeDialationSpeedUp() {
var currentTimeScale = Time.timeScale;
var testName = System.Reflection.MethodBase.GetCurrentMethod().Name;
_eventAggregator.OnTimeDilationSpeedUp(this, System.EventArgs.Empty);
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 Mono.Cecil;
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Reflection.Emit;
using TagFighter.Effects;
using TagFighter.Effects.ResourceLocationAccessors.ContextRegisters;
using TagFighter.Optics;
using TagFighter.Resources;
using TMPro;
using Unity.VisualScripting;
using static Unity.VisualScripting.Metadata;
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;
//object BuildResourceLens(Type resourceType) {
// var lensType = typeof(Lens<,>).MakeGenericType(typeof(Transform), resourceType);
// var lensCtor = lensType.GetConstructors().First();
// ParameterExpression t = Expression.Parameter(typeof(Transform), "t");
// Expression call = Expression.Call(t, "GetComponent", new Type[] { resourceType });
// var lambda1 = Expression.Lambda(call, new ParameterExpression[] { t }).Compile();
// ParameterExpression r = Expression.Parameter(resourceType, "r");
// LabelTarget returnTarget = Expression.Label(typeof(Transform));
// var returnExpression = Expression.Return(returnTarget, t, typeof(Transform));
// LabelExpression returnLabel = Expression.Label(returnTarget, t);
// var block = Expression.Block(returnExpression, returnLabel);
// var lambda2 = Expression.Lambda(block, new ParameterExpression[] { t, r, }).Compile();
// var resourceLens = lensCtor.Invoke(new object[] { lambda1, lambda2 });
// return resourceLens;
//}
//object BuildStatLens(Type resourceType, Type unitType) {
// var lensType = typeof(Lens<,>).MakeGenericType(resourceType, unitType);
// var lensCtor = lensType.GetConstructors().First();
// ParameterExpression r = Expression.Parameter(resourceType, "r");
// Expression call = Expression.Call(r, "GetCurrent", Type.EmptyTypes);
// var lambda1 = Expression.Lambda(call, new ParameterExpression[] { r }).Compile();
// ParameterExpression u = Expression.Parameter(unitType, "u");
// LabelTarget returnTarget = Expression.Label(resourceType);
// var returnExpression = Expression.Return(returnTarget, r, resourceType);
// LabelExpression returnLabel = Expression.Label(returnTarget, r);
// var block = Expression.Block(returnExpression, returnLabel);
// var lambda2 = Expression.Lambda(block, new ParameterExpression[] { r, u }).Compile();
// var statLens = lensCtor.Invoke(new object[] { lambda1, lambda2 });
// return statLens;
//}
//object BuildValueLens(Type unitType) {
// var lensType = typeof(Lens<,>).MakeGenericType(unitType, typeof(int));
// var lensCtor = lensType.GetConstructors().First();
// ParameterExpression u = Expression.Parameter(unitType, "u");
// Expression call = Expression.Call(u, "AsPrimitive", Type.EmptyTypes);
// var lambda1 = Expression.Lambda(call, new ParameterExpression[] { u }).Compile();
// ParameterExpression v = Expression.Parameter(typeof(int), "v");
// Expression assignValueExpression = Expression.Assign(u, Expression.ConvertChecked(v, unitType));
// LabelTarget returnTarget = Expression.Label(unitType);
// var returnExpression = Expression.Return(returnTarget, u, unitType);
// LabelExpression returnLabel = Expression.Label(returnTarget, u);
// var block = Expression.Block(assignValueExpression, returnExpression, returnLabel);
// var lambda2 = Expression.Lambda(block, new ParameterExpression[] { u, v }).Compile();
// var valueLens = lensCtor.Invoke(new object[] { lambda1, lambda2 });
// return valueLens;
//}
//object ComposeDynamicLens(object parent, object child, Type TWhole, Type TPart) {
// ParameterExpression whole = Expression.Parameter(TWhole, "whole");
// var parentGet = Expression.Call(Expression.Constant(parent), "Get", Type.EmptyTypes, whole);
// var childGet = Expression.Call(Expression.Constant(child), "Get", Type.EmptyTypes, parentGet);
// var lambda1 = Expression.Lambda(childGet, new ParameterExpression[] { whole }).Compile();
// //(whole, part) => parent.Set(whole, child.Set(parent.Get(whole), part)));
// ParameterExpression part = Expression.Parameter(TPart, "part");
// var childSet = Expression.Call(Expression.Constant(child), "Set", Type.EmptyTypes, parentGet, part);
// var parentSet = Expression.Call(Expression.Constant(parent), "Set", Type.EmptyTypes, whole, childSet);
// var lambda2 = Expression.Lambda(parentSet, new ParameterExpression[] { whole, part }).Compile();
// var composedLensType = typeof(Lens<,>).MakeGenericType(TWhole, TPart);
// var lensCtor = composedLensType.GetConstructors().First();
// var composedLens = lensCtor.Invoke(new object[] { lambda1, lambda2 });
// return composedLens;
//}
Lens<Transform, int> CreateDynamicResourceLens(Type TResource, Type TUnit) {
var lensType = typeof(Lens<,>).MakeGenericType(typeof(Transform), typeof(int));
var lensCtor = lensType.GetConstructors().First();
ParameterExpression t = Expression.Parameter(typeof(Transform), "t");
Expression getResource = Expression.Call(t, "GetComponent", new Type[] { TResource });
Expression getCurrent = Expression.Call(getResource, "GetCurrent", Type.EmptyTypes);
Expression asPrimitive = Expression.Call(getCurrent, "AsPrimitive", Type.EmptyTypes);
var getterLambda = Expression.Lambda(asPrimitive, new ParameterExpression[] { t }).Compile();
ParameterExpression v = Expression.Parameter(typeof(int), "v");
//Expression assignValueExpression = Expression.Assign(getCurrent, Expression.ConvertChecked(v, TUnit));
LabelTarget returnTarget = Expression.Label(typeof(Transform));
var returnExpression = Expression.Return(returnTarget, t, typeof(Transform));
LabelExpression returnLabel = Expression.Label(returnTarget, t);
var block = Expression.Block(
//assignValueExpression,
returnExpression,
returnLabel
);
var setterLambda = Expression.Lambda(block, new ParameterExpression[] { t, v }).Compile();
var resourceLens = lensCtor.Invoke(new object[] { getterLambda, setterLambda });
return (Lens<Transform, int>)resourceLens;
}
public void TestAll() {
var TResource = typeof(GreenTag);
var TUnitInner = TResource.BaseType.GetGenericArguments()[0];
var TUnit = typeof(Unit<>).MakeGenericType(TUnitInner);
//var resource = new Lens<Transform, GreenTag>(t => t.GetComponent<GreenTag>(), (t, r) => t);
//var resource = (Lens<Transform, GreenTag>)BuildResourceLens(TResource);
//var statLens = new Lens<GreenTag, Unit<GreenTagUnit>>(r => r.GetCurrent(), (r, u) => r);
//var statLens = (Lens<GreenTag, Unit<GreenTagUnit>>)BuildStatLens(TResource, TUnit);
//var valueLens = new Lens<Unit<GreenTagUnit>, int>(u => u.AsPrimitive(), (u, v) => u = (Unit<GreenTagUnit>)v);
//var valueLens = (Lens<Unit<GreenTagUnit>, int>)BuildValueLens(TUnit);
//var lens = resource.Compose(statLens).Compose(valueLens);
//var lens = ComposeDynamicLens(resource, statLens, valueLens, typeof(GreenTag), typeof(Unit<GreenTagUnit>));
//var lens = (Lens<Transform, int>)ComposeDynamicLens(ComposeDynamicLens(resource, statLens, typeof(Transform), TUnit), valueLens, typeof(Transform), typeof(int));
var lens = CreateDynamicResourceLens(TResource, TUnit);
Debug.Log(lens.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>");
}
}
public void TestMoveToActionConcreteEqual() {
var testName = System.Reflection.MethodBase.GetCurrentMethod().Name;
var action1 = new MoveToAction(_weaver, _transform1.position);
var 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>");
}
}
public void TestMoveToActionInterfaceEqual() {
var testName = System.Reflection.MethodBase.GetCurrentMethod().Name;
IAction action1 = new MoveToAction(_weaver, _transform1.position);
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>");
}
}
}
}
namespace Testing.TimeDialation
{
public class TimeDialationTester
{
EventAggregator _eventAggregator;
public TimeDialationTester(EventAggregator eventAggregator) {
_eventAggregator = eventAggregator;
}
public void TestAll() {
TestTimeDialationSpeedUp();
TestTimeDialationSpeedDown();
TestTimeDialationSpeedPause();
TestTimeDialationSpeedReset();
}
public void TestTimeDialationSpeedUp() {
var currentTimeScale = Time.timeScale;
var testName = System.Reflection.MethodBase.GetCurrentMethod().Name;
_eventAggregator.OnTimeDilationSpeedUp(this, System.EventArgs.Empty);