Files
Voile/TestGame/TestGame.cs
2025-06-29 22:21:13 +02:00

182 lines
4.9 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);
// _uiSystem.RenderDebugRects = true;
_particleSystem = new ParticleSystem();
AddSystemToUpdate(_uiSystem);
AddSystemToUpdate(_particleSystem);
}
public override void Shutdown()
{
ShutdownDefault();
}
protected override void LoadResources()
{
ResourceManager.AddResourceLoaderAssociation(new ParticleEmitterSettingsResourceLoader());
ResourceManager.AddResourceLoaderAssociation(new StyleSheetLoader());
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!");
}
if (ResourceManager.TryLoad("default.style.toml", out _styleSheet))
{
}
_defaultFontSet = new([_font, _jpFont]);
}
protected override void Ready()
{
Input.AddInputMapping("reload", new IInputAction[] { new KeyInputAction(KeyboardKey.R) });
// _emitterId = _particleSystem.CreateEmitter(Renderer.WindowSize / 2, _fireEffect);
_uiSystem.SetStyleSheet(_styleSheet);
var addButton = new Button("Default button", _defaultFontSet);
var removeButton = new Button("Danger button", _defaultFontSet);
removeButton.StyleVariant = "Danger";
var outlineButton = new Button("Outline button", _defaultFontSet);
outlineButton.StyleVariant = "Outline";
_buttonContainer.AddChild(addButton);
_buttonContainer.AddChild(removeButton);
var c = new HorizontalContainer()
{
StyleVariant = "Layer01",
ConfineToContents = true,
Anchor = Anchor.TopCenter
};
c.AddChild(addButton);
c.AddChild(removeButton);
c.AddChild(outlineButton);
var vc = new VerticalContainer(0.0f);
vc.AddChild(c);
var f = new MarginContainer(new Size(0.0f));
f.AddChild(_container);
vc.AddChild(f);
_rootFill.AddChild(vc);
_uiSystem.AddElement(_rootFill);
}
protected override void Update(double deltaTime)
{
if (Input.IsActionPressed("reload"))
{
ResourceManager.Reload();
// _particleSystem!.RestartEmitter(_emitterId);
}
}
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 ResourceRef<StyleSheet> _styleSheet;
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,
StyleVariant = "Layer02",
};
[NotNull] private Label _label;
private FillContainer _rootFill = new();
private HorizontalContainer _buttonContainer = new(16)
{
ConfineToContents = true,
};
}