diff --git a/MonoGame.sln b/MonoGame.sln index 80bae64..5257721 100644 --- a/MonoGame.sln +++ b/MonoGame.sln @@ -35,6 +35,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests.Entities", "src\U EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Monogame.ImGuiNet", "src\Libs\External\MonoGame.ImGuiNet\Monogame.ImGuiNet.csproj", "{56FF7C50-4B5F-4480-A5BE-BA95925152A1}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UI", "src\Libs\UI\UI.csproj", "{B7BAA8DC-A62F-4746-B6C4-9908C0AFD30A}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -88,6 +90,10 @@ Global {56FF7C50-4B5F-4480-A5BE-BA95925152A1}.Debug|Any CPU.Build.0 = Debug|Any CPU {56FF7C50-4B5F-4480-A5BE-BA95925152A1}.Release|Any CPU.ActiveCfg = Release|Any CPU {56FF7C50-4B5F-4480-A5BE-BA95925152A1}.Release|Any CPU.Build.0 = Release|Any CPU + {B7BAA8DC-A62F-4746-B6C4-9908C0AFD30A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B7BAA8DC-A62F-4746-B6C4-9908C0AFD30A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B7BAA8DC-A62F-4746-B6C4-9908C0AFD30A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B7BAA8DC-A62F-4746-B6C4-9908C0AFD30A}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {5E3B6238-A92E-4703-8527-1C2410D7906A} = {F5258F7E-B0BA-466D-8CF8-4DAB84722BE3} @@ -105,5 +111,6 @@ Global {5D45D0B2-6B53-4487-B9C2-3F685B2EC1E4} = {FE9CE932-6F98-4455-A80F-2A0CDB6DB46E} {57F0A75A-5AD6-4187-9DC6-3CBA44333061} = {FC0D63F3-9B01-46BD-9435-A7E26C905825} {56FF7C50-4B5F-4480-A5BE-BA95925152A1} = {FE9CE932-6F98-4455-A80F-2A0CDB6DB46E} + {B7BAA8DC-A62F-4746-B6C4-9908C0AFD30A} = {7AE4DC46-59EB-4F69-9240-C7363F0778AA} EndGlobalSection EndGlobal diff --git a/src/Apps/GameDesktop/CompositionRoots/Components/ComponentsCompositionRoot.cs b/src/Apps/GameDesktop/CompositionRoots/Components/ComponentsCompositionRoot.cs index d966186..9ec5d3e 100644 --- a/src/Apps/GameDesktop/CompositionRoots/Components/ComponentsCompositionRoot.cs +++ b/src/Apps/GameDesktop/CompositionRoots/Components/ComponentsCompositionRoot.cs @@ -131,7 +131,7 @@ private static void RegisterRectangleColliderComponent(IServiceRegistry serviceR new RectangleColliderComponent { Size = new(0, 0, 8, 8), - // IsTrigger = true + IsTrigger = true }, "DummyEntity"); serviceRegistry.RegisterSingleton(_ => diff --git a/src/Apps/GameDesktop/CompositionRoots/Features/RootFeatureCompositionRoot.cs b/src/Apps/GameDesktop/CompositionRoots/Features/RootFeatureCompositionRoot.cs index ff96574..485d0da 100644 --- a/src/Apps/GameDesktop/CompositionRoots/Features/RootFeatureCompositionRoot.cs +++ b/src/Apps/GameDesktop/CompositionRoots/Features/RootFeatureCompositionRoot.cs @@ -4,6 +4,7 @@ using Entities.Factories.Items; using Entities.Factories.Meta; using Features; +using FontStashSharp.RichText; using LightInject; using GameDesktop.CompositionRoots.Components; using GameDesktop.CompositionRoots.Entities; @@ -18,6 +19,7 @@ using Systems.Debugging.Diagnostics; using Systems.Debugging.World; using Systems.Render; +using UI; #if DEBUG using Systems.Debugging.Render; using GameDesktop.CompositionRoots.DebugFeatures; @@ -77,35 +79,47 @@ private static void RegisterEntryPoint(IServiceRegistry serviceRegistry) // serviceRegistry.RegisterSingleton(factory => new ImGuiRenderer(factory.GetInstance())); // UI - serviceRegistry.RegisterSingleton(_ => - { - // var grid = new Grid { RowSpacing = 8, ColumnSpacing = 8 }; - // - // grid.ColumnsProportions.Add(new Proportion(ProportionType.Auto)); - // grid.ColumnsProportions.Add(new Proportion(ProportionType.Auto)); - // grid.RowsProportions.Add(new Proportion(ProportionType.Auto)); - // grid.RowsProportions.Add(new Proportion(ProportionType.Auto)); - // - // return grid; - - var panel = new Panel(); - var rightBottomText = new Label() - { - Text = "Pre-Alpha v0.1.1", - HorizontalAlignment = HorizontalAlignment.Right, - VerticalAlignment = VerticalAlignment.Bottom, - Left = -30, - Top = -20 - }; - panel.Widgets.Add(rightBottomText); - - return panel; - }); + // serviceRegistry.RegisterSingleton(_ => + // { + // return new Panel(); + // var grid = new Grid { RowSpacing = 8, ColumnSpacing = 8 }; + // + // grid.ColumnsProportions.Add(new Proportion(ProportionType.Auto)); + // grid.ColumnsProportions.Add(new Proportion(ProportionType.Auto)); + // grid.RowsProportions.Add(new Proportion(ProportionType.Auto)); + // grid.RowsProportions.Add(new Proportion(ProportionType.Auto)); + // + // return grid; + // }); + serviceRegistry.RegisterSingleton(factory => new CollisionSystem(factory.GetInstance())); + serviceRegistry.RegisterSingleton(factory => + new TriggerSystem(factory.GetInstance(), factory.GetInstance())); serviceRegistry.RegisterSingleton(factory => { + var panel = new Panel(); + new TestElement(panel, + new Label + { + HorizontalAlignment = HorizontalAlignment.Right, + VerticalAlignment = VerticalAlignment.Bottom, + Left = -30, + Top = -40, + TextAlign = TextHorizontalAlignment.Right + }, + new Label + { + Text = "Pre-Alpha v0.2.0", + HorizontalAlignment = HorizontalAlignment.Right, + VerticalAlignment = VerticalAlignment.Bottom, + Left = -30, + Top = -20 + }, factory.GetInstance()) + .AddCounterLabel() + .AddVersionLabel(); + Desktop desktop = new(); - desktop.Root = factory.GetInstance(); + desktop.Root = panel; return desktop; }); @@ -122,7 +136,8 @@ private static void RegisterEntryPoint(IServiceRegistry serviceRegistry) // ⚠ Order-sensitive zone ⚠ var movement = new Feature(factory.GetInstance(), factory.GetInstance(), new InputSystem(factory.GetInstance(), new KeyboardInput()), - new CollisionSystem(factory.GetInstance()), + factory.GetInstance(), + factory.GetInstance(), new MovementSystem(factory.GetInstance(), new SimpleMovement())); var preRender = new Feature(factory.GetInstance(), diff --git a/src/Apps/GameDesktop/GameDesktop.csproj b/src/Apps/GameDesktop/GameDesktop.csproj index 2193f63..6d711dd 100644 --- a/src/Apps/GameDesktop/GameDesktop.csproj +++ b/src/Apps/GameDesktop/GameDesktop.csproj @@ -70,6 +70,7 @@ + diff --git a/src/Libs/Systems/CollisionSystem.cs b/src/Libs/Systems/CollisionSystem.cs index 8287b69..0873192 100644 --- a/src/Libs/Systems/CollisionSystem.cs +++ b/src/Libs/Systems/CollisionSystem.cs @@ -5,12 +5,20 @@ namespace Systems; +public class CustomEventArgs(string message) : EventArgs +{ + public string Message = message; +} + // Input & Collision systems both have to be fixed execute systems, // otherwise it'll lead to the desynchronized behaviour. public class CollisionSystem(World world) : IFixedSystem { public World World { get; set; } = world; + public delegate void TriggerHandler(Entity sender, CustomEventArgs args); + public event TriggerHandler? RaiseTriggerEntered; + public void OnAwake() { } @@ -37,15 +45,21 @@ public void OnUpdate(float deltaTime) if (!AreColliding(new(leftTransform, leftCollider), new(rightTransform, rightCollider))) continue; - if (leftCollider.IsTrigger || rightCollider.IsTrigger) HandleTrigger(); + if (leftCollider.IsTrigger || rightCollider.IsTrigger) + OnRaiseTriggerEnter(other, new CustomEventArgs("Entered")); else HandleCollision(ref leftTransform, ref rightTransform); } } } - private static void HandleTrigger() + private void OnRaiseTriggerEnter(Entity sender, CustomEventArgs customEventArgs) { - // raise event of triggered + var raiseEvent = RaiseTriggerEntered; + + if (raiseEvent is null) return; + + customEventArgs.Message += $" at {DateTime.Now}"; + raiseEvent(sender, customEventArgs); } private static void HandleCollision(ref TransformComponent left, ref TransformComponent right) diff --git a/src/Libs/Systems/TriggerSystem.cs b/src/Libs/Systems/TriggerSystem.cs new file mode 100644 index 0000000..e8f2de8 --- /dev/null +++ b/src/Libs/Systems/TriggerSystem.cs @@ -0,0 +1,35 @@ +using Scellecs.Morpeh; + +namespace Systems; + +public class TriggerSystem : IFixedSystem +{ + public World World { get; set; } + private bool _entered; + + public TriggerSystem(World world, CollisionSystem collisionSystem) + { + World = world; + collisionSystem.RaiseTriggerEntered += HandleTriggerEntered; + } + + private void HandleTriggerEntered(Entity sender, CustomEventArgs e) + { + if (_entered) return; + + Console.WriteLine(e.Message); + _entered = true; + } + + public void Dispose() + { + } + + public void OnAwake() + { + } + + public void OnUpdate(float deltaTime) + { + } +} diff --git a/src/Libs/UI/TestElement.cs b/src/Libs/UI/TestElement.cs new file mode 100644 index 0000000..45a30a9 --- /dev/null +++ b/src/Libs/UI/TestElement.cs @@ -0,0 +1,49 @@ +using Myra.Graphics2D.UI; +using Scellecs.Morpeh; +using Systems; + +namespace UI; + +public class TestElement +{ + public readonly Panel Panel; + public readonly Label Counter; + public readonly Label Version; + private int _counter; + + public TestElement(Panel panel, Label counter, Label version, CollisionSystem collisionSystem) + { + Panel = panel; + Counter = counter; + Version = version; + + collisionSystem.RaiseTriggerEntered += HandleTriggerEntered; + } + + public TestElement AddCounterLabel() + { + Panel.Widgets.Add(Counter); + + UpdateCounterText(); + + return this; + } + + public TestElement AddVersionLabel() + { + Panel.Widgets.Add(Version); + return this; + } + + private void HandleTriggerEntered(Entity sender, CustomEventArgs args) + { + ++_counter; + UpdateCounterText(); + } + + private void UpdateCounterText() => Counter.Text = $"Counter: {_counter}"; + // public TestElement() + // { + // Panel.Widgets.Add(Label); + // } +} diff --git a/src/Libs/UI/UI.csproj b/src/Libs/UI/UI.csproj new file mode 100644 index 0000000..cfd26dd --- /dev/null +++ b/src/Libs/UI/UI.csproj @@ -0,0 +1,17 @@ + + + + net8.0 + enable + enable + + + + + + + + + + +