Skip to content

Commit

Permalink
Update example
Browse files Browse the repository at this point in the history
  • Loading branch information
Tornado-Technology committed Aug 17, 2024
1 parent 358cf91 commit 3b873a2
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions Hypercube.Example.Client/Example.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
using Hypercube.Client.Graphics.ImGui.Events;
using Hypercube.Client.Graphics.Rendering;
using Hypercube.Client.Graphics.Viewports;
using Hypercube.Client.Input.Events;
using Hypercube.Dependencies;
using Hypercube.EventBus;
using Hypercube.Example.Client.Controls;
using Hypercube.Input;
using Hypercube.Mathematics.Vectors;
using Hypercube.Shared.Entities.Realisation.Manager;
using Hypercube.Shared.Entities.Systems.Physics;
Expand All @@ -31,6 +33,9 @@ public sealed class Example : IEventSubscriber, IPostInject
[Dependency] private readonly IResourceContainer _resourceContainer = default!;

private readonly Random _random = new();

private bool _showSpawnWindow;
private bool _showDemoWindow;

public void Start(string[] args, DependenciesContainer root)
{
Expand All @@ -41,8 +46,10 @@ public void PostInject()
{
_eventBus.Subscribe<RuntimeStartupEvent>(this, Startup);
_eventBus.Subscribe<ImGuiRenderEvent>(this, ImGuiRender);

_eventBus.Subscribe<KeyHandledEvent>(this, OnKey);
}

private void Startup(ref RuntimeStartupEvent args)
{

Expand Down Expand Up @@ -72,16 +79,36 @@ private void ImGuiRender(ref ImGuiRenderEvent args)
{
var imGui = args.Instance;

imGui.Begin("Test");
if (imGui.Button("Spawn"))
if (_showSpawnWindow)
{
imGui.Begin("Test");
if (imGui.Button("Spawn"))
{
var x = _random.NextSingle() * 10 - 5;
var y = _random.NextSingle() * 10 - 5;
var coord = new SceneCoordinates(SceneId.Nullspace, new Vector2(x, y));
CreateEntity(coord, new RectangleShape(Vector2.One * 2f));
CreateEntity(coord, new CircleShape(1f));
}
imGui.End();
}

if (_showDemoWindow)
{
var x = _random.NextSingle() * 10 - 5;
var y = _random.NextSingle() * 10 - 5;
var coord = new SceneCoordinates(SceneId.Nullspace, new Vector2(x, y));
CreateEntity(coord, new RectangleShape(Vector2.One * 2f));
CreateEntity(coord, new CircleShape(1f));
imGui.ShowDemoWindow();
}
imGui.End();
}

private void OnKey(ref KeyHandledEvent args)
{
if (args.State != KeyState.Pressed)
return;

if (args.Key == Key.F1)
_showSpawnWindow = !_showSpawnWindow;

if (args.Key == Key.F2)
_showDemoWindow = !_showDemoWindow;
}

private void CreateEntity(SceneCoordinates coordinates, IShape shape, BodyType type = BodyType.Dynamic)
Expand Down

0 comments on commit 3b873a2

Please sign in to comment.