Fixed a bug with multiple rotations at the same time.

Made sample actions behavior a subclass of the Actor class.

Added new subclass for the Actor - MeshActor. This actor let's the actions to modify the mesh (i.e. Skew).

Added new actions:
ActionSkewBy.cs
ActionRandom.cs
This commit is contained in:
2014-06-21 02:00:27 +04:00
parent 06e25ca6c7
commit 0450492554
10 changed files with 198 additions and 78 deletions

View File

@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using UnityEngine;
class ActionRandom : ActionInterval
{
protected Action[] actions;
protected int index;
public ActionRandom(Action action1, Action action2)
: base(0)
{
actions = new Action[] {action1, action2};
}
public ActionRandom(Action[] actionsList)
: base(0)
{
actions = actionsList;
}
public override Action clone()
{
Action[] aList = new Action[actions.Length];
for (int i = 0; i < actions.Length; i++)
{
aList[i] = actions[i].clone();
}
return new ActionRandom(aList);
}
public override Action reverse()
{
Action[] aList = new Action[actions.Length];
for (int i = 0; i < actions.Length; i++)
{
aList[i] = actions[i].reverse();
}
return new ActionRandom(aList);
}
public override void start()
{
base.start();
index = UnityEngine.Random.Range(0, actions.Length);
actions[index].setActor(target);
actions[index].start();
}
public override void step(float dt)
{
dt *= timeScale;
if (actions[index].isRunning())
actions[index].step(dt);
if (!actions[index].isRunning())
{
stop();
dtr = actions[index].dtr;
}
}
public override void stop()
{
base.stop();
actions[index].stop();
}
}

View File

@@ -26,13 +26,7 @@ class ActionSetRotation : Action
public override void start()
{
base.start();
Vector3 path = new Vector3();
for (int i = 0; i < 3; i++)
{
path[i] = value[i] - transform.rotation.eulerAngles[i];
}
transform.Rotate(Vector3.up, path.y);
transform.Rotate(Vector3.right, path.x);
transform.Rotate(Vector3.forward, path.z);
transform.rotation = Quaternion.Euler(value);
}
}

View File

@@ -32,8 +32,6 @@ class ActionRotateBy : ActionInterval
{
float d = dt / duration;
Vector3 tgt = delta * d;
transform.Rotate(Vector3.up, tgt.y);
transform.Rotate(Vector3.right, tgt.x);
transform.Rotate(Vector3.forward, tgt.z);
transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles + tgt);
}
}

View File

@@ -42,8 +42,6 @@ class ActionRotateTo : ActionInterval
{
float d = dt / duration;
Vector3 tgt = path * d;
transform.Rotate(Vector3.up, tgt.y);
transform.Rotate(Vector3.right, tgt.x);
transform.Rotate(Vector3.forward, tgt.z);
transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles + tgt);
}
}

View File

@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using UnityEngine;
class ActionSkewBy : ActionInterval
{
protected Vector3 skewAngles1;
protected Vector3 skewAngles2;
protected Mesh mesh;
public ActionSkewBy(Vector3 tgtAngles1, Vector3 tgtAngles2, float tgtDuration)
: base(tgtDuration)
{
skewAngles1 = tgtAngles1;
skewAngles2 = tgtAngles2;
}
public override Action clone()
{
return new ActionSkewBy(skewAngles1, skewAngles2, duration);
}
public override Action reverse()
{
return new ActionSkewBy(-skewAngles1, -skewAngles2, duration);
}
public override void start()
{
base.start();
if (!(target is MeshActor))
{
throw new Exception("You should use MeshActor class instead of Actor, if you want to skew your object.");
}
}
public override void stepInterval(float dt)
{
float d = dt / duration;
Vector3 tgt = skewAngles1 * d;
((MeshActor)target).skewAngles1 += tgt;
tgt = skewAngles2 * d;
((MeshActor)target).skewAngles2 += tgt;
}
}

View File

