Extracted Trigger Checking into TriggerAreas

This commit is contained in:
Syntriax 2022-12-17 14:39:54 +03:00
parent f137f1202b
commit bbbf2b5609
35 changed files with 213 additions and 130 deletions

View File

@ -1,47 +0,0 @@
using System;
using System.Collections.Generic;
using Syntriax.Modules.ToggleState;
using UnityEngine;
namespace Syntriax.Modules.Trigger
{
public class Box2DColliderTrigger : Trigger, IColliderTrigger
{
[SerializeField] private LayerMask colliderMask = 0;
public LayerMask ColliderMask { get => colliderMask; set => colliderMask = value; }
public Action<List<Collider2D>> OnTriggeredCollidersUpdated { get; set; } = null;
private Collider2D[] nonAllocResults = new Collider2D[32];
public List<Collider2D> results = new List<Collider2D>(32);
private int previousTriggerCount = 0;
protected void FixedUpdate()
{
if (!ToggleStateMember.IsToggledNullChecked())
return;
int triggerCount = Physics2D.OverlapBoxNonAlloc(transform.position, transform.localScale, 0, nonAllocResults, ColliderMask);
if (triggerCount == previousTriggerCount)
return;
previousTriggerCount = triggerCount;
results.Clear();
for (int i = 0; i < triggerCount; i++)
results.Add(nonAllocResults[i]);
IsTrigerred = triggerCount > 0;
OnTriggeredCollidersUpdated?.Invoke(results);
}
#if UNITY_EDITOR
private void OnDrawGizmosSelected()
{
Gizmos.color = IsTrigerred ? Color.green : Color.red;
Gizmos.DrawWireCube(transform.position, transform.lossyScale);
}
#endif
}
}

View File

@ -1,48 +0,0 @@
using System;
using System.Collections.Generic;
using Syntriax.Modules.ToggleState;
using UnityEngine;
namespace Syntriax.Modules.Trigger
{
public class Box2DColliderTriggerWithRotation : Trigger, IColliderTrigger
{
[SerializeField] private LayerMask colliderMask = 0;
public LayerMask ColliderMask { get => colliderMask; set => colliderMask = value; }
public Action<List<Collider2D>> OnTriggeredCollidersUpdated { get; set; } = null;
private Collider2D[] nonAllocResults = new Collider2D[32];
public List<Collider2D> results = new List<Collider2D>(32);
private int previousTriggerCount = 0;
protected void FixedUpdate()
{
if (!ToggleStateMember.IsToggledNullChecked())
return;
int triggerCount = Physics2D.OverlapBoxNonAlloc(transform.position, transform.localScale, transform.rotation.eulerAngles.z, nonAllocResults, ColliderMask);
if (triggerCount == previousTriggerCount)
return;
previousTriggerCount = triggerCount;
results.Clear();
for (int i = 0; i < triggerCount; i++)
results.Add(nonAllocResults[i]);
IsTrigerred = triggerCount > 0;
OnTriggeredCollidersUpdated?.Invoke(results);
}
#if UNITY_EDITOR
private void OnDrawGizmosSelected()
{
Gizmos.color = IsTrigerred ? Color.green : Color.red;
Gizmos.matrix = transform.localToWorldMatrix;
Gizmos.DrawWireCube(Vector3.zero, Vector3.one);
}
#endif
}
}

