Skip to content

Commit

Permalink
サンプルコードを作り直した
Browse files Browse the repository at this point in the history
  • Loading branch information
EbiseLutica committed Feb 1, 2024
1 parent b464a74 commit 1c8af7d
Show file tree
Hide file tree
Showing 18 changed files with 456 additions and 66 deletions.
9 changes: 9 additions & 0 deletions Promete.Example/Kernel/DemoAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Promete.Example.Kernel;

[AttributeUsage(AttributeTargets.Class)]
public class DemoAttribute(string path, string description) : Attribute
{
public string Path { get; } = path;

public string Description { get; } = description;
}
63 changes: 63 additions & 0 deletions Promete.Example/Kernel/DemoFileSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Reflection;

namespace Promete.Example.Kernel;

public class DemoFileSystem
{
public Folder Root { get; } = new Folder("");

public DemoFileSystem()
{
Initialize();
}

private void Initialize()
{
var scenes = GetType().Assembly.GetTypes()
.Select(type => (type, attribute: type.GetCustomAttribute<DemoAttribute>()))
.Where(t => t.attribute is not null) as IEnumerable<(Type, DemoAttribute)>;

foreach (var (type, attribute) in scenes)
{
var path = attribute.Path;
if (path.IndexOf('/') < 0) path = "/" + path;
var a = path.LastIndexOf('/');
var folderPath = path.Remove(a);
var fileName = path[(a + 1)..];
var folder = CreateOrGetFolder(folderPath);

var file = new SceneFile(fileName, attribute.Description, type, folder);

folder.Files.Add(file);
folder.Files.Sort((f1, f2) => string.Compare(f1.Name, f2.Name, StringComparison.Ordinal));
}
}

private Folder CreateOrGetFolder(string path)
{
path = path.ToLowerInvariant();
var nest = path.Split('/').Where(s => !string.IsNullOrEmpty(s));
var current = Root;
foreach (var name in nest)
{
var el = current.Files.FirstOrDefault(f => f.Name == name);
switch (el)
{
case null:
{
var folder = new Folder(name, current);
current.Files.Add(folder);
current = folder;
break;
}
case Folder f:
current = f;
break;
default:
// el is other non-null type
throw new Exception($"'{path}' already exists");
}
}
return current;
}
}
10 changes: 10 additions & 0 deletions Promete.Example/Kernel/DemoKernel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Promete.Example.Kernel;

public static class DemoKernel
{
public static DemoFileSystem FileSystem { get; } = new();

public static Folder CurrentFolder { get; set; } = FileSystem.Root;

public static int CurrentIndex { get; set; }
}
17 changes: 17 additions & 0 deletions Promete.Example/Kernel/Folder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Promete.Example.Kernel;

public class Folder(string name, Folder? parent = null) : IFileSystemElement
{
public string Name { get; } = name;

public List<IFileSystemElement> Files { get; } = [];

public Folder? Parent { get; } = parent;

public int Count => Files.Count;

public string GetFullPath()
{
return Parent == null ? Name : $"{Parent.GetFullPath()}/{Name}";
}
}
7 changes: 7 additions & 0 deletions Promete.Example/Kernel/IFileSystemElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Promete.Example.Kernel;

public interface IFileSystemElement
{
string Name { get; }
Folder? Parent { get; }
}
9 changes: 9 additions & 0 deletions Promete.Example/Kernel/SceneFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Promete.Example.Kernel;

public class SceneFile(string name, string description, Type scene, Folder? parent = null) : IFileSystemElement
{
public string Name { get; } = name;
public string Description { get; } = description;
public Type Scene { get; } = scene;
public Folder? Parent { get; } = parent;
}
116 changes: 50 additions & 66 deletions Promete.Example/MainScene.cs
Original file line number Diff line number Diff line change
@@ -1,96 +1,80 @@
using Promete;
using Promete.Audio;
using Promete.Elements;
using Promete.Example.Kernel;
using Promete.Graphics;
using Promete.Input;
using Promete.Windowing;
using Color = System.Drawing.Color;

