-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added Dash action, which enable short bursts of movement, using var…
…iables or values
- Loading branch information
1 parent
5cbb0ad
commit ca56935
Showing
9 changed files
with
278 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
using Random = UnityEngine.Random; | ||
|
||
namespace OkapiKit | ||
{ | ||
[AddComponentMenu("Okapi/Action/Dash")] | ||
public class ActionDash : Action | ||
{ | ||
public enum RelativeTo { Right, Up, LocalRight, LocalUp }; | ||
|
||
[SerializeField] | ||
private Transform target; | ||
[SerializeField, OVNoFunction] | ||
private OkapiValue speed = new OkapiValue(100.0f); | ||
[SerializeField] | ||
private RelativeTo direction; | ||
[SerializeField, OVNoFunction] | ||
private OkapiValue angle = new OkapiValue(0.0f); | ||
[SerializeField, OVNoFunction] | ||
private OkapiValue duration = new OkapiValue(0.1f); | ||
|
||
float timer = 0; | ||
float actualSpeed; | ||
|
||
public override void Execute() | ||
{ | ||
if (!enableAction) return; | ||
if (!EvaluatePreconditions()) return; | ||
if (target == null) return; | ||
|
||
timer = duration.GetFloat(gameObject); | ||
actualSpeed = speed.GetFloat(gameObject); | ||
} | ||
|
||
public override string GetActionTitle() => "Dash"; | ||
|
||
public override string GetRawDescription(string ident, GameObject gameObject) | ||
{ | ||
string desc = GetPreconditionsString(gameObject); | ||
|
||
if (target) | ||
{ | ||
desc += $"Moves object {target.name} "; | ||
} | ||
else | ||
{ | ||
desc += "Moves this object "; | ||
} | ||
|
||
switch (direction) | ||
{ | ||
case RelativeTo.Right: | ||
desc += "in the right direction"; | ||
break; | ||
case RelativeTo.Up: | ||
desc += "in the up direction"; | ||
break; | ||
case RelativeTo.LocalRight: | ||
desc += "in the local right direction"; | ||
break; | ||
case RelativeTo.LocalUp: | ||
desc += "in the local up direction"; | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
desc += $", plus {angle.GetDescription()} degrees, "; | ||
desc += $"at a speed of {speed.GetDescription()} units/second, "; | ||
desc += $"for {duration.GetDescription()} seconds."; | ||
|
||
return desc; | ||
} | ||
|
||
protected override void CheckErrors() | ||
{ | ||
base.CheckErrors(); | ||
|
||
if (target == null) | ||
{ | ||
_logs.Add(new LogEntry(LogEntry.Type.Warning, "Transform to modify is this object, but it should be explicitly linked!", "Setting options explicitly is always better than letting the system find them, since it might have to guess our intentions.")); | ||
} | ||
} | ||
|
||
protected override void Awake() | ||
{ | ||
base.Awake(); | ||
} | ||
|
||
protected void FixedUpdate() | ||
{ | ||
timer -= Time.fixedDeltaTime; | ||
if (timer > 0) | ||
{ | ||
target.position += GetDirection() * actualSpeed * Time.fixedDeltaTime; | ||
} | ||
} | ||
|
||
Vector3 GetDirection() | ||
{ | ||
var dir = Vector2.zero; | ||
switch (direction) | ||
{ | ||
case RelativeTo.Right: | ||
dir = Vector2.right; | ||
break; | ||
case RelativeTo.Up: | ||
dir = Vector2.up; | ||
break; | ||
case RelativeTo.LocalRight: | ||
dir = target.right; | ||
break; | ||
case RelativeTo.LocalUp: | ||
dir = target.up; | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
var newDir = Vector2.zero; | ||
var radAngle = Mathf.Deg2Rad * angle.GetFloat(gameObject); | ||
var c = Mathf.Cos(radAngle); | ||
var s = Mathf.Sin(radAngle); | ||
|
||
newDir.x = dir.x * c - dir.y * s; | ||
newDir.y = dir.x * s + dir.y * c; | ||
|
||
dir = newDir; | ||
|
||
return dir; | ||
} | ||
} | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using UnityEditor; | ||
using UnityEngine; | ||
using static OkapiKit.ActionDash; | ||
|
||
namespace OkapiKit.Editor | ||
{ | ||
[CustomEditor(typeof(ActionDash))] | ||
public class ActionDashEditor : ActionEditor | ||
{ | ||
SerializedProperty propTarget; | ||
SerializedProperty propSpeed; | ||
SerializedProperty propDirection; | ||
SerializedProperty propAngle; | ||
SerializedProperty propDuration; | ||
|
||
protected override void OnEnable() | ||
{ | ||
base.OnEnable(); | ||
|
||
propTarget = serializedObject.FindProperty("target"); ; | ||
propSpeed = serializedObject.FindProperty("speed"); ; | ||
propDirection = serializedObject.FindProperty("direction"); ; | ||
propAngle = serializedObject.FindProperty("angle"); ; | ||
propDuration = serializedObject.FindProperty("duration"); ; | ||
} | ||
|
||
public override void OnInspectorGUI() | ||
{ | ||
serializedObject.Update(); | ||
|
||
if (WriteTitle()) | ||
{ | ||
StdEditor(false); | ||
|
||
var action = (target as ActionDash); | ||
if (action == null) return; | ||
|
||
EditorGUI.BeginChangeCheck(); | ||
EditorGUILayout.PropertyField(propTarget, new GUIContent("Target", "Which object transform will be moved.\nDashing is just changing the position of an object in a certain way.")); | ||
EditorGUILayout.PropertyField(propSpeed, new GUIContent("Speed Range", "How fast should the object move, in units/second?\nThis is a range, everytime this action runs, a random number will be selected between these two and used as the speed.")); | ||
EditorGUILayout.PropertyField(propDirection, new GUIContent("Direction", "In which direction should the object move? This is the basis from where the direction itself is calculated, using the next property (angle)")); | ||
EditorGUILayout.PropertyField(propAngle, new GUIContent("Angle", "What's the angle of movement, measured from the axis above.")); | ||
EditorGUILayout.PropertyField(propDuration, new GUIContent("Duration", "For how long should the movement happen? If this action is triggered again, it will cancel previous dashes.")); | ||
|
||
if (EditorGUI.EndChangeCheck()) | ||
{ | ||
serializedObject.ApplyModifiedProperties(); | ||
(target as Action).UpdateExplanation(); | ||
} | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters