From 5e4f812f194aa05c5bd5d5f4a3fe119b30d6d808 Mon Sep 17 00:00:00 2001 From: Ludovic 'Archivist' Lagouardette Date: Sat, 16 Mar 2024 22:14:49 +0100 Subject: [PATCH] Expanded more features for making simple game objects --- Expansion1/CrosshairGizmoComponent.cs | 19 +++---- Expansion2/ExampleShootingInputComponent.cs | 6 ++- Expansion2/InertiaComponent.cs | 59 +++++++++++++++++++++ Expansion2/RaylibAssetManagerModule.cs | 12 +++++ Expansion2/SpriteComponent.cs | 10 +--- Program.cs | 2 +- Smoll/Entity.cs | 2 +- Smoll/Layer.cs | 6 ++- 8 files changed, 91 insertions(+), 25 deletions(-) create mode 100644 Expansion2/InertiaComponent.cs diff --git a/Expansion1/CrosshairGizmoComponent.cs b/Expansion1/CrosshairGizmoComponent.cs index d5a9e68..1591fa8 100644 --- a/Expansion1/CrosshairGizmoComponent.cs +++ b/Expansion1/CrosshairGizmoComponent.cs @@ -34,29 +34,22 @@ namespace Smoll.Ex1 { Color.Red); Raylib.DrawText( tr.angle.ToString(), - (int)tr.position.Real+10, - (int)tr.position.Imaginary+10, - 14, - Color.Magenta - ); - Raylib.DrawText( - tr.angle.ToString(), - (int)tr.position.Real+10, - (int)tr.position.Imaginary+10, + (int)tr.position.Real+7, + (int)tr.position.Imaginary+7, 14, Color.Magenta ); Raylib.DrawText( tr.position.Real.ToString(), - (int)tr.position.Real+10, - (int)tr.position.Imaginary+26, + (int)tr.position.Real+7, + (int)tr.position.Imaginary+23, 14, Color.Red ); Raylib.DrawText( tr.position.Imaginary.ToString(), - (int)tr.position.Real+10, - (int)tr.position.Imaginary+42, + (int)tr.position.Real+7, + (int)tr.position.Imaginary+39, 14, Color.Lime ); diff --git a/Expansion2/ExampleShootingInputComponent.cs b/Expansion2/ExampleShootingInputComponent.cs index 4fbdc1d..3e25f5f 100644 --- a/Expansion2/ExampleShootingInputComponent.cs +++ b/Expansion2/ExampleShootingInputComponent.cs @@ -1,6 +1,7 @@ using Smoll; using Raylib_cs; using System.Numerics; +using Smoll.Ex1; namespace Smoll.Ex2 { @@ -26,10 +27,13 @@ namespace Smoll.Ex2 if(arrow == null) throw new Exception("Example Shooter used on arrowless object"); if(Raylib.IsKeyPressed(KeyboardKey.Space)) { var shoot = new Entity(owner.layers.First()); + shoot.zOrder = -5; var details = arrow.OriginAndVector(); - shoot.Attach(new Transform2DComponent((float)details.Item1.Real,(float)details.Item1.Imaginary,transform.transform.angle, transform.transform.scale)); + var selfTransform = transform.AbsoluteTransform(); + shoot.Attach(new Transform2DComponent((float)details.Item1.Real, (float)details.Item1.Imaginary, selfTransform.angle, selfTransform.scale)); shoot.Attach(new TimeToLiveComponent(4f)); shoot.Attach(new SpriteComponent("Beam.png")); + shoot.Attach(new InertiaComponent(1.0f, 0.001f*(float)details.Item2.Real, 0.001f*(float)details.Item2.Imaginary)); } } } diff --git a/Expansion2/InertiaComponent.cs b/Expansion2/InertiaComponent.cs new file mode 100644 index 0000000..0d84424 --- /dev/null +++ b/Expansion2/InertiaComponent.cs @@ -0,0 +1,59 @@ +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(); + } + + public override void Update(float deltaTimeSeconds) + { + ownerTransform ??= owner.GetComponent(); + 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; + } + } + } + } + +} diff --git a/Expansion2/RaylibAssetManagerModule.cs b/Expansion2/RaylibAssetManagerModule.cs index a2f48cf..54c5946 100644 --- a/Expansion2/RaylibAssetManagerModule.cs +++ b/Expansion2/RaylibAssetManagerModule.cs @@ -6,11 +6,13 @@ namespace Smoll.Ex2 string path; Dictionary images; + Dictionary textures; Dictionary sounds; public RaylibAssetManagerModule(string path = "./assets") { this.path = path; images = new Dictionary(); + textures = new Dictionary(); sounds = new Dictionary(); } @@ -23,6 +25,16 @@ namespace Smoll.Ex2 return image; } + public Texture2D GetTexture(string name) { + Texture2D texture; + if(! textures.TryGetValue(name, out texture)) { + Image image = GetImage(name); + texture = Raylib.LoadTextureFromImage(image); + textures.Add(name, texture); + } + return texture; + } + public Sound GetSound(string name) { Sound sound; if(! sounds.TryGetValue(name, out sound)) { diff --git a/Expansion2/SpriteComponent.cs b/Expansion2/SpriteComponent.cs index 104065b..fc67952 100644 --- a/Expansion2/SpriteComponent.cs +++ b/Expansion2/SpriteComponent.cs @@ -2,7 +2,7 @@ using System.Numerics; using Raylib_cs; namespace Smoll.Ex2 { - class SpriteComponent : Smoll.Component , IDisposable{ + class SpriteComponent : Smoll.Component { Complex offset; string asset_name; Transform2DComponent? ownerTransform; @@ -16,8 +16,7 @@ namespace Smoll.Ex2 { public override void OnAttached() { ownerTransform = owner.GetComponent(); - Image image = owner.layers.First().attachedEngine.GetModule().GetImage(asset_name); - texture = Raylib.LoadTextureFromImage(image); + texture = owner.layers.First().attachedEngine.GetModule().GetTexture(asset_name); } public override void Draw(Smoll.Layer.DrawMode drawMode) { @@ -28,10 +27,5 @@ namespace Smoll.Ex2 { Raylib.DrawTextureEx(texture, new Vector2((float)tr.position.Real, (float)tr.position.Imaginary), tr.angle*180f/MathF.PI, tr.scale, Color.White); } } - - public void Dispose() - { - Raylib.UnloadTexture(texture); - } } } \ No newline at end of file diff --git a/Program.cs b/Program.cs index 8005e82..78a1d95 100644 --- a/Program.cs +++ b/Program.cs @@ -30,7 +30,7 @@ entity.Attach(ship); var entity2 = new Entity(entity); entity2.Attach(new Transform2DComponent(5, -10, 0)); entity2.Attach(new SpriteComponent("Laser.png")); -var arrow = new ArrowComponent(4, 4); +var arrow = new ArrowComponent(2, 0); arrow.rawValue = new System.Numerics.Complex(0, -12); entity2.Attach(arrow); entity2.Attach(new ExampleShootingInputComponent()); diff --git a/Smoll/Entity.cs b/Smoll/Entity.cs index bcf022e..bede4fb 100644 --- a/Smoll/Entity.cs +++ b/Smoll/Entity.cs @@ -12,7 +12,7 @@ namespace Smoll { } else { foreach (var item in layers) { - item.actionables.Sort(); + item.mustReorder = true; } } } diff --git a/Smoll/Layer.cs b/Smoll/Layer.cs index f82f6ab..03b6960 100644 --- a/Smoll/Layer.cs +++ b/Smoll/Layer.cs @@ -9,6 +9,8 @@ namespace Smoll { watch = new Stopwatch(); } + public bool mustReorder = false; + internal Engine attachedEngine; internal List actionables; @@ -43,7 +45,9 @@ namespace Smoll { { actionables[i].Update(deltaTime); } - actionables.Sort(); + if(mustReorder) { + actionables.Sort(); + } return deltaTime; } public void Draw() {