From c8186552b9e6d564f36edfce088d5bcabed3dc24 Mon Sep 17 00:00:00 2001 From: "Ivan \"KaiSD\" Korystin" Date: Sun, 29 Jun 2014 13:14:43 +0400 Subject: [PATCH] Replaced "is2d" field with axis locks. Made Actor to work with gameBojects without the MeshFilter component. Made AddMethodToCache simpler. --- Assets/scripts/coa4u/ActionsBase/Action.cs | 54 +++++++++++++++++++ .../ActionsInstant/ActionSetDirection.cs | 6 +-- .../coa4u/ActionsInstant/ActionSetPlace.cs | 6 +-- .../coa4u/ActionsInstant/ActionSetRotation.cs | 4 +- .../coa4u/ActionsInterval/ActionBezierAbs.cs | 12 ++--- .../coa4u/ActionsInterval/ActionBezierRel.cs | 1 - .../coa4u/ActionsInterval/ActionJumpBy.cs | 1 - .../coa4u/ActionsInterval/ActionJumpTo.cs | 6 +-- .../coa4u/ActionsInterval/ActionMoveBy.cs | 1 - .../coa4u/ActionsInterval/ActionMoveTo.cs | 6 +-- .../coa4u/ActionsInterval/ActionRotateBy.cs | 1 - .../coa4u/ActionsInterval/ActionRotateTo.cs | 4 +- .../coa4u/ActionsInterval/ActionScaleBy.cs | 1 - .../coa4u/ActionsInterval/ActionScaleTo.cs | 6 +-- Assets/scripts/coa4u/UnityComponents/Actor.cs | 31 +++++++++-- .../UnityComponents/ActorSampleActions.cs | 4 +- 16 files changed, 109 insertions(+), 35 deletions(-) diff --git a/Assets/scripts/coa4u/ActionsBase/Action.cs b/Assets/scripts/coa4u/ActionsBase/Action.cs index ff4f07a..997157f 100644 --- a/Assets/scripts/coa4u/ActionsBase/Action.cs +++ b/Assets/scripts/coa4u/ActionsBase/Action.cs @@ -4,11 +4,24 @@ using UnityEngine; namespace coa4u { + public enum Axises + { + none, + x, + y, + z, + xy, + xz, + yz, + xyz + } + /// /// Basic action class for subclassing. To inherit interval actions, consider using the ActionInterval as the base class. /// public class ActionInstant { + public Axises locks = Axises.none; protected Actor target; protected Transform transform; protected Renderer renderer; @@ -74,6 +87,47 @@ namespace coa4u target = actor; } + /// + /// This method modifies the given vector to keep the locked axises untouched. + /// + protected void lockAxises(ref Vector3 point) + { + { + if (target == null) + throw new Exception("Can calculate axises only after the action is atached to the actor"); + } + + switch (locks) + { + case Axises.x: + point.x = transform.position.x; + break; + case Axises.y: + point.y = transform.position.y; + break; + case Axises.z: + point.z = transform.position.z; + break; + case Axises.xy: + point.x = transform.position.x; + point.y = transform.position.y; + break; + case Axises.xz: + point.x = transform.position.x; + point.z = transform.position.z; + break; + case Axises.yz: + point.y = transform.position.y; + point.z = transform.position.z; + break; + case Axises.xyz: + point.x = transform.position.x; + point.y = transform.position.y; + point.z = transform.position.z; + break; + } + } + /// /// Readonly property. Shows the remaining time of the action. /// diff --git a/Assets/scripts/coa4u/ActionsInstant/ActionSetDirection.cs b/Assets/scripts/coa4u/ActionsInstant/ActionSetDirection.cs index 1827897..8a1f75b 100644 --- a/Assets/scripts/coa4u/ActionsInstant/ActionSetDirection.cs +++ b/Assets/scripts/coa4u/ActionsInstant/ActionSetDirection.cs @@ -21,7 +21,7 @@ namespace coa4u public ActionSetDirection(Vector2 tgtValue) : this((Vector3)tgtValue) { - is2d = true; + locks = Axises.z; } public ActionSetDirection(Actor tgtActor) @@ -48,11 +48,11 @@ namespace coa4u { value = other.transform.position; } - if (is2d) + if (locks != Axises.none) { value.z = transform.position.z; } - transform.rotation = Quaternion.LookRotation(transform.position - value); + transform.rotation = Quaternion.LookRotation(value - transform.position); } } } \ No newline at end of file diff --git a/Assets/scripts/coa4u/ActionsInstant/ActionSetPlace.cs b/Assets/scripts/coa4u/ActionsInstant/ActionSetPlace.cs index 87f375f..d84ddc4 100644 --- a/Assets/scripts/coa4u/ActionsInstant/ActionSetPlace.cs +++ b/Assets/scripts/coa4u/ActionsInstant/ActionSetPlace.cs @@ -20,7 +20,7 @@ namespace coa4u public ActionSetPlace(Vector2 tgtPlace) : this((Vector3)tgtPlace) { - is2d = true; + locks = Axises.z; } /// @@ -37,8 +37,8 @@ namespace coa4u public override void start() { base.start(); - if (is2d) - value.z = transform.position.z; + if (locks != Axises.none) + lockAxises(ref value); transform.position = value; } } diff --git a/Assets/scripts/coa4u/ActionsInstant/ActionSetRotation.cs b/Assets/scripts/coa4u/ActionsInstant/ActionSetRotation.cs index 25d8820..49e6c64 100644 --- a/Assets/scripts/coa4u/ActionsInstant/ActionSetRotation.cs +++ b/Assets/scripts/coa4u/ActionsInstant/ActionSetRotation.cs @@ -20,7 +20,7 @@ namespace coa4u public ActionSetRotation(float angle) : this(new Vector3(0, 0, angle)) { - is2d = true; + locks = Axises.xy; } /// @@ -37,6 +37,8 @@ namespace coa4u public override void start() { base.start(); + if (locks != Axises.none) + lockAxises(ref value); transform.rotation = Quaternion.Euler(value); } } diff --git a/Assets/scripts/coa4u/ActionsInterval/ActionBezierAbs.cs b/Assets/scripts/coa4u/ActionsInterval/ActionBezierAbs.cs index c105cd8..4253b4c 100644 --- a/Assets/scripts/coa4u/ActionsInterval/ActionBezierAbs.cs +++ b/Assets/scripts/coa4u/ActionsInterval/ActionBezierAbs.cs @@ -26,7 +26,7 @@ namespace coa4u public ActionBezierAbs(Vector2 tgtStart, Vector2 tgtSCP, Vector2 tgtECP, Vector2 tgtEnd, float tgtDuration) : this((Vector3)tgtStart, (Vector3)tgtSCP, (Vector3)tgtECP, (Vector3)tgtEnd, tgtDuration) { - is2d = true; + locks = Axises.z; } /// @@ -52,12 +52,12 @@ namespace coa4u { base.start(); float z = transform.position.z; - if (is2d) + if (locks != Axises.none) { - startPoint.z = z; - endPoint.z = z; - startControlPoint.z = z; - endControlPoint.z = z; + lockAxises(ref startPoint); + lockAxises(ref endPoint); + lockAxises(ref startControlPoint); + lockAxises(ref endControlPoint); } } diff --git a/Assets/scripts/coa4u/ActionsInterval/ActionBezierRel.cs b/Assets/scripts/coa4u/ActionsInterval/ActionBezierRel.cs index b903c37..140d245 100644 --- a/Assets/scripts/coa4u/ActionsInterval/ActionBezierRel.cs +++ b/Assets/scripts/coa4u/ActionsInterval/ActionBezierRel.cs @@ -28,7 +28,6 @@ namespace coa4u public ActionBezierRel(Vector2 tgtSCP, Vector2 tgtECP, Vector2 tgtEnd, float tgtDuration) : this((Vector3)tgtSCP, (Vector3)tgtECP, (Vector3)tgtEnd, tgtDuration) { - is2d = true; } /// diff --git a/Assets/scripts/coa4u/ActionsInterval/ActionJumpBy.cs b/Assets/scripts/coa4u/ActionsInterval/ActionJumpBy.cs index ce7b03d..64e2cfc 100644 --- a/Assets/scripts/coa4u/ActionsInterval/ActionJumpBy.cs +++ b/Assets/scripts/coa4u/ActionsInterval/ActionJumpBy.cs @@ -22,7 +22,6 @@ namespace coa4u public ActionJumpBy(Vector2 tgtPoint, float tgtHeight, int tgtJumps, float tgtDuration) : this((Vector3)tgtPoint, tgtHeight, tgtJumps, tgtDuration) { - is2d = true; } public override ActionInstant clone() diff --git a/Assets/scripts/coa4u/ActionsInterval/ActionJumpTo.cs b/Assets/scripts/coa4u/ActionsInterval/ActionJumpTo.cs index 049f732..8531089 100644 --- a/Assets/scripts/coa4u/ActionsInterval/ActionJumpTo.cs +++ b/Assets/scripts/coa4u/ActionsInterval/ActionJumpTo.cs @@ -22,7 +22,7 @@ namespace coa4u public ActionJumpTo(Vector2 tgtPoint, float tgtHeight, int tgtJumps, float tgtDuration) : this((Vector3)tgtPoint, tgtHeight, tgtJumps, tgtDuration) { - is2d = true; + locks = Axises.z; } public override ActionInstant clone() @@ -33,8 +33,8 @@ namespace coa4u public override void start() { float coeff = 1F / jumps; - if (is2d) - point.z = transform.position.z; + if (locks != Axises.none) + lockAxises(ref point); Vector3 start = target.gameObject.transform.position; Vector3 end = (point - start) * coeff; Vector3 cp1 = Vector3.up * height; diff --git a/Assets/scripts/coa4u/ActionsInterval/ActionMoveBy.cs b/Assets/scripts/coa4u/ActionsInterval/ActionMoveBy.cs index ae6a682..0f63512 100644 --- a/Assets/scripts/coa4u/ActionsInterval/ActionMoveBy.cs +++ b/Assets/scripts/coa4u/ActionsInterval/ActionMoveBy.cs @@ -17,7 +17,6 @@ namespace coa4u public ActionMoveBy(Vector2 tgtValue, float tgtDuration) : this((Vector3)tgtValue, tgtDuration) { - is2d = true; } public override ActionInstant clone() diff --git a/Assets/scripts/coa4u/ActionsInterval/ActionMoveTo.cs b/Assets/scripts/coa4u/ActionsInterval/ActionMoveTo.cs index dfcfd22..bf42ad4 100644 --- a/Assets/scripts/coa4u/ActionsInterval/ActionMoveTo.cs +++ b/Assets/scripts/coa4u/ActionsInterval/ActionMoveTo.cs @@ -18,7 +18,7 @@ namespace coa4u public ActionMoveTo(Vector2 tgtValue, float tgtDuration) : this((Vector3)tgtValue, tgtDuration) { - is2d = true; + locks = Axises.z; } public override ActionInstant clone() @@ -29,8 +29,8 @@ namespace coa4u public override void start() { base.start(); - if (is2d) - value.z = transform.position.z; + if (locks != Axises.none) + lockAxises(ref value); path = value - transform.position; } diff --git a/Assets/scripts/coa4u/ActionsInterval/ActionRotateBy.cs b/Assets/scripts/coa4u/ActionsInterval/ActionRotateBy.cs index acbabc3..f75e177 100644 --- a/Assets/scripts/coa4u/ActionsInterval/ActionRotateBy.cs +++ b/Assets/scripts/coa4u/ActionsInterval/ActionRotateBy.cs @@ -17,7 +17,6 @@ namespace coa4u public ActionRotateBy(float angle, float tgtDuration) : this(new Vector3(0, 0, angle), tgtDuration) { - is2d = true; } public override ActionInstant clone() diff --git a/Assets/scripts/coa4u/ActionsInterval/ActionRotateTo.cs b/Assets/scripts/coa4u/ActionsInterval/ActionRotateTo.cs index bfd92a3..9667e61 100644 --- a/Assets/scripts/coa4u/ActionsInterval/ActionRotateTo.cs +++ b/Assets/scripts/coa4u/ActionsInterval/ActionRotateTo.cs @@ -18,7 +18,7 @@ namespace coa4u public ActionRotateTo(float angle, float tgtDuration) : this(new Vector3(0, 0, angle), tgtDuration) { - is2d = true; + locks = Axises.xy; } public override ActionInstant clone() @@ -39,6 +39,8 @@ namespace coa4u else path[i] = t + 360 - f; } + if (locks != Axises.none) + lockAxises(ref path); } public override void stepInterval(float dt) { diff --git a/Assets/scripts/coa4u/ActionsInterval/ActionScaleBy.cs b/Assets/scripts/coa4u/ActionsInterval/ActionScaleBy.cs index ea4d5eb..90d573f 100644 --- a/Assets/scripts/coa4u/ActionsInterval/ActionScaleBy.cs +++ b/Assets/scripts/coa4u/ActionsInterval/ActionScaleBy.cs @@ -18,7 +18,6 @@ namespace coa4u public ActionScaleBy(Vector2 tgtValue, float tgtDuration) : this((Vector3)tgtValue, tgtDuration) { - is2d = true; } public override ActionInstant clone() diff --git a/Assets/scripts/coa4u/ActionsInterval/ActionScaleTo.cs b/Assets/scripts/coa4u/ActionsInterval/ActionScaleTo.cs index b80a11f..de8140c 100644 --- a/Assets/scripts/coa4u/ActionsInterval/ActionScaleTo.cs +++ b/Assets/scripts/coa4u/ActionsInterval/ActionScaleTo.cs @@ -18,7 +18,7 @@ namespace coa4u public ActionScaleTo(Vector2 tgtValue, float tgtDuration) : this((Vector3)tgtValue, tgtDuration) { - is2d = true; + locks = Axises.z; } public override ActionInstant clone() @@ -29,8 +29,8 @@ namespace coa4u public override void start() { base.start(); - if (is2d) - value.z = transform.localScale.z; + if (locks != Axises.none) + lockAxises(ref value); path = value - transform.localScale; } diff --git a/Assets/scripts/coa4u/UnityComponents/Actor.cs b/Assets/scripts/coa4u/UnityComponents/Actor.cs index 612bccd..3fbeb3d 100644 --- a/Assets/scripts/coa4u/UnityComponents/Actor.cs +++ b/Assets/scripts/coa4u/UnityComponents/Actor.cs @@ -16,10 +16,15 @@ public class Actor : MonoBehaviour protected Vector3 angles2prev = Vector3.zero; protected const float coeff = Mathf.PI / 180; protected Vector3[] initialState; + [HideInInspector] public Transform transformCached; + [HideInInspector] public Renderer rendererCached; + [HideInInspector] public Mesh meshCached; + [HideInInspector] public Vector3 skewAngles1; + [HideInInspector] public Vector3 skewAngles2; /// @@ -29,8 +34,13 @@ public class Actor : MonoBehaviour { transformCached = gameObject.transform; rendererCached = gameObject.renderer; - meshCached = gameObject.GetComponent().mesh; - initialState = meshCached.vertices; + + Component component = gameObject.GetComponent(); + if (component != null) + { + meshCached = ((MeshFilter)component).mesh; + initialState = meshCached.vertices; + } } /// @@ -51,6 +61,9 @@ public class Actor : MonoBehaviour /// void updateSkew() { + if (meshCached == null) + return; + Matrix4x4 m = Matrix4x4.zero; m[0, 0] = 1; m[1, 1] = 1; @@ -130,13 +143,21 @@ public class Actor : MonoBehaviour } /// - /// Adds method to cache to speed-up + /// Adds method to cache to speed-up the ActionSendMessage. /// - public void AddMethodToCache(MethodHolder method) + public void AddMethodToCache(Action method) { - methodsCache.Add(method.name, method); + methodsCache.Add(method.Method.Name, new MethodHolder(method)); } + public void AddMethodToCache(Action method) + { + methodsCache.Add(method.Method.Name, new MethodHolder(method)); + } + + /// + /// Adds method from cache. + /// public void RemoveMethodFromCache(string key) { if (methodsCache.ContainsKey(key)) diff --git a/Assets/scripts/coa4u/UnityComponents/ActorSampleActions.cs b/Assets/scripts/coa4u/UnityComponents/ActorSampleActions.cs index 560b518..001a940 100644 --- a/Assets/scripts/coa4u/UnityComponents/ActorSampleActions.cs +++ b/Assets/scripts/coa4u/UnityComponents/ActorSampleActions.cs @@ -39,8 +39,8 @@ public class ActorSampleActions : Actor }), 5); this.AttachAction(seq); - AddMethodToCache(new MethodHolder(msgHello)); - AddMethodToCache(new MethodHolder(msgHelloTo)); + AddMethodToCache(msgHello); + AddMethodToCache(msgHelloTo); } void msgHello()