Selaa lähdekoodia

Replaced "is2d" field with axis locks.

Made Actor to work with gameBojects without the MeshFilter component.
Made AddMethodToCache simpler.
master
Ivan "KaiSD" Korystin 10 vuotta sitten
vanhempi
commit
c8186552b9
16 muutettua tiedostoa jossa 109 lisäystä ja 35 poistoa
  1. +54
    -0
      Assets/scripts/coa4u/ActionsBase/Action.cs
  2. +3
    -3
      Assets/scripts/coa4u/ActionsInstant/ActionSetDirection.cs
  3. +3
    -3
      Assets/scripts/coa4u/ActionsInstant/ActionSetPlace.cs
  4. +3
    -1
      Assets/scripts/coa4u/ActionsInstant/ActionSetRotation.cs
  5. +6
    -6
      Assets/scripts/coa4u/ActionsInterval/ActionBezierAbs.cs
  6. +0
    -1
      Assets/scripts/coa4u/ActionsInterval/ActionBezierRel.cs
  7. +0
    -1
      Assets/scripts/coa4u/ActionsInterval/ActionJumpBy.cs
  8. +3
    -3
      Assets/scripts/coa4u/ActionsInterval/ActionJumpTo.cs
  9. +0
    -1
      Assets/scripts/coa4u/ActionsInterval/ActionMoveBy.cs
  10. +3
    -3
      Assets/scripts/coa4u/ActionsInterval/ActionMoveTo.cs
  11. +0
    -1
      Assets/scripts/coa4u/ActionsInterval/ActionRotateBy.cs
  12. +3
    -1
      Assets/scripts/coa4u/ActionsInterval/ActionRotateTo.cs
  13. +0
    -1
      Assets/scripts/coa4u/ActionsInterval/ActionScaleBy.cs
  14. +3
    -3
      Assets/scripts/coa4u/ActionsInterval/ActionScaleTo.cs
  15. +26
    -5
      Assets/scripts/coa4u/UnityComponents/Actor.cs
  16. +2
    -2
      Assets/scripts/coa4u/UnityComponents/ActorSampleActions.cs

+ 54
- 0
Assets/scripts/coa4u/ActionsBase/Action.cs Näytä tiedosto

@@ -4,11 +4,24 @@ using UnityEngine;

namespace coa4u
{
public enum Axises
{
none,
x,
y,
z,
xy,
xz,
yz,
xyz
}

/// <summary>
/// Basic action class for subclassing. To inherit interval actions, consider using the ActionInterval as the base class.
/// </summary>
public class ActionInstant
{
public Axises locks = Axises.none;
protected Actor target;
protected Transform transform;
protected Renderer renderer;
@@ -74,6 +87,47 @@ namespace coa4u
target = actor;
}

/// <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>
/// Readonly property. Shows the remaining time of the action.
/// </summary>


+ 3
- 3
Assets/scripts/coa4u/ActionsInstant/ActionSetDirection.cs Näytä tiedosto

@@ -21,7 +21,7 @@ namespace coa4u
public ActionSetDirection(Vector2 tgtValue)
: this((Vector3)tgtValue)
{
is2d = true;
locks = Axises.z;
}

public ActionSetDirection(Actor tgtActor)
@@ -48,11 +48,11 @@ namespace coa4u
{
value = other.transform.position;
}
if (is2d)
if (locks != Axises.none)
{
value.z = transform.position.z;
}
transform.rotation = Quaternion.LookRotation(transform.position - value);
transform.rotation = Quaternion.LookRotation(value - transform.position);
}
}
}

+ 3
- 3
Assets/scripts/coa4u/ActionsInstant/ActionSetPlace.cs Näytä tiedosto

@@ -20,7 +20,7 @@ namespace coa4u
public ActionSetPlace(Vector2 tgtPlace)
: this((Vector3)tgtPlace)
{
is2d = true;
locks = Axises.z;
}

/// <summary>
@@ -37,8 +37,8 @@ namespace coa4u
public override void start()
{
base.start();
if (is2d)
value.z = transform.position.z;
if (locks != Axises.none)
lockAxises(ref value);
transform.position = value;
}
}

