Skip to content

Commit

Permalink
Fixed bug with inactive triggers
Browse files Browse the repository at this point in the history
Added tagged action search inside a collider
Added UI for tagged actions
  • Loading branch information
DiogoDeAndrade committed Mar 1, 2023
1 parent 3f98047 commit dc1bb69
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 28 deletions.
22 changes: 18 additions & 4 deletions Assets/OkapiKit/Scripts/Actions/Action.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,31 @@
abstract public class Action : OkapiElement
{
[SerializeField]
protected bool enableAction = true;
protected bool enableAction = true;
[SerializeField]
protected bool hasTags = false;
protected bool hasTags = false;
[SerializeField, FormerlySerializedAsAttribute("tags"), ShowIf("hasTags")]
private Hypertag[] actionTags;
private Hypertag[] actionTags;
[SerializeField]
private bool hasConditions = false;
private bool hasConditions = false;
[SerializeField, ShowIf("hasConditions")]
private Condition[] actionConditions;

public bool isActionEnabled { get { return enableAction; } set { enableAction = value; } }
public bool isTagged
{
get
{
if (hasTags)
{
if (actionTags == null) return false;
return (actionTags.Length > 0);
}
return false;
}
}

public Hypertag[] GetActionTags() => actionTags;

public abstract void Execute();

Expand Down
1 change: 0 additions & 1 deletion Assets/OkapiKit/Scripts/Actions/ActionBlink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public override void Execute()
if (!EvaluatePreconditions()) return;
if (target == null) return;

timer = duration;
startState = target.enabled;
EnableRenderers(!startState);
timer = duration;
Expand Down
93 changes: 81 additions & 12 deletions Assets/OkapiKit/Scripts/Actions/ActionTagged.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@

public class ActionTagged : Action
{
public enum SearchType { Global = 0, Tagged = 1, Children = 2 };
public enum SearchType { Global = 0, Tagged = 1, Children = 2, WithinCollider = 3 };
public enum TriggerType { All = 0, Sequence = 1, Random = 2 };

[SerializeField]
private SearchType searchType = SearchType.Global;
[SerializeField, ShowIf("needSearchTags")]
private Hypertag[] searchTags;
[SerializeField, ShowIf("needSearchTags")]
private TriggerType triggerType = TriggerType.All;
private SearchType searchType = SearchType.Global;
[SerializeField]
private Hypertag[] triggerTags;

private bool needSearchTags => searchType == SearchType.Tagged;
private Hypertag[] searchTags;
[SerializeField]
private Collider2D[] colliders;
[SerializeField]
private TriggerType triggerType = TriggerType.All;
[SerializeField]
private Hypertag[] triggerTags;

private int sequenceIndex = 0;

Expand Down Expand Up @@ -51,13 +51,45 @@ public override string GetRawDescription(string ident, GameObject gameObject)

desc += "] and in them find actions tagged with any of ["; ;
}

else if (searchType == SearchType.WithinCollider)
{
desc = "find all objects tagged with any of [";

if ((searchTags != null) && (searchTags.Length > 0))
{
for (int i = 0; i < searchTags.Length; i++)
{
desc += searchTags[i].name;
if (i < searchTags.Length - 1) desc += ",";
}
}
else desc += "UNDEFINED";

int nColliders = (colliders != null) ? (colliders.Length) : (0);
if (nColliders == 0)
{
desc += $"] within any of the undefined colliders and in them \nfind actions tagged with any of [";
}
else if (nColliders == 1)
{
string cname = (colliders[0]) ? (colliders[0].name) : ("UNDEFINED");
desc += $"] inside the collider [{cname}] and in them \nfind actions tagged with any of [";
}
else
{
desc += $"] inside any of the colliders defined and in them \nfind actions tagged with any of [";
}
}

if ((triggerTags != null) && (triggerTags.Length > 0))
{
for (int i = 0; i < triggerTags.Length; i++)
{
desc += triggerTags[i].name;
if (i < triggerTags.Length - 1) desc += ",";
if (triggerTags != null)
{
desc += triggerTags[i].name;
if (i < triggerTags.Length - 1) desc += ",";
}
}
}
else desc += "MISSING";
Expand Down Expand Up @@ -156,6 +188,43 @@ void RefreshActions()

targetActions.RemoveAll((action) => !action.HasTag(triggerTags));
}
else if (searchType == SearchType.WithinCollider)
{
if (colliders.Length > 0)
{
var contactFilter = new ContactFilter2D();
contactFilter.NoFilter();
contactFilter.useTriggers = true;

targetActions = new List<Action>();

List<Collider2D> results = new List<Collider2D>();

foreach (var c in colliders)
{
if (c == null) return;

if (Physics2D.OverlapCollider(c, contactFilter, results) > 0)
{
foreach (var r in results)
{
if (r.gameObject.HasHypertags(searchTags))
{
var actions = r.GetComponents<Action>();

foreach (var a in actions)
{
if (a.HasTag(triggerTags))
{
targetActions.Add(a);
}
}
}
}
}
}
}
}
else
{
targetActions = new List<Action>();
Expand Down
33 changes: 33 additions & 0 deletions Assets/OkapiKit/Scripts/Actions/Editor/ActionEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,39 @@ protected override void OnEnable()
propConditions = serializedObject.FindProperty("actionConditions");
}


