Initial Commit

This commit is contained in:
Simeon Radivoev 2022-02-12 12:53:50 +02:00
commit ee5c2f922d
Signed by: simeonradivoev
GPG key ID: 7611A451D2A5D37A
2255 changed files with 547750 additions and 0 deletions

View file

@ -0,0 +1,267 @@
using DefaultNamespace;
using System;
using Unity.Entities;
using UnityEngine;
namespace Tween
{
public enum EaseType
{
linear,
easeInQuad,
easeOutQuad,
easeInOutQuad,
easeInCubic,
easeOutCubic,
easeInOutCubic,
easeInQuart,
easeOutQuart,
easeInOutQuart,
easeInQuint,
easeOutQuint,
easeInOutQuint,
easeInSine,
easeOutSine,
easeInOutSine,
easeInExpo,
easeOutExpo,
easeInOutExpo,
easeInCirc,
easeOutCirc,
easeInOutCirc,
spring,
easeInBounce,
easeOutBounce,
easeInOutBounce,
easeInBack,
easeOutBack,
easeInOutBack,
easeInElastic,
easeOutElastic,
easeInOutElastic
}
public struct TweenData : IComponentData
{
public bool1 Cancel;
public bool1 Initialized;
public float Time;
public float Speed;
public EaseType EaseType;
public Entity Target;
public TweenData(float speed, EaseType easeType, Entity target)
{
Time = 0;
Speed = speed;
EaseType = easeType;
Target = target;
Initialized = false;
Cancel = false;
}
}
public interface ITween : ISharedComponentData
{
}
public struct TweenValueFromToData : ITween, IEquatable<TweenValueFromToData>
{
public float From;
public float To;
public Action<float> OnUpdate;
public TweenValueFromToData(float from, float to, Action<float> onUpdate)
{
From = from;
To = to;
OnUpdate = onUpdate;
}
public bool Equals(TweenValueFromToData other)
{
return From.Equals(other.From) && To.Equals(other.To) && Equals(OnUpdate, other.OnUpdate);
}
public override bool Equals(object obj)
{
return obj is TweenValueFromToData other && Equals(other);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = From.GetHashCode();
hashCode = (hashCode * 397) ^ To.GetHashCode();
hashCode = (hashCode * 397) ^ (OnUpdate != null ? OnUpdate.GetHashCode() : 0);
return hashCode;
}
}
}
public struct TweenMoveFromToData : ITween
{
public Vector2 From;
public Vector2 To;
public Space Space;
public bool IgnorePhysics;
public TweenMoveFromToData(Vector2 from, Vector2 to, Space space, bool ignorePhysics = false)
{
IgnorePhysics = ignorePhysics;
From = from;
To = to;
Space = space;
}
}
public struct TweenMoveToMouseData : ITween
{
public Vector2 FromPosition;
public TweenMoveToMouseData(Vector2 fromPosition, float depth)
{
FromPosition = fromPosition;
}
}
public struct TweenRotateFromToData : ITween
{
public float From;
public float To;
public Space Space;
public TweenRotateFromToData(float from, float to, Space space)
{
From = from;
To = to;
Space = space;
}
}
public struct TweenScaleFromToData : ITween
{
public Vector2 From;
public Vector2 To;
public TweenScaleFromToData(Vector2 from, Vector2 to)
{
From = from;
To = to;
}
}
public struct ParticleEmissionData : ITween, IEquatable<ParticleEmissionData>
{
public int Rate;
public ParticleSystem ParticleSystem;
public Transform Target;
public Vector2 Position;
public float SpawnCount;
public ParticleEmissionData(int rate, ParticleSystem particleSystem, Transform target)
{
Rate = rate;
ParticleSystem = particleSystem;
Target = target;
Position = Vector2.zero;
SpawnCount = 1;
}
public ParticleEmissionData(int rate, ParticleSystem particleSystem, Vector2 position)
{
Rate = rate;
ParticleSystem = particleSystem;
Target = null;
Position = position;
SpawnCount = 1;
}
public bool Equals(ParticleEmissionData other)
{
return Rate == other.Rate &&
Equals(ParticleSystem, other.ParticleSystem) &&
Equals(Target, other.Target) &&
Position.Equals(other.Position) &&
SpawnCount.Equals(other.SpawnCount);
}
public override bool Equals(object obj)
{
return obj is ParticleEmissionData other && Equals(other);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = Rate;
hashCode = (hashCode * 397) ^ (ParticleSystem != null ? ParticleSystem.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ (Target != null ? Target.GetHashCode() : 0);
hashCode = (hashCode * 397) ^ Position.GetHashCode();
hashCode = (hashCode * 397) ^ SpawnCount.GetHashCode();
return hashCode;
}
}
}
public struct TweenFollowPath : ITween, IEquatable<TweenFollowPath>
{
public Vector2[] path;
public Space Space;
public static TweenFollowPath Build(Vector2[] path, Space space)
{
var tween = new TweenFollowPath { Space = space };
Vector2[] suppliedPath;
Vector2[] vector3s;
//create and store path points:
suppliedPath = path;
//populate calculate path;
var offset = 2;
vector3s = new Vector2[suppliedPath.Length + offset];
Array.Copy(suppliedPath, 0, vector3s, 1, suppliedPath.Length);
//populate start and end control points:
//vector3s[0] = vector3s[1] - vector3s[2];
vector3s[0] = vector3s[1] + (vector3s[1] - vector3s[2]);
vector3s[vector3s.Length - 1] = vector3s[vector3s.Length - 2] + (vector3s[vector3s.Length - 2] - vector3s[vector3s.Length - 3]);
//is this a closed, continuous loop? yes? well then so let's make a continuous Catmull-Rom spline!
if (vector3s[1] == vector3s[vector3s.Length - 2])
{
var tmpLoopSpline = new Vector2[vector3s.Length];
Array.Copy(vector3s, tmpLoopSpline, vector3s.Length);
tmpLoopSpline[0] = tmpLoopSpline[tmpLoopSpline.Length - 3];
tmpLoopSpline[tmpLoopSpline.Length - 1] = tmpLoopSpline[2];
vector3s = new Vector2[tmpLoopSpline.Length];
Array.Copy(tmpLoopSpline, vector3s, tmpLoopSpline.Length);
}
tween.path = vector3s;
return tween;
}
public bool Equals(TweenFollowPath other)
{
return Equals(path, other.path) && Space == other.Space;
}
public override bool Equals(object obj)
{
return obj is TweenFollowPath other && Equals(other);
}
public override int GetHashCode()
{
unchecked
{
return ((path != null ? path.GetHashCode() : 0) * 397) ^ (int)Space;
}
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 6bff71545ad3428d8e92e3bd93fc8bba
timeCreated: 1532429514

View file

@ -0,0 +1,511 @@
using Tween;
using UnityEngine;
public static class TweenEase
{
public static float EaseInQuad(float start, float end, float value)
{
end -= start;
return end * value * value + start;
}
public static float EaseOutQuad(float start, float end, float value)
{
end -= start;
return -end * value * (value - 2) + start;
}
public static float EaseInOutQuad(float start, float end, float value)
{
value /= .5f;
end -= start;
if (value < 1)
{
return end * 0.5f * value * value + start;
}
value--;
return -end * 0.5f * (value * (value - 2) - 1) + start;
}
public static float EaseInCubic(float start, float end, float value)
{
end -= start;
return end * value * value * value + start;
}
public static float EaseOutCubic(float start, float end, float value)
{
value--;
end -= start;
return end * (value * value * value + 1) + start;
}
public static float EaseInOutCubic(float start, float end, float value)
{
value /= .5f;
end -= start;
if (value < 1)
{
return end * 0.5f * value * value * value + start;
}
value -= 2;
return end * 0.5f * (value * value * value + 2) + start;
}
public static float EaseInQuart(float start, float end, float value)
{
end -= start;
return end * value * value * value * value + start;
}
public static float EaseOutQuart(float start, float end, float value)
{
value--;
end -= start;
return -end * (value * value * value * value - 1) + start;
}
public static float EaseInOutQuart(float start, float end, float value)
{
value /= .5f;
end -= start;
if (value < 1)
{
return end * 0.5f * value * value * value * value + start;
}
value -= 2;
return -end * 0.5f * (value * value * value * value - 2) + start;
}
public static float EaseInQuint(float start, float end, float value)
{
end -= start;
return end * value * value * value * value * value + start;
}
public static float EaseOutQuint(float start, float end, float value)
{
value--;
end -= start;
return end * (value * value * value * value * value + 1) + start;
}
public static float EaseInOutQuint(float start, float end, float value)
{
value /= .5f;
end -= start;
if (value < 1)
{
return end * 0.5f * value * value * value * value * value + start;
}
value -= 2;
return end * 0.5f * (value * value * value * value * value + 2) + start;
}
public static float EaseInSine(float start, float end, float value)
{
end -= start;
return -end * Mathf.Cos(value * (Mathf.PI * 0.5f)) + end + start;
}
public static float EaseOutSine(float start, float end, float value)
{
end -= start;
return end * Mathf.Sin(value * (Mathf.PI * 0.5f)) + start;
}
public static float EaseInOutSine(float start, float end, float value)
{
end -= start;
return -end * 0.5f * (Mathf.Cos(Mathf.PI * value) - 1) + start;
}
public static float EaseInExpo(float start, float end, float value)
{
end -= start;
return end * Mathf.Pow(2, 10 * (value - 1)) + start;
}
public static float EaseOutExpo(float start, float end, float value)
{
end -= start;
return end * (-Mathf.Pow(2, -10 * value) + 1) + start;
}
public static float EaseInOutExpo(float start, float end, float value)
{
value /= .5f;
end -= start;
if (value < 1)
{
return end * 0.5f * Mathf.Pow(2, 10 * (value - 1)) + start;
}
value--;
return end * 0.5f * (-Mathf.Pow(2, -10 * value) + 2) + start;
}
public static float EaseInCirc(float start, float end, float value)
{
end -= start;
return -end * (Mathf.Sqrt(1 - value * value) - 1) + start;
}
public static float EaseOutCirc(float start, float end, float value)
{
value--;
end -= start;
return end * Mathf.Sqrt(1 - value * value) + start;
}
public static float EaseInOutCirc(float start, float end, float value)
{
value /= .5f;
end -= start;
if (value < 1)
{
return -end * 0.5f * (Mathf.Sqrt(1 - value * value) - 1) + start;
}
value -= 2;
return end * 0.5f * (Mathf.Sqrt(1 - value * value) + 1) + start;
}
/* GFX47 MOD START */
public static float EaseInBounce(float start, float end, float value)
{
end -= start;
var d = 1f;
return end - EaseOutBounce(0, end, d - value) + start;
}
/* GFX47 MOD END */
/* GFX47 MOD START */
//private float bounce(float start, float end, float value){
public static float EaseOutBounce(float start, float end, float value)
{
value /= 1f;
end -= start;
if (value < 1 / 2.75f)
{
return end * (7.5625f * value * value) + start;
}
if (value < 2 / 2.75f)
{
value -= 1.5f / 2.75f;
return end * (7.5625f * value * value + .75f) + start;
}
if (value < 2.5 / 2.75)
{
value -= 2.25f / 2.75f;
return end * (7.5625f * value * value + .9375f) + start;
}
value -= 2.625f / 2.75f;
return end * (7.5625f * value * value + .984375f) + start;
}
/* GFX47 MOD END */
/* GFX47 MOD START */
public static float EaseInOutBounce(float start, float end, float value)
{
end -= start;
var d = 1f;
if (value < d * 0.5f)
{
return EaseInBounce(0, end, value * 2) * 0.5f + start;
}
return EaseOutBounce(0, end, value * 2 - d) * 0.5f + end * 0.5f + start;
}
/* GFX47 MOD END */
public static float EaseInBack(float start, float end, float value)
{
end -= start;
value /= 1;
var s = 1.70158f;
return end * value * value * ((s + 1) * value - s) + start;
}
public static float EaseOutBack(float start, float end, float value)
{
var s = 1.70158f;
end -= start;
value = value - 1;
return end * (value * value * ((s + 1) * value + s) + 1) + start;
}
public static float EaseInOutBack(float start, float end, float value)
{
var s = 1.70158f;
end -= start;
value /= .5f;
if (value < 1)
{
s *= 1.525f;
return end * 0.5f * (value * value * ((s + 1) * value - s)) + start;
}
value -= 2;
s *= 1.525f;
return end * 0.5f * (value * value * ((s + 1) * value + s) + 2) + start;
}
public static float Punch(float amplitude, float value)
{
float s = 9;
if (value == 0)
{
return 0;
}
if (value == 1)
{
return 0;
}
var period = 1 * 0.3f;
s = period / (2 * Mathf.PI) * Mathf.Asin(0);
return amplitude * Mathf.Pow(2, -10 * value) * Mathf.Sin((value * 1 - s) * (2 * Mathf.PI) / period);
}
/* GFX47 MOD START */
public static float EaseInElastic(float start, float end, float value)
{
end -= start;
var d = 1f;
var p = d * .3f;
float s = 0;
float a = 0;
if (value == 0)
{
return start;
}
if ((value /= d) == 1)
{
return start + end;
}
if (a == 0f || a < Mathf.Abs(end))
{
a = end;
s = p / 4;
}
else
{
s = p / (2 * Mathf.PI) * Mathf.Asin(end / a);
}
return -(a * Mathf.Pow(2, 10 * (value -= 1)) * Mathf.Sin((value * d - s) * (2 * Mathf.PI) / p)) + start;
}
/* GFX47 MOD END */
/* GFX47 MOD START */
//private float elastic(float start, float end, float value){
public static float EaseOutElastic(float start, float end, float value)
{
/* GFX47 MOD END */
//Thank you to rafael.marteleto for fixing this as a port over from Pedro's UnityTween
end -= start;
var d = 1f;
var p = d * .3f;
float s = 0;
float a = 0;
if (value == 0)
{
return start;
}
if ((value /= d) == 1)
{
return start + end;
}
if (a == 0f || a < Mathf.Abs(end))
{
a = end;
s = p * 0.25f;
}
else
{
s = p / (2 * Mathf.PI) * Mathf.Asin(end / a);
}
return a * Mathf.Pow(2, -10 * value) * Mathf.Sin((value * d - s) * (2 * Mathf.PI) / p) + end + start;
}
/* GFX47 MOD START */
public static float EaseInOutElastic(float start, float end, float value)
{
end -= start;
var d = 1f;
var p = d * .3f;
float s = 0;
float a = 0;
if (value == 0)
{
return start;
}
if ((value /= d * 0.5f) == 2)
{
return start + end;
}
if (a == 0f || a < Mathf.Abs(end))
{
a = end;
s = p / 4;
}
else
{
s = p / (2 * Mathf.PI) * Mathf.Asin(end / a);
}
if (value < 1)
{
return -0.5f * (a * Mathf.Pow(2, 10 * (value -= 1)) * Mathf.Sin((value * d - s) * (2 * Mathf.PI) / p)) + start;
}
return a * Mathf.Pow(2, -10 * (value -= 1)) * Mathf.Sin((value * d - s) * (2 * Mathf.PI) / p) * 0.5f + end + start;
}
public static float Linear(float start, float end, float value)
{
return Mathf.Lerp(start, end, value);
}
public static float Spring(float start, float end, float value)
{
value = Mathf.Clamp01(value);
value = (Mathf.Sin(value * Mathf.PI * (0.2f + 2.5f * value * value * value)) * Mathf.Pow(1f - value, 2.2f) + value) *
(1f + 1.2f * (1f - value));
return start + (end - start) * value;
}
public static float Ease(this EaseType easeType, float value)
{
return Ease(easeType, 0, 1, value);
}
public static float Ease(this EaseType easeType, float start, float end, float value)
{
switch (easeType)
{
case EaseType.easeInQuad:
return EaseInQuad(start, end, value);
case EaseType.easeOutQuad:
return EaseOutQuad(start, end, value);
case EaseType.easeInOutQuad:
return EaseInOutQuad(start, end, value);
case EaseType.easeInCubic:
return EaseInCubic(start, end, value);
case EaseType.easeOutCubic:
return EaseOutCubic(start, end, value);
case EaseType.easeInOutCubic:
return EaseInOutCubic(start, end, value);
case EaseType.easeInQuart:
return EaseInQuart(start, end, value);
case EaseType.easeOutQuart:
return EaseOutQuart(start, end, value);
case EaseType.easeInOutQuart:
return EaseInOutQuart(start, end, value);
case EaseType.easeInQuint:
return EaseInQuint(start, end, value);
case EaseType.easeOutQuint:
return EaseOutQuint(start, end, value);
case EaseType.easeInOutQuint:
return EaseInOutQuint(start, end, value);
case EaseType.easeInSine:
return EaseInSine(start, end, value);
case EaseType.easeOutSine:
return EaseOutSine(start, end, value);
case EaseType.easeInOutSine:
return EaseInOutSine(start, end, value);
case EaseType.easeInExpo:
return EaseInExpo(start, end, value);
case EaseType.easeOutExpo:
return EaseOutExpo(start, end, value);
case EaseType.easeInOutExpo:
return EaseInOutExpo(start, end, value);
case EaseType.easeInCirc:
return EaseInCirc(start, end, value);
case EaseType.easeOutCirc:
return EaseOutCirc(start, end, value);
case EaseType.easeInOutCirc:
return EaseInOutCirc(start, end, value);
case EaseType.linear:
return Linear(start, end, value);
case EaseType.spring:
return Spring(start, end, value);
/* GFX47 MOD START */
/*case EaseType.bounce:
return new EasingFunction(bounce);
*/
case EaseType.easeInBounce:
return EaseInBounce(start, end, value);
case EaseType.easeOutBounce:
return EaseOutBounce(start, end, value);
case EaseType.easeInOutBounce:
return EaseInOutBounce(start, end, value);
/* GFX47 MOD END */
case EaseType.easeInBack:
return EaseInBack(start, end, value);
case EaseType.easeOutBack:
return EaseOutBack(start, end, value);
case EaseType.easeInOutBack:
return EaseInOutBack(start, end, value);
/* GFX47 MOD START */
/*case EaseType.elastic:
return new EasingFunction(elastic);
*/
case EaseType.easeInElastic:
return EaseInElastic(start, end, value);
case EaseType.easeOutElastic:
return EaseOutElastic(start, end, value);
case EaseType.easeInOutElastic:
return EaseInOutElastic(start, end, value);
/* GFX47 MOD END */
default:
return Linear(start, end, value);
}
}
/* GFX47 MOD END */
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0b0f14b48616f984787fbcdd4e03ada7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,16 @@
using Unity.Entities;
using UnityEngine;
namespace Tween
{
public static class TweenExtensions
{
public static void StartTween
<T>(this EntityCommandBuffer buffer, Entity target, float time, EaseType easeType, T tween) where T : struct, ITween
{
var entity = buffer.CreateEntity();
buffer.AddComponent(entity, new TweenData(1f / Mathf.Max(time, float.Epsilon), easeType, target));
buffer.AddSharedComponent(entity, tween);
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f6d54c5f1b5c48458554a6e2088543ae
timeCreated: 1532432715

View file

@ -0,0 +1,301 @@
using Unity.Collections;
using Unity.Entities;
using UnityEngine;
namespace Tween
{
public class TweenSystem : ComponentSystem
{
private NativeMultiHashMap<Entity, Entity> ActiveTweens;
protected override void OnCreate()
{
ActiveTweens = new NativeMultiHashMap<Entity, Entity>(8, Allocator.Persistent);
}
protected override void OnDestroy()
{
ActiveTweens.Dispose();
}
protected override void OnUpdate()
{
var deltaTime = Time.DeltaTime;
Entities.ForEach(
(TweenValueFromToData props, ref TweenData data) =>
{
if (data.Cancel)
{
return;
}
props.OnUpdate?.Invoke(Mathf.Lerp(props.From, props.To, data.EaseType.Ease(data.Time)));
});
Entities.ForEach(
(TweenMoveFromToData props, ref TweenData data) =>
{
if (data.Cancel)
{
return;
}
var pos = Vector2.Lerp(props.From, props.To, data.EaseType.Ease(data.Time));
if (EntityManager.HasComponent<Rigidbody2D>(data.Target))
{
var rigidBody = EntityManager.GetComponentObject<Rigidbody2D>(data.Target);
if (props.IgnorePhysics)
{
if (props.Space == Space.World)
{
rigidBody.position = pos;
}
else
{
rigidBody.position = rigidBody.transform.TransformPoint(pos);
}
}
else
{
if (props.Space == Space.World)
{
rigidBody.MovePosition(pos);
}
else
{
rigidBody.MovePosition(rigidBody.transform.TransformPoint(pos));
}
}
}
else if (EntityManager.HasComponent<Transform>(data.Target))
{
var transform = EntityManager.GetComponentObject<Transform>(data.Target);
switch (props.Space)
{
case Space.World:
transform.position = pos;
break;
case Space.Self:
transform.localPosition = pos;
break;
}
}
else if (EntityManager.HasComponent<RectTransform>(data.Target))
{
var transform = EntityManager.GetComponentObject<RectTransform>(data.Target);
switch (props.Space)
{
case Space.World:
transform.position = pos;
break;
case Space.Self:
transform.localPosition = pos;
break;
}
}
});
Entities.ForEach(
(TweenMoveToMouseData props, ref TweenData data) =>
{
if (data.Cancel)
{
return;
}
var time = data.EaseType.Ease(data.Time);
if (EntityManager.HasComponent<RectTransform>(data.Target))
{
var rectTransform = EntityManager.GetComponentObject<RectTransform>(data.Target);
rectTransform.anchoredPosition = Vector2.Lerp(props.FromPosition, Input.mousePosition, time);
}
});
Entities.ForEach(
(TweenRotateFromToData props, ref TweenData data) =>
{
if (data.Cancel)
{
return;
}
var angle = Mathf.LerpAngle(props.From, props.To, data.EaseType.Ease(data.Time));
if (EntityManager.HasComponent<Rigidbody2D>(data.Target))
{
var rigidBody = EntityManager.GetComponentObject<Rigidbody2D>(data.Target);
if (props.Space == Space.World)
{
rigidBody.MoveRotation(angle);
}
}
else if (EntityManager.HasComponent<Transform>(data.Target))
{
var transform = EntityManager.GetComponentObject<Transform>(data.Target);
switch (props.Space)
{
case Space.World:
transform.rotation = Quaternion.Euler(0, 0, angle);
break;
case Space.Self:
transform.localRotation = Quaternion.Euler(0, 0, angle);
break;
}
}
else if (EntityManager.HasComponent<RectTransform>(data.Target))
{
var rectTransform = EntityManager.GetComponentObject<RectTransform>(data.Target);
switch (props.Space)
{
case Space.World:
rectTransform.rotation = Quaternion.Euler(0, 0, angle);
break;
case Space.Self:
rectTransform.localRotation = Quaternion.Euler(0, 0, angle);
break;
}
}
});
Entities.ForEach(
(TweenScaleFromToData props, ref TweenData data) =>
{
if (data.Cancel)
{
return;
}
var scale = Vector2.Lerp(props.From, props.To, data.EaseType.Ease(data.Time));
if (EntityManager.HasComponent<Transform>(data.Target))
{
var transform = EntityManager.GetComponentObject<Transform>(data.Target);
transform.localScale = new Vector3(scale.x, scale.y, transform.localScale.z);
}
else if (EntityManager.HasComponent<RectTransform>(data.Target))
{
var rectTransform = EntityManager.GetComponentObject<RectTransform>(data.Target);
rectTransform.localScale = new Vector3(scale.x, scale.y, rectTransform.localScale.z);
}
});
Entities.ForEach(
(Entity entity, ParticleEmissionData props, ref TweenData data) =>
{
if (data.Cancel)
{
return;
}
var rate = deltaTime * props.Rate;
props.SpawnCount += rate;
var spawnCount = Mathf.FloorToInt(props.SpawnCount);
if (spawnCount > 0)
{
props.ParticleSystem.Emit(
new ParticleSystem.EmitParams { position = props.Target != null ? (Vector2)props.Target.position : props.Position },
spawnCount);
props.SpawnCount -= spawnCount;
}
PostUpdateCommands.SetSharedComponent(entity, props);
});
Entities.ForEach(
(TweenFollowPath props, ref TweenData data) =>
{
var point = Interp(props.path, data.EaseType.Ease(data.Time));
if (EntityManager.HasComponent<Rigidbody2D>(data.Target))
{
var rigidBody = EntityManager.GetComponentObject<Rigidbody2D>(data.Target);
if (props.Space == Space.World)
{
rigidBody.MovePosition(point);
}
else
{
rigidBody.MovePosition(rigidBody.transform.TransformPoint(point));
}
}
else if (EntityManager.HasComponent<Transform>(data.Target))
{
var transform = EntityManager.GetComponentObject<Transform>(data.Target);
if (props.Space == Space.World)
{
transform.position = point;
}
else
{
transform.localPosition = point;
}
}
else if (EntityManager.HasComponent<RectTransform>(data.Target))
{
var rectTransform = EntityManager.GetComponentObject<RectTransform>(data.Target);
if (props.Space == Space.World)
{
rectTransform.position = point;
}
else
{
rectTransform.anchoredPosition = point;
}
}
});
Entities.ForEach(
(Entity entity, ref TweenData data) =>
{
if (data.Cancel)
{
return;
}
if (!data.Initialized)
{
ActiveTweens.Add(data.Target, entity);
}
data.Time = Mathf.Clamp01(data.Time + deltaTime * data.Speed);
if (data.Time >= 1)
{
ActiveTweens.Remove(data.Target);
PostUpdateCommands.DestroyEntity(entity);
}
});
}
private static Vector2 Interp(Vector2[] pts, float t)
{
var numSections = pts.Length - 3;
var currPt = Mathf.Min(Mathf.FloorToInt(t * numSections), numSections - 1);
var u = t * numSections - currPt;
var a = pts[currPt];
var b = pts[currPt + 1];
var c = pts[currPt + 2];
var d = pts[currPt + 3];
return .5f * ((-a + 3f * b - 3f * c + d) * (u * u * u) + (2f * a - 5f * b + 4f * c - d) * (u * u) + (-a + c) * u + 2f * b);
}
public void StopAllTweens(Entity target)
{
if (!ActiveTweens.TryGetFirstValue(target, out var tween, out var it))
{
return;
}
var tData = EntityManager.GetComponentData<TweenData>(tween);
tData.Cancel = true;
EntityManager.SetComponentData(tween, tData);
while (ActiveTweens.TryGetNextValue(out tween, ref it))
{
tData = EntityManager.GetComponentData<TweenData>(tween);
tData.Cancel = true;
EntityManager.SetComponentData(tween, tData);
}
}
public bool HasTween(Entity target)
{
return ActiveTweens.TryGetFirstValue(target, out _, out _);
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 16174ed7ebcd41dba92c2c0a6a058c9e
timeCreated: 1532429811