Files
Voile/TestGame/TestGame.cs
2025-06-29 17:29:47 +02:00

155 lines
4.2 KiB
C#

using Voile;
using Voile.Resources;
using Voile.Input;
using Voile.Systems.Particles;
using System.Numerics;
using System.Diagnostics.CodeAnalysis;
using Voile.Rendering;
using Voile.OpenAL;
using Voile.UI;
using Voile.UI.Widgets;
using Voile.UI.Containers;
public class TestGame : Game
{
public override string Name => "Test Game";
public override string ResourceRoot => "Resources/";
public override void Initialize()
{
InitializeSystemsDefault();
_uiSystem = new UISystem(Input, ResourceRef<Style>.Empty());
_uiSystem.RenderDebugRects = true;
_particleSystem = new ParticleSystem();
AddSystemToUpdate(_uiSystem);
AddSystemToUpdate(_particleSystem);
}
public override void Shutdown()
{
ShutdownDefault();
}
protected override void LoadResources()
{
ResourceManager.AddResourceLoaderAssociation(new ParticleEmitterSettingsResourceLoader());
if (!ResourceManager.TryLoad("fonts/Inter-Regular.ttf", out _font))
{
}
if (!ResourceManager.TryLoad("fonts/NotoSansJP-Regular.ttf", out _jpFont))
{
}
ResourceManager.TryLoad("icon.png", out _icon);
ResourceManager.TryLoad("sounds/test_sound_mono.ogg", out _sound);
if (!ResourceManager.TryLoad("fire_effect.toml", out _fireEffect))
{
throw new Exception("Failed to load emitter settings!");
}
_defaultFontSet = new([_font, _jpFont]);
}
protected override void Ready()
{
Input.AddInputMapping("reload", new IInputAction[] { new KeyInputAction(KeyboardKey.R) });
_emitterId = _particleSystem.CreateEmitter(Renderer.WindowSize / 2, _fireEffect);
var button = new Button("Add element", _defaultFontSet, () => { _container.AddChild(new Label("Hello, World!", _defaultFontSet)); });
button.Padding = new Margin(8.0f);
_buttonContainer.AddChild(button);
var c = new MarginContainer();
c.AddChild(_container);
_buttonContainer.AddChild(c);
_root.AddChild(_buttonContainer);
_uiSystem.AddElement(_root);
}
protected override void Update(double deltaTime)
{
if (Input.IsActionPressed("reload"))
{
ResourceManager.Reload();
_particleSystem!.RestartEmitter(_emitterId);
}
if (Input.IsActionPressed("cancel") && _container.Children.Count != 0)
{
var lastChild = _container.Children.Last();
_container.RemoveChild(lastChild);
}
}
protected override void Render(double deltaTime)
{
Renderer.ClearBackground(Color.Black);
// foreach (var emitter in _particleSystem!.Emitters)
// {
// DrawEmitter(emitter);
// }
Renderer.ResetTransform();
_uiSystem.Render(Renderer);
}
private void DrawEmitter(ParticleEmitter emitter)
{
Renderer.BeginBlended(BlendMode.BlendAlpha);
var maxParticles = emitter.Settings.MaxParticles;
var particleSize = new Vector2(16.0f, 16.0f);
var pivot = particleSize / 2;
for (int i = maxParticles - 1; i >= 0; i--)
{
var particle = emitter.GetParticle(i);
Renderer.SetTransform(particle.Position, pivot, particle.Rotation);
Renderer.DrawRectangle(particleSize * particle.Scale, particle.Color);
}
Renderer.EndBlended();
}
[NotNull] private ParticleSystem _particleSystem;
[NotNull] private UISystem _uiSystem;
private int _emitterId;
private ResourceRef<ParticleEmitterSettingsResource> _fireEffect;
private ResourceRef<Font> _font;
private ResourceRef<Font> _jpFont;
private FontSet _defaultFontSet;
private ResourceRef<Sound> _sound;
private ResourceRef<Texture2d> _icon;
private FlexContainer _container = new(minimumSize: new Rect(256.0f, 256.0f), new())
{
Anchor = Anchor.Center,
Direction = FlexDirection.Column,
Justify = JustifyContent.Start,
Align = AlignItems.Center,
Wrap = true,
Gap = 8.0f
};
[NotNull] private Label _label;
private FillContainer _root = new();
private VerticalContainer _buttonContainer = new(16);
}