Skip to content

Commit

Permalink
- Run Tagged Actions can now search on the object with which a collis…
Browse files Browse the repository at this point in the history
…ion just happened
  • Loading branch information
DiogoDeAndrade committed Feb 26, 2024
1 parent 23dd812 commit 664e45c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
24 changes: 20 additions & 4 deletions Assets/OkapiKit/Scripts/Actions/ActionTagged.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace OkapiKit
[AddComponentMenu("Okapi/Action/Run Tagged Action")]
public class ActionTagged : Action
{
public enum SearchType { Global = 0, Tagged = 1, Children = 2, WithinCollider = 3 };
public enum SearchType { Global = 0, Tagged = 1, Children = 2, WithinCollider = 3, CollisionObject = 4 };
public enum TriggerType { All = 0, Sequence = 1, Random = 2 };

[SerializeField]
Expand All @@ -33,11 +33,11 @@ public override string GetRawDescription(string ident, GameObject gameObject)

if (searchType == SearchType.Global)
{
desc = "find actions tagged with any of ["; ;
desc = "find actions tagged with any of [";
}
else if (searchType == SearchType.Children)
{
desc = "find actions underneath this object tagged with any of ["; ;
desc = "find actions underneath this object tagged with any of [";
}
else if (searchType == SearchType.Tagged)
{
Expand Down Expand Up @@ -87,6 +87,10 @@ public override string GetRawDescription(string ident, GameObject gameObject)
desc += $"] inside any of the colliders defined and in them \nfind actions tagged with any of [";
}
}
else if (searchType == SearchType.CollisionObject)
{
desc = "find actions underneath the object with which this one collided, tagged with any of [";
}

if ((triggerTags != null) && (triggerTags.Length > 0))
{
Expand Down Expand Up @@ -190,7 +194,7 @@ public override void Execute()
if (!enableAction) return;
if (!EvaluatePreconditions()) return;

if ((targetActions == null) || (triggerType != TriggerType.Sequence))
if ((targetActions == null) || (triggerType != TriggerType.Sequence) || (searchType == SearchType.CollisionObject))
{
RefreshActions();
}
Expand Down Expand Up @@ -296,6 +300,18 @@ void RefreshActions()
}
}
}
else if (searchType == SearchType.CollisionObject)
{
targetActions = new List<Action>();

var lastCollider = TriggerOnCollision.GetLastCollider();
if (lastCollider != null)
{
targetActions = new List<Action>(lastCollider.GetComponentsInChildren<Action>());

targetActions.RemoveAll((action) => !action.HasTag(triggerTags));
}
}
else
{
targetActions = new List<Action>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public override void OnInspectorGUI()
{
EditorGUILayout.PropertyField(propVariable, new GUIContent("Variable", "What's the variable you want to set?\nYou can only choose either a value instance (instance on an object) or a variable (global value), not both at the same time."));
}
EditorGUILayout.PropertyField(propOperation, new GUIContent("Operation", "What is the operation to perform on the variable/value instance?\nSet: Sets the variable to a certain value\nChange: Modifies the variable (add/subtract)\nReset: Sets a variable/value instance to its default value."));
EditorGUILayout.PropertyField(propOperation, new GUIContent("Operation", "What is the operation to perform on the variable/value instance?\nReset: Sets a variable/value instance to its default value.\nSet: Sets the variable to a certain value\nAdd: Adds a value to a variable\nSubtract: Subtracts a value from a variable\nRevSubtract: Subtracts this variable from a value\nMultiply: Multiplies the variable by a value\nDivide: Divides the variable by a value\nRevDivide: Divides a value by this variable"));

var opType = (ActionChangeValueV2.OperationType)propOperation.enumValueIndex;

Expand Down
25 changes: 23 additions & 2 deletions Assets/OkapiKit/Scripts/Triggers/TriggerOnCollision.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ public enum CollisionEvent { Enter, Stay, Exit };
[SerializeField]
private Hypertag[] tags;

private static GameObject lastCollider = null;

public static GameObject GetLastCollider() => lastCollider;

public override string GetTriggerTitle() { return "On Collision"; }

public override string GetRawDescription(string ident, GameObject refObject)
Expand Down Expand Up @@ -76,70 +80,87 @@ protected override void CheckErrors()

private void OnTriggerEnter2D(Collider2D collision)
{
lastCollider = null;

if (!isTrigger) return;
if (!isTriggerEnabled) return;
if (eventType != CollisionEvent.Enter) return;
if (!collision.gameObject.HasHypertags(tags)) return;
if (!EvaluatePreconditions()) return;

lastCollider = collision.gameObject;
ExecuteTrigger();
}


private void OnCollisionEnter2D(Collision2D collision)
{
lastCollider = null;

if (isTrigger) return;
if (!isTriggerEnabled) return;
if (eventType != CollisionEvent.Enter) return;
if (!collision.gameObject.HasHypertags(tags)) return;
if (!EvaluatePreconditions()) return;

lastCollider = collision.otherCollider.gameObject;
ExecuteTrigger();
}
private void OnTriggerStay2D(Collider2D collision)
{
lastCollider = null;

if (!isTrigger) return;
if (!isTriggerEnabled) return;
if (eventType != CollisionEvent.Stay) return;
if (!collision.gameObject.HasHypertags(tags)) return;
if (!EvaluatePreconditions()) return;

lastCollider = collision.gameObject;
ExecuteTrigger();
}


private void OnCollisionStay2D(Collision2D collision)
{
lastCollider = null;

if (isTrigger) return;
if (!isTriggerEnabled) return;
if (eventType != CollisionEvent.Stay) return;
if (!collision.gameObject.HasHypertags(tags)) return;
if (!EvaluatePreconditions()) return;

lastCollider = collision.otherCollider.gameObject;
ExecuteTrigger();
}

private void OnTriggerExit2D(Collider2D collision)
{
lastCollider = null;

if (!isTrigger) return;
if (!isTriggerEnabled) return;
if (eventType != CollisionEvent.Exit) return;
if (!collision.gameObject.HasHypertags(tags)) return;
if (!EvaluatePreconditions()) return;

lastCollider = collision.gameObject;
ExecuteTrigger();
}


private void OnCollisionExit2D(Collision2D collision)
{
lastCollider = null;

if (isTrigger) return;
if (!isTriggerEnabled) return;
if (eventType != CollisionEvent.Exit) return;
if (!collision.gameObject.HasHypertags(tags)) return;
if (!EvaluatePreconditions()) return;

lastCollider = collision.otherCollider.gameObject;
ExecuteTrigger();
}
}
}
}
2 changes: 1 addition & 1 deletion ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
- Change Value action can now multiply/divide, besides adding/subtracting
- Fixed a series of bugs with variable instances
- Comparisons can now include variables instead of just literal values

- Run Tagged Actions can now search on the object with which a collision just happened

## V1.11.3

Expand Down

0 comments on commit 664e45c

Please sign in to comment.