Made Actor to work with gameBojects without the MeshFilter component. Made AddMethodToCache simpler.master
@@ -4,11 +4,24 @@ using UnityEngine; | |||||
namespace coa4u | namespace coa4u | ||||
{ | { | ||||
public enum Axises | |||||
{ | |||||
none, | |||||
x, | |||||
y, | |||||
z, | |||||
xy, | |||||
xz, | |||||
yz, | |||||
xyz | |||||
} | |||||
/// <summary> | /// <summary> | ||||
/// Basic action class for subclassing. To inherit interval actions, consider using the ActionInterval as the base class. | /// Basic action class for subclassing. To inherit interval actions, consider using the ActionInterval as the base class. | ||||
/// </summary> | /// </summary> | ||||
public class ActionInstant | public class ActionInstant | ||||
{ | { | ||||
public Axises locks = Axises.none; | |||||
protected Actor target; | protected Actor target; | ||||
protected Transform transform; | protected Transform transform; | ||||
protected Renderer renderer; | protected Renderer renderer; | ||||
@@ -74,6 +87,47 @@ namespace coa4u | |||||
target = actor; | target = actor; | ||||
} | } | ||||
/// <summary> | |||||
/// This method modifies the given vector to keep the locked axises untouched. | |||||
/// </summary> | |||||
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; | |||||
} | |||||
} | |||||
/// <summary> | /// <summary> | ||||
/// Readonly property. Shows the remaining time of the action. | /// Readonly property. Shows the remaining time of the action. | ||||
/// </summary> | /// </summary> | ||||
@@ -21,7 +21,7 @@ namespace coa4u | |||||
public ActionSetDirection(Vector2 tgtValue) | public ActionSetDirection(Vector2 tgtValue) | ||||
: this((Vector3)tgtValue) | : this((Vector3)tgtValue) | ||||
{ | { | ||||
is2d = true; | |||||
locks = Axises.z; | |||||
} | } | ||||
public ActionSetDirection(Actor tgtActor) | public ActionSetDirection(Actor tgtActor) | ||||
@@ -48,11 +48,11 @@ namespace coa4u | |||||
{ | { | ||||
value = other.transform.position; | value = other.transform.position; | ||||
} | } | ||||
if (is2d) | |||||
if (locks != Axises.none) | |||||
{ | { | ||||
value.z = transform.position.z; | value.z = transform.position.z; | ||||
} | } | ||||
transform.rotation = Quaternion.LookRotation(transform.position - value); | |||||
transform.rotation = Quaternion.LookRotation(value - transform.position); | |||||
} | } | ||||
} | } | ||||
} | } |
@@ -20,7 +20,7 @@ namespace coa4u | |||||
public ActionSetPlace(Vector2 tgtPlace) | public ActionSetPlace(Vector2 tgtPlace) | ||||
: this((Vector3)tgtPlace) | : this((Vector3)tgtPlace) | ||||
{ | { | ||||
is2d = true; | |||||
locks = Axises.z; | |||||
} | } | ||||
/// <summary> | /// <summary> | ||||
@@ -37,8 +37,8 @@ namespace coa4u | |||||
public override void start() | public override void start() | ||||
{ | { | ||||
base.start(); | base.start(); | ||||
if (is2d) | |||||
value.z = transform.position.z; | |||||
if (locks != Axises.none) | |||||
lockAxises(ref value); | |||||
transform.position = value; | transform.position = value; | ||||
} | } | ||||
} | } |
@@ -20,7 +20,7 @@ namespace coa4u | |||||
public ActionSetRotation(float angle) | public ActionSetRotation(float angle) | ||||
: this(new Vector3(0, 0, angle)) | : this(new Vector3(0, 0, angle)) | ||||
{ | { | ||||
is2d = true; | |||||
locks = Axises.xy; | |||||
} | } | ||||
/// <summary> | /// <summary> | ||||
@@ -37,6 +37,8 @@ namespace coa4u | |||||
public override void start() | public override void start() | ||||
{ | { | ||||
base.start(); | base.start(); | ||||
if (locks != Axises.none) | |||||
lockAxises(ref value); | |||||
transform.rotation = Quaternion.Euler(value); | transform.rotation = Quaternion.Euler(value); | ||||
} | } | ||||
} | } |
@@ -26,7 +26,7 @@ namespace coa4u | |||||
public ActionBezierAbs(Vector2 tgtStart, Vector2 tgtSCP, Vector2 tgtECP, Vector2 tgtEnd, float tgtDuration) | public ActionBezierAbs(Vector2 tgtStart, Vector2 tgtSCP, Vector2 tgtECP, Vector2 tgtEnd, float tgtDuration) | ||||
: this((Vector3)tgtStart, (Vector3)tgtSCP, (Vector3)tgtECP, (Vector3)tgtEnd, tgtDuration) | : this((Vector3)tgtStart, (Vector3)tgtSCP, (Vector3)tgtECP, (Vector3)tgtEnd, tgtDuration) | ||||
{ | { | ||||
is2d = true; | |||||
locks = Axises.z; | |||||
} | } | ||||
/// <summary> | /// <summary> | ||||
@@ -52,12 +52,12 @@ namespace coa4u | |||||
{ | { | ||||
base.start(); | base.start(); | ||||
float z = transform.position.z; | 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); | |||||
} | } | ||||
} | } | ||||
@@ -28,7 +28,6 @@ namespace coa4u | |||||
public ActionBezierRel(Vector2 tgtSCP, Vector2 tgtECP, Vector2 tgtEnd, float tgtDuration) | public ActionBezierRel(Vector2 tgtSCP, Vector2 tgtECP, Vector2 tgtEnd, float tgtDuration) | ||||
: this((Vector3)tgtSCP, (Vector3)tgtECP, (Vector3)tgtEnd, tgtDuration) | : this((Vector3)tgtSCP, (Vector3)tgtECP, (Vector3)tgtEnd, tgtDuration) | ||||
{ | { | ||||
is2d = true; | |||||
} | } | ||||
/// <summary> | /// <summary> | ||||
@@ -22,7 +22,6 @@ namespace coa4u | |||||
public ActionJumpBy(Vector2 tgtPoint, float tgtHeight, int tgtJumps, float tgtDuration) | public ActionJumpBy(Vector2 tgtPoint, float tgtHeight, int tgtJumps, float tgtDuration) | ||||
: this((Vector3)tgtPoint, tgtHeight, tgtJumps, tgtDuration) | : this((Vector3)tgtPoint, tgtHeight, tgtJumps, tgtDuration) | ||||
{ | { | ||||
is2d = true; | |||||
} | } | ||||
public override ActionInstant clone() | public override ActionInstant clone() | ||||
@@ -22,7 +22,7 @@ namespace coa4u | |||||
public ActionJumpTo(Vector2 tgtPoint, float tgtHeight, int tgtJumps, float tgtDuration) | public ActionJumpTo(Vector2 tgtPoint, float tgtHeight, int tgtJumps, float tgtDuration) | ||||
: this((Vector3)tgtPoint, tgtHeight, tgtJumps, tgtDuration) | : this((Vector3)tgtPoint, tgtHeight, tgtJumps, tgtDuration) | ||||
{ | { | ||||
is2d = true; | |||||
locks = Axises.z; | |||||
} | } | ||||
public override ActionInstant clone() | public override ActionInstant clone() | ||||
@@ -33,8 +33,8 @@ namespace coa4u | |||||
public override void start() | public override void start() | ||||
{ | { | ||||
float coeff = 1F / jumps; | 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 start = target.gameObject.transform.position; | ||||
Vector3 end = (point - start) * coeff; | Vector3 end = (point - start) * coeff; | ||||
Vector3 cp1 = Vector3.up * height; | Vector3 cp1 = Vector3.up * height; | ||||
@@ -17,7 +17,6 @@ namespace coa4u | |||||
public ActionMoveBy(Vector2 tgtValue, float tgtDuration) | public ActionMoveBy(Vector2 tgtValue, float tgtDuration) | ||||
: this((Vector3)tgtValue, tgtDuration) | : this((Vector3)tgtValue, tgtDuration) | ||||
{ | { | ||||
is2d = true; | |||||
} | } | ||||
public override ActionInstant clone() | public override ActionInstant clone() | ||||
@@ -18,7 +18,7 @@ namespace coa4u | |||||
public ActionMoveTo(Vector2 tgtValue, float tgtDuration) | public ActionMoveTo(Vector2 tgtValue, float tgtDuration) | ||||
: this((Vector3)tgtValue, tgtDuration) | : this((Vector3)tgtValue, tgtDuration) | ||||
{ | { | ||||
is2d = true; | |||||
locks = Axises.z; | |||||
} | } | ||||
public override ActionInstant clone() | public override ActionInstant clone() | ||||
@@ -29,8 +29,8 @@ namespace coa4u | |||||
public override void start() | public override void start() | ||||
{ | { | ||||
base.start(); | base.start(); | ||||
if (is2d) | |||||
value.z = transform.position.z; | |||||
if (locks != Axises.none) | |||||
lockAxises(ref value); | |||||
path = value - transform.position; | path = value - transform.position; | ||||
} | } | ||||
@@ -17,7 +17,6 @@ namespace coa4u | |||||
public ActionRotateBy(float angle, float tgtDuration) | public ActionRotateBy(float angle, float tgtDuration) | ||||
: this(new Vector3(0, 0, angle), tgtDuration) | : this(new Vector3(0, 0, angle), tgtDuration) | ||||
{ | { | ||||
is2d = true; | |||||
} | } | ||||
public override ActionInstant clone() | public override ActionInstant clone() | ||||
@@ -18,7 +18,7 @@ namespace coa4u | |||||
public ActionRotateTo(float angle, float tgtDuration) | public ActionRotateTo(float angle, float tgtDuration) | ||||
: this(new Vector3(0, 0, angle), tgtDuration) | : this(new Vector3(0, 0, angle), tgtDuration) | ||||
{ | { | ||||
is2d = true; | |||||
locks = Axises.xy; | |||||
} | } | ||||
public override ActionInstant clone() | public override ActionInstant clone() | ||||
@@ -39,6 +39,8 @@ namespace coa4u | |||||
else | else | ||||
path[i] = t + 360 - f; | path[i] = t + 360 - f; | ||||
} | } | ||||
if (locks != Axises.none) | |||||
lockAxises(ref path); | |||||
} | } | ||||
public override void stepInterval(float dt) | public override void stepInterval(float dt) | ||||
{ | { | ||||
@@ -18,7 +18,6 @@ namespace coa4u | |||||
public ActionScaleBy(Vector2 tgtValue, float tgtDuration) | public ActionScaleBy(Vector2 tgtValue, float tgtDuration) | ||||
: this((Vector3)tgtValue, tgtDuration) | : this((Vector3)tgtValue, tgtDuration) | ||||
{ | { | ||||
is2d = true; | |||||
} | } | ||||
public override ActionInstant clone() | public override ActionInstant clone() | ||||
@@ -18,7 +18,7 @@ namespace coa4u | |||||
public ActionScaleTo(Vector2 tgtValue, float tgtDuration) | public ActionScaleTo(Vector2 tgtValue, float tgtDuration) | ||||
: this((Vector3)tgtValue, tgtDuration) | : this((Vector3)tgtValue, tgtDuration) | ||||
{ | { | ||||
is2d = true; | |||||
locks = Axises.z; | |||||
} | } | ||||
public override ActionInstant clone() | public override ActionInstant clone() | ||||
@@ -29,8 +29,8 @@ namespace coa4u | |||||
public override void start() | public override void start() | ||||
{ | { | ||||
base.start(); | base.start(); | ||||
if (is2d) | |||||
value.z = transform.localScale.z; | |||||
if (locks != Axises.none) | |||||
lockAxises(ref value); | |||||
path = value - transform.localScale; | path = value - transform.localScale; | ||||
} | } | ||||
@@ -16,10 +16,15 @@ public class Actor : MonoBehaviour | |||||
protected Vector3 angles2prev = Vector3.zero; | protected Vector3 angles2prev = Vector3.zero; | ||||
protected const float coeff = Mathf.PI / 180; | protected const float coeff = Mathf.PI / 180; | ||||
protected Vector3[] initialState; | protected Vector3[] initialState; | ||||
[HideInInspector] | |||||
public Transform transformCached; | public Transform transformCached; | ||||
[HideInInspector] | |||||
public Renderer rendererCached; | public Renderer rendererCached; | ||||
[HideInInspector] | |||||
public Mesh meshCached; | public Mesh meshCached; | ||||
[HideInInspector] | |||||
public Vector3 skewAngles1; | public Vector3 skewAngles1; | ||||
[HideInInspector] | |||||
public Vector3 skewAngles2; | public Vector3 skewAngles2; | ||||
/// <summary> | /// <summary> | ||||
@@ -29,8 +34,13 @@ public class Actor : MonoBehaviour | |||||
{ | { | ||||
transformCached = gameObject.transform; | transformCached = gameObject.transform; | ||||
rendererCached = gameObject.renderer; | rendererCached = gameObject.renderer; | ||||
meshCached = gameObject.GetComponent<MeshFilter>().mesh; | |||||
initialState = meshCached.vertices; | |||||
Component component = gameObject.GetComponent<MeshFilter>(); | |||||
if (component != null) | |||||
{ | |||||
meshCached = ((MeshFilter)component).mesh; | |||||
initialState = meshCached.vertices; | |||||
} | |||||
} | } | ||||
/// <summary> | /// <summary> | ||||
@@ -51,6 +61,9 @@ public class Actor : MonoBehaviour | |||||
/// </summary> | /// </summary> | ||||
void updateSkew() | void updateSkew() | ||||
{ | { | ||||
if (meshCached == null) | |||||
return; | |||||
Matrix4x4 m = Matrix4x4.zero; | Matrix4x4 m = Matrix4x4.zero; | ||||
m[0, 0] = 1; | m[0, 0] = 1; | ||||
m[1, 1] = 1; | m[1, 1] = 1; | ||||
@@ -130,13 +143,21 @@ public class Actor : MonoBehaviour | |||||
} | } | ||||
/// <summary> | /// <summary> | ||||
/// Adds method to cache to speed-up | |||||
/// Adds method to cache to speed-up the ActionSendMessage. | |||||
/// </summary> | /// </summary> | ||||
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<T>(Action<T> method) | |||||
{ | |||||
methodsCache.Add(method.Method.Name, new MethodHolder<T>(method)); | |||||
} | |||||
/// <summary> | |||||
/// Adds method from cache. | |||||
/// </summary> | |||||
public void RemoveMethodFromCache(string key) | public void RemoveMethodFromCache(string key) | ||||
{ | { | ||||
if (methodsCache.ContainsKey(key)) | if (methodsCache.ContainsKey(key)) | ||||
@@ -39,8 +39,8 @@ public class ActorSampleActions : Actor | |||||
}), 5); | }), 5); | ||||
this.AttachAction(seq); | this.AttachAction(seq); | ||||
AddMethodToCache(new MethodHolder(msgHello)); | |||||
AddMethodToCache(new MethodHolder<string>(msgHelloTo)); | |||||
AddMethodToCache(msgHello); | |||||
AddMethodToCache<string>(msgHelloTo); | |||||
} | } | ||||
void msgHello() | void msgHello() | ||||