protected override bool WriteTitle()
{
bool ret = base.WriteTitle();

var action = target as Action;
if (action != null)
{
var width = EditorGUIUtility.currentViewWidth;

if ((action.isTagged) && (width > 400.0f))
{
var tags = action.GetActionTags();
var tagTexture = GUIUtils.GetTexture("Tag");
var x = titleRect.x + width - 48 - 200.0f;
GUIStyle explanationStyle = GetExplanationStyle();

float y = titleRect.y = titleRect.y + 5;
foreach (var t in tags)
{
if (t == null) continue;

GUI.DrawTexture(new Rect(x, y, 16, 16), tagTexture, ScaleMode.ScaleToFit, true, 1.0f);
EditorGUI.LabelField(new Rect(x + 18, y, width, 20.0f), t.name, explanationStyle);

y += 18;
}
}
}

return ret;
}

public override void OnInspectorGUI()
{
if (WriteTitle())
Expand Down
16 changes: 11 additions & 5 deletions Assets/OkapiKit/Scripts/Actions/Editor/ActionTaggedEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class ActionTaggedEditor : ActionEditor
SerializedProperty propSearchTags;
SerializedProperty propTriggerType;
SerializedProperty propTriggerTags;
SerializedProperty propColliders;

protected override void OnEnable()
{
Expand All @@ -20,6 +21,7 @@ protected override void OnEnable()
propSearchTags = serializedObject.FindProperty("searchTags");
propTriggerType = serializedObject.FindProperty("triggerType");
propTriggerTags = serializedObject.FindProperty("triggerTags");
propColliders = serializedObject.FindProperty("colliders");
}

public override void OnInspectorGUI()
Expand All @@ -39,14 +41,18 @@ public override void OnInspectorGUI()
{
EditorGUILayout.PropertyField(propSearchTags, new GUIContent("Search Tags"));
}
if (propSearchType.enumValueIndex == (int)ActionTagged.SearchType.WithinCollider)
{
EditorGUILayout.PropertyField(propSearchTags, new GUIContent("Search Tags"));
EditorGUILayout.PropertyField(propColliders, new GUIContent("Search Colliders"));
}
EditorGUILayout.PropertyField(propTriggerType, new GUIContent("Trigger Type"));
EditorGUILayout.PropertyField(propTriggerTags, new GUIContent("Trigger Tags"));

if (EditorGUI.EndChangeCheck())
{
serializedObject.ApplyModifiedProperties();
(target as Action).UpdateExplanation();
}
EditorGUI.EndChangeCheck();

serializedObject.ApplyModifiedProperties();
(target as Action).UpdateExplanation();
}
}
}
4 changes: 3 additions & 1 deletion Assets/OkapiKit/Scripts/Base/Editor/OkapiBaseEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ protected virtual void OnEnable()
propExplanation = serializedObject.FindProperty("_explanation");
}

protected Rect titleRect;

