diff --git a/Assets/Plugins/UnityActions/Scripts/Actions/Action.cs b/Assets/Plugins/UnityActions/Scripts/Actions/Action.cs index a5379da..aaa0deb 100644 --- a/Assets/Plugins/UnityActions/Scripts/Actions/Action.cs +++ b/Assets/Plugins/UnityActions/Scripts/Actions/Action.cs @@ -4,7 +4,7 @@ namespace CC { - public class Action : BaseAction + public abstract class Action : BaseAction { protected Transform target; bool isStarted; @@ -21,14 +21,9 @@ public static void Run(Transform targetTransform ,Action anAction) ActionRunner.Setup(targetTransform,anAction); } - public virtual void LerpAction(float delta) - { - } + public abstract void LerpAction(float delta); - public virtual void Update(float delta) - { - - } + public abstract void Update(float delta); public virtual void StartWithTarget(Transform inTarget) { @@ -45,6 +40,20 @@ public virtual bool IsDone() { return true; } + + + + + + /** Returns a new action that performs the exactly the reverse action. + * + * @return A new action that performs the exactly the reverse action. + */ + public abstract Action Reverse() ; + + + public abstract Action Clone(); + } @@ -57,7 +66,7 @@ public virtual bool IsDone() * Infinite time actions are valid. */ - public class FiniteTimeAction : Action + public abstract class FiniteTimeAction : Action { protected float duration; @@ -65,6 +74,16 @@ public float GetDuration() { return duration; } + + public FiniteTimeAction(float aduration) + { + duration = aduration; + } + + + + + } } diff --git a/Assets/Plugins/UnityActions/Scripts/Actions/ActionInterval.cs b/Assets/Plugins/UnityActions/Scripts/Actions/ActionInterval.cs index bf9cad1..336147f 100644 --- a/Assets/Plugins/UnityActions/Scripts/Actions/ActionInterval.cs +++ b/Assets/Plugins/UnityActions/Scripts/Actions/ActionInterval.cs @@ -20,7 +20,7 @@ then running it again in Reverse mode. Example: Action *pingPongAction = Sequence::actions(action, action.reverse(), nullptr); */ - public class ActionInterval : FiniteTimeAction + public abstract class ActionInterval : FiniteTimeAction { float completedTime; bool isFirstTick; @@ -29,11 +29,11 @@ public float GetCompletedTime() { return completedTime; } - protected ActionInterval() + protected ActionInterval():base(0) { } - public ActionInterval(float duration) + public ActionInterval(float duration):base(duration) { completedTime = 0; this.duration = duration; @@ -83,8 +83,28 @@ public override void StartWithTarget(Transform inTarget) // Extra action for making a Sequence or Spawn when only adding one action to it. class ExtraAction : FiniteTimeAction { - public ExtraAction() + public ExtraAction():base(0) {} + + public override void LerpAction (float delta) + { + + } + + public override void Update (float delta) + { + + } + + public override Action Reverse () + { + return new ExtraAction(); + } + + public override Action Clone () + { + return new ExtraAction(); + } }; @@ -169,14 +189,7 @@ private Sequence( bool dummy,FiniteTimeAction[] list):base(1) } - public Sequence( FiniteTimeAction action1,FiniteTimeAction action2) - { - float totalduration = action1.GetDuration()+action2.GetDuration(); - duration = totalduration; - finiteTimeActions[0]=action1; - finiteTimeActions[1]=action2; - } @@ -262,10 +275,28 @@ public override void LerpAction(float t) - + public Sequence( FiniteTimeAction action1,FiniteTimeAction action2) + { + float totalduration = action1.GetDuration()+action2.GetDuration(); + duration = totalduration; + + finiteTimeActions[0]=action1; + finiteTimeActions[1]=action2; + } + public override Action Reverse () + { + FiniteTimeAction action1 = finiteTimeActions[1].Reverse() as FiniteTimeAction; + FiniteTimeAction action0 = finiteTimeActions[0].Reverse() as FiniteTimeAction ; + + return new Sequence(action1,action0); + } + public override Action Clone () + { + return new Sequence(finiteTimeActions[0].Clone() as FiniteTimeAction,finiteTimeActions[1].Clone() as FiniteTimeAction); + } } @@ -359,7 +390,15 @@ public override bool IsDone() return _total == _times; } + public override Action Reverse() + { + return new Repeat(_innerAction.Reverse() as FiniteTimeAction, _times); + } + public override Action Clone() + { + return new Repeat(_innerAction.Clone() as FiniteTimeAction, _times); + } @@ -412,10 +451,21 @@ public override bool IsDone() return false; } -// RepeatForever Reverse() const -// { -// return RepeatForever::create(_innerAction->reverse()); -// } + public override Action Reverse() + { + return new RepeatForever(_innerAction.Reverse() as ActionInterval); + } + + + public override Action Clone() + { + return new RepeatForever(_innerAction as ActionInterval); + } + + public override void LerpAction (float delta) + { + throw new System.NotImplementedException (); + } // }; @@ -435,7 +485,10 @@ public DelayTime(float d):base(d) } - + + + + // // Overrides // @@ -445,7 +498,22 @@ public DelayTime(float d):base(d) public override void LerpAction(float time) { + + } + + + + public override Action Reverse() + { + return new DelayTime(duration); + } + + + public override Action Clone() + { + return new DelayTime(duration); } + }; @@ -562,7 +630,18 @@ public override void LerpAction(float time) protected FiniteTimeAction _one; protected FiniteTimeAction _two; - + + + public override Action Clone() + { + return new Spawn(_one.Clone() as FiniteTimeAction,_two.Clone() as FiniteTimeAction); + } + + public override Action Reverse() + { + return new Spawn(_one.Reverse() as FiniteTimeAction ,_two.Reverse() as FiniteTimeAction ); + } + }; @@ -584,7 +663,18 @@ public MoveTo(float duration ,Vector3 endPosition ):base(duration) { this.endPosition = endPosition; } - + + public override Action Reverse () + { + throw new System.NotImplementedException (); + } + + + public override Action Clone () + { + return new MoveTo(duration,endPosition); + } + public override void LerpAction(float deltaTime) { target.position = Vector3.Lerp(startPosition,endPosition,deltaTime); @@ -635,6 +725,18 @@ public override void StartWithTarget(Transform inTarget) base.StartWithTarget(inTarget); _previousPosition = startPosition = inTarget.position; } + + public override Action Reverse () + { + return new MoveBy(duration,-_positionDelta); + } + + + public override Action Clone () + { + return new MoveBy(duration,_positionDelta); + } + } @@ -652,6 +754,12 @@ public RotateTo(float duration, Vector3 dstAngle3D):base(duration) _dstAngle = Quaternion.Euler(dstAngle3D); } + + public RotateTo(float duration, Quaternion deltaAngle3D):base(duration) + { + _diffAngle = deltaAngle3D; + + } public override void LerpAction(float deltaTime) @@ -667,7 +775,16 @@ public override void StartWithTarget(Transform inTarget) } - + public override Action Reverse () + { + throw new System.NotImplementedException (); + } + + public override Action Clone () + { + return new RotateTo(duration,_dstAngle); + } + protected Quaternion _dstAngle; protected Quaternion _startRotation; @@ -689,6 +806,12 @@ public RotateBy(float duration, Vector3 deltaAngle3D):base(duration) _diffAngle = Quaternion.Euler(deltaAngle3D); } + + public RotateBy(float duration, Quaternion deltaAngle3D):base(duration) + { + _diffAngle = deltaAngle3D; + + } public override void LerpAction(float deltaTime) @@ -703,7 +826,15 @@ public override void StartWithTarget(Transform inTarget) _dstAngle = _diffAngle * _startRotation; } + public override Action Reverse () + { + return new RotateTo(duration,Quaternion.Inverse( _diffAngle)); + } + public override Action Clone () + { + return new RotateTo(duration,_diffAngle); + } protected Quaternion _dstAngle; @@ -712,8 +843,93 @@ public override void StartWithTarget(Transform inTarget) }; + + + + + public class JumpBy : ActionInterval + { + /** + * Creates the action. + * @param duration Duration time, in seconds. + * @param position The jumping distance. + * @param height The jumping height. + * @param jumps The jumping times. + * + */ + public JumpBy(float aduration, Vector3 position, float height, int jumps):base(aduration) + { + _delta = position; + _height = height; + _jumps = jumps; + + } + + // + // Overrides + // + public override Action Clone () + { + return new JumpBy(duration,_delta,_height,_jumps); + } + + + + public override Action Reverse () + { + return new JumpBy(duration,-_delta,_height,_jumps); + } + public override void StartWithTarget(Transform aTransform) + { + base.StartWithTarget(aTransform); + _previousPos = _startPosition = aTransform.position; + + } + /** + * @param time In seconds. + */ + public override void LerpAction(float t) + { + //TODO: Implement 3d jump + // parabolic jump (since v0.8.2) + if (target) + { + float frac = (t * _jumps)% 1.0f ; + float y = _height * 4 * frac * (1 - frac); + y += _delta.y * t; + float x = _delta.x * t; + //#if CC_ENABLE_STACKABLE_ACTIONS + Vector3 currentPos = target.position; + Vector3 diff = currentPos - _previousPos; + _startPosition = diff + _startPosition; + Vector3 newPos = _startPosition + new Vector3(x,y,0); + target.position= newPos; + _previousPos = newPos; +// #else +// _target->setPosition(_startPosition + Vec2(x,y)); +// #endif // !CC_ENABLE_STACKABLE_ACTIONS + } + } + + + protected Vector3 _startPosition; + protected Vector3 _delta; + protected float _height; + protected int _jumps; + protected Vector3 _previousPos; + + }; + + + + + + + + + diff --git a/Assets/Plugins/UnityActions/Scripts/Actions/Actor.cs b/Assets/Plugins/UnityActions/Scripts/Actions/Actor.cs new file mode 100644 index 0000000..14d42e5 --- /dev/null +++ b/Assets/Plugins/UnityActions/Scripts/Actions/Actor.cs @@ -0,0 +1,72 @@ +using UnityEngine; +using System.Collections; + + +namespace CC +{ + + + +public class Actor:MonoBehaviour { + + public FiniteTimeAction action; + + + + static public Actor GetActor(Transform intransform) + { + Actor actor = intransform.GetComponent(); + if(actor == null) + { + actor = intransform.gameObject.AddComponent(); + } + + return actor; + } + + + + + + + public IEnumerator YieldAction(FiniteTimeAction anAction) + { + if(action !=null) + Debug.LogError("An action is already running"); + else + { + action = anAction; + + Action.Run(transform,action); + yield return new WaitForSeconds(action.GetDuration()); + while(!action.IsDone()) + { + yield return null; + } + action=null; + } + + + } + + + public Coroutine PerformAction(FiniteTimeAction anAction) + { + return StartCoroutine(YieldAction(anAction)); + + } + + public Coroutine MoveBy(float aduration,Vector3 diff) + { + return PerformAction(new CC.MoveBy(aduration,diff)); + + } + + public Coroutine MoveTo(float duraction,Vector3 targetPos) + { + return PerformAction(new CC.MoveTo(duraction,targetPos)); + } + + +} +} \ No newline at end of file diff --git a/Assets/Plugins/UnityActions/Scripts/Actions/Actor.cs.meta b/Assets/Plugins/UnityActions/Scripts/Actions/Actor.cs.meta new file mode 100644 index 0000000..6afca56 --- /dev/null +++ b/Assets/Plugins/UnityActions/Scripts/Actions/Actor.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: cc8320165bac94ccea2209b0d7bfb374 +timeCreated: 1437353674 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Plugins/UnityActions/Scripts/TestCases/TestActions.cs b/Assets/Plugins/UnityActions/Scripts/TestCases/TestActions.cs index bedb880..39d87f8 100644 --- a/Assets/Plugins/UnityActions/Scripts/TestCases/TestActions.cs +++ b/Assets/Plugins/UnityActions/Scripts/TestCases/TestActions.cs @@ -6,6 +6,21 @@ public class TestActions : MonoBehaviour { public Transform movingObject; + + IEnumerator anAnimation() + { + Actor anActor = Actor.GetActor(movingObject); + yield return anActor.MoveBy(3,new Vector3(10,10,10)); + + Debug.Log("Now cube must be at the top of the screen"); + + + yield return anActor.MoveTo(4,new Vector3(0,0,0)); + + Debug.Log("Both the action completed now must be completed at 4+3=7 seconds"); + + } + void Start () { // Action.Run(movingObject,new MoveBy(10 ,new Vector3(10,1,1))); @@ -13,9 +28,9 @@ void Start () // - Action.Run(movingObject,new Spawn(new MoveBy(10 ,new Vector3(10,1,1)),new MoveBy(10,new Vector3(0,30,0)),new RotateBy(10,new Vector3(0,180,0)))); - // Action.Run(movingObject,new RotateBy(5,new Vector3(10,0,0))); - // +// Action.Run(movingObject,new Spawn(new MoveBy(10 ,new Vector3(10,1,1)),new MoveBy(10,new Vector3(0,30,0)),new RotateBy(10,new Vector3(0,180,0)))); +// // Action.Run(movingObject,new RotateBy(5,new Vector3(10,0,0))); +// // // Sequence q= new Sequence(new MoveBy(3 ,new Vector3(10,1,1)), // new RotateBy(5,new Vector3(180,0,0)), @@ -33,6 +48,10 @@ void Start () //Action.Run(movingObject,new RepeatForever(new RotateBy(0.5f,new Vector3(10,0,0)))); + + + StartCoroutine(anAnimation()); + } void Update () diff --git a/README.md b/README.md index ad1b61d..cf49e74 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,65 @@ -# UnityActions -This is Port of Cocos2d actions to unity. Enjoy! Hoped to replace *-tween libraries. +# Unity Actions +Unity Actions is a port of Cocos2d actions to unity. +Actions are one of the powerfull feature of cocos2d that I always missed in Unity3d. I never satisfied with iTween or related library. This code line by line port of original cocos2d (Cosos2d-X actually) code. -Making an animation may not be simpler than this. - +With actions, making an animation may not be simpler than this. +```c# Action.Run(aTransform,new MoveBy(10 ,new Vector3(10,1,1))); +``` +Above code move/transforms 'aTransform' by Vector3(10,1,1) units in 10 seconds. Now this is basic one, more complex example follows. +```c# + Sequence seq= new Sequence(new MoveBy(3 ,new Vector3(10,1,1)), + new RotateBy(5,new Vector3(180,0,0)), + new MoveBy(3 ,new Vector3(-10,1,1)), + new RotateBy(3,new Vector3(0,60,0)) + ); + + Action.Run(movingObject, new Repeat(seq,2)); + +``` +That must be self-explanatory. + +# Welcome Actors! +Power of cocos2d actions and Unity3d coroutines can be nicely combined to produce yet more powerfull feature called **Actors**. Right now it can be thought as syntax sugar over action. + +```c# +public Transform movingObject; + +IEnumerator anAnimation() +{ + Actor anActor = Actor.GetActor(movingObject); + yield return anActor.MoveBy(3,new Vector3(10,10,10)); + + Debug.Log("Now cube must be at the top of the screen"); + + yield return anActor.MoveTo(4,new Vector3(0,0,0)); + + Debug.Log("Both the action completed now. Must be completed at 4+3=7 seconds"); +} + +void Start() +{ + StartCoroutine(anAnimation()); +} +``` +Code you see here is *actually* part of **Actor** testcase! Isn't it intutive, and as simple as *screenwriting*. +## Contribution +There is a lot of sugar in cocos2d in waiting to be ported to Unity3d! All contribution are eagerly accepted. +## Todos + + - Write Testcases + - Port CCInstantActions,CCTransitions... + - Add Code Comments + + +## License +Apache License +--- +**Free Software, Hell Yeah!** -- hehe markdown porked from http://dillinger.io + + +- [Cocos2d iphone project ](https://github.com/cocos2d/cocos2d-objc) +- [Cosco2d-X project](https://github.com/cocos2d/cocos2d-x) +