Skip to content

Commit

Permalink
feat: TriggerSystem & UI layer
Browse files Browse the repository at this point in the history
  • Loading branch information
cherrynik committed Feb 29, 2024
1 parent 25a65f9 commit b85c770
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 30 deletions.
7 changes: 7 additions & 0 deletions MonoGame.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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(_ =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -77,35 +79,47 @@ private static void RegisterEntryPoint(IServiceRegistry serviceRegistry)
// serviceRegistry.RegisterSingleton(factory => new ImGuiRenderer(factory.GetInstance<Game>()));

// 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<World>()));
serviceRegistry.RegisterSingleton(factory =>
new TriggerSystem(factory.GetInstance<World>(), factory.GetInstance<CollisionSystem>()));

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<CollisionSystem>())
.AddCounterLabel()
.AddVersionLabel();

Desktop desktop = new();
desktop.Root = factory.GetInstance<Panel>();
desktop.Root = panel;

return desktop;
});
Expand All @@ -122,7 +136,8 @@ private static void RegisterEntryPoint(IServiceRegistry serviceRegistry)
// ⚠ Order-sensitive zone ⚠
var movement = new Feature(factory.GetInstance<World>(), factory.GetInstance<SystemsEngine>(),
new InputSystem(factory.GetInstance<World>(), new KeyboardInput()),
new CollisionSystem(factory.GetInstance<World>()),
factory.GetInstance<CollisionSystem>(),
factory.GetInstance<TriggerSystem>(),
new MovementSystem(factory.GetInstance<World>(), new SimpleMovement()));

var preRender = new Feature(factory.GetInstance<World>(),
Expand Down
1 change: 1 addition & 0 deletions src/Apps/GameDesktop/GameDesktop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<ProjectReference Include="..\..\Libs\Features\Features.csproj" />
<ProjectReference Include="..\..\Libs\Services\Services.csproj" />
<ProjectReference Include="..\..\Libs\Systems.Debugging\Systems.Debugging.csproj" />
<ProjectReference Include="..\..\Libs\UI\UI.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Update="Resources\SpriteSheet.Designer.cs">
Expand Down
20 changes: 17 additions & 3 deletions src/Libs/Systems/CollisionSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
}
Expand All @@ -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)
Expand Down
35 changes: 35 additions & 0 deletions src/Libs/Systems/TriggerSystem.cs
Original file line number Diff line number Diff line change
@@ -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)
{
}
}
50 changes: 50 additions & 0 deletions src/Libs/UI/TestElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Myra.Graphics2D.UI;
using Scellecs.Morpeh;
using Systems;

namespace UI;

// TODO: Use factory method, reactive update, divide on more UI components, use DI
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);
// }
}
17 changes: 17 additions & 0 deletions src/Libs/UI/UI.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Myra" Version="1.5.3"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Systems\Systems.csproj"/>
</ItemGroup>

</Project>

0 comments on commit b85c770

Please sign in to comment.