From 06e25ca6c7eec588a6184409ef59c8850bb1a589 Mon Sep 17 00:00:00 2001 From: "Ivan \"KaiSD\" Korystin" Date: Thu, 19 Jun 2014 23:03:18 +0400 Subject: [PATCH] 2D support --- README.md | 3 +-- src/ActionsBase/Action.cs | 1 + src/ActionsInstant/ActionSetPlace.cs | 16 ++++++++++++---- src/ActionsInstant/ActionSetRotation.cs | 6 ++++++ src/ActionsInterval/ActionBezierAbs.cs | 19 +++++++++++++++++++ src/ActionsInterval/ActionBezierRel.cs | 6 ++++++ src/ActionsInterval/ActionBlink.cs | 25 +++++++++++++++++++++---- src/ActionsInterval/ActionJumpBy.cs | 6 ++++++ src/ActionsInterval/ActionJumpTo.cs | 8 ++++++++ src/ActionsInterval/ActionMoveBy.cs | 6 ++++++ src/ActionsInterval/ActionMoveTo.cs | 7 +++++++ src/ActionsInterval/ActionRotateBy.cs | 6 ++++++ src/ActionsInterval/ActionRotateTo.cs | 6 ++++++ src/ActionsInterval/ActionScaleBy.cs | 6 ++++++ src/ActionsInterval/ActionScaleTo.cs | 13 ++++++++----- src/Examples/SampleActions.cs | 10 ++-------- 16 files changed, 121 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index ba3395c..251b3b8 100644 --- a/README.md +++ b/README.md @@ -16,8 +16,7 @@ See the examples folder, there's a sample script there. You can also subclass the Actor and add all the actions you want to the Start() method. ### Included actions (ready and WIP) -All actions are designed to word in 3D scene (i.e. using Vector3 for movement, rotation and scaling). -You can use it in 2D with apropriate vectors, but i'm going to add support for 2D actions soon. +All actions are capable to work both in 3D (use Vector3) and 2D (use Vector2) scenes. Base actions - [x] Sequence diff --git a/src/ActionsBase/Action.cs b/src/ActionsBase/Action.cs index 1d11bd4..4551c62 100644 --- a/src/ActionsBase/Action.cs +++ b/src/ActionsBase/Action.cs @@ -8,6 +8,7 @@ public class Action public float duration = 0; protected Transform transform; protected Renderer renderer; + protected bool is2d = false; public Action() { diff --git a/src/ActionsInstant/ActionSetPlace.cs b/src/ActionsInstant/ActionSetPlace.cs index f8ef495..daafe6f 100644 --- a/src/ActionsInstant/ActionSetPlace.cs +++ b/src/ActionsInstant/ActionSetPlace.cs @@ -4,22 +4,30 @@ using UnityEngine; class ActionSetPlace : Action { - protected Vector3 place; + protected Vector3 value; public ActionSetPlace(Vector3 tgtPlace) : base() { - place = tgtPlace; + value = tgtPlace; + } + + public ActionSetPlace(Vector2 tgtPlace) + : this((Vector3) tgtPlace) + { + is2d = true; } public override Action clone() { - return new ActionSetPlace(place); + return new ActionSetPlace(value); } public override void start() { base.start(); - transform.position = place; + if (is2d) + value.z = transform.position.z; + transform.position = value; } } \ No newline at end of file diff --git a/src/ActionsInstant/ActionSetRotation.cs b/src/ActionsInstant/ActionSetRotation.cs index 4b1e65b..8710eac 100644 --- a/src/ActionsInstant/ActionSetRotation.cs +++ b/src/ActionsInstant/ActionSetRotation.cs @@ -12,6 +12,12 @@ class ActionSetRotation : Action value = tgtValue; } + public ActionSetRotation(float angle) + : this(new Vector3(0, 0, angle)) + { + is2d = true; + } + public override Action clone() { return new ActionSetRotation(value); diff --git a/src/ActionsInterval/ActionBezierAbs.cs b/src/ActionsInterval/ActionBezierAbs.cs index 0bc9bb1..d5dbbed 100644 --- a/src/ActionsInterval/ActionBezierAbs.cs +++ b/src/ActionsInterval/ActionBezierAbs.cs @@ -18,6 +18,12 @@ class ActionBezierAbs : ActionInterval endControlPoint = tgtECP; } + public ActionBezierAbs(Vector2 tgtStart, Vector2 tgtSCP, Vector2 tgtECP, Vector2 tgtEnd, float tgtDuration) + : this((Vector3) tgtStart, (Vector3) tgtSCP, (Vector3) tgtECP, (Vector3) tgtEnd, tgtDuration) + { + is2d = true; + } + public override Action clone() { return new ActionBezierAbs(startPoint, startControlPoint, endControlPoint, endPoint, duration); @@ -28,6 +34,19 @@ class ActionBezierAbs : ActionInterval return new ActionBezierAbs(endPoint, endControlPoint, startControlPoint, startPoint, duration); } + public override void start() + { + base.start(); + float z = transform.position.z; + if (is2d) + { + startPoint.z = z; + endPoint.z = z; + startControlPoint.z = z; + endControlPoint.z = z; + } + } + public override void stepInterval(float dt) { float t = timer / duration; diff --git a/src/ActionsInterval/ActionBezierRel.cs b/src/ActionsInterval/ActionBezierRel.cs index 3cbb43d..4029e04 100644 --- a/src/ActionsInterval/ActionBezierRel.cs +++ b/src/ActionsInterval/ActionBezierRel.cs @@ -20,6 +20,12 @@ class ActionBezierRel : ActionInterval cp2 = tgtECP; } + public ActionBezierRel(Vector2 tgtSCP, Vector2 tgtECP, Vector2 tgtEnd, float tgtDuration) + : this((Vector3)tgtSCP, (Vector3)tgtECP, (Vector3)tgtEnd, tgtDuration) + { + is2d = true; + } + public override Action clone() { return new ActionBezierRel(startControlPoint, endControlPoint, endPoint, duration); diff --git a/src/ActionsInterval/ActionBlink.cs b/src/ActionsInterval/ActionBlink.cs index 2c57df1..3f1f228 100644 --- a/src/ActionsInterval/ActionBlink.cs +++ b/src/ActionsInterval/ActionBlink.cs @@ -7,11 +7,14 @@ class ActionBlink : ActionRepeat protected bool randomDelay; protected ActionDelay delay = new ActionDelay(0); protected Action[] blinkSeq; + protected float durationMin; + protected float durationMax; public ActionBlink(int tgtBlinks, float tgtDuration) - : base(null, tgtBlinks) + : base(null, 0) { - duration = tgtDuration; + durationMin = tgtDuration; + durationMax = tgtDuration; count = (tgtBlinks) * 2; blinkSeq = new Action[] { @@ -21,13 +24,27 @@ class ActionBlink : ActionRepeat action = new ActionSequence(blinkSeq); } + public ActionBlink(int tgtBlinks, float tgtDurationMin, float tgtDurationMax) + : base(null, 0) + { + durationMin = tgtDurationMin; + durationMax = tgtDurationMax; + count = (tgtBlinks) * 2; + blinkSeq = new Action[] + { + new ActionToggleVisibility(), + new ActionDelay(tgtDurationMin / tgtBlinks, tgtDurationMax / tgtBlinks) + }; + action = new ActionSequence(blinkSeq); + } + public override Action clone() { - return new ActionBlink(count / 2 - 1, duration); + return new ActionBlink(count / 2 - 1, durationMin, durationMax); } public override Action reverse() { - return new ActionBlink(count / 2 - 1, duration); + return new ActionBlink(count / 2 - 1, durationMin, durationMax); } } \ No newline at end of file diff --git a/src/ActionsInterval/ActionJumpBy.cs b/src/ActionsInterval/ActionJumpBy.cs index 490589a..1264723 100644 --- a/src/ActionsInterval/ActionJumpBy.cs +++ b/src/ActionsInterval/ActionJumpBy.cs @@ -17,6 +17,12 @@ class ActionJumpBy : ActionSequence duration = tgtDuration; } + public ActionJumpBy(Vector2 tgtPoint, float tgtHeight, int tgtJumps, float tgtDuration) + : this((Vector3)tgtPoint, tgtHeight, tgtJumps, tgtDuration) + { + is2d = true; + } + public override Action clone() { return new ActionJumpBy(point, height, jumps, duration); diff --git a/src/ActionsInterval/ActionJumpTo.cs b/src/ActionsInterval/ActionJumpTo.cs index e991e61..97892ba 100644 --- a/src/ActionsInterval/ActionJumpTo.cs +++ b/src/ActionsInterval/ActionJumpTo.cs @@ -17,6 +17,12 @@ class ActionJumpTo : ActionSequence duration = tgtDuration; } + public ActionJumpTo(Vector2 tgtPoint, float tgtHeight, int tgtJumps, float tgtDuration) + : this((Vector3)tgtPoint, tgtHeight, tgtJumps, tgtDuration) + { + is2d = true; + } + public override Action clone() { return new ActionJumpTo(point, height, jumps, duration); @@ -25,6 +31,8 @@ class ActionJumpTo : ActionSequence public override void start() { float coeff = 1F / jumps; + if (is2d) + point.z = transform.position.z; Vector3 start = target.gameObject.transform.position; Vector3 end = (point - start) * coeff; Vector3 cp1 = Vector3.up * height; diff --git a/src/ActionsInterval/ActionMoveBy.cs b/src/ActionsInterval/ActionMoveBy.cs index a5ae133..43f5130 100644 --- a/src/ActionsInterval/ActionMoveBy.cs +++ b/src/ActionsInterval/ActionMoveBy.cs @@ -12,6 +12,12 @@ class ActionMoveBy : ActionInterval delta = tgtDelta; } + public ActionMoveBy(Vector2 tgtValue, float tgtDuration) + : this((Vector3) tgtValue, tgtDuration) + { + is2d = true; + } + public override Action clone() { return new ActionMoveBy(delta, duration); diff --git a/src/ActionsInterval/ActionMoveTo.cs b/src/ActionsInterval/ActionMoveTo.cs index a6df596..656fb74 100644 --- a/src/ActionsInterval/ActionMoveTo.cs +++ b/src/ActionsInterval/ActionMoveTo.cs @@ -13,6 +13,11 @@ class ActionMoveTo : ActionInterval value = tgtValue; } + public ActionMoveTo(Vector2 tgtValue, float tgtDuration) + : this((Vector3) tgtValue, tgtDuration) + { + is2d = true; + } public override Action clone() { @@ -22,6 +27,8 @@ class ActionMoveTo : ActionInterval public override void start() { base.start(); + if (is2d) + value.z = transform.position.z; path = value - transform.position; } diff --git a/src/ActionsInterval/ActionRotateBy.cs b/src/ActionsInterval/ActionRotateBy.cs index ab0d7be..3883a71 100644 --- a/src/ActionsInterval/ActionRotateBy.cs +++ b/src/ActionsInterval/ActionRotateBy.cs @@ -12,6 +12,12 @@ class ActionRotateBy : ActionInterval delta = tgtDelta; } + public ActionRotateBy(float angle, float tgtDuration) + : this(new Vector3(0, 0, angle), tgtDuration) + { + is2d = true; + } + public override Action clone() { return new ActionRotateBy(delta, duration); diff --git a/src/ActionsInterval/ActionRotateTo.cs b/src/ActionsInterval/ActionRotateTo.cs index 859f435..a5c69b2 100644 --- a/src/ActionsInterval/ActionRotateTo.cs +++ b/src/ActionsInterval/ActionRotateTo.cs @@ -13,6 +13,12 @@ class ActionRotateTo : ActionInterval value = tgtValue; } + public ActionRotateTo(float angle, float tgtDuration) + : this(new Vector3(0, 0, angle), tgtDuration) + { + is2d = true; + } + public override Action clone() { return new ActionRotateTo(value, duration); diff --git a/src/ActionsInterval/ActionScaleBy.cs b/src/ActionsInterval/ActionScaleBy.cs index bfba18b..63d9cad 100644 --- a/src/ActionsInterval/ActionScaleBy.cs +++ b/src/ActionsInterval/ActionScaleBy.cs @@ -13,6 +13,12 @@ class ActionScaleBy : ActionInterval delta = tgtDelta; } + public ActionScaleBy(Vector2 tgtValue, float tgtDuration) + : this((Vector3)tgtValue, tgtDuration) + { + is2d = true; + } + public override Action clone() { return new ActionScaleBy(delta, duration); diff --git a/src/ActionsInterval/ActionScaleTo.cs b/src/ActionsInterval/ActionScaleTo.cs index 2d89856..19ba880 100644 --- a/src/ActionsInterval/ActionScaleTo.cs +++ b/src/ActionsInterval/ActionScaleTo.cs @@ -13,6 +13,12 @@ class ActionScaleTo : ActionInterval value = tgtValue; } + public ActionScaleTo(Vector2 tgtValue, float tgtDuration) + : this((Vector3) tgtValue, tgtDuration) + { + is2d = true; + } + public override Action clone() { return new ActionScaleTo(value, duration); @@ -21,6 +27,8 @@ class ActionScaleTo : ActionInterval public override void start() { base.start(); + if (is2d) + value.z = transform.localScale.z; path = value - transform.localScale; } @@ -30,9 +38,4 @@ class ActionScaleTo : ActionInterval Vector3 tgt = path * d; transform.localScale += tgt; } - - public override void stop() - { - base.stop(); - } } \ No newline at end of file diff --git a/src/Examples/SampleActions.cs b/src/Examples/SampleActions.cs index 0da1066..2d59edb 100644 --- a/src/Examples/SampleActions.cs +++ b/src/Examples/SampleActions.cs @@ -16,21 +16,15 @@ public class SampleActions : MonoBehaviour new ActionScaleBy(new Vector3(2, 2, 1), 1), new ActionScaleBy(new Vector3(0.5F, 0.5F, 2), 1), new ActionDelay(1), - new ActionRepeat(new ActionSequence(new Action[] { - new ActionHide(), - new ActionDelay(0F, 0.2F), - new ActionShow(), - new ActionDelay(0F, 0.2F), - }), 5), + new ActionBlink(5, 0.1F, 0.4F), new ActionDelay(1), new ActionJumpBy(new Vector3(-10, 0, 0), 1, 4, 1), new ActionJumpTo(new Vector3(10, 10, 10), 1, 3, 1), new ActionJumpBy(new Vector3(-10, 0, 0), 1, 2, 1), new ActionDelay(0.5F), - new ActionBezierRel(new Vector3 (5, 0, 0), new Vector3(5, -10, 0), new Vector3 (0, -10, 0), 2), + new ActionBezierRel(new Vector2 (5, 0), new Vector2(5, -10), new Vector2 (0, -10), 2), new ActionScaleTo(new Vector3(2, 2, 2), 1), new ActionRotateTo(new Vector3(0, 0, 0), 1), - new ActionBlink(2, 1), new ActionFadeOut(2), new ActionSetTint(new Vector4(67, 105, 181)) }), 5);