@@ -6,7 +6,7 @@ public class Actor : MonoBehaviour
protected Action action;
private bool paused = false;
void Update()
protected void Update()
{
if (paused || action == null)
return;

View File

@@ -1,7 +1,7 @@
using UnityEngine;
using System.Collections;
public class SampleActions : MonoBehaviour
public class ActorSampleActions : MeshActor
{
public void Start()
{
@@ -10,7 +10,7 @@ public class SampleActions : MonoBehaviour
new ActionFadeIn(2),
new ActionParallel(new Action[] {
new ActionMoveBy(new Vector3(10, 10, 0), 1),
new ActionRotateBy(new Vector3(90, 0, 0), 1),
new ActionRotateBy(new Vector3(90, 90, 0), 1),
new ActionTintBy(new Vector4(-50, 50, -150), 1)
}),
new ActionScaleBy(new Vector3(2, 2, 1), 1),
@@ -18,9 +18,15 @@ public class SampleActions : MonoBehaviour
new ActionDelay(1),
new ActionBlink(5, 0.1F, 0.4F),
new ActionDelay(1),
new ActionSkewBy(Vector3.zero, new Vector3(0, 30, 0), 1),
new ActionDelay(1),
new ActionJumpBy(new Vector3(-10, 0, 0), 1, 4, 1),
new ActionSkewBy(Vector3.zero, new Vector3(30, 0, 30), 1),
new ActionJumpTo(new Vector3(10, 10, 10), 1, 3, 1),
new ActionRotateBy(new Vector3(90, 0, 0), 1),
new ActionSkewBy(Vector3.zero, new Vector3(-30, 0, -30), 1),
new ActionJumpBy(new Vector3(-10, 0, 0), 1, 2, 1),
new ActionSkewBy(Vector3.zero, new Vector3(0, -30, 0), 1),
new ActionDelay(0.5F),
new ActionBezierRel(new Vector2 (5, 0), new Vector2(5, -10), new Vector2 (0, -10), 2),
new ActionScaleTo(new Vector3(2, 2, 2), 1),
@@ -28,6 +34,7 @@ public class SampleActions : MonoBehaviour
new ActionFadeOut(2),
new ActionSetTint(new Vector4(67, 105, 181))
}), 5);
gameObject.SendMessage("AttachAction", seq);
SetTimeScale(2);
this.AttachAction(seq);
}
}

View File

@@ -0,0 +1,54 @@
using UnityEngine;
using System.Collections;
[System.Serializable]
public class MeshActor : Actor
{
public Vector3 skewAngles1;
public Vector3 skewAngles2;
protected Vector3 angles1prev = Vector3.zero;
protected Vector3 angles2prev = Vector3.zero;
protected const float coeff = Mathf.PI/180;
protected Mesh mesh;
protected Vector3[] initialState;
void Awake()
{
mesh = gameObject.GetComponent<MeshFilter>().mesh;
initialState = mesh.vertices;
}
protected void Update()
{
base.Update();
if (skewAngles1 != angles1prev || skewAngles2 != angles2prev)
updateMesh();
}
protected void updateMesh()
{
Matrix4x4 m = Matrix4x4.zero;
m[0, 0] = 1;
m[1, 1] = 1;
m[2, 2] = 1;
m[3, 3] = 1;
m[0, 1] = Mathf.Tan(skewAngles1.x * coeff); // skewing in xy
m[0, 2] = Mathf.Tan(skewAngles2.x * coeff); // skewing in xz
m[1, 0] = Mathf.Tan(skewAngles1.y * coeff); // skewing in yx
m[1, 2] = Mathf.Tan(skewAngles2.y * coeff); // skewing in yz
m[2, 0] = Mathf.Tan(skewAngles1.z * coeff); // skewing in zx
m[2, 1] = Mathf.Tan(skewAngles2.z * coeff); // skewing in zy
Vector3[] verts = new Vector3[initialState.Length];
int i = 0;
while (i < verts.Length)
{
verts[i] = m.MultiplyPoint3x4(initialState[i]);
i++;
}
mesh.vertices = verts;
angles1prev = skewAngles1;
angles2prev = skewAngles2;
}
}

View File

@@ -1,51 +0,0 @@
using UnityEngine;
using System.Collections;
[System.Serializable]
public class SkewModifier : MonoBehaviour
{
public Vector3 angles1;
public Vector3 angles2;
protected Vector3 angles1prev = Vector3.zero;
protected Vector3 angles2prev = Vector3.zero;
protected const float coeff = Mathf.PI/180;
protected Mesh mesh;
void Awake()
{
mesh = gameObject.GetComponent<MeshFilter>().mesh;
}
void Update()
{
if (angles1 != angles1prev || angles2 != angles2prev)
updateMesh();
}
protected void updateMesh()
{
Matrix4x4 m = Matrix4x4.zero;
m[0, 0] = 1;
m[1, 1] = 1;
m[2, 2] = 1;
m[3, 3] = 1;
m[0, 1] = Mathf.Tan(angles1.x * coeff) - Mathf.Tan(angles1prev.x * coeff); // skewing in xy
m[0, 2] = Mathf.Tan(angles2.x * coeff) - Mathf.Tan(angles2prev.x * coeff); // skewing in xz
m[1, 0] = Mathf.Tan(angles1.y * coeff) - Mathf.Tan(angles1prev.y * coeff); // skewing in yx
m[1, 2] = Mathf.Tan(angles2.y * coeff) - Mathf.Tan(angles2prev.y * coeff); // skewing in yz
m[2, 0] = Mathf.Tan(angles1.z * coeff) - Mathf.Tan(angles1prev.z * coeff); // skewing in zx
m[2, 1] = Mathf.Tan(angles2.z * coeff) - Mathf.Tan(angles2prev.z * coeff); // skewing in zy
Vector3[] verts = mesh.vertices;
int i = 0;
while (i < verts.Length)
{
verts[i] = m.MultiplyPoint3x4(verts[i]);
i++;
}
mesh.vertices = verts;
angles1prev = angles1;
angles2prev = angles2;
}
}