Moved all actions to coa4u namespace.

Added methods caching for quick message processing.
Integrated MeshActor into Actor.

new Actions:
ActionStop.cs
ActionSetDirection.cs
This commit is contained in:
2014-06-22 02:57:08 +04:00
parent 5f5d7b423e
commit 2a5f20879b
40 changed files with 1801 additions and 1155 deletions

View File

@@ -63,8 +63,6 @@ Base actions
Interval actions Interval actions
- [ ] Follow - follows another actor with the given speed for the given amount of time (of forever). - [ ] Follow - follows another actor with the given speed for the given amount of time (of forever).
- [ ] LookAt - rotates the object to look at the given actor. - [ ] LookAt - rotates the object to look at the given actor.
*These two actions requires the using of MeshActor script instead of Actor.*
- [x] SkewBy - skews the mesh by given values. - [x] SkewBy - skews the mesh by given values.
- [ ] SkewTo - skews the mesh to the given values. - [ ] SkewTo - skews the mesh to the given values.

View File

@@ -2,80 +2,124 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public class Action namespace coa4u
{ {
protected Actor target;
public float duration = 0;
protected Transform transform;
protected Renderer renderer;
protected bool is2d = false;
public Action()
{
}
public virtual Action clone()
{
return new Action();
}
public virtual bool isRunning()
{
return false;
}
/// <summary> /// <summary>
/// This method is called at the action start. Usable for instant and interval actions. /// Basic action class for subclassing. To inherit interval actions, consider using the ActionInterval as the base class.
/// </summary> /// </summary>
public virtual Action reverse() public class ActionInstant
{ {
throw new Exception("Can reverse only the reversable interval actions"); protected Actor target;
} protected Transform transform;
protected Renderer renderer;
protected bool is2d = false;
private float durationValue = 0;
private float dtrValue = 0;
private bool isRunning = false;
/// <summary> public ActionInstant()
/// This method is called at the action start. Usable for instant and interval actions.
/// </summary>
public virtual void start()
{
if (target == null)
throw new Exception("Can start the action only after it's atached to the actor");
transform = target.gameObject.transform;
renderer = target.gameObject.renderer;
}
/// <summary>
/// This method is called at every frame update. Not usable for instant actions.
/// </summary>
public virtual void step(float dt)
{
if (target == null)
throw new Exception("Can update the action only after it's atached to the actor");
}
/// <summary>
/// This method is called after the interval action is stopped. Not usable for instant actions.
/// </summary>
public virtual void stop()
{
if (target == null)
throw new Exception("Can stop the action only after it's atached to the actor");
}
public void setActor(Actor actor)
{
target = actor;
}
public virtual float dtr
{
get
{
return 0;
}
protected set
{ {
} }
}
/// <summary>
/// Returns a copy of the action.
/// </summary>
public virtual ActionInstant clone()
{
return new ActionInstant();
}
/// <summary>
/// Returns the reversed version of the action, if it is possible.
/// </summary>
public virtual ActionInstant reverse()
{
throw new Exception("Can reverse only the reversable interval actions");
}
/// <summary>
/// This method is called at the action start. Usable for instant and interval actions.
/// </summary>
public virtual void start()
{
if (target == null)
throw new Exception("Can start the action only after it's atached to the actor");
transform = target.transformCached;
renderer = target.rendererCached;
}
/// <summary>
/// This method is called at every frame update. Not usable for instant actions.
/// </summary>
public virtual void step(float dt)
{
if (target == null)
throw new Exception("Can update the action only after it's atached to the actor");
}
/// <summary>
/// This method is called after the interval action is stopped. Not usable for instant actions.
/// </summary>
public virtual void stop()
{
if (target == null)
throw new Exception("Can stop the action only after it's atached to the actor");
}
/// <summary>
/// This method sets the actor for the action.
/// </summary>
public void setActor(Actor actor)
{
target = actor;
}
/// <summary>
/// Readonly property. Shows the remaining time of the action.
/// </summary>
public float duration
{
get
{
return durationValue;
}
protected set
{
durationValue = value;
}
}
/// <summary>
/// Readonly property. Shows if the action is running or not.
/// </summary>
public bool running
{
get
{
return isRunning;
}
protected set
{
isRunning = value;
}
}
/// <summary>
/// Readonly property. Contains the remaining tick time after action finished.
/// </summary>
public float dtr
{
get
{
return dtrValue;
}
protected set
{
dtrValue = value;
}
}
}
} }

View File

@@ -2,102 +2,98 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
class ActionInterval : Action namespace coa4u
{ {
protected float timer;
protected float timeScale;
protected bool running;
private float dtrdata;
public ActionInterval(float tgtDuration)
: base()
{
duration = tgtDuration;
timeScale = 1F;
dtr = 0;
}
public override Action clone()
{
return new ActionInterval(duration);
}
public override Action reverse()
{
throw new Exception("Can reverse only the reversable interval actions");
}
public override bool isRunning()
{
return running;
}
public override void start()
{
base.start();
running = true;
timer = 0F;
}
public override void stop()
{
base.stop();
running = false;
}
/// <summary> /// <summary>
/// Step method for interval actions. Don't override this method, when inheriting, put your code in stepInterval instead. /// Interval action class for subclassing.
/// </summary> /// </summary>
public override void step(float dt) public class ActionInterval : ActionInstant
{ {
dt *= timeScale; protected float timer;
base.step(dt); protected float timeScale;
if (timer + dt > duration) private float dtrdata;
{
float odt = dt;
dt = duration - timer;
timer += odt;
}
else
{
timer += dt;
}
stepInterval(dt);
if (timer > duration)
{
stop();
dtr = timer - duration;
}
}
/// <summary> public ActionInterval(float tgtDuration)
/// Step method for interval actions. Put your code here. : base()
/// </summary>
public virtual void stepInterval(float dt)
{
}
/// <summary>
/// Property to get the remaining tick time after the action has ended.
/// </summary>
public override float dtr
{
get
{ {
return dtrdata; duration = tgtDuration;
timeScale = 1F;
dtr = 0;
} }
protected set /// <summary>
/// Returns a copy of the action.
/// </summary>
public override ActionInstant clone()
{ {
dtrdata = value; return new ActionInterval(duration);
} }
}
/// <summary> /// <summary>
/// Immediately changes the time scale for this action and all nested ones. /// Returns the reversed version of the action, if it is possible.
/// </summary> /// </summary>
public void setTimeScale(float ts) public override ActionInstant reverse()
{ {
timeScale = ts; throw new Exception("Can reverse only the reversable interval actions");
}
/// <summary>
/// This method is called at the action start.
/// </summary>
public override void start()
{
base.start();
running = true;
timer = 0F;
}
/// <summary>
/// This method is called after the interval action is stopped.
/// </summary>
public override void stop()
{
base.stop();
running = false;
}
/// <summary>
/// This method is called every frame update. Don't override this method, when inheriting, put your code in stepInterval instead.
/// </summary>
public override void step(float dt)
{
dt *= timeScale;
base.step(dt);
if (timer + dt > duration)
{
float odt = dt;
dt = duration - timer;
timer += odt;
}
else
{
timer += dt;
}
stepInterval(dt);
if (timer > duration)
{
stop();
dtr = timer - duration;
}
}
/// <summary>
/// This method is called every frame update. Put your code here.
/// </summary>
public virtual void stepInterval(float dt)
{
}
/// <summary>
/// Immediately changes the time scale for this action and all nested ones.
/// </summary>
public void setTimeScale(float ts)
{
timeScale = ts;
}
} }
} }

View File

@@ -2,84 +2,105 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
class ActionParallel : ActionInterval namespace coa4u
{ {
protected Action[] actions; /// <summary>
/// Runs several actions at the same time.
public ActionParallel(Action action1, Action action2) /// </summary>
: base(0) public class ActionParallel : ActionInterval
{ {
actions = new Action[] {action1, action2}; protected ActionInstant[] actions;
}
public ActionParallel(Action[] actionsList) public ActionParallel(ActionInstant action1, ActionInstant action2)
: base(0) : 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(); actions = new ActionInstant[] { action1, action2 };
} }
return new ActionSequence(aList);
}
public override Action reverse() public ActionParallel(ActionInstant[] actionsList)
{ : base(0)
Action[] aList = new Action[actions.Length];
for (int i = 0; i < actions.Length; i++)
{ {
aList[i] = actions[i].reverse(); actions = actionsList;
}
return new ActionSequence(aList);
}
public override void start()
{
base.start();
for (int i = 0; i < actions.Length; i++)
{
actions[i].setActor(target);
actions[i].start();
} }
}
public override void step(float dt) /// <summary>
{ /// Returns a copy of the action.
dt *= timeScale; /// </summary>
for (int i = 0; i < actions.Length; i++) public override ActionInstant clone()
{ {
if (actions[i].isRunning()) ActionInstant[] aList = new ActionInstant[actions.Length];
actions[i].step(dt); for (int i = 0; i < actions.Length; i++)
}
bool canStopNow = true;
float dtrdata = 0;
for (int i = 0; i < actions.Length; i++)
{
if (actions[i].isRunning())
{ {
canStopNow = false; aList[i] = actions[i].clone();
dtrdata = Math.Max(actions[i].dtr, dtrdata); }
return new ActionSequence(aList);
}
/// <summary>
/// Returns the reversed version of the action, if it is possible.
/// </summary>
public override ActionInstant reverse()
{
ActionInstant[] aList = new ActionInstant[actions.Length];
for (int i = 0; i < actions.Length; i++)
{
aList[i] = actions[i].reverse();
}
return new ActionSequence(aList);
}
/// <summary>
/// This method is called at the action start.
/// </summary>
public override void start()
{
base.start();
for (int i = 0; i < actions.Length; i++)
{
actions[i].setActor(target);
actions[i].start();
} }
} }
if (canStopNow)
{
stop();
dtr = dtrdata;
}
}
public override void stop() /// <summary>
{ /// This method is called every frame update.
base.stop(); /// </summary>
for (int i = 0; i < actions.Length; i++) public override void step(float dt)
{ {
actions[i].stop(); dt *= timeScale;
for (int i = 0; i < actions.Length; i++)
{
if (actions[i].running)
actions[i].step(dt);
}
bool canStopNow = true;
float dtrdata = 0;
for (int i = 0; i < actions.Length; i++)
{
if (actions[i].running)
{
canStopNow = false;
dtrdata = Math.Max(actions[i].dtr, dtrdata);
}
}
if (canStopNow)
{
stop();
dtr = dtrdata;
}
}
/// <summary>
/// This method is called after the interval action is stopped.
/// </summary>
public override void stop()
{
base.stop();
for (int i = 0; i < actions.Length; i++)
{
actions[i].stop();
}
} }
} }
} }

View File

@@ -2,68 +2,89 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
class ActionRandom : ActionInterval namespace coa4u
{ {
/// <summary>
protected Action[] actions; /// Runs one random action from the given list.
protected int index; /// </summary>
class ActionRandom : ActionInterval
public ActionRandom(Action action1, Action action2)
: base(0)
{ {
actions = new Action[] {action1, action2};
}
public ActionRandom(Action[] actionsList) protected ActionInstant[] actions;
: base(0) protected int index;
{
actions = actionsList;
} public ActionRandom(ActionInstant action1, ActionInstant action2)
: base(0)
public override Action clone()
{
Action[] aList = new Action[actions.Length];
for (int i = 0; i < actions.Length; i++)
{ {
aList[i] = actions[i].clone(); actions = new ActionInstant[] { action1, action2 };
} }
return new ActionRandom(aList);
}
public override Action reverse() public ActionRandom(ActionInstant[] actionsList)
{ : base(0)
Action[] aList = new Action[actions.Length];
for (int i = 0; i < actions.Length; i++)
{ {
aList[i] = actions[i].reverse(); actions = actionsList;
} }
return new ActionRandom(aList);
}
public override void start() /// <summary>
{ /// Returns a copy of the action.
base.start(); /// </summary>
index = UnityEngine.Random.Range(0, actions.Length); public override ActionInstant clone()
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(); ActionInstant[] aList = new ActionInstant[actions.Length];
dtr = actions[index].dtr; for (int i = 0; i < actions.Length; i++)
{
aList[i] = actions[i].clone();
}
return new ActionRandom(aList);
} }
}
public override void stop() /// <summary>
{ /// Returns the reversed version of the action, if it is possible.
base.stop(); /// </summary>
actions[index].stop(); public override ActionInstant reverse()
{
ActionInstant[] aList = new ActionInstant[actions.Length];
for (int i = 0; i < actions.Length; i++)
{
aList[i] = actions[i].reverse();
}
return new ActionRandom(aList);
}
/// <summary>
/// This method is called at the action start.
/// </summary>
public override void start()
{
base.start();
index = UnityEngine.Random.Range(0, actions.Length);
actions[index].setActor(target);
actions[index].start();
}
/// <summary>
/// This method is called every frame update.
/// </summary>
public override void step(float dt)
{
dt *= timeScale;
if (actions[index].running)
actions[index].step(dt);
if (!actions[index].running)
{
stop();
dtr = actions[index].dtr;
}
}
/// <summary>
/// This method is called after the interval action is stopped.
/// </summary>
public override void stop()
{
base.stop();
actions[index].stop();
}
} }
} }

View File

@@ -2,68 +2,86 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
class ActionRepeat : ActionInterval namespace coa4u
{ {
protected ActionInterval action; /// <summary>
protected int count; /// Runs the given action the several times. Also can repeat the action forever.
protected int counter; /// </summary>
protected bool forever; class ActionRepeat : ActionInterval
public ActionRepeat(ActionInterval tgtAction, int tgtCount)
: base(0)
{ {
action = tgtAction; protected ActionInterval action;
count = tgtCount; protected int count;
counter = 0; protected int counter;
forever = false; protected bool forever;
}
public ActionRepeat(ActionInterval tgtAction) public ActionRepeat(ActionInterval tgtAction, int tgtCount)
: base(0) : base(0)
{
action = tgtAction;
count = 0;
counter = 0;
forever = true;
}
public override Action clone()
{
return new ActionRepeat((ActionInterval) action.clone(), count);
}
public override Action reverse()
{
return new ActionRepeat((ActionInterval) action.reverse(), count);
}
public override void start()
{
base.start();
action.setActor(target);
action.start();
counter = 0;
}
public override void step(float dt)
{
dt *= timeScale;
if (action.isRunning())
{ {
action.step(dt); action = tgtAction;
count = tgtCount;
counter = 0;
forever = false;
} }
if (!action.isRunning() && (forever || counter < count - 1))
public ActionRepeat(ActionInterval tgtAction)
: base(0)
{ {
float dtrdata = action.dtr; action = tgtAction;
count = 0;
counter = 0;
forever = true;
}
/// <summary>
/// Returns a copy of the action.
/// </summary>
public override ActionInstant clone()
{
return new ActionRepeat((ActionInterval)action.clone(), count);
}
/// <summary>
/// Returns the reversed version of the action, if it is possible.
/// </summary>
public override ActionInstant reverse()
{
return new ActionRepeat((ActionInterval)action.reverse(), count);
}
/// <summary>
/// This method is called at the action start.
/// </summary>
public override void start()
{
base.start();
action.setActor(target);
action.start(); action.start();
if (dtrdata > 0) counter = 0;
action.step(dtrdata);
counter += 1;
} }
else if (!action.isRunning() && counter >= count - 1)
/// <summary>
/// This method is called every frame update.
/// </summary>
public override void step(float dt)
{ {
dtr = action.dtr; dt *= timeScale;
stop(); if (action.running)
{
action.step(dt);
}
if (!action.running && (forever || counter < count - 1))
{
float dtrdata = action.dtr;
action.start();
if (dtrdata > 0)
action.step(dtrdata);
counter += 1;
}
else if (!action.running && counter >= count - 1)
{
dtr = action.dtr;
stop();
}
} }
} }
} }

View File

@@ -2,89 +2,110 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
class ActionSequence : ActionInterval namespace coa4u
{ {
protected Action[] actions; /// <summary>
protected int index; /// Runs several actions in a sequence.
/// </summary>
public ActionSequence(Action action1, Action action2) class ActionSequence : ActionInterval
: base(0)
{ {
actions = new Action[] {action1, action2}; protected ActionInstant[] actions;
} protected int index;
public ActionSequence(Action[] actionsList) public ActionSequence(ActionInstant action1, ActionInstant action2)
: base(0) : 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(); actions = new ActionInstant[] { action1, action2 };
} }
return new ActionSequence(aList);
}
public override Action reverse() public ActionSequence(ActionInstant[] actionsList)
{ : base(0)
Action[] aList = new Action[actions.Length];
for (int i = 0; i < actions.Length; i++)
{ {
aList[actions.Length - 1 - i] = actions[i].reverse(); actions = actionsList;
}
return new ActionSequence(aList);
}
public override void start()
{
base.start();
index = 0;
actions[0].setActor(target);
actions[0].start();
while (!actions[index].isRunning() && index < actions.Length)
{
index += 1;
actions[index].setActor(target);
actions[index].start();
} }
}
public override void step(float dt) /// <summary>
{ /// Returns a copy of the action.
dt *= timeScale; /// </summary>
float dtrdata = 0; public override ActionInstant clone()
if (actions[index].isRunning())
{ {
actions[index].step(dt); ActionInstant[] aList = new ActionInstant[actions.Length];
if (!actions[index].isRunning()) for (int i = 0; i < actions.Length; i++)
dtrdata = actions[index].dtr; {
aList[i] = actions[i].clone();
}
return new ActionSequence(aList);
} }
while (!actions[index].isRunning() && index < actions.Length - 1)
{
index += 1;
actions[index].setActor(target);
actions[index].start();
if (actions[index].isRunning() && dtrdata > 0)
actions[index].step(dtrdata);
}
if (!actions[index].isRunning())
{
stop();
dtr = dtrdata;
}
}
public override void stop() /// <summary>
{ /// Returns the reversed version of the action, if it is possible.
base.stop(); /// </summary>
for (int i = 0; i < actions.Length; i++) public override ActionInstant reverse()
{ {
actions[i].stop(); ActionInstant[] aList = new ActionInstant[actions.Length];
for (int i = 0; i < actions.Length; i++)
{
aList[actions.Length - 1 - i] = actions[i].reverse();
}
return new ActionSequence(aList);
}
/// <summary>
/// This method is called at the action start.
/// </summary>
public override void start()
{
base.start();
index = 0;
actions[0].setActor(target);
actions[0].start();
while (!actions[index].running && index < actions.Length)
{
index += 1;
actions[index].setActor(target);
actions[index].start();
}
}
/// <summary>
/// This method is called every frame update.
/// </summary>
public override void step(float dt)
{
dt *= timeScale;
float dtrdata = 0;
if (actions[index].running)
{
actions[index].step(dt);
if (!actions[index].running)
dtrdata = actions[index].dtr;
}
while (!actions[index].running && index < actions.Length - 1)
{
index += 1;
actions[index].setActor(target);
actions[index].start();
if (actions[index].running && dtrdata > 0)
actions[index].step(dtrdata);
}
if (!actions[index].running)
{
stop();
dtr = dtrdata;
}
}
/// <summary>
/// This method is called after the interval action is stopped.
/// </summary>
public override void stop()
{
base.stop();
for (int i = 0; i < actions.Length; i++)
{
actions[i].stop();
}
} }
} }
} }