using static Promete.Example.Kernel.DemoKernel;

namespace Promete.Example;

public class MainScene : Scene
public class MainScene(PrometeApp app, IWindow window, Mouse mouse, Keyboard keyboard, ConsoleLayer console) : Scene
{
private Tilemap map;

private readonly PrometeApp app;
private readonly IWindow window;
private readonly Keyboard keyboard;
private readonly ConsoleLayer console;
private readonly Mouse mouse;
private readonly ITile tile;
private readonly AudioPlayer player;
private readonly VorbisAudioSource sound;
private readonly WaveAudioSource sfx1;

public MainScene(PrometeApp app, IWindow window, Keyboard keyboard, Mouse mouse, ConsoleLayer console)
public override void OnStart()
{
this.app = app;
this.window = window;
this.keyboard = keyboard;
this.mouse = mouse;
this.console = console;

player = new AudioPlayer();
sound = new VorbisAudioSource("assets/kagerou.ogg");
sfx1 = new WaveAudioSource("assets/lineclear.wav");

tile = new Tile(window.TextureFactory.CreateSolid(Color.Red, (16, 16)));
}

public override Container Setup()
public override void OnUpdate()
{
map = new Tilemap((16, 16));
string a = "";
return new Container
{
map,
};
OutputUI();
HandleInput();
}

public override void OnUpdate()
private void OutputUI()
{
console.Clear();
console.Print("Hello, Promete!");
console.Print($"FPS: {window.FramePerSeconds}");
console.Print($"UPS: {window.UpdatePerSeconds}");
var pan = (mouse.Position.X - window.Size.X / 2) / (window.Size.X / 2f);
console.Print($"pan: {pan}");
console.Print("Promete Demo\n");
console.Print($"現在のディレクトリ: /{CurrentFolder.GetFullPath()}\n");

if (keyboard.Escape.IsKeyDown)
for (var i = 0; i < CurrentFolder.Files.Count; i++)
{
app.Exit();
var item = CurrentFolder.Files[i];
var label = item is SceneFile file ? $"{file.Name} - {file.Description}" : item.Name;
console.Print($"{(i == CurrentIndex ? ">" : " ")} {label}");
}

if (mouse[MouseButtonType.Left])
{
var (x, y) = mouse.Position / 16;
map[x, y] = tile;
}
console.Print($"{(CurrentIndex == CurrentFolder.Files.Count ? ">" : " ")} もどる");
}

if (keyboard.Space.IsKeyDown)
private void HandleInput()
{
if (keyboard.Up.IsKeyDown)
{
if (player.IsPlaying) player.Stop();
else player.Play(sound);
CurrentIndex--;
if (CurrentIndex < 0) CurrentIndex = 0;
}

if (mouse[MouseButtonType.Right].IsButtonDown)
else if (keyboard.Down.IsKeyDown)
{
player.PlayOneShotAsync(sfx1, mouse.Position.Y / (float)window.Height, 2f, pan);
CurrentIndex++;
if (CurrentIndex > CurrentFolder.Files.Count) CurrentIndex = CurrentFolder.Files.Count;
}
else if (keyboard.Enter.IsKeyDown)
{
if (CurrentIndex == CurrentFolder.Files.Count)
{
if (CurrentFolder.Parent == null) return;

if (keyboard.Number1.IsKeyDown)
CurrentFolder = CurrentFolder.Parent;
CurrentIndex = 0;
}
else
{
var item = CurrentFolder.Files[CurrentIndex];
switch (item)
{
case Folder folder:
CurrentFolder = folder;
CurrentIndex = 0;
break;
case SceneFile file:
app.LoadScene(file.Scene);
break;
}
}
}
else if (keyboard.Escape.IsKeyDown)
{
TestAwaitingTaskAsync();
app.Exit();
}
}

private async void TestAwaitingTaskAsync()
{
player.PlayOneShot(sfx1);
await Task.Delay(1000);
player.PlayOneShot(sfx1);
await Task.Delay(1000);
player.PlayOneShot(sfx1);
await Task.Delay(1000);
}
}
10 changes: 10 additions & 0 deletions Promete.Example/Promete.Example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@
<None Update="assets\lineclear.wav">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="assets\icons.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="assets\ichigo.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
<Folder Include="Attributes\" />
</ItemGroup>