8
Runtime/Collider.meta Normal file
View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 09a25591637cbdf45ad71415d94bf641
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,18 @@
using UnityEngine;
namespace Syntriax.Modules.Trigger
{
public class Box2DColliderTriggerArea : ColliderTriggerAreaBase
{
protected override int TriggerCount
=> Physics2D.OverlapBoxNonAlloc(transform.position, transform.localScale, 0, nonAllocResults, ColliderMask);
#if UNITY_EDITOR
private void OnDrawGizmosSelected()
{
Gizmos.color = previousTriggerCount > 0 ? Color.green : Color.red;
Gizmos.DrawWireCube(transform.position, transform.lossyScale);
}
#endif
}
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 7804f3e8384c7a54482f585b4f43264a guid: 13b8a72003d46b24fa2e67093c42c49d
MonoImporter: MonoImporter:
externalObjects: {} externalObjects: {}
serializedVersion: 2 serializedVersion: 2

View File

@ -0,0 +1,19 @@
using UnityEngine;
namespace Syntriax.Modules.Trigger
{
public class Box2DColliderTriggerAreaWithRotation : ColliderTriggerAreaBase
{
protected override int TriggerCount
=> Physics2D.OverlapBoxNonAlloc(transform.position, transform.localScale, transform.rotation.eulerAngles.z, nonAllocResults, ColliderMask);
#if UNITY_EDITOR
private void OnDrawGizmosSelected()
{
Gizmos.color = previousTriggerCount > 0 ? Color.green : Color.red;
Gizmos.matrix = transform.localToWorldMatrix;
Gizmos.DrawWireCube(Vector3.zero, Vector3.one);
}
#endif
}
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 20a6baf9a256917448a4d7ad09d0c51a guid: b2d9ae6bcc3b69a4093f8c740f040229
MonoImporter: MonoImporter:
externalObjects: {} externalObjects: {}
serializedVersion: 2 serializedVersion: 2

View File

@ -0,0 +1,22 @@
using Syntriax.Modules.ToggleState;
namespace Syntriax.Modules.Trigger
{
public class ColliderTrigger : TriggerBase
{
protected IColliderTrigger colliderTrigger = null;
private void Awake()
{
colliderTrigger = GetComponent<IColliderTrigger>();
}
private void FixedUpdate()
{
if (!ToggleStateMember.IsToggledNullChecked())
return;
IsTrigerred = colliderTrigger.Check();
}
}
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: d8b544fd215b0df4192a7889e6f2db4c guid: 7302d5358a0cd4a4c9a2c8dd9a4a7697
MonoImporter: MonoImporter:
externalObjects: {} externalObjects: {}
serializedVersion: 2 serializedVersion: 2

View File

@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Syntriax.Modules.Trigger
{
public abstract class ColliderTriggerAreaBase : MonoBehaviour, IColliderTrigger
{
protected List<Collider2D> results = new List<Collider2D>(32);
protected Collider2D[] nonAllocResults = new Collider2D[32];
protected int previousTriggerCount = 0;
public LayerMask ColliderMask { get; set; } = ~0;
public Action<List<Collider2D>> OnTriggeredCollidersUpdated { get; set; } = null;
protected abstract int TriggerCount { get; }
public virtual bool Check()
{
int triggerCount = TriggerCount;
if (triggerCount != previousTriggerCount)
{
results.Clear();
for (int i = 0; i < triggerCount; i++)
results.Add(nonAllocResults[i]);
OnTriggeredCollidersUpdated?.Invoke(results);
}
previousTriggerCount = triggerCount;
return triggerCount > 0;
}
}
}

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: e56ea33695c0c83438ea8b7bf11d1cf8 guid: d9e5082ab403a7240a6bf0554a93518f
MonoImporter: MonoImporter:
externalObjects: {} externalObjects: {}
serializedVersion: 2 serializedVersion: 2

View File

@ -0,0 +1,4 @@
namespace Syntriax.Modules.Trigger
{
public class Box2DGroundTriggerArea : Box2DColliderTriggerArea, IGroundTriggerArea { }
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 4b3db9faa3a5e9f4db37172c452dbf2a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,4 @@
namespace Syntriax.Modules.Trigger
{
public class Box2DGroundTriggerAreaWithRotation : Box2DColliderTriggerAreaWithRotation, IGroundTriggerArea { }
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: dbb4ef661e6f9fc4fb8584059bc8e5cc
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,4 @@
namespace Syntriax.Modules.Trigger
{
public class GroundTrigger : ColliderTrigger, IGroundTrigger { }
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 91a5b9cf888de6f4da572ac875fc826c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,4 @@
namespace Syntriax.Modules.Trigger
{
public interface IGroundTrigger : ITrigger { }
}

View File

@ -0,0 +1,4 @@
namespace Syntriax.Modules.Trigger
{
public interface IGroundTriggerArea : ITriggerArea { }
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7a6e6150c32aa1f4d87db161c5fb65db
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -4,7 +4,7 @@ using UnityEngine;
namespace Syntriax.Modules.Trigger namespace Syntriax.Modules.Trigger
{ {
public interface IColliderTrigger : ITrigger public interface IColliderTrigger : ITriggerArea
{ {
/// <summary> /// <summary>
/// <see cref="LayerMask"/> value for checking triggers /// <see cref="LayerMask"/> value for checking triggers

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: e199ccade2250b646abfd859e4aa1e7a guid: a101e5476051bb74b8012198b1594327
MonoImporter: MonoImporter:
externalObjects: {} externalObjects: {}
serializedVersion: 2 serializedVersion: 2

View File

@ -0,0 +1,11 @@
namespace Syntriax.Modules.Trigger
{
public interface ITriggerArea
{
/// <summary>
/// Check if the <see cref="ITriggerArea"/> is Triggered
/// </summary>
/// <returns>true if <see cref="ITriggerArea"/> is triggered, false if not</returns>
bool Check();
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d8fff127b37aecc4495535a383f8f4bf
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,4 +0,0 @@
namespace Syntriax.Modules.Trigger
{
public class Box2DGroundTrigger : Box2DColliderTrigger, IGroundTrigger { }
}

View File

@ -1,4 +0,0 @@
namespace Syntriax.Modules.Trigger
{
public class Box2DGroundTriggerWithRotation : Box2DColliderTriggerWithRotation, IGroundTrigger { }
}

View File

@ -1,4 +0,0 @@
namespace Syntriax.Modules.Trigger
{
public interface IGroundTrigger : IColliderTrigger { }
}

View File

@ -15,6 +15,7 @@ namespace Syntriax.Modules.Trigger
/// </summary> /// </summary>
/// <value>The new value of IsTrigerred</value> /// <value>The new value of IsTrigerred</value>
Action<bool> OnTriggerStateChanged { get; set; } Action<bool> OnTriggerStateChanged { get; set; }
bool IsTrigerred { get; } bool IsTrigerred { get; }
} }
} }

View File

@ -3,11 +3,11 @@ namespace Syntriax.Modules.Trigger
public static class ITriggerExtensions public static class ITriggerExtensions
{ {
/// <summary> /// <summary>
/// Checks if the provided parameter ITrigger is toggled, if the parameter is null returns the nullValue parameter /// Checks if the provided parameter <see cref="ITrigger"/> is toggled, if the parameter is null returns the nullValue parameter
/// </summary> /// </summary>
/// <param name="trigger">ITrigger to be checked if toggled or not</param> /// <param name="trigger"><see cref="ITrigger"/> to be checked if toggled or not</param>
/// <param name="nullValue">The value that will be returned if toggleState is null. Default value: true</param> /// <param name="nullValue">The value that will be returned if trigger is null. Default value: true</param>
/// <returns>ITrigger's toggle value, or if null return nullValue parameter</returns> /// <returns><see cref="ITrigger"/>'s toggle value, or if null return nullValue parameter</returns>
public static bool IsTriggeredNullChecked(this ITrigger trigger, bool nullValue = true) public static bool IsTriggeredNullChecked(this ITrigger trigger, bool nullValue = true)
=> trigger == null ? nullValue : trigger.IsTrigerred; => trigger == null ? nullValue : trigger.IsTrigerred;
} }

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 1e3718079b1e1024a997ca7eaa425903
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -4,7 +4,7 @@ using UnityEngine;
namespace Syntriax.Modules.Trigger namespace Syntriax.Modules.Trigger
{ {
public class Trigger : MonoBehaviour, ITrigger public class TriggerBase : MonoBehaviour, ITrigger
{ {
public IToggleState ToggleStateMember { get; protected set; } = new ToggleStateMember(true); public IToggleState ToggleStateMember { get; protected set; } = new ToggleStateMember(true);

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f28f409248975dc4fb1d9e01e88ea65e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,6 +1,6 @@
{ {
"name": "com.syntriax.trigger", "name": "com.syntriax.trigger",
"version": "0.2.0", "version": "0.3.0",
"displayName": "Trigger Module", "displayName": "Trigger Module",
"unity": "2019.1", "unity": "2019.1",
"documentationUrl": "https://git.syntriax.com/Syntriax/Trigger.git", "documentationUrl": "https://git.syntriax.com/Syntriax/Trigger.git",