View File

@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace coa4u
{
/// <summary>
/// This action stops all actions for the current target.
/// </summary>
class ActionStop : ActionInstant
{
public ActionStop()
: base()
{
}
/// <summary>
/// Returns a copy of the action.
/// </summary>
public override ActionInstant clone()
{
return new ActionStop();
}
/// <summary>
/// This method is called at the action start.
/// </summary>
public override void start()
{
base.start();
target.StopAction();
}
}
}

View File

@@ -2,22 +2,42 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
class ActionHide : Action namespace coa4u
{ {
/// <summary>
public ActionHide() /// Instantly hides the target. This action does not require the transparency support in shaders.
: base() /// </summary>
class ActionHide : ActionInstant
{ {
}
public override Action clone() public ActionHide()
{ : base()
return new ActionHide(); {
} }
public override void start() /// <summary>
{ /// Returns a copy of the action.
base.start(); /// </summary>
renderer.enabled = false; public override ActionInstant clone()
{
return new ActionHide();
}
/// <summary>
/// Returns the reversed version of the action, if it is possible.
/// </summary>
public override ActionInstant reverse()
{
return new ActionShow();
}
/// <summary>
/// This method is called at the action start.
/// </summary>
public override void start()
{
base.start();
renderer.enabled = false;
}
} }
} }

View File

@@ -2,19 +2,44 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
class ActionLog : Action namespace coa4u
{ {
string message; /// <summary>
/// Prints a message in Unity debug console.
public ActionLog(string tgtMessage) /// </summary>
: base() class ActionLog : ActionInstant
{ {
message = tgtMessage; string message;
}
public override void start() public ActionLog(string tgtMessage)
{ : base()
base.start(); {
Debug.Log(message); message = tgtMessage;
}
/// <summary>
/// Returns a copy of the action.
/// </summary>
public override ActionInstant clone()
{
return new ActionLog(message);
}
/// <summary>
/// Returns the reversed version of the action, if it is possible.
/// </summary>
public override ActionInstant reverse()
{
return new ActionLog(message);
}
/// <summary>
/// This method is called at the action start.
/// </summary>
public override void start()
{
base.start();
Debug.Log(message);
}
} }
} }

View File

@@ -2,43 +2,70 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
class ActionSendMessage : Action namespace coa4u
{ {
protected string message; /// <summary>
protected object param; /// Sends the message to the current target. First, it checks the Actor's methods cache.
protected SendMessageOptions options = SendMessageOptions.DontRequireReceiver; /// If the receiving method found in cache, it will be called directly without passing the message to the GameObject.
/// If there's no listener in cache, message will be sent GameObject (and Unity does it VERY slow).
public ActionSendMessage(string tgtMessage) /// </summary>
: base() class ActionSendMessage : ActionInstant
{ {
message = tgtMessage; protected string message;
} protected object param;
protected SendMessageOptions options = SendMessageOptions.DontRequireReceiver;
public ActionSendMessage(string tgtMessage, object tgtParam) public ActionSendMessage(string tgtMessage)
: base() : base()
{
message = tgtMessage;
param = tgtParam;
}
public ActionSendMessage(string tgtMessage, object tgtParam, SendMessageOptions tgtOptions)
: base()
{
message = tgtMessage;
param = tgtParam;
options = tgtOptions;
}
public override void start()
{
base.start();
if (param != null)
{ {
target.SendMessage(message, param); message = tgtMessage;
} }
else
public ActionSendMessage(string tgtMessage, object tgtParam)
: base()
{ {
target.SendMessage(message); message = tgtMessage;
param = tgtParam;
}
public ActionSendMessage(string tgtMessage, object tgtParam, SendMessageOptions tgtOptions)
: base()
{
message = tgtMessage;
param = tgtParam;
options = tgtOptions;
}
/// <summary>
/// Returns a copy of the action.
/// </summary>
public override ActionInstant clone()
{
return new ActionSendMessage(message, param, options);
}
/// <summary>
/// Returns the reversed version of the action, if it is possible.
/// </summary>
public override ActionInstant reverse()
{
return new ActionSendMessage(message, param, options);
}
/// <summary>
/// This method is called at the action start.
/// </summary>
public override void start()
{
base.start();
if (param != null)
{
target.ReceiveMessage(message, param, options);
}
else
{
target.ReceiveMessage(message, options);
}
} }
} }
} }

View File

@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace coa4u
{
/// <summary>
/// Instantly rotates the target to face the given point or actor.
/// </summary>
class ActionSetDirection : ActionInstant
{
protected Vector3 value;
protected Actor other;
public ActionSetDirection(Vector3 tgtValue)
: base()
{
value = tgtValue;
}
public ActionSetDirection(Vector2 tgtValue)
: this((Vector3)tgtValue)
{
is2d = true;
}
public ActionSetDirection(Actor tgtActor)
: base()
{
other = tgtActor;
}
/// <summary>
/// Returns a copy of the action.
/// </summary>
public override ActionInstant clone()
{
return new ActionSetDirection(value);
}
/// <summary>
/// This method is called at the action start.
/// </summary>
public override void start()
{
base.start();
if (other != null)
{
value = other.transform.position;
}
if (is2d)
{
value.z = transform.position.z;
}
transform.rotation = Quaternion.LookRotation(transform.position - value);
}
}
}

View File

@@ -2,32 +2,44 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
class ActionSetPlace : Action namespace coa4u
{ {
protected Vector3 value; /// <summary>
/// Instantly moves the target to the given point.
public ActionSetPlace(Vector3 tgtPlace) /// </summary>
: base() class ActionSetPlace : ActionInstant
{ {
value = tgtPlace; protected Vector3 value;
}
public ActionSetPlace(Vector2 tgtPlace) public ActionSetPlace(Vector3 tgtPlace)
: this((Vector3) tgtPlace) : base()
{ {
is2d = true; value = tgtPlace;
} }
public override Action clone() public ActionSetPlace(Vector2 tgtPlace)
{ : this((Vector3)tgtPlace)
return new ActionSetPlace(value); {
} is2d = true;
}
public override void start() /// <summary>
{ /// Returns a copy of the action.
base.start(); /// </summary>
if (is2d) public override ActionInstant clone()
value.z = transform.position.z; {
transform.position = value; return new ActionSetPlace(value);
}
/// <summary>
/// This method is called at the action start.
/// </summary>
public override void start()
{
base.start();
if (is2d)
value.z = transform.position.z;
transform.position = value;
}
} }
} }

View File

@@ -2,31 +2,42 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
class ActionSetRotation : Action namespace coa4u
{ {
protected Vector3 value; /// <summary>
/// Instantly rotates the target.
public ActionSetRotation(Vector3 tgtValue) /// </summary>
: base() class ActionSetRotation : ActionInstant
{ {
value = tgtValue; protected Vector3 value;
}
public ActionSetRotation(float angle) public ActionSetRotation(Vector3 tgtValue)
: this(new Vector3(0, 0, angle)) : base()
{ {
is2d = true; value = tgtValue;
} }
public override Action clone() public ActionSetRotation(float angle)
{ : this(new Vector3(0, 0, angle))
return new ActionSetRotation(value); {
} is2d = true;
}
public override void start() /// <summary>
{ /// Returns a copy of the action.
base.start(); /// </summary>
transform.rotation = Quaternion.Euler(value); public override ActionInstant clone()
{
return new ActionSetRotation(value);
}
/// <summary>
/// This method is called at the action start.
/// </summary>
public override void start()
{
base.start();
transform.rotation = Quaternion.Euler(value);
}
} }
} }

