Skip to content

Commit

Permalink
- DestroyObject action can now reference collider object
Browse files Browse the repository at this point in the history
- Grid follow movement now doesn't keep moving when next to target position (and can't push)
- With grid follow movement, object turns to the right direction even if it can't move that way
- Grid object now has collision filtering options - events aren't triggered in these cases
- Added Teleport action
- Added grid events WasHit and WasPushed, that get triggered when an object is hit or pushed by another.
  • Loading branch information
DiogoDeAndrade committed Dec 17, 2024
1 parent 090cb2f commit 3d27e74
Show file tree
Hide file tree
Showing 19 changed files with 1,052 additions and 72 deletions.
14 changes: 13 additions & 1 deletion Assets/OkapiKit/Scripts/Actions/ActionDestroyObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace OkapiKit
[AddComponentMenu("Okapi/Action/Destroy Object")]
public class ActionDestroyObject : Action
{
public enum Target { Self = 0, Parent = 1, Topmost = 2, Object = 3, Tag = 4 };
public enum Target { Self = 0, Parent = 1, Topmost = 2, Object = 3, Tag = 4, Collider = 5 };

[SerializeField]
private Target target = Target.Object;
Expand Down Expand Up @@ -54,6 +54,8 @@ public bool WillDestroyThis(GameObject otherGameObject)
if (IsChild(gameObject, otherGameObject)) return true;
}
break;
case Target.Collider:
return false;
}

return false;
Expand Down Expand Up @@ -124,6 +126,9 @@ public override string GetRawDescription(string ident, GameObject gameObject)
desc += "destroys objects with tags [undefined]!";
}
break;
case Target.Collider:
desc += "destroys object that collided with this - only valid when called from a collision event";
break;
default:
break;
}
Expand Down Expand Up @@ -203,6 +208,13 @@ public override void Execute()
Destroy(obj);
}
break;
case Target.Collider:
var collider = TriggerOnCollision.GetLastCollider();
if (collider != null)
{
Destroy(collider);
}
break;
default:
break;
}
Expand Down
225 changes: 225 additions & 0 deletions Assets/OkapiKit/Scripts/Actions/ActionTeleport.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using NaughtyAttributes;

