ToggleStateMembers added to ITrigger & OnTriggeredCollidersUpdated to IColliderTrigger & Added Dependency to ToggleState Module
This commit is contained in:
parent
56575cd107
commit
f137f1202b
|
@ -1,2 +1,4 @@
|
||||||
# Trigger
|
# Trigger
|
||||||
|
|
||||||
|
## Dependencies
|
||||||
|
1. [ToggleState Module](https://git.syntriax.com/Syntriax/ToggleState)
|
||||||
|
|
|
@ -1,17 +1,40 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Syntriax.Modules.ToggleState;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace Syntriax.Modules.Trigger
|
namespace Syntriax.Modules.Trigger
|
||||||
{
|
{
|
||||||
|
|
||||||
public class Box2DColliderTrigger : Trigger, IColliderTrigger
|
public class Box2DColliderTrigger : Trigger, IColliderTrigger
|
||||||
{
|
{
|
||||||
public LayerMask ColliderMask { get => colliderMask; set => colliderMask = value; }
|
|
||||||
[SerializeField] private LayerMask colliderMask = 0;
|
[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()
|
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
|
#if UNITY_EDITOR
|
||||||
private void OnDrawGizmosSelected()
|
private void OnDrawGizmosSelected()
|
||||||
|
|
|
@ -1,17 +1,40 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Syntriax.Modules.ToggleState;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace Syntriax.Modules.Trigger
|
namespace Syntriax.Modules.Trigger
|
||||||
{
|
{
|
||||||
|
|
||||||
public class Box2DColliderTriggerWithRotation : Trigger, IColliderTrigger
|
public class Box2DColliderTriggerWithRotation : Trigger, IColliderTrigger
|
||||||
{
|
{
|
||||||
public LayerMask ColliderMask { get => colliderMask; set => colliderMask = value; }
|
|
||||||
[SerializeField] private LayerMask colliderMask = 0;
|
[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()
|
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
|
#if UNITY_EDITOR
|
||||||
private void OnDrawGizmosSelected()
|
private void OnDrawGizmosSelected()
|
||||||
|
|
|
@ -1,9 +1,20 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace Syntriax.Modules.Trigger
|
namespace Syntriax.Modules.Trigger
|
||||||
{
|
{
|
||||||
public interface IColliderTrigger : ITrigger
|
public interface IColliderTrigger : ITrigger
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// <see cref="LayerMask"/> value for checking triggers
|
||||||
|
/// </summary>
|
||||||
LayerMask ColliderMask { get; set; }
|
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; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,20 @@
|
||||||
using System;
|
using System;
|
||||||
|
using Syntriax.Modules.ToggleState;
|
||||||
|
|
||||||
namespace Syntriax.Modules.Trigger
|
namespace Syntriax.Modules.Trigger
|
||||||
{
|
{
|
||||||
public interface ITrigger
|
public interface ITrigger
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Member <see cref="IToggleState"/> to switch on or off
|
||||||
|
/// </summary>
|
||||||
|
IToggleState ToggleStateMember { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Called everytime the IsTrigerred field is changed
|
/// Called everytime the IsTrigerred field is changed
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>The new value of IsTrigerred</value>
|
/// <value>The new value of IsTrigerred</value>
|
||||||
Action<bool> OnTriggered { get; set; }
|
Action<bool> OnTriggerStateChanged { get; set; }
|
||||||
bool IsTrigerred { get; }
|
bool IsTrigerred { get; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
|
@ -1,11 +1,14 @@
|
||||||
using System;
|
using System;
|
||||||
|
using Syntriax.Modules.ToggleState;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
namespace Syntriax.Modules.Trigger
|
namespace Syntriax.Modules.Trigger
|
||||||
{
|
{
|
||||||
public class Trigger : MonoBehaviour, ITrigger
|
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;
|
private bool _isTrigerred = false;
|
||||||
public bool IsTrigerred
|
public bool IsTrigerred
|
||||||
|
@ -13,11 +16,11 @@ namespace Syntriax.Modules.Trigger
|
||||||
get => _isTrigerred;
|
get => _isTrigerred;
|
||||||
protected set
|
protected set
|
||||||
{
|
{
|
||||||
if (value == _isTrigerred)
|
if (value == _isTrigerred || !ToggleStateMember.IsToggledNullChecked())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_isTrigerred = value;
|
_isTrigerred = value;
|
||||||
OnTriggered?.Invoke(value);
|
OnTriggerStateChanged?.Invoke(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "com.syntriax.trigger",
|
"name": "com.syntriax.trigger",
|
||||||
"version": "0.1.0",
|
"version": "0.2.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",
|
||||||
|
|
Loading…
Reference in New Issue