protected virtual bool WriteTitle()
{
GUIStyle styleTitle = GetTitleSyle();
Expand All @@ -49,7 +51,7 @@ protected virtual bool WriteTitle()

// Background and title
float inspectorWidth = EditorGUIUtility.currentViewWidth - 20;
Rect titleRect = EditorGUILayout.BeginVertical("box");
titleRect = EditorGUILayout.BeginVertical("box");
Rect rect = new Rect(titleRect.x, titleRect.y, inspectorWidth - titleRect.x, styleTitle.fontSize + 14);
Rect fullRect = rect;
if (explanation != "")
Expand Down
6 changes: 6 additions & 0 deletions Assets/OkapiKit/Scripts/Triggers/TriggerOnCollision.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public override string GetRawDescription(string ident, GameObject refObject)
private void OnTriggerEnter2D(Collider2D collision)
{
if (!isTrigger) return;
if (!isTriggerEnabled) return;
if (eventType != CollisionEvent.Enter) return;
if (!collision.gameObject.HasHypertags(tags)) return;
if (!EvaluatePreconditions()) return;
Expand All @@ -59,6 +60,7 @@ private void OnTriggerEnter2D(Collider2D collision)
private void OnCollisionEnter2D(Collision2D collision)
{
if (isTrigger) return;
if (!isTriggerEnabled) return;
if (eventType != CollisionEvent.Enter) return;
if (!collision.gameObject.HasHypertags(tags)) return;
if (!EvaluatePreconditions()) return;
Expand All @@ -68,6 +70,7 @@ private void OnCollisionEnter2D(Collision2D collision)
private void OnTriggerStay2D(Collider2D collision)
{
if (!isTrigger) return;
if (!isTriggerEnabled) return;
if (eventType != CollisionEvent.Stay) return;
if (!collision.gameObject.HasHypertags(tags)) return;
if (!EvaluatePreconditions()) return;
Expand All @@ -79,6 +82,7 @@ private void OnTriggerStay2D(Collider2D collision)
private void OnCollisionStay2D(Collision2D collision)
{
if (isTrigger) return;
if (!isTriggerEnabled) return;
if (eventType != CollisionEvent.Stay) return;
if (!collision.gameObject.HasHypertags(tags)) return;
if (!EvaluatePreconditions()) return;
Expand All @@ -89,6 +93,7 @@ private void OnCollisionStay2D(Collision2D collision)
private void OnTriggerExit2D(Collider2D collision)
{
if (!isTrigger) return;
if (!isTriggerEnabled) return;
if (eventType != CollisionEvent.Exit) return;
if (!collision.gameObject.HasHypertags(tags)) return;
if (!EvaluatePreconditions()) return;
Expand All @@ -100,6 +105,7 @@ private void OnTriggerExit2D(Collider2D collision)
private void OnCollisionExit2D(Collision2D collision)
{
if (isTrigger) return;
if (!isTriggerEnabled) return;
if (eventType != CollisionEvent.Exit) return;
if (!collision.gameObject.HasHypertags(tags)) return;
if (!EvaluatePreconditions()) return;
Expand Down
1 change: 1 addition & 0 deletions Assets/OkapiKit/Scripts/Triggers/TriggerOnCondition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public override string GetRawDescription(string ident, GameObject refObject)

private void Update()
{
if (!isTriggerEnabled) return;
if (!EvaluatePreconditions()) return;

bool b = true;
Expand Down
1 change: 1 addition & 0 deletions Assets/OkapiKit/Scripts/Triggers/TriggerOnEveryFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public override string GetRawDescription(string ident, GameObject refObject)

private void Update()
{
if (!isTriggerEnabled) return;
if (!EvaluatePreconditions()) return;

ExecuteTrigger();
Expand Down
1 change: 1 addition & 0 deletions Assets/OkapiKit/Scripts/Triggers/TriggerOnInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public override string GetRawDescription(string ident, GameObject refObject)

void Update()
{
if (!isTriggerEnabled) return;
if (!EvaluatePreconditions()) return;

if (cooldownTimer >= 0.0f)
Expand Down
1 change: 1 addition & 0 deletions Assets/OkapiKit/Scripts/Triggers/TriggerOnStart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public override string GetRawDescription(string ident, GameObject refObject)

private void Start()
{
if (!isTriggerEnabled) return;
if (!EvaluatePreconditions()) return;

ExecuteTrigger();
Expand Down
19 changes: 15 additions & 4 deletions Assets/OkapiKit/Scripts/Triggers/TriggerOnTimer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,35 @@ public override string GetRawDescription(string ident, GameObject refObject)
{
if (timeInterval.x == timeInterval.y)
{
return $"Every {timeInterval.x} seconds, trigger the actions:";
if (allowRetrigger)
return $"Every {timeInterval.x} seconds, trigger the actions:";
else
return $"After {timeInterval.x} seconds, trigger the actions:";
}

return $"Every [{timeInterval.x},{timeInterval.y}] seconds, trigger the actions:";
if (allowRetrigger)
return $"Every [{timeInterval.x},{timeInterval.y}] seconds, trigger the actions:";
else
return $"After [{timeInterval.x},{timeInterval.y}] seconds, trigger the actions:";
}

void Start()
{
if (startTriggered)
if (isTriggerEnabled)
{
ExecuteTrigger();
if (startTriggered)
{
ExecuteTrigger();
}
}
timer = Random.Range(timeInterval.x, timeInterval.y);
}

// Update is called once per frame
void Update()
{
if (!isTriggerEnabled) return;

timer -= Time.deltaTime;

if (timer <= 0)
Expand Down
1 change: 0 additions & 1 deletion Todo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
- Warning: No probabilities set for ActionRandom
- Warning: No tags set for OnCollision
- Warning: Change values with deltaValue = 0
- Figure out a way to change responsibility (i.e. bullet causes damage to object, not object detects bullet and cause itself damage)
- Movement targets should be redefinable
- Comment Code
- Documentation
Expand Down

0 comments on commit dc1bb69

Please sign in to comment.