+ 3
- 1
Assets/scripts/coa4u/ActionsInstant/ActionSetRotation.cs Näytä tiedosto

@@ -20,7 +20,7 @@ namespace coa4u
public ActionSetRotation(float angle)
: this(new Vector3(0, 0, angle))
{
is2d = true;
locks = Axises.xy;
}

/// <summary>
@@ -37,6 +37,8 @@ namespace coa4u
public override void start()
{
base.start();
if (locks != Axises.none)
lockAxises(ref value);
transform.rotation = Quaternion.Euler(value);
}
}

+ 6
- 6
Assets/scripts/coa4u/ActionsInterval/ActionBezierAbs.cs Näytä tiedosto

@@ -26,7 +26,7 @@ namespace coa4u
public ActionBezierAbs(Vector2 tgtStart, Vector2 tgtSCP, Vector2 tgtECP, Vector2 tgtEnd, float tgtDuration)
: this((Vector3)tgtStart, (Vector3)tgtSCP, (Vector3)tgtECP, (Vector3)tgtEnd, tgtDuration)
{
is2d = true;
locks = Axises.z;
}

/// <summary>
@@ -52,12 +52,12 @@ namespace coa4u
{
base.start();
float z = transform.position.z;
if (is2d)
if (locks != Axises.none)
{
startPoint.z = z;
endPoint.z = z;
startControlPoint.z = z;
endControlPoint.z = z;
lockAxises(ref startPoint);
lockAxises(ref endPoint);
lockAxises(ref startControlPoint);
lockAxises(ref endControlPoint);
}
}



+ 0
- 1
Assets/scripts/coa4u/ActionsInterval/ActionBezierRel.cs Näytä tiedosto

@@ -28,7 +28,6 @@ namespace coa4u
public ActionBezierRel(Vector2 tgtSCP, Vector2 tgtECP, Vector2 tgtEnd, float tgtDuration)
: this((Vector3)tgtSCP, (Vector3)tgtECP, (Vector3)tgtEnd, tgtDuration)
{
is2d = true;
}

/// <summary>


+ 0
- 1
Assets/scripts/coa4u/ActionsInterval/ActionJumpBy.cs Näytä tiedosto

@@ -22,7 +22,6 @@ namespace coa4u
public ActionJumpBy(Vector2 tgtPoint, float tgtHeight, int tgtJumps, float tgtDuration)
: this((Vector3)tgtPoint, tgtHeight, tgtJumps, tgtDuration)
{
is2d = true;
}

public override ActionInstant clone()


+ 3
- 3
Assets/scripts/coa4u/ActionsInterval/ActionJumpTo.cs Näytä tiedosto

@@ -22,7 +22,7 @@ namespace coa4u
public ActionJumpTo(Vector2 tgtPoint, float tgtHeight, int tgtJumps, float tgtDuration)
: this((Vector3)tgtPoint, tgtHeight, tgtJumps, tgtDuration)
{
is2d = true;
locks = Axises.z;
}

public override ActionInstant clone()
@@ -33,8 +33,8 @@ namespace coa4u
public override void start()
{
float coeff = 1F / jumps;
if (is2d)
point.z = transform.position.z;
if (locks != Axises.none)
lockAxises(ref point);
Vector3 start = target.gameObject.transform.position;
Vector3 end = (point - start) * coeff;
Vector3 cp1 = Vector3.up * height;


+ 0
- 1
Assets/scripts/coa4u/ActionsInterval/ActionMoveBy.cs Näytä tiedosto

@@ -17,7 +17,6 @@ namespace coa4u
public ActionMoveBy(Vector2 tgtValue, float tgtDuration)
: this((Vector3)tgtValue, tgtDuration)
{
is2d = true;
}

public override ActionInstant clone()


+ 3
- 3
Assets/scripts/coa4u/ActionsInterval/ActionMoveTo.cs Näytä tiedosto

@@ -18,7 +18,7 @@ namespace coa4u
public ActionMoveTo(Vector2 tgtValue, float tgtDuration)
: this((Vector3)tgtValue, tgtDuration)
{
is2d = true;
locks = Axises.z;
}

public override ActionInstant clone()
@@ -29,8 +29,8 @@ namespace coa4u
public override void start()
{
base.start();
if (is2d)
value.z = transform.position.z;
if (locks != Axises.none)
lockAxises(ref value);
path = value - transform.position;
}



+ 0
- 1
Assets/scripts/coa4u/ActionsInterval/ActionRotateBy.cs Näytä tiedosto

@@ -17,7 +17,6 @@ namespace coa4u
public ActionRotateBy(float angle, float tgtDuration)
: this(new Vector3(0, 0, angle), tgtDuration)
{
is2d = true;
}

public override ActionInstant clone()


+ 3
- 1
Assets/scripts/coa4u/ActionsInterval/ActionRotateTo.cs Näytä tiedosto

@@ -18,7 +18,7 @@ namespace coa4u
public ActionRotateTo(float angle, float tgtDuration)
: this(new Vector3(0, 0, angle), tgtDuration)
{
is2d = true;
locks = Axises.xy;
}

public override ActionInstant clone()
@@ -39,6 +39,8 @@ namespace coa4u
else
path[i] = t + 360 - f;
}
if (locks != Axises.none)
lockAxises(ref path);
}
public override void stepInterval(float dt)
{


+ 0
- 1
Assets/scripts/coa4u/ActionsInterval/ActionScaleBy.cs Näytä tiedosto

@@ -18,7 +18,6 @@ namespace coa4u
public ActionScaleBy(Vector2 tgtValue, float tgtDuration)
: this((Vector3)tgtValue, tgtDuration)
{
is2d = true;
}

public override ActionInstant clone()


+ 3
- 3
Assets/scripts/coa4u/ActionsInterval/ActionScaleTo.cs Näytä tiedosto

@@ -18,7 +18,7 @@ namespace coa4u
public ActionScaleTo(Vector2 tgtValue, float tgtDuration)
: this((Vector3)tgtValue, tgtDuration)
{
is2d = true;
locks = Axises.z;
}

public override ActionInstant clone()
@@ -29,8 +29,8 @@ namespace coa4u
public override void start()
{
base.start();
if (is2d)
value.z = transform.localScale.z;
if (locks != Axises.none)
lockAxises(ref value);
path = value - transform.localScale;
}



+ 26
- 5
Assets/scripts/coa4u/UnityComponents/Actor.cs Näytä tiedosto

@@ -16,10 +16,15 @@ public class Actor : MonoBehaviour
protected Vector3 angles2prev = Vector3.zero;
protected const float coeff = Mathf.PI / 180;
protected Vector3[] initialState;
[HideInInspector]
public Transform transformCached;
[HideInInspector]
public Renderer rendererCached;
[HideInInspector]
public Mesh meshCached;
[HideInInspector]
public Vector3 skewAngles1;
[HideInInspector]
public Vector3 skewAngles2;

/// <summary>
@@ -29,8 +34,13 @@ public class Actor : MonoBehaviour
{
transformCached = gameObject.transform;
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>
@@ -51,6 +61,9 @@ public class Actor : MonoBehaviour
/// </summary>
void updateSkew()
{
if (meshCached == null)
return;

Matrix4x4 m = Matrix4x4.zero;
m[0, 0] = 1;
m[1, 1] = 1;
@@ -130,13 +143,21 @@ public class Actor : MonoBehaviour
}

/// <summary>
/// Adds method to cache to speed-up
/// Adds method to cache to speed-up the ActionSendMessage.
/// </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)
{
if (methodsCache.ContainsKey(key))


+ 2
- 2
Assets/scripts/coa4u/UnityComponents/ActorSampleActions.cs Näytä tiedosto

@@ -39,8 +39,8 @@ public class ActorSampleActions : Actor
}), 5);
this.AttachAction(seq);

AddMethodToCache(new MethodHolder(msgHello));
AddMethodToCache(new MethodHolder<string>(msgHelloTo));
AddMethodToCache(msgHello);
AddMethodToCache<string>(msgHelloTo);
}

void msgHello()