</Project>
Binary file added Promete.Example/assets/ichigo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Promete.Example/assets/icons.aseprite
Binary file not shown.
Binary file added Promete.Example/assets/icons.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
74 changes: 74 additions & 0 deletions Promete.Example/examples/audio/ogg_vorbis.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using Promete.Audio;
using Promete.Example.Kernel;
using Promete.Input;
using Promete.Windowing;

namespace Promete.Example.examples.audio;

[Demo("/audio/ogg vorbis", "BGM を再生します")]
public class OggVorbisExampleScene(PrometeApp app, IWindow window, Keyboard keyboard, ConsoleLayer console) : Scene
{
private readonly AudioPlayer audio = new();
private IAudioSource bgm = new VorbisAudioSource("./assets/kagerou.ogg");

public override void OnStart()
{
window.Title = "Ogg Vorbis playback example";
console.Print("Ogg Vorbis playback Example");

window.FileDropped += WindowOnFileDropped;
}

public override void OnUpdate()
{
console.Cursor += VectorInt.Up * 2;
console.Print($"""
Location: {audio.Time / 1000f:0.000} / {audio.Length / 1000f:0.000}
[↑] Volume Up
[↓] Volume Down
[←] Pitch Down
[→] Pitch Up
PRESS ESC TO RETURN
""");

if (keyboard.Escape.IsKeyUp)
app.LoadScene<MainScene>();

if (keyboard.Up.IsKeyDown)
audio.Gain += 0.1f;

if (keyboard.Down.IsKeyDown)
audio.Gain -= 0.1f;

if (keyboard.Left.IsKeyDown)
audio.Pitch -= 0.1f;

if (keyboard.Right.IsKeyDown)
audio.Pitch += 0.1f;

if (keyboard.Space.IsKeyDown)
{
if (audio.IsPlaying)
audio.Stop();
else
audio.Play(bgm);
}
}

public override void OnDestroy()
{
audio.Stop();
audio.Dispose();
window.FileDropped -= WindowOnFileDropped;
}

private void WindowOnFileDropped(FileDroppedEventArgs e)
{
var path = e.Path;
if (!path.EndsWith(".ogg")) return;

audio.Stop();
bgm = new VorbisAudioSource(path);
audio.Play(bgm);
}
}
29 changes: 29 additions & 0 deletions Promete.Example/examples/audio/wav_sfx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Promete.Audio;
using Promete.Example.Kernel;
using Promete.Input;
using Promete.Windowing;

namespace Promete.Example.examples.audio;

[Demo("/audio/wav sfx", "短い効果音をいくつか再生します")]
public class WavExampleScene(PrometeApp app, IWindow window, Keyboard keyboard, ConsoleLayer console) : Scene
{
private readonly AudioPlayer player = new();
private readonly WaveAudioSource sfx = new("assets/lineclear.wav");

public override void OnStart()
{
console.Print("SFX Example\n");
console.Print("[SPACE]: Replay");
console.Print("[ESC]: Quit");
}

public override void OnUpdate()
{
if (keyboard.Space.IsKeyUp)
player.PlayOneShotAsync(sfx);

if (keyboard.Escape.IsKeyUp)
app.LoadScene<MainScene>();
}
}
Loading

0 comments on commit 1c8af7d

Please sign in to comment.