namespace OkapiKit
{

[AddComponentMenu("Okapi/Action/Teleport")]
public class ActionTeleport : Action
{
public enum TeleportSubject { Self, Target, Tag, Collider };
public enum TeleportTarget { Target, Tag };

[SerializeField]
private TeleportSubject subject;
[SerializeField]
private Transform subjectTransform;
[SerializeField]
private Hypertag subjectTag;

[SerializeField]
private TeleportTarget teleportTarget;
[SerializeField]
private Transform[] targetTransforms;
[SerializeField]
private Hypertag[] targetTags;

public override string GetActionTitle() => "Teleport";

public override string GetRawDescription(string ident, GameObject gameObject)
{
string desc = GetPreconditionsString(gameObject);

desc += "Teleports ";
switch (subject)
{
case TeleportSubject.Self:
desc += "this object";
break;
case TeleportSubject.Target:
if (subjectTransform != null) desc += $"object [{subjectTransform.name}]";
else desc += $"object [UNDEFINED]";
break;
case TeleportSubject.Tag:
if (subjectTag != null) desc += $"object with tag [{subjectTag.name}]";
else desc += $"object with tag [UNDEFINED]";
break;
case TeleportSubject.Collider:
desc += $"colliding object";
break;
default:
break;
}

desc += " to ";

switch (teleportTarget)
{
case TeleportTarget.Target:
if ((targetTransforms == null) || (targetTransforms.Length == 0) || (targetTransforms[0] == null))
desc += "an [UNDEFINED] position";
else if (targetTransforms.Length > 1)
{
desc += "a randomly position selected from [";
for (int i = 0; i < targetTransforms.Length; i++)
{
if (targetTransforms[i] == null) desc += "NULL";
else desc += $"{targetTransforms[i].name}";
if (i < targetTransforms.Length - 1) desc += ", ";
}

desc += "]";
}
else
{
desc += $"the position of {targetTransforms[0].name}";
}
break;
case TeleportTarget.Tag:
if ((targetTags == null) || (targetTags.Length == 0) || (targetTags[0] == null))
desc += "an [UNDEFINED] tag";
else
{
desc += "a randomly position selected from objects with tags [";
for (int i = 0; i < targetTags.Length; i++)
{
if (targetTags[i] == null) desc += "NULL";
else desc += $"{targetTags[i].name}";
if (i < targetTags.Length - 1) desc += ", ";
}

desc += "]";
}
break;
default:
break;
}

return desc;
}

protected override void CheckErrors()
{
base.CheckErrors();

switch (subject)
{
case TeleportSubject.Target:
if (subjectTransform == null)
{
_logs.Add(new LogEntry(LogEntry.Type.Error, "Subject transform is not set!", "You need to select what object to teleport."));
}
break;
case TeleportSubject.Tag:
if (subjectTag == null)
{
_logs.Add(new LogEntry(LogEntry.Type.Error, "Subject tag is not set!", "You need to select what object to teleport."));
}
break;
default:
break;
}

switch (teleportTarget)
{
case TeleportTarget.Target:
if ((targetTransforms == null) || (targetTransforms.Length == 0) || (targetTransforms[0] == null))
_logs.Add(new LogEntry(LogEntry.Type.Error, "Teleport target is not set!", "You need to select where the subject will teleport to."));
else
{
foreach (var t in targetTransforms)
{
if (t == null)
{
_logs.Add(new LogEntry(LogEntry.Type.Error, "Target is undefined", "This will lead to wrong behaviours"));
}
}
}
break;
case TeleportTarget.Tag:
if ((targetTags == null) || (targetTags.Length == 0) || (targetTags[0] == null))
_logs.Add(new LogEntry(LogEntry.Type.Error, "Teleport tag is not set!", "You need to select where the subject will teleport to."));
else
{
foreach (var t in targetTags)
{
if (t == null)
{
_logs.Add(new LogEntry(LogEntry.Type.Error, "Target is undefined", "This will lead to wrong behaviours"));
}
}
}
break;
default:
break;
}
}

public override void Execute()
{
if (!enableAction) return;
if (!EvaluatePreconditions()) return;

Transform objToTeleport = null;
switch (subject)
{
case TeleportSubject.Self:
objToTeleport = transform;
break;
case TeleportSubject.Target:
objToTeleport = subjectTransform;
break;
case TeleportSubject.Tag:
var taggedObjects = HypertaggedObject.FindObjectsByHypertag<Transform>(subjectTag);
if ((taggedObjects != null) && (taggedObjects.Count > 0))
{
objToTeleport = taggedObjects[Random.Range(0, taggedObjects.Count)];
}
break;
case TeleportSubject.Collider:
var collider = TriggerOnCollision.GetLastCollider();
if (collider) objToTeleport = collider.transform;
break;
default:
break;
}

if (objToTeleport == null) return;

Transform target = null;
switch (teleportTarget)
{
case TeleportTarget.Target:
if ((targetTransforms != null) && (targetTransforms.Length > 0))
{
target = targetTransforms[Random.Range(0, targetTransforms.Length)];
}
break;
case TeleportTarget.Tag:
var taggedObjects = HypertaggedObject.FindObjectsByHypertag<Transform>(subjectTag);
if ((taggedObjects != null) && (taggedObjects.Count > 0))
{
target = taggedObjects[Random.Range(0, taggedObjects.Count)];
}
break;
default:
break;
}

if (target == null) return;

var gridObject = objToTeleport.GetComponent<GridObject>();
if (gridObject)
{
gridObject.TeleportTo(target.position);
}
else
{
objToTeleport.position = target.position;
objToTeleport.rotation = target.rotation;
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/OkapiKit/Scripts/Actions/ActionTeleport.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 79 additions & 0 deletions Assets/OkapiKit/Scripts/Editor/ActionTeleportEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using UnityEditor;
using UnityEngine;
using static OkapiKit.ActionTeleport;

namespace OkapiKit.Editor
{
[CustomEditor(typeof(ActionTeleport))]
public class ActionTeleportEditor : ActionEditor
{
SerializedProperty propSubject;
SerializedProperty propSubjectTransform;
SerializedProperty propSubjectTag;
SerializedProperty propTeleportTarget;
SerializedProperty propTargetTransforms;
SerializedProperty propTargetTags;

protected override void OnEnable()
{
base.OnEnable();

propSubject = serializedObject.FindProperty("subject");
propSubjectTransform = serializedObject.FindProperty("subjectTransform");
propSubjectTag = serializedObject.FindProperty("subjectTag");
propTeleportTarget = serializedObject.FindProperty("teleportTarget");
propTargetTransforms = serializedObject.FindProperty("targetTransforms");
propTargetTags = serializedObject.FindProperty("targetTags");
}

public override void OnInspectorGUI()
{
serializedObject.Update();
if (WriteTitle())
{
StdEditor(false);

var action = (target as ActionTeleport);
if (action == null) return;

EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(propSubject, new GUIContent("Subject", "What's the type of object that should be teleported?"));

var subjectType = (ActionTeleport.TeleportSubject)propSubject.enumValueIndex;
switch (subjectType)
{
case TeleportSubject.Target:
EditorGUILayout.PropertyField(propSubjectTransform, new GUIContent("Subject Transform", "What is the subject's transform?"));
break;
case TeleportSubject.Tag:
EditorGUILayout.PropertyField(propSubjectTransform, new GUIContent("Subject Tag", "What is the subject's tag?"));
break;
default:
break;
}

EditorGUILayout.PropertyField(propTeleportTarget, new GUIContent("Target", "Where to teleport the object?"));

var targetType = (ActionTeleport.TeleportTarget)propTeleportTarget.enumValueIndex;

switch (targetType)
{
case TeleportTarget.Target:
EditorGUILayout.PropertyField(propTargetTransforms, new GUIContent("Targets", "What are the potential targets?"));
break;
case TeleportTarget.Tag:
EditorGUILayout.PropertyField(propTargetTransforms, new GUIContent("Tags", "What are the potential target tags?"));
break;
default:
break;
}

if (EditorGUI.EndChangeCheck())
{
serializedObject.ApplyModifiedProperties();
(target as Action).UpdateExplanation();
}
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/OkapiKit/Scripts/Editor/ActionTeleportEditor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3d27e74

Please sign in to comment.