121 lines
3.3 KiB
C#
121 lines
3.3 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(new ResourceRef<Style>(Guid.Empty));
|
|
|
|
_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))
|
|
{
|
|
|
|
}
|
|
|
|
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!");
|
|
}
|
|
}
|
|
|
|
protected override void Ready()
|
|
{
|
|
Input.AddInputMapping("reload", new IInputAction[] { new KeyInputAction(KeyboardKey.R) });
|
|
_emitterId = _particleSystem.CreateEmitter(Renderer.WindowSize / 2, _fireEffect);
|
|
|
|
_uiSystem.AddElement(_container);
|
|
}
|
|
|
|
|
|
protected override void Update(double deltaTime)
|
|
{
|
|
if (Input.IsActionPressed("reload"))
|
|
{
|
|
ResourceManager.Reload();
|
|
_particleSystem!.RestartEmitter(_emitterId);
|
|
}
|
|
|
|
if (Input.IsActionPressed("accept"))
|
|
{
|
|
_container.AddChild(new RectangleWidget(new Rect(32.0f, 32.0f), MathUtils.RandomColor()));
|
|
}
|
|
|
|
if (Input.IsMouseButtonDown(MouseButton.Left))
|
|
{
|
|
_particleSystem.SetEmitterPosition(_emitterId, Input.GetMousePosition());
|
|
}
|
|
}
|
|
|
|
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<Sound> _sound;
|
|
private ResourceRef<Texture2d> _icon;
|
|
|
|
private GridContainer _container = new();
|
|
} |