View File

@@ -2,20 +2,37 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
class ActionSetTint : Action namespace coa4u
{ {
public Vector4 color; /// <summary>
protected const float coeff = 1F / 255F; /// Instantly tints the target to the given color.
/// </summary>
public ActionSetTint(Vector4 tgtColor) class ActionSetTint : ActionInstant
: base()
{ {
color = tgtColor * coeff; public Vector4 color;
} protected const float coeff = 1F / 255F;
public override void start() public ActionSetTint(Vector4 tgtColor)
{ : base()
base.start(); {
renderer.material.color = new Color(color[0], color[1], color[2], color[3]); color = tgtColor * coeff;
}
/// <summary>
/// Returns a copy of the action.
/// </summary>
public override ActionInstant clone()
{
return new ActionSetTint(color * 255F);
}
/// <summary>
/// This method is called at the action start.
/// </summary>
public override void start()
{
base.start();
renderer.material.color = new Color(color[0], color[1], color[2], color[3]);
}
} }
} }

View File

@@ -2,22 +2,42 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
class ActionShow : Action namespace coa4u
{ {
/// <summary>
public ActionShow() /// Instantly shows the hidden target. This action does not require the transparency support in shaders.
: base() /// </summary>
class ActionShow : ActionInstant
{ {
}
public override Action clone() public ActionShow()
{ : base()
return new ActionShow(); {
} }
public override void start() /// <summary>
{ /// Returns a copy of the action.
base.start(); /// </summary>
renderer.enabled = true; public override ActionInstant clone()
{
return new ActionShow();
}
/// <summary>
/// Returns the reversed version of the action, if it is possible.
/// </summary>
public override ActionInstant reverse()
{
return new ActionHide();
}
/// <summary>
/// This method is called at the action start.
/// </summary>
public override void start()
{
base.start();
renderer.enabled = true;
}
} }
} }

View File

@@ -2,17 +2,42 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
class ActionToggleVisibility : Action namespace coa4u
{ {
/// <summary>
public ActionToggleVisibility() /// Instantly hides the target or showes it, if it's hidden. This action does not require the transparency support in shaders.
: base() /// </summary>
class ActionToggleVisibility : ActionInstant
{ {
}
public override void start() public ActionToggleVisibility()
{ : base()
base.start(); {
renderer.enabled = !renderer.enabled; }
/// <summary>
/// Returns a copy of the action.
/// </summary>
public override ActionInstant clone()
{
return new ActionToggleVisibility();
}
/// <summary>
/// Returns the reversed version of the action, if it is possible.
/// </summary>
public override ActionInstant reverse()
{
return new ActionToggleVisibility();
}
/// <summary>
/// This method is called at the action start.
/// </summary>
public override void start()
{
base.start();
renderer.enabled = !renderer.enabled;
}
} }
} }

View File

@@ -2,58 +2,76 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
class ActionBezierAbs : ActionInterval namespace coa4u
{ {
protected Vector3 startPoint; /// <summary>
protected Vector3 endPoint; /// Moves the target with a cubic Bezier in absolute coordinates (if the target is not in the start point, it'll be positioned there when the action starts).
protected Vector3 startControlPoint; /// </summary>
protected Vector3 endControlPoint; class ActionBezierAbs : ActionInterval
public ActionBezierAbs(Vector3 tgtStart, Vector3 tgtSCP, Vector3 tgtECP, Vector3 tgtEnd, float tgtDuration)
: base(tgtDuration)
{ {
startPoint = tgtStart; protected Vector3 startPoint;
endPoint = tgtEnd; protected Vector3 endPoint;
startControlPoint = tgtSCP; protected Vector3 startControlPoint;
endControlPoint = tgtECP; protected Vector3 endControlPoint;
}
public ActionBezierAbs(Vector2 tgtStart, Vector2 tgtSCP, Vector2 tgtECP, Vector2 tgtEnd, float tgtDuration) public ActionBezierAbs(Vector3 tgtStart, Vector3 tgtSCP, Vector3 tgtECP, Vector3 tgtEnd, float tgtDuration)
: this((Vector3) tgtStart, (Vector3) tgtSCP, (Vector3) tgtECP, (Vector3) tgtEnd, tgtDuration) : base(tgtDuration)
{
is2d = true;
}
public override Action clone()
{
return new ActionBezierAbs(startPoint, startControlPoint, endControlPoint, endPoint, duration);
}
public override Action reverse()
{
return new ActionBezierAbs(endPoint, endControlPoint, startControlPoint, startPoint, duration);
}
public override void start()
{
base.start();
float z = transform.position.z;
if (is2d)
{ {
startPoint.z = z; startPoint = tgtStart;
endPoint.z = z; endPoint = tgtEnd;
startControlPoint.z = z; startControlPoint = tgtSCP;
endControlPoint.z = z; 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;
}
/// <summary>
/// Returns a copy of the action.
/// </summary>
public override ActionInstant clone()
{
return new ActionBezierAbs(startPoint, startControlPoint, endControlPoint, endPoint, duration);
}
/// <summary>
/// Returns the reversed version of the action, if it is possible.
/// </summary>
public override ActionInstant reverse()
{
return new ActionBezierAbs(endPoint, endControlPoint, startControlPoint, startPoint, duration);
}
/// <summary>
/// This method is called at the action start.
/// </summary>
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;
}
}
/// <summary>
/// This method is called every frame update.
/// </summary>
public override void stepInterval(float dt)
{
float t = timer / duration;
Vector3 newPosition = (((-startPoint
+ 3 * (startControlPoint - endControlPoint) + endPoint) * t
+ (3 * (startPoint + endControlPoint) - 6 * startControlPoint)) * t +
3 * (startControlPoint - startPoint)) * t + startPoint;
transform.position = newPosition;
} }
} }
public override void stepInterval(float dt)
{
float t = timer / duration;
Vector3 newPosition = (((-startPoint
+ 3 * (startControlPoint - endControlPoint) + endPoint) * t
+ (3 * (startPoint + endControlPoint) - 6 * startControlPoint)) * t +
3 * (startControlPoint - startPoint)) * t + startPoint;
transform.position = newPosition;
}
} }

View File

@@ -2,56 +2,74 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
class ActionBezierRel : ActionInterval namespace coa4u
{ {
protected Vector3 ep; /// <summary>
protected Vector3 cp1; /// Moves the target with a cubic Bezier in relative coordinates.
protected Vector3 cp2; /// </summary>
protected Vector3 startPoint; class ActionBezierRel : ActionInterval
protected Vector3 endPoint;
protected Vector3 startControlPoint;
protected Vector3 endControlPoint;
public ActionBezierRel(Vector3 tgtSCP, Vector3 tgtECP, Vector3 tgtEnd, float tgtDuration)
: base(tgtDuration)
{ {
ep = tgtEnd; protected Vector3 ep;
cp1 = tgtSCP; protected Vector3 cp1;
cp2 = tgtECP; protected Vector3 cp2;
} protected Vector3 startPoint;
protected Vector3 endPoint;
protected Vector3 startControlPoint;
protected Vector3 endControlPoint;
public ActionBezierRel(Vector2 tgtSCP, Vector2 tgtECP, Vector2 tgtEnd, float tgtDuration) public ActionBezierRel(Vector3 tgtSCP, Vector3 tgtECP, Vector3 tgtEnd, float tgtDuration)
: this((Vector3)tgtSCP, (Vector3)tgtECP, (Vector3)tgtEnd, tgtDuration) : base(tgtDuration)
{ {
is2d = true; ep = tgtEnd;
} cp1 = tgtSCP;
cp2 = tgtECP;
}
public override Action clone() public ActionBezierRel(Vector2 tgtSCP, Vector2 tgtECP, Vector2 tgtEnd, float tgtDuration)
{ : this((Vector3)tgtSCP, (Vector3)tgtECP, (Vector3)tgtEnd, tgtDuration)
return new ActionBezierRel(startControlPoint, endControlPoint, endPoint, duration); {
} is2d = true;
}
public override Action reverse() /// <summary>
{ /// Returns a copy of the action.
return new ActionBezierRel(-startControlPoint, -endControlPoint, -endPoint, duration); /// </summary>
} public override ActionInstant clone()
{
return new ActionBezierRel(startControlPoint, endControlPoint, endPoint, duration);
}
public override void start() /// <summary>
{ /// Returns the reversed version of the action, if it is possible.
base.start(); /// </summary>
startPoint = target.transform.position; public override ActionInstant reverse()
endPoint = startPoint + ep; {
startControlPoint = startPoint + cp1; return new ActionBezierRel(-startControlPoint, -endControlPoint, -endPoint, duration);
endControlPoint = startControlPoint + cp2; }
}
public override void stepInterval(float dt) /// <summary>
{ /// This method is called at the action start.
float t = timer / duration; /// </summary>
Vector3 newPosition = (((-startPoint public override void start()
+ 3 * (startControlPoint - endControlPoint) + endPoint) * t {
+ (3 * (startPoint + endControlPoint) - 6 * startControlPoint)) * t + base.start();
3 * (startControlPoint - startPoint)) * t + startPoint; startPoint = target.transform.position;
transform.position = newPosition; endPoint = startPoint + ep;
startControlPoint = startPoint + cp1;
endControlPoint = startControlPoint + cp2;
}
/// <summary>
/// This method is called every frame update.
/// </summary>
public override void stepInterval(float dt)
{
float t = timer / duration;
Vector3 newPosition = (((-startPoint
+ 3 * (startControlPoint - endControlPoint) + endPoint) * t
+ (3 * (startPoint + endControlPoint) - 6 * startControlPoint)) * t +
3 * (startControlPoint - startPoint)) * t + startPoint;
transform.position = newPosition;
}
} }
} }

