A Smoll game engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
2.0 KiB

using System.Numerics;
using Raylib_cs;
namespace Smoll.Ex2 {
class ArrowComponent : Smoll.Component {
Complex offset;
public Complex rawValue;
Transform2DComponent? ownerTransform;
public ArrowComponent(int offset_x = 0, int offset_y = 0)
{
this.offset = offset_x + Complex.ImaginaryOne*offset_y;
}
public override void OnAttached() {
ownerTransform = owner.GetComponent<Transform2DComponent>();
}
public Complex Origin() {
ownerTransform ??= owner.GetComponent<Transform2DComponent>();
ownerTransform ??= new Transform2DComponent();
var tr = ownerTransform.AbsoluteTransform();
return tr.position + offset*Complex.Exp(Complex.ImaginaryOne * tr.angle) * tr.scale;
}
public Complex Vector() {
ownerTransform ??= owner.GetComponent<Transform2DComponent>();
ownerTransform ??= new Transform2DComponent();
var tr = ownerTransform.AbsoluteTransform();
return rawValue*Complex.Exp(Complex.ImaginaryOne * tr.angle) * tr.scale;
}
public Tuple<Complex, Complex> OriginAndVector() {
ownerTransform ??= owner.GetComponent<Transform2DComponent>();
ownerTransform ??= new Transform2DComponent();
var tr = ownerTransform.AbsoluteTransform();
return Tuple.Create(tr.position + offset*Complex.Exp(Complex.ImaginaryOne * tr.angle) * tr.scale, rawValue*Complex.Exp(Complex.ImaginaryOne * tr.angle) * tr.scale);
}
public override void Draw(Smoll.Layer.DrawMode drawMode) {
if(drawMode == Layer.DrawMode.Debug) {
var data = OriginAndVector();
Complex start = data.Item1;
Complex end = data.Item1 + data.Item2;
Raylib.DrawLineEx(new Vector2((float)start.Real, (float)start.Imaginary),new Vector2((float)end.Real, (float)end.Imaginary),2f, Color.Red);
}
}
}
}