ToggleStateMembers added to ITrigger & OnTriggeredCollidersUpdated to IColliderTrigger & Added Dependency to ToggleState Module

This commit is contained in:
Syntriax 2022-12-17 11:55:01 +03:00
parent 56575cd107
commit f137f1202b
8 changed files with 96 additions and 15 deletions

View File

@ -1,2 +1,4 @@
# Trigger
## Dependencies
1. [ToggleState Module](https://git.syntriax.com/Syntriax/ToggleState)

View File

@ -1,17 +1,40 @@
using System;
using System.Collections.Generic;
using Syntriax.Modules.ToggleState;
using UnityEngine;
namespace Syntriax.Modules.Trigger
{
public class Box2DColliderTrigger : Trigger, IColliderTrigger
{
public LayerMask ColliderMask { get => colliderMask; set => colliderMask = value; }
[SerializeField] private LayerMask colliderMask = 0;
private Collider2D[] results = new Collider2D[8];
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()
=> IsTrigerred = Physics2D.OverlapBoxNonAlloc(transform.position, transform.localScale, 0, results, ColliderMask) > 0;
{
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()

View File

@ -1,17 +1,40 @@
using System;
using System.Collections.Generic;
using Syntriax.Modules.ToggleState;
using UnityEngine;
namespace Syntriax.Modules.Trigger
{
public class Box2DColliderTriggerWithRotation : Trigger, IColliderTrigger
{
public LayerMask ColliderMask { get => colliderMask; set => colliderMask = value; }
[SerializeField] private LayerMask colliderMask = 0;
private Collider2D[] results = new Collider2D[8];
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()
=> IsTrigerred = Physics2D.OverlapBoxNonAlloc(transform.position, transform.localScale, transform.rotation.eulerAngles.z, results, ColliderMask) > 0;
{
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()

View File

@ -1,9 +1,20 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace Syntriax.Modules.Trigger
{
public interface IColliderTrigger : ITrigger
{
/// <summary>
/// <see cref="LayerMask"/> value for checking triggers
/// </summary>
LayerMask ColliderMask { get; set; }
/// <summary>
/// Called whenever the list of <see cref="Collider2D"/> changes
/// </summary>
/// <value>List of <see cref="Collider2D"/> that triggered the <see cref="IColliderTrigger"/></value>
Action<List<Collider2D>> OnTriggeredCollidersUpdated { get; set; }
}
}

View File

@ -1,14 +1,20 @@
using System;
using Syntriax.Modules.ToggleState;
namespace Syntriax.Modules.Trigger
{
public interface ITrigger
{
/// <summary>
/// Member <see cref="IToggleState"/> to switch on or off
/// </summary>
IToggleState ToggleStateMember { get; }
/// <summary>
/// Called everytime the IsTrigerred field is changed
/// </summary>
/// <value>The new value of IsTrigerred</value>
Action<bool> OnTriggered { get; set; }
Action<bool> OnTriggerStateChanged { get; set; }
bool IsTrigerred { get; }
}
}

View File

@ -1,3 +1,16 @@
{
"name": "Syntriax.Modules.Trigger"
}
"name": "Syntriax.Modules.Trigger",
"rootNamespace": "",
"references": [
"GUID:efa9a9bc94c60c74684aafb7428fbf61"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

View File

@ -1,11 +1,14 @@
using System;
using Syntriax.Modules.ToggleState;
using UnityEngine;
namespace Syntriax.Modules.Trigger
{
public class Trigger : MonoBehaviour, ITrigger
{
public Action<bool> OnTriggered { get; set; } = null;
public IToggleState ToggleStateMember { get; protected set; } = new ToggleStateMember(true);
public Action<bool> OnTriggerStateChanged { get; set; } = null;
private bool _isTrigerred = false;
public bool IsTrigerred
@ -13,11 +16,11 @@ namespace Syntriax.Modules.Trigger
get => _isTrigerred;
protected set
{
if (value == _isTrigerred)
if (value == _isTrigerred || !ToggleStateMember.IsToggledNullChecked())
return;
_isTrigerred = value;
OnTriggered?.Invoke(value);
OnTriggerStateChanged?.Invoke(value);
}
}
}

View File

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