diff --git a/Expansion1/ExampleInputComponent.cs b/Expansion1/ExampleInputComponent.cs new file mode 100644 index 0000000..be6c937 --- /dev/null +++ b/Expansion1/ExampleInputComponent.cs @@ -0,0 +1,35 @@ +using Smoll; +using Raylib_cs; +using System.Numerics; + +namespace Smoll.Ex1 +{ + class ExampleInputComponent : Component { + Transform2DComponent? transform; + + public float speedPixelPerSecond; + public float angularSpeedTurnPerSecond; + + public ExampleInputComponent(float speedPixelPerSecond, float angularSpeedTurnPerSecond) { + this.speedPixelPerSecond = speedPixelPerSecond; + this.angularSpeedTurnPerSecond = angularSpeedTurnPerSecond; + } + + public override void OnAttached() + { + base.OnAttached(); + transform = owner.GetComponent(); + } + + public override void Update(float deltaTimeSeconds) + { + transform ??= owner.GetComponent(); + if(transform == null) throw new Exception("Example Input used on immovable object"); + transform.transform.position -= Raylib.IsKeyDown(KeyboardKey.Up) * speedPixelPerSecond * deltaTimeSeconds * Complex.ImaginaryOne; + transform.transform.position += Raylib.IsKeyDown(KeyboardKey.Down) * speedPixelPerSecond * deltaTimeSeconds * Complex.ImaginaryOne; + transform.transform.position -= Raylib.IsKeyDown(KeyboardKey.Left) * speedPixelPerSecond * deltaTimeSeconds; + transform.transform.position += Raylib.IsKeyDown(KeyboardKey.Right) * speedPixelPerSecond * deltaTimeSeconds; + transform.transform.angle += Raylib.IsKeyDown(KeyboardKey.L) * angularSpeedTurnPerSecond * 2 * float.Pi * deltaTimeSeconds; + } + } +} \ No newline at end of file diff --git a/Expansion1/RectangleGizmoComponent.cs b/Expansion1/RectangleGizmoComponent.cs index 370e626..25d603e 100644 --- a/Expansion1/RectangleGizmoComponent.cs +++ b/Expansion1/RectangleGizmoComponent.cs @@ -29,8 +29,8 @@ namespace Smoll.Ex1 { (float)(size.Imaginary*tr.scale.Imaginary) ), new Vector2( - (float)(tr.anchor.Real - tr.position.Real), - (float)(tr.anchor.Imaginary - tr.position.Imaginary) + (float)(tr.anchor.Real), + (float)(tr.anchor.Imaginary) ), tr.angle, color diff --git a/Program.cs b/Program.cs index a89ba9e..e80f2e8 100644 --- a/Program.cs +++ b/Program.cs @@ -5,15 +5,23 @@ using Smoll.Ex1; Layer layer = new Layer(); var entity = new Entity(layer); -entity.Attach(new Transform2DComponent()); -entity.Attach(new RectangleGizmoComponent(128, 64, Color.Red)); +entity.Attach(new Transform2DComponent(128, 128, 0, 64, 32)); +entity.Attach(new RectangleGizmoComponent(128, 64, Color.Blue)); +entity.Attach(new ExampleInputComponent(100f, 0.25f)); + +var entity2 = new Entity(entity); +entity2.Attach(new Transform2DComponent(128, 128, 0, 32, 32)); +entity2.Attach(new RectangleGizmoComponent(64, 64, Color.Red)); + +Engine engine = new Engine(); +engine.layers.Add(layer); Raylib.InitWindow(800, 600, "Smoll"); while(!Raylib.WindowShouldClose()) { - layer.Update(); + engine.Update(); Raylib.BeginDrawing(); Raylib.ClearBackground(Color.White); - layer.Draw(); + engine.Draw(); Raylib.DrawFPS(0,0); Raylib.EndDrawing(); } diff --git a/Smoll/Engine.cs b/Smoll/Engine.cs index e9a7f14..7ccc3f7 100644 --- a/Smoll/Engine.cs +++ b/Smoll/Engine.cs @@ -3,6 +3,24 @@ using System.Collections.Generic; namespace Smoll { public class Engine { - List layers; + public Engine() { + layers = new List(); + } + + public List layers; + + public void Update() { + foreach (var layer in layers) + { + layer.Update(); + } + } + + public void Draw() { + foreach (var layer in layers) + { + layer.Draw(); + } + } } } \ No newline at end of file diff --git a/Smoll/Transform2D.cs b/Smoll/Transform2D.cs index 9557688..0adbc79 100644 --- a/Smoll/Transform2D.cs +++ b/Smoll/Transform2D.cs @@ -12,9 +12,28 @@ struct Transform2D { sealed class Transform2DComponent : Component { public Transform2D transform; - public Transform2DComponent() { + public Transform2DComponent() + : this(0,0,0,0,0,1,1) + {} + + public Transform2DComponent(float x, float y) + : this(x,y,0,0,0,1,1) + {} + + public Transform2DComponent(float x, float y, float angle) + : this(x,y,angle,0,0,1,1) + {} + + public Transform2DComponent(float x, float y, float angle, float center_x, float center_y) + : this(x,y,angle,center_x,center_y,1,1) + {} + + public Transform2DComponent(float x, float y, float angle, float center_x, float center_y, float scale_x, float scale_y) { transform = new Transform2D(); - transform.scale = Complex.ImaginaryOne + 1; + transform.position = y*Complex.ImaginaryOne + x; + transform.angle = angle; + transform.anchor = center_y*Complex.ImaginaryOne + center_x; + transform.scale = scale_y*Complex.ImaginaryOne + scale_x; } public Transform2D AbsoluteTransform() { @@ -25,17 +44,12 @@ sealed class Transform2DComponent : Component { var component = up.GetComponent(); if(component != null) { Transform2D combined = component.AbsoluteTransform(); - var unscaled_pos = - (combined.position + transform.position - combined.anchor) - /(Complex.Exp(Complex.ImaginaryOne * combined.angle) - combined.anchor); - var normalized_pos = unscaled_pos/unscaled_pos.Magnitude; - combined.position = unscaled_pos.Magnitude*(transform.scale.Real*normalized_pos.Real + transform.scale.Imaginary*normalized_pos.Imaginary*Complex.ImaginaryOne); - var unscaled_anchor = - (combined.position + transform.anchor - combined.anchor) - /(Complex.Exp(Complex.ImaginaryOne * combined.angle) - combined.anchor); - var normalized_anchor = unscaled_anchor/unscaled_anchor.Magnitude; - combined.anchor = unscaled_anchor.Magnitude*(transform.scale.Real*normalized_anchor.Real + transform.scale.Imaginary*normalized_anchor.Imaginary*Complex.ImaginaryOne); - combined.scale *= transform.scale; + var unscaled_pos = (transform.position - transform.anchor)*Complex.Exp(Complex.ImaginaryOne * combined.angle) + transform.anchor; + combined.position = combined.scale.Real*unscaled_pos.Real + combined.scale.Imaginary*unscaled_pos.Imaginary*Complex.ImaginaryOne + + combined.position + combined.anchor; + var unscaled_anchor = (transform.anchor - transform.position)*Complex.Exp(Complex.ImaginaryOne * combined.angle) + transform.position; + combined.anchor = transform.scale.Real*unscaled_anchor.Real + transform.scale.Imaginary*unscaled_anchor.Imaginary*Complex.ImaginaryOne; + combined.scale = transform.scale.Imaginary*combined.scale.Imaginary*Complex.ImaginaryOne + transform.scale.Real * combined.scale.Real; combined.angle += transform.angle; return combined; }