112 lines
3.1 KiB
C#
112 lines
3.1 KiB
C#
using Voile;
|
|
using Voile.Resources;
|
|
using Voile.Input;
|
|
using Voile.Systems.Particles;
|
|
using System.Numerics;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using Voile.Rendering;
|
|
|
|
public class TestGame : Game
|
|
{
|
|
public override string Name => "Jump Adventures 2";
|
|
public override string ResourceRoot => "Resources/";
|
|
|
|
public override void Initialize()
|
|
{
|
|
InitializeSystemsDefault();
|
|
|
|
_particleSystem = new ParticleSystem();
|
|
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);
|
|
|
|
if (!ResourceManager.TryLoad("fire_effect.toml", out _fireEffect))
|
|
{
|
|
throw new Exception("Failed to load emitter settings!");
|
|
}
|
|
}
|
|
|
|
protected override void Ready()
|
|
{
|
|
Input.AddInputMapping("reload", new InputAction[] { new KeyInputAction(KeyboardKey.R) });
|
|
_emitterId = _particleSystem.CreateEmitter(Renderer.WindowSize / 2, _fireEffect);
|
|
}
|
|
|
|
|
|
protected override void Update(double deltaTime)
|
|
{
|
|
|
|
}
|
|
|
|
protected override void Render(double deltaTime)
|
|
{
|
|
if (Input.IsActionPressed("reload"))
|
|
{
|
|
ResourceManager.Reload();
|
|
_particleSystem!.RestartEmitter(_emitterId);
|
|
}
|
|
|
|
if (Input.KeyboardKeyJustPressed(KeyboardKey.One))
|
|
{
|
|
_particleSystem.CreateEmitter(Input.GetMousePosition(), _fireEffect);
|
|
}
|
|
|
|
if (Input.IsMouseButtonDown(MouseButton.Left))
|
|
{
|
|
_particleSystem.SetEmitterPosition(_emitterId, Input.GetMousePosition());
|
|
}
|
|
|
|
Renderer.ClearBackground(Color.Black);
|
|
foreach (var emitter in _particleSystem!.Emitters)
|
|
{
|
|
DrawEmitter(emitter);
|
|
}
|
|
|
|
Renderer.ResetTransform();
|
|
|
|
Renderer.DrawText(_font, $"Render: {RenderFrameTime.TotalMilliseconds:F1} ms", Color.White);
|
|
|
|
Renderer.SetTransform(new Vector2(0.0f, 16.0f), Vector2.Zero);
|
|
Renderer.DrawText(_font, $"Update: {UpdateTimeStep * 1000:F1} ms", Color.White);
|
|
}
|
|
|
|
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;
|
|
private int _emitterId;
|
|
private ResourceRef<ParticleEmitterSettingsResource> _fireEffect;
|
|
private ResourceRef<Font> _font;
|
|
private ResourceRef<Texture2d> _icon;
|
|
} |