using System.Numerics;
|
|
|
|
namespace Smoll.Ex2 {
|
|
sealed class InertiaComponent : Component {
|
|
public Transform2D transform;
|
|
public float dampening;
|
|
public float minimalDampening = 2.0f;
|
|
private Transform2DComponent? ownerTransform;
|
|
|
|
public InertiaComponent(float dampening = 1)
|
|
: this(dampening,0,0,0)
|
|
{}
|
|
|
|
public InertiaComponent(float dampening, float x, float y)
|
|
: this(dampening,x,y,0)
|
|
{}
|
|
|
|
public InertiaComponent(float dampening, float x, float y, float angle) {
|
|
transform = new Transform2D();
|
|
transform.position = y*Complex.ImaginaryOne + x;
|
|
transform.angle = angle;
|
|
transform.scale = 1;
|
|
this.dampening = dampening;
|
|
}
|
|
|
|
public InertiaComponent(float dampening, Transform2D tr) {
|
|
transform = tr;
|
|
this.dampening = dampening;
|
|
}
|
|
|
|
|
|
public override void OnAttached() {
|
|
ownerTransform = owner.GetComponent<Transform2DComponent>();
|
|
}
|
|
|
|
public override void Update(float deltaTimeSeconds)
|
|
{
|
|
ownerTransform ??= owner.GetComponent<Transform2DComponent>();
|
|
ownerTransform ??= new Transform2DComponent();
|
|
ownerTransform.transform.position += transform.position;
|
|
ownerTransform.transform.angle += transform.angle;
|
|
|
|
if(dampening != 1) {
|
|
if(transform.position.Magnitude > minimalDampening) {
|
|
transform.position *= dampening;
|
|
} else {
|
|
transform.position *= 0;
|
|
}
|
|
|
|
if(transform.angle > minimalDampening) {
|
|
transform.angle *= dampening;
|
|
} else {
|
|
transform.angle *= 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|