View File

@@ -2,49 +2,61 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
class ActionBlink : ActionRepeat namespace coa4u
{ {
protected bool randomDelay; /// <summary>
protected ActionDelay delay = new ActionDelay(0); /// Blinks the target.
protected Action[] blinkSeq; /// </summary>
protected float durationMin; class ActionBlink : ActionRepeat
protected float durationMax;
public ActionBlink(int tgtBlinks, float tgtDuration)
: base(null, 0)
{ {
durationMin = tgtDuration; protected bool randomDelay;
durationMax = tgtDuration; protected ActionDelay delay = new ActionDelay(0);
count = (tgtBlinks) * 2; protected ActionInstant[] blinkSeq;
blinkSeq = new Action[] protected float durationMin;
protected float durationMax;
public ActionBlink(int tgtBlinks, float tgtDuration)
: base(null, 0)
{
durationMin = tgtDuration;
durationMax = tgtDuration;
count = (tgtBlinks) * 2;
blinkSeq = new ActionInstant[]
{ {
new ActionToggleVisibility(), new ActionToggleVisibility(),
new ActionDelay(tgtDuration / tgtBlinks) new ActionDelay(tgtDuration / tgtBlinks)
}; };
action = new ActionSequence(blinkSeq); action = new ActionSequence(blinkSeq);
} }
public ActionBlink(int tgtBlinks, float tgtDurationMin, float tgtDurationMax) public ActionBlink(int tgtBlinks, float tgtDurationMin, float tgtDurationMax)
: base(null, 0) : base(null, 0)
{ {
durationMin = tgtDurationMin; durationMin = tgtDurationMin;
durationMax = tgtDurationMax; durationMax = tgtDurationMax;
count = (tgtBlinks) * 2; count = (tgtBlinks) * 2;
blinkSeq = new Action[] blinkSeq = new ActionInstant[]
{ {
new ActionToggleVisibility(), new ActionToggleVisibility(),
new ActionDelay(tgtDurationMin / tgtBlinks, tgtDurationMax / tgtBlinks) new ActionDelay(tgtDurationMin / tgtBlinks, tgtDurationMax / tgtBlinks)
}; };
action = new ActionSequence(blinkSeq); action = new ActionSequence(blinkSeq);
} }
public override Action clone() /// <summary>
{ /// Returns a copy of the action.
return new ActionBlink(count / 2 - 1, durationMin, durationMax); /// </summary>
} public override ActionInstant clone()
{
return new ActionBlink(count / 2 - 1, durationMin, durationMax);
}
public override Action reverse() /// <summary>
{ /// Returns the reversed version of the action, if it is possible.
return new ActionBlink(count / 2 - 1, durationMin, durationMax); /// </summary>
public override ActionInstant reverse()
{
return new ActionBlink(count / 2 - 1, durationMin, durationMax);
}
} }
} }

View File

@@ -2,31 +2,40 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
class ActionDelay : ActionInterval namespace coa4u
{ {
protected float durationMin; /// <summary>
protected float durationMax; /// Delays the action for the given amount of seconds.
/// </summary>
public ActionDelay(float tgtDuration) class ActionDelay : ActionInterval
: base(tgtDuration)
{ {
durationMin = tgtDuration; protected float durationMin;
durationMax = tgtDuration; protected float durationMax;
}
public ActionDelay(float tgtDuration, float tgtDurationMax) public ActionDelay(float tgtDuration)
: base(tgtDuration) : base(tgtDuration)
{
durationMin = tgtDuration;
durationMax = tgtDurationMax;
}
public override void start()
{
base.start();
if (durationMax != null)
{ {
duration = UnityEngine.Random.Range(durationMin, durationMax); durationMin = tgtDuration;
durationMax = tgtDuration;
}
public ActionDelay(float tgtDuration, float tgtDurationMax)
: base(tgtDuration)
{
durationMin = tgtDuration;
durationMax = tgtDurationMax;
}
/// <summary>
/// This method is called at the action start.
/// </summary>
public override void start()
{
base.start();
if (durationMax != null)
{
duration = UnityEngine.Random.Range(durationMin, durationMax);
}
} }
} }
} }

View File

@@ -2,31 +2,46 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
class ActionFadeBy : ActionInterval namespace coa4u
{ {
protected float delta; /// <summary>
/// Fades the target by the given alpha value. The tartet shaders should support transparency in order to use this action.
public ActionFadeBy(float tgtDelta, float tgtDuration) /// </summary>
: base(tgtDuration) class ActionFadeBy : ActionInterval
{ {
delta = tgtDelta; protected float delta;
}
public override Action clone() public ActionFadeBy(float tgtDelta, float tgtDuration)
{ : base(tgtDuration)
return new ActionFadeBy(delta, duration); {
} delta = tgtDelta;
}
public override Action reverse() /// <summary>
{ /// Returns a copy of the action.
return new ActionFadeBy(-delta, duration); /// </summary>
} public override ActionInstant clone()
{
return new ActionFadeBy(delta, duration);
}
public override void stepInterval(float dt) /// <summary>
{ /// Returns the reversed version of the action, if it is possible.
float d = dt / duration; /// </summary>
Color tgtColor = renderer.material.color; public override ActionInstant reverse()
tgtColor[3] += delta * d; {
renderer.material.color = tgtColor; return new ActionFadeBy(-delta, duration);
}
/// <summary>
/// This method is called every frame update.
/// </summary>
public override void stepInterval(float dt)
{
float d = dt / duration;
Color tgtColor = renderer.material.color;
tgtColor[3] += delta * d;
renderer.material.color = tgtColor;
}
} }
} }

View File

@@ -2,21 +2,33 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
class ActionFadeIn : ActionFadeTo namespace coa4u
{ {
/// <summary>
public ActionFadeIn(float tgtDuration) /// Fades in the target. The tartet shaders should support transparency in order to use this action.
: base(1, tgtDuration) /// </summary>
class ActionFadeIn : ActionFadeTo
{ {
}
public override Action clone() public ActionFadeIn(float tgtDuration)
{ : base(1, tgtDuration)
return new ActionFadeIn(duration); {
} }
public override Action reverse() /// <summary>
{ /// Returns a copy of the action.
return new ActionFadeIn(duration); /// </summary>
public override ActionInstant clone()
{
return new ActionFadeIn(duration);
}
/// <summary>
/// Returns the reversed version of the action, if it is possible.
/// </summary>
public override ActionInstant reverse()
{
return new ActionFadeOut(duration);
}
} }
} }

View File

@@ -2,21 +2,33 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
class ActionFadeOut : ActionFadeTo namespace coa4u
{ {
/// <summary>
public ActionFadeOut(float tgtDuration) /// Fades out the target. The tartet shaders should support transparency in order to use this action.
: base(0, tgtDuration) /// </summary>
class ActionFadeOut : ActionFadeTo
{ {
}
public override Action clone() public ActionFadeOut(float tgtDuration)
{ : base(0, tgtDuration)
return new ActionFadeOut(duration); {
} }
public override Action reverse() /// <summary>
{ /// Returns a copy of the action.
return new ActionFadeOut(duration); /// </summary>
public override ActionInstant clone()
{
return new ActionFadeOut(duration);
}
/// <summary>
/// Returns the reversed version of the action, if it is possible.
/// </summary>
public override ActionInstant reverse()
{
return new ActionFadeIn(duration);
}
} }
} }

View File

@@ -2,33 +2,48 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
class ActionFadeTo : ActionInterval namespace coa4u
{ {
protected float value; /// <summary>
protected float delta; /// Fades the target to the given alpha value. The tartet shaders should support transparency in order to use this action.
/// </summary>
public ActionFadeTo(float tgtValue, float tgtDuration) class ActionFadeTo : ActionInterval
: base(tgtDuration)
{ {
value = tgtValue; protected float value;
} protected float delta;
public override Action clone() public ActionFadeTo(float tgtValue, float tgtDuration)
{ : base(tgtDuration)
return new ActionFadeTo(value, duration); {
} value = tgtValue;
}
public override void start() /// <summary>
{ /// Returns a copy of the action.
base.start(); /// </summary>
delta = value - renderer.material.color.a; public override ActionInstant clone()
} {
return new ActionFadeTo(value, duration);
}
public override void stepInterval(float dt) /// <summary>
{ /// This method is called at the action start.
float d = dt / duration; /// </summary>
Color tgtColor = renderer.material.color; public override void start()
tgtColor[3] += delta * d; {
renderer.material.color = tgtColor; base.start();
delta = value - renderer.material.color.a;
}
/// <summary>
/// This method is called every frame update.
/// </summary>
public override void stepInterval(float dt)
{
float d = dt / duration;
Color tgtColor = renderer.material.color;
tgtColor[3] += delta * d;
renderer.material.color = tgtColor;
}
} }
} }

View File

@@ -2,48 +2,51 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
class ActionJumpBy : ActionSequence namespace coa4u
{ {
protected Vector3 point; class ActionJumpBy : ActionSequence
float height;
int jumps;
public ActionJumpBy(Vector3 tgtPoint, float tgtHeight, int tgtJumps, float tgtDuration)
: base(new Action[tgtJumps])
{ {
point = tgtPoint; protected Vector3 point;
jumps = tgtJumps; float height;
height = tgtHeight; int jumps;
duration = tgtDuration;
}
public ActionJumpBy(Vector2 tgtPoint, float tgtHeight, int tgtJumps, float tgtDuration) public ActionJumpBy(Vector3 tgtPoint, float tgtHeight, int tgtJumps, float tgtDuration)
: this((Vector3)tgtPoint, tgtHeight, tgtJumps, tgtDuration) : base(new ActionInstant[tgtJumps])
{
is2d = true;
}
public override Action clone()
{
return new ActionJumpBy(point, height, jumps, duration);
}
public override Action reverse()
{
return new ActionJumpBy(-point, height, jumps, duration);
}
public override void start()
{
float coeff = 1F / jumps;
Vector3 end = point * coeff;
Vector3 cp1 = Vector3.up * height;
Vector3 cp2 = end + cp1;
ActionBezierRel singleJump = new ActionBezierRel(cp1, cp2, end, duration * coeff);
for (int i = 0; i < jumps; i++)
{ {
actions[i] = singleJump; point = tgtPoint;
jumps = tgtJumps;
height = tgtHeight;
duration = tgtDuration;
}
public ActionJumpBy(Vector2 tgtPoint, float tgtHeight, int tgtJumps, float tgtDuration)
: this((Vector3)tgtPoint, tgtHeight, tgtJumps, tgtDuration)
{
is2d = true;
}
public override ActionInstant clone()
{
return new ActionJumpBy(point, height, jumps, duration);
}
public override ActionInstant reverse()
{
return new ActionJumpBy(-point, height, jumps, duration);
}
public override void start()
{
float coeff = 1F / jumps;
Vector3 end = point * coeff;
Vector3 cp1 = Vector3.up * height;
Vector3 cp2 = end + cp1;
ActionBezierRel singleJump = new ActionBezierRel(cp1, cp2, end, duration * coeff);
for (int i = 0; i < jumps; i++)
{
actions[i] = singleJump;
}
base.start();
} }
base.start();
} }
} }

View File

@@ -2,46 +2,49 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
class ActionJumpTo : ActionSequence namespace coa4u
{ {
protected Vector3 point; class ActionJumpTo : ActionSequence
float height;
int jumps;
public ActionJumpTo(Vector3 tgtPoint, float tgtHeight, int tgtJumps, float tgtDuration)
: base(new Action[tgtJumps])
{ {
point = tgtPoint; protected Vector3 point;
jumps = tgtJumps; float height;
height = tgtHeight; int jumps;
duration = tgtDuration;
}
public ActionJumpTo(Vector2 tgtPoint, float tgtHeight, int tgtJumps, float tgtDuration) public ActionJumpTo(Vector3 tgtPoint, float tgtHeight, int tgtJumps, float tgtDuration)
: this((Vector3)tgtPoint, tgtHeight, tgtJumps, tgtDuration) : base(new ActionInstant[tgtJumps])
{
is2d = true;
}
public override Action clone()
{
return new ActionJumpTo(point, height, jumps, duration);
}
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;
Vector3 cp2 = end + cp1;
ActionBezierRel singleJump = new ActionBezierRel(cp1, cp2, end, duration * coeff);
for (int i = 0; i < jumps; i++)
{ {
actions[i] = singleJump; point = tgtPoint;
jumps = tgtJumps;
height = tgtHeight;
duration = tgtDuration;
}
public ActionJumpTo(Vector2 tgtPoint, float tgtHeight, int tgtJumps, float tgtDuration)
: this((Vector3)tgtPoint, tgtHeight, tgtJumps, tgtDuration)
{
is2d = true;
}
public override ActionInstant clone()
{
return new ActionJumpTo(point, height, jumps, duration);
}
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;
Vector3 cp2 = end + cp1;
ActionBezierRel singleJump = new ActionBezierRel(cp1, cp2, end, duration * coeff);
for (int i = 0; i < jumps; i++)
{
actions[i] = singleJump;
}
base.start();
} }
base.start();
} }
} }

View File

@@ -2,36 +2,39 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
class ActionMoveBy : ActionInterval namespace coa4u
{ {
protected Vector3 delta; class ActionMoveBy : ActionInterval
public ActionMoveBy(Vector3 tgtDelta, float tgtDuration)
: base(tgtDuration)
{ {
delta = tgtDelta; protected Vector3 delta;
}
public ActionMoveBy(Vector2 tgtValue, float tgtDuration) public ActionMoveBy(Vector3 tgtDelta, float tgtDuration)
: this((Vector3) tgtValue, tgtDuration) : base(tgtDuration)
{ {
is2d = true; delta = tgtDelta;
} }
public override Action clone() public ActionMoveBy(Vector2 tgtValue, float tgtDuration)
{ : this((Vector3)tgtValue, tgtDuration)
return new ActionMoveBy(delta, duration); {
} is2d = true;
}
public override Action reverse() public override ActionInstant clone()
{ {
return new ActionMoveBy(delta * -1F, duration); return new ActionMoveBy(delta, duration);
} }
public override void stepInterval(float dt) public override ActionInstant reverse()
{ {
float d = dt / duration; return new ActionMoveBy(delta * -1F, duration);
Vector3 tgt = delta * d; }
transform.Translate(tgt, Space.World);
public override void stepInterval(float dt)
{
float d = dt / duration;
Vector3 tgt = delta * d;
transform.Translate(tgt, Space.World);
}
} }
} }

View File

@@ -2,40 +2,43 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
class ActionMoveTo : ActionInterval namespace coa4u
{ {
protected Vector3 value; class ActionMoveTo : ActionInterval
protected Vector3 path;
public ActionMoveTo(Vector3 tgtValue, float tgtDuration)
: base(tgtDuration)
{ {
value = tgtValue; protected Vector3 value;
} protected Vector3 path;
public ActionMoveTo(Vector2 tgtValue, float tgtDuration) public ActionMoveTo(Vector3 tgtValue, float tgtDuration)
: this((Vector3) tgtValue, tgtDuration) : base(tgtDuration)
{ {
is2d = true; value = tgtValue;
} }
public override Action clone() public ActionMoveTo(Vector2 tgtValue, float tgtDuration)
{ : this((Vector3)tgtValue, tgtDuration)
return new ActionMoveBy(value, duration); {
} is2d = true;
}
public override void start() public override ActionInstant clone()
{ {
base.start(); return new ActionMoveBy(value, duration);
if (is2d) }
value.z = transform.position.z;
path = value - transform.position;
}
public override void stepInterval(float dt) public override void start()
{ {
float d = dt / duration; base.start();
Vector3 tgt = path * d; if (is2d)
transform.Translate(tgt, Space.World); value.z = transform.position.z;
path = value - transform.position;
}
public override void stepInterval(float dt)
{
float d = dt / duration;
Vector3 tgt = path * d;
transform.Translate(tgt, Space.World);
}
} }
} }

View File

@@ -2,36 +2,39 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
class ActionRotateBy : ActionInterval namespace coa4u
{ {
protected Vector3 delta; class ActionRotateBy : ActionInterval
public ActionRotateBy(Vector3 tgtDelta, float tgtDuration)
: base(tgtDuration)
{ {
delta = tgtDelta; protected Vector3 delta;
}
public ActionRotateBy(float angle, float tgtDuration) public ActionRotateBy(Vector3 tgtDelta, float tgtDuration)
: this(new Vector3(0, 0, angle), tgtDuration) : base(tgtDuration)
{ {
is2d = true; delta = tgtDelta;
} }
public override Action clone() public ActionRotateBy(float angle, float tgtDuration)
{ : this(new Vector3(0, 0, angle), tgtDuration)
return new ActionRotateBy(delta, duration); {
} is2d = true;
}
public override Action reverse() public override ActionInstant clone()
{ {
return new ActionRotateBy(delta * -1F, duration); return new ActionRotateBy(delta, duration);
} }
public override void stepInterval(float dt) public override ActionInstant reverse()
{ {
float d = dt / duration; return new ActionRotateBy(delta * -1F, duration);
Vector3 tgt = delta * d; }
transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles + tgt);
public override void stepInterval(float dt)
{
float d = dt / duration;
Vector3 tgt = delta * d;
transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles + tgt);
}
} }
} }

View File

@@ -2,46 +2,49 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
class ActionRotateTo : ActionInterval namespace coa4u
{ {
protected Vector3 value; class ActionRotateTo : ActionInterval
protected Vector3 path;
public ActionRotateTo(Vector3 tgtValue, float tgtDuration)
: base(tgtDuration)
{ {
value = tgtValue; protected Vector3 value;
} protected Vector3 path;
public ActionRotateTo(float angle, float tgtDuration) public ActionRotateTo(Vector3 tgtValue, float tgtDuration)
: this(new Vector3(0, 0, angle), tgtDuration) : base(tgtDuration)
{
is2d = true;
}
public override Action clone()
{
return new ActionRotateTo(value, duration);
}
public override void start()
{
base.start();
path = new Vector3();
for (int i = 0; i < 3; i++)
{ {
float t = value[i]; value = tgtValue;
float f = transform.rotation.eulerAngles[i]; }
if (Math.Abs(t - f) < Math.Abs(t + 360 - f))
path[i] = t - f; public ActionRotateTo(float angle, float tgtDuration)
else : this(new Vector3(0, 0, angle), tgtDuration)
path[i] = t + 360 - f; {
is2d = true;
}
public override ActionInstant clone()
{
return new ActionRotateTo(value, duration);
}
public override void start()
{
base.start();
path = new Vector3();
for (int i = 0; i < 3; i++)
{
float t = value[i];
float f = transform.rotation.eulerAngles[i];
if (Math.Abs(t - f) < Math.Abs(t + 360 - f))
path[i] = t - f;
else
path[i] = t + 360 - f;
}
}
public override void stepInterval(float dt)
{
float d = dt / duration;
Vector3 tgt = path * d;
transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles + tgt);
} }
} }
public override void stepInterval(float dt)
{
float d = dt / duration;
Vector3 tgt = path * d;
transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles + tgt);
}
} }

View File

@@ -2,52 +2,55 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
class ActionScaleBy : ActionInterval namespace coa4u
{ {
protected Vector3 delta; class ActionScaleBy : ActionInterval
protected Vector3 path;
public ActionScaleBy(Vector3 tgtDelta, float tgtDuration)
: base(tgtDuration)
{ {
delta = tgtDelta; protected Vector3 delta;
} protected Vector3 path;
public ActionScaleBy(Vector2 tgtValue, float tgtDuration) public ActionScaleBy(Vector3 tgtDelta, float tgtDuration)
: this((Vector3)tgtValue, tgtDuration) : base(tgtDuration)
{ {
is2d = true; delta = tgtDelta;
} }
public override Action clone() public ActionScaleBy(Vector2 tgtValue, float tgtDuration)
{ : this((Vector3)tgtValue, tgtDuration)
return new ActionScaleBy(delta, duration); {
} is2d = true;
}
public override Action reverse() public override ActionInstant clone()
{ {
return new ActionScaleBy(delta * -1F, duration); return new ActionScaleBy(delta, duration);
} }
public override void start() public override ActionInstant reverse()
{ {
base.start(); return new ActionScaleBy(delta * -1F, duration);
Vector3 scale = transform.localScale; }
scale.x *= delta.x;
scale.y *= delta.y;
scale.z *= delta.z;
path = scale - transform.localScale;
}
public override void stepInterval(float dt) public override void start()
{ {
float d = dt / duration; base.start();
Vector3 tgt = path * d; Vector3 scale = transform.localScale;
transform.localScale += tgt; scale.x *= delta.x;
} scale.y *= delta.y;
scale.z *= delta.z;
path = scale - transform.localScale;
}
public override void stop() public override void stepInterval(float dt)
{ {
base.stop(); float d = dt / duration;
Vector3 tgt = path * d;
transform.localScale += tgt;
}
public override void stop()
{
base.stop();
}
} }
} }

View File

@@ -2,40 +2,43 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
class ActionScaleTo : ActionInterval namespace coa4u
{ {
protected Vector3 value; class ActionScaleTo : ActionInterval
protected Vector3 path;
public ActionScaleTo(Vector3 tgtValue, float tgtDuration)
: base(tgtDuration)
{ {
value = tgtValue; protected Vector3 value;
} protected Vector3 path;
public ActionScaleTo(Vector2 tgtValue, float tgtDuration) public ActionScaleTo(Vector3 tgtValue, float tgtDuration)
: this((Vector3) tgtValue, tgtDuration) : base(tgtDuration)
{ {
is2d = true; value = tgtValue;
} }
public override Action clone() public ActionScaleTo(Vector2 tgtValue, float tgtDuration)
{ : this((Vector3)tgtValue, tgtDuration)
return new ActionScaleTo(value, duration); {
} is2d = true;
}
public override void start() public override ActionInstant clone()
{ {
base.start(); return new ActionScaleTo(value, duration);
if (is2d) }
value.z = transform.localScale.z;
path = value - transform.localScale;
}
public override void stepInterval(float dt) public override void start()
{ {
float d = dt / duration; base.start();
Vector3 tgt = path * d; if (is2d)
transform.localScale += tgt; value.z = transform.localScale.z;
path = value - transform.localScale;
}
public override void stepInterval(float dt)
{
float d = dt / duration;
Vector3 tgt = path * d;
transform.localScale += tgt;
}
} }
} }

View File

@@ -2,44 +2,47 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
class ActionSkewBy : ActionInterval namespace coa4u
{ {
protected Vector3 skewAngles1; class ActionSkewBy : ActionInterval
protected Vector3 skewAngles2;
protected Mesh mesh;
public ActionSkewBy(Vector3 tgtAngles1, Vector3 tgtAngles2, float tgtDuration)
: base(tgtDuration)
{ {
skewAngles1 = tgtAngles1; protected Vector3 skewAngles1;
skewAngles2 = tgtAngles2; protected Vector3 skewAngles2;
} protected Mesh mesh;
public override Action clone() public ActionSkewBy(Vector3 tgtAngles1, Vector3 tgtAngles2, float tgtDuration)
{ : base(tgtDuration)
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."); skewAngles1 = tgtAngles1;
skewAngles2 = tgtAngles2;
}
public override ActionInstant clone()
{
return new ActionSkewBy(skewAngles1, skewAngles2, duration);
}
public override ActionInstant reverse()
{
return new ActionSkewBy(-skewAngles1, -skewAngles2, duration);
}
public override void start()
{
base.start();
if (!(target is Actor))
{
throw new Exception("You should use Actor 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;
((Actor)target).skewAngles1 += tgt;
tgt = skewAngles2 * d;
((Actor)target).skewAngles2 += tgt;
} }
} }
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

@@ -2,41 +2,44 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
class ActionTintBy : ActionInterval namespace coa4u
{ {
protected Vector4 color; class ActionTintBy : ActionInterval
protected const float coeff = 1F / 255F;
public ActionTintBy(Vector4 tgtColor, float tgtDuration)
: base(tgtDuration)
{ {
color = tgtColor * coeff; protected Vector4 color;
} protected const float coeff = 1F / 255F;
public ActionTintBy(Vector3 tgtColor, float tgtDuration) public ActionTintBy(Vector4 tgtColor, float tgtDuration)
: this(new Vector4(tgtColor.x, tgtColor.y, tgtColor.z), tgtDuration) : base(tgtDuration)
{ {
} color = tgtColor * coeff;
}
public override Action clone() public ActionTintBy(Vector3 tgtColor, float tgtDuration)
{ : this(new Vector4(tgtColor.x, tgtColor.y, tgtColor.z), tgtDuration)
return new ActionTintBy(color / coeff, duration); {
} }
public override Action reverse() public override ActionInstant clone()
{ {
return new ActionTintBy(-color / coeff, duration); return new ActionTintBy(color / coeff, duration);
} }
public override void stepInterval(float dt) public override ActionInstant reverse()
{ {
float d = dt / duration; return new ActionTintBy(-color / coeff, duration);
Vector4 tgt = color * d; }
Color tgtColor = renderer.material.color;
tgtColor[0] += tgt[0]; public override void stepInterval(float dt)
tgtColor[1] += tgt[1]; {
tgtColor[2] += tgt[2]; float d = dt / duration;
tgtColor[3] += tgt[3]; Vector4 tgt = color * d;
renderer.material.color = tgtColor; Color tgtColor = renderer.material.color;
tgtColor[0] += tgt[0];
tgtColor[1] += tgt[1];
tgtColor[2] += tgt[2];
tgtColor[3] += tgt[3];
renderer.material.color = tgtColor;
}
} }
} }

View File

@@ -2,48 +2,51 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
class ActionTintTo : ActionInterval namespace coa4u
{ {
protected Vector4 color; class ActionTintTo : ActionInterval
protected Vector4 path;
protected const float coeff = 1F / 255F;
public ActionTintTo(Vector4 tgtColor, float tgtDuration)
: base(tgtDuration)
{ {
color = tgtColor * coeff; protected Vector4 color;
path = Vector4.zero; protected Vector4 path;
} protected const float coeff = 1F / 255F;
public ActionTintTo(Vector3 tgtColor, float tgtDuration) public ActionTintTo(Vector4 tgtColor, float tgtDuration)
: this(new Vector4(tgtColor.x, tgtColor.y, tgtColor.z), tgtDuration) : base(tgtDuration)
{ {
} color = tgtColor * coeff;
path = Vector4.zero;
}
public override Action clone() public ActionTintTo(Vector3 tgtColor, float tgtDuration)
{ : this(new Vector4(tgtColor.x, tgtColor.y, tgtColor.z), tgtDuration)
return new ActionTintTo(color / coeff, duration); {
} }
public override void start() public override ActionInstant clone()
{ {
base.start(); return new ActionTintTo(color / coeff, duration);
Color tgtColor = renderer.material.color; }
path[0] = color[0] - tgtColor[0];
path[1] = color[1] - tgtColor[1];
path[2] = color[2] - tgtColor[2];
path[3] = color[3] - tgtColor[3];
}
public override void stepInterval(float dt) public override void start()
{ {
float d = dt / duration; base.start();
Vector4 tgt = path * d; Color tgtColor = renderer.material.color;
Color tgtColor = renderer.material.color; path[0] = color[0] - tgtColor[0];
tgtColor[0] += tgt[0]; path[1] = color[1] - tgtColor[1];
tgtColor[1] += tgt[1]; path[2] = color[2] - tgtColor[2];
tgtColor[2] += tgt[2]; path[3] = color[3] - tgtColor[3];
tgtColor[3] += tgt[3]; }
renderer.material.color = tgtColor;
public override void stepInterval(float dt)
{
float d = dt / duration;
Vector4 tgt = path * d;
Color tgtColor = renderer.material.color;
tgtColor[0] += tgt[0];
tgtColor[1] += tgt[1];
tgtColor[2] += tgt[2];
tgtColor[3] += tgt[3];
renderer.material.color = tgtColor;
}
} }
} }

View File

@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using UnityEngine;
namespace coa4u
{
public class MethodHolder
{
protected Action method;
protected string methodName;
public MethodHolder()
{
}
public MethodHolder(Action tgtMethod)
{
method = tgtMethod;
methodName = tgtMethod.Method.Name;
}
public virtual void run(object param = null)
{
if (method != null)
method.Invoke();
}
public string name
{
get
{
return methodName;
}
}
}
public class MethodHolder<T> : MethodHolder
{
protected Action<T> methodParam;
public MethodHolder(Action<T> tgtMethod)
{
methodParam = tgtMethod;
methodName = tgtMethod.Method.Name;
}
public override void run(object param)
{
if (methodParam != null)
methodParam.Invoke((T)param);
}
}
}

View File

@@ -1,42 +1,126 @@
using UnityEngine; using System;
using UnityEngine;
using System.Collections; using System.Collections;
using System.Collections.Generic;
using coa4u;
/// <summary>
/// The default Actor class for the Action system.
/// </summary>
public class Actor : MonoBehaviour public class Actor : MonoBehaviour
{ {
protected Action action; protected Dictionary<string, MethodHolder> methodsCache = new Dictionary<string, MethodHolder>();
protected ActionInstant action;
private bool paused = false; private bool paused = false;
protected Vector3 angles1prev = Vector3.zero;
protected Vector3 angles2prev = Vector3.zero;
protected const float coeff = Mathf.PI / 180;
protected Vector3[] initialState;
public Transform transformCached;
public Renderer rendererCached;
public Mesh meshCached;
public Vector3 skewAngles1;
public Vector3 skewAngles2;
/// <summary>
/// This method is called when the script instance is being loaded.
/// </summary>
protected void Awake()
{
transformCached = gameObject.transform;
rendererCached = gameObject.renderer;
meshCached = gameObject.GetComponent<MeshFilter>().mesh;
initialState = meshCached.vertices;
}
/// <summary>
/// This method is called at every frame update.
/// </summary>
protected void Update() protected void Update()
{ {
if (paused || action == null) if (paused || action == null)
return; return;
if (action.isRunning()) if (action.running)
action.step(Time.deltaTime); action.step(Time.deltaTime);
if (skewAngles1 != angles1prev || skewAngles2 != angles2prev)
updateSkew();
} }
public void AttachAction(Action tgtAction) /// <summary>
/// Updates the skew for the mesh.
/// </summary>
void updateSkew()
{ {
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++;
}
meshCached.vertices = verts;
angles1prev = skewAngles1;
angles2prev = skewAngles2;
}
/// <summary>
/// Attaches and starts the action.
/// </summary>
public void AttachAction(ActionInstant tgtAction)
{
if (action != null)
{
action.stop();
}
action = tgtAction; action = tgtAction;
action.setActor(this); action.setActor(this);
action.start(); action.start();
} }
/// <summary>
/// Stops all running actions for this actor.
/// </summary>
public void StopAction() public void StopAction()
{ {
action.stop(); if (action == null)
return;
if (action.running)
action.stop();
action = null; action = null;
} }
/// <summary>
/// Pauses actions for this actor.
/// </summary>
public void PauseAction() public void PauseAction()
{ {
paused = true; paused = true;
} }
/// <summary>
/// Unpauses actions for this actor.
/// </summary>
public void UnpauseAction() public void UnpauseAction()
{ {
paused = false; paused = false;
} }
/// <summary>
/// Sets the timescale for current action.
/// </summary>
public void SetTimeScale(float ts) public void SetTimeScale(float ts)
{ {
if (action is ActionInterval) if (action is ActionInterval)
@@ -44,4 +128,40 @@ public class Actor : MonoBehaviour
((ActionInterval)action).setTimeScale(ts); ((ActionInterval)action).setTimeScale(ts);
} }
} }
/// <summary>
/// Adds method to cache to speed-up
/// </summary>
public void AddMethodToCache(MethodHolder method)
{
methodsCache.Add(method.name, method);
}
public void RemoveMethodFromCache(string key)
{
if (methodsCache.ContainsKey(key))
{
methodsCache.Remove(key);
}
else
{
throw new Exception("Method " + key + " not found in cache.");
}
}
public void ReceiveMessage(string key, object param = null, SendMessageOptions options = SendMessageOptions.DontRequireReceiver)
{
if (methodsCache.ContainsKey(key))
{
if (param == null)
methodsCache[key].run();
else
methodsCache[key].run(param);
}
else
{
gameObject.SendMessage(key, param, options);
}
}
} }

View File

@@ -1,14 +1,15 @@
using UnityEngine; using UnityEngine;
using System.Collections; using System.Collections;
using coa4u;
public class ActorSampleActions : MeshActor public class ActorSampleActions : Actor
{ {
public void Start() public void Start()
{ {
Action seq = new ActionRepeat (new ActionSequence(new Action[] ActionInstant seq = new ActionRepeat (new ActionSequence(new ActionInstant[]
{ {
new ActionFadeIn(2), new ActionFadeIn(2),
new ActionParallel(new Action[] { new ActionParallel(new ActionInstant[] {
new ActionMoveBy(new Vector3(10, 10, 0), 1), new ActionMoveBy(new Vector3(10, 10, 0), 1),
new ActionRotateBy(new Vector3(90, 90, 0), 1), new ActionRotateBy(new Vector3(90, 90, 0), 1),
new ActionTintBy(new Vector4(-50, 50, -150), 1) new ActionTintBy(new Vector4(-50, 50, -150), 1)
@@ -32,9 +33,23 @@ public class ActorSampleActions : MeshActor
new ActionScaleTo(new Vector3(2, 2, 2), 1), new ActionScaleTo(new Vector3(2, 2, 2), 1),
new ActionRotateTo(new Vector3(0, 0, 0), 1), new ActionRotateTo(new Vector3(0, 0, 0), 1),
new ActionFadeOut(2), new ActionFadeOut(2),
new ActionSetTint(new Vector4(67, 105, 181)) new ActionSetTint(new Vector4(67, 105, 181)),
new ActionSendMessage("msgHello"),
new ActionSendMessage("msgHelloTo", "world"),
}), 5); }), 5);
SetTimeScale(2);
this.AttachAction(seq); this.AttachAction(seq);
AddMethodToCache(new MethodHolder(msgHello));
AddMethodToCache(new MethodHolder<string>(msgHelloTo));
}
void msgHello()
{
Debug.Log("Hello!");
}
void msgHelloTo(string who)
{
Debug.Log("Hello " + who.ToString() + "!");
} }
} }

View File

@@ -1,54 +0,0 @@
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;
}
}