Add new methods to input handler, add RectangleShape2d, modify test game.

This commit is contained in:
2023-09-25 17:31:33 +02:00
parent 0b018e081e
commit ddf62f1834
12 changed files with 318 additions and 77 deletions

View File

@@ -4,6 +4,7 @@ using DaggerFramework;
using DaggerFramework.Rendering;
using DaggerFramework.Audio;
using DaggerFramework.Resources;
using DaggerFramework.SceneGraph;
public class TestGame : Game
@@ -20,56 +21,17 @@ public class TestGame : Game
{
Title = "Test Game",
Size = new Vector2(1280, 720)
}, RendererSettings.Default);
_renderer.SetTargetFps(60);
}, new RendererSettings()
{
UseVSync = false
});
_audioBackend.Initialize();
}
protected override void LoadResources()
{
if (_resourceManager.TryLoad<Sound>("my_sound", "sounds/test_sound.ogg"))
{
_resourceManager.TryGetResource<Sound>("my_sound", out _testSound);
}
_scene = new Scene(_renderer, _inputHandler, _audioBackend, _resourceManager);
if (_resourceManager.TryLoad<Font>("inter_regular", "fonts/Inter-Regular.ttf"))
{
_resourceManager.TryGetResource<Font>("inter_regular", out _font);
}
}
protected override void Ready()
{
// _audioBackend.AddBusEffect<AudioEffectReverb>(new AudioEffectReverb());
_inputHandler.AddInputMapping("play", new InputAction[] { new KeyInputAction(KeyboardKey.Spacebar) });
_fontHandle = _renderer.LoadFont(_font);
}
protected override void Run()
{
while (_renderer.ShouldRun)
{
_renderer.BeginFrame();
_renderer.ClearBackground(Color.Black);
if (_inputHandler.IsActionJustPressed("play"))
{
var instance = _audioBackend.CreateInstance(_testSound)
.PitchVariation(min: 0.9f, max: 1.2f)
.VolumeVariation(min: 0.90f, max: 1.0f);
instance.Play();
}
_audioBackend.Update();
_renderer.SetTransform(new Vector2(640, 480));
_renderer.DrawCircle(16f, Color.Chocolate);
_renderer.DrawText(_fontHandle, "Hello World!", 24, Color.White);
_renderer.EndFrame();
}
_uiLayer = new UiLayer();
_worldLayer = new EntityLayer();
}
public override void Shutdown()
@@ -78,6 +40,60 @@ public class TestGame : Game
_audioBackend.Shutdown();
}
protected override void LoadResources()
{
if (_resourceManager.TryLoad<Sound>("my_sound", "sounds/test_sound.ogg"))
{
_resourceManager.TryGetResource("my_sound", out _testSound);
}
if (_resourceManager.TryLoad<Font>("inter_regular", "fonts/Inter-Regular.ttf"))
{
_resourceManager.TryGetResource("inter_regular", out _font);
}
}
protected override void Ready()
{
_inputHandler.AddInputMapping("play", new InputAction[] { new KeyInputAction(KeyboardKey.Spacebar) });
_inputHandler.AddInputMapping("sprint", new InputAction[] { new KeyInputAction(KeyboardKey.LeftShift) });
_fontHandle = _renderer.LoadFont(_font);
_scene.AddLayer("World", _worldLayer);
_scene.AddLayer("UI", _uiLayer);
_worldLayer.AddEntity(new TestPlayer());
_scene.Start();
}
protected override void Run()
{
while (!_scene.ShouldStop())
{
// _renderer.BeginFrame();
// _renderer.ClearBackground(Color.Black);
// if (_inputHandler.IsActionJustPressed("play"))
// {
// var instance = _audioBackend.CreateInstance(_testSound)
// .PitchVariation(min: 0.9f, max: 1.2f)
// .VolumeVariation(min: 0.90f, max: 1.0f);
// instance.Play();
// }
// _audioBackend.Update();
// _renderer.SetTransform(new Vector2(32, 32));
// _renderer.DrawCircle(16f, Color.Chocolate);
// _renderer.DrawText(_fontHandle, $"{(int)(1 / _renderer.GetFrameTime())} FPS", 20, Color.White);
// _renderer.EndFrame();
_scene.Update();
}
}
private Renderer _renderer;
private ResourceManager _resourceManager = new();
private Sound? _testSound;
@@ -85,4 +101,8 @@ public class TestGame : Game
private int _fontHandle;
private FmodAudioBackend _audioBackend;
private InputHandler _inputHandler;
private Scene _scene;
private UiLayer _uiLayer;
private EntityLayer _worldLayer;
}

25
TestGame/TestPlayer.cs Normal file
View File

@@ -0,0 +1,25 @@
using DaggerFramework;
using DaggerFramework.SceneGraph;
using DaggerFramework.Utils;
public class TestPlayer : RectangleShape2d
{
protected override void OnStart()
{
base.OnStart();
Color = Color.Cyan;
}
protected override void OnUpdate(double dt)
{
base.OnUpdate(dt);
var sprinting = Input.IsActionDown("sprint");
_speed = sprinting ? 400f : 200f;
var velocity = Input.GetInputDirection("left", "right", "up", "down") * _speed;
Position += velocity * (float)dt;
}
private float _speed = 200f;
}

View File

@@ -1,23 +1,41 @@
using System.Numerics;
using DaggerFramework;
using DaggerFramework.Rendering;
using DaggerFramework.SceneGraph;
public class UiLayer : Layer
public class UiLayer : EntityLayer
{
protected override void OnStart()
{
base.OnStart();
GetResources();
CreateAndAddEntities();
}
protected override void OnUpdate(double dt)
{
base.OnUpdate(dt);
_time += dt;
}
protected override void OnDraw(Renderer renderer)
{
renderer.SetTransform(Vector2.Zero);
renderer.DrawRectangle(new Vector2(720 / 2, 1280), Color.Green);
renderer.SetTransform(new Vector2(720 / 4 - 24, 64));
renderer.DrawDebugText("UI :)", 24, Color.White);
_fpsText.Text = $"{MathF.Round(1 / (float)dt)} FPS";
}
private double _time;
private void CreateAndAddEntities()
{
_fpsText = new Text2d()
{
Font = _defaultFont,
Position = Vector2.One * 16,
FontSize = 20
};
AddEntity(_fpsText);
}
private void GetResources()
{
ResourceManager.TryGetResource("inter_regular", out _defaultFont);
}
private Text2d _fpsText;
private Font _defaultFont;
}