From 092d01dcaeac8ce9a75f8ae0d32bc070269b52e0 Mon Sep 17 00:00:00 2001 From: dnesov Date: Mon, 25 Sep 2023 18:54:17 +0200 Subject: [PATCH] Update test game to showcase camera system. --- DaggerFramework/Source/Utils/MathUtils.cs | 10 ++++++++ TestGame/TestGame.cs | 2 ++ TestGame/TestPlayer.cs | 26 ++++++++++---------- TestGame/World.cs | 30 +++++++++++++++++++++++ 4 files changed, 55 insertions(+), 13 deletions(-) create mode 100644 TestGame/World.cs diff --git a/DaggerFramework/Source/Utils/MathUtils.cs b/DaggerFramework/Source/Utils/MathUtils.cs index 15b777a..9530994 100644 --- a/DaggerFramework/Source/Utils/MathUtils.cs +++ b/DaggerFramework/Source/Utils/MathUtils.cs @@ -25,6 +25,14 @@ namespace DaggerFramework return new Vector2(x, y); } + public static Vector2 RandomVector2(Vector2 min, Vector2 max) + { + var x = _random.NextDouble(min.X, max.X); + var y = _random.NextDouble(min.Y, max.Y); + + return new Vector2((float)x, (float)y); + } + public static float EaseOutBack(float x) { var c1 = 1.70158f; @@ -43,5 +51,7 @@ namespace DaggerFramework ? 1 : (float)Math.Pow(2, -10 * x) * (float)Math.Sin((x * 10 - 0.75f) * c4) + 1; } + + private static LehmerRandom _random = new(); } } \ No newline at end of file diff --git a/TestGame/TestGame.cs b/TestGame/TestGame.cs index f619b7e..d92ba2f 100644 --- a/TestGame/TestGame.cs +++ b/TestGame/TestGame.cs @@ -62,6 +62,8 @@ public class TestGame : Game _scene.AddLayer("World", _worldLayer); _scene.AddLayer("UI", _uiLayer); + + _worldLayer.AddEntity(new World()); _worldLayer.AddEntity(new TestPlayer()); _scene.Start(); diff --git a/TestGame/TestPlayer.cs b/TestGame/TestPlayer.cs index 4926ed3..8e24769 100644 --- a/TestGame/TestPlayer.cs +++ b/TestGame/TestPlayer.cs @@ -10,29 +10,29 @@ public class TestPlayer : RectangleShape2d Color = Color.Cyan; _logger.Echo("OnStart"); - // _camera = new Camera2d() - // { - // Current = true, - // }; + _camera = new Camera2d() + { + Current = true, + }; - // Layer.AddEntity(_camera); + Layer.AddEntity(_camera); } protected override void OnUpdate(double dt) { base.OnUpdate(dt); - // var sprinting = Input.IsActionDown("sprint"); - // _speed = sprinting ? 400f : 200f; + var sprinting = Input.IsActionDown("sprint"); + _speed = sprinting ? 400f : 200f; - // var velocity = Input.GetInputDirection("left", "right", "up", "down") * _speed; - // Position += velocity * (float)dt; + var velocity = Input.GetInputDirection("left", "right", "up", "down") * _speed; + Position += velocity * (float)dt; - // _camera.Position = MathUtils.LerpVector2(_camera.Position, Position, dt * 5f); + _camera.Position = MathUtils.LerpVector2(_camera.Position, Position, dt * 5f); - var mousePos = Input.GetMousePosition(); - Position = MathUtils.LerpVector2(Position, mousePos, dt * 5f); - Rotation += (float)dt * 100f; + // var mousePos = Input.GetMousePosition(); + // Position = MathUtils.LerpVector2(Position, mousePos, dt * 5f); + // Rotation += (float)dt * 100f; } private Logger _logger = new(nameof(TestPlayer)); diff --git a/TestGame/World.cs b/TestGame/World.cs new file mode 100644 index 0000000..af4426b --- /dev/null +++ b/TestGame/World.cs @@ -0,0 +1,30 @@ +using System.Numerics; +using DaggerFramework; +using DaggerFramework.Rendering; +using DaggerFramework.SceneGraph; + +public class World : Drawable2d +{ + protected override void OnStart() + { + base.OnStart(); + + _randomPositions = new Vector2[_rectangleAmount]; + for (int i = 0; i < _rectangleAmount - 1; i++) + { + _randomPositions[i] = MathUtils.RandomVector2(Position, Vector2.One * 2048f); + } + } + public override void OnDraw(Renderer renderer) + { + for (int i = 0; i < _rectangleAmount; i++) + { + renderer.SetTransform(_randomPositions[i], PivotOffset, 0); + renderer.DrawRectangle(Vector2.One * 16f, _rectangleColor); + } + } + + private Color _rectangleColor = new Color(0.25f, 0.25f, 0.25f, 1.0f); + private int _rectangleAmount = 6000; + private Vector2[] _randomPositions; +} \ No newline at end of file