mirror of
https://github.com/tonytins/playbark.git
synced 2025-04-30 10:31:42 -04:00
Compare commits
2 commits
ccfe5193c1
...
b8cd12302e
Author | SHA1 | Date | |
---|---|---|---|
b8cd12302e | |||
f1141d836c |
7 changed files with 147 additions and 61 deletions
8
Config.cs
Normal file
8
Config.cs
Normal file
|
@ -0,0 +1,8 @@
|
|||
// I hereby waive this project under the public domain - see UNLICENSE for details.
|
||||
namespace PlayBark;
|
||||
|
||||
internal class Config
|
||||
{
|
||||
public int Width { get; set; }
|
||||
public int Height { get; set; }
|
||||
}
|
|
@ -1,2 +1,7 @@
|
|||
global using System.Diagnostics;
|
||||
global using System.Numerics;
|
||||
global using Tomlyn;
|
||||
global using Tomlyn.Model;
|
||||
global using static Raylib_cs.Raylib;
|
||||
global using Raylib_cs;
|
||||
global using PlayBark;
|
|
@ -9,6 +9,7 @@
|
|||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Raylib-cs" Version="7.0.1" />
|
||||
<PackageReference Include="Tomlyn" Version="0.19.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
111
Program.cs
111
Program.cs
|
@ -1,73 +1,62 @@
|
|||
// I hereby waive this project under the public domain - see UNLICENSE for details.
|
||||
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 600;
|
||||
const int screenHeight = 450;
|
||||
Raylib.InitWindow(screenWidth, screenHeight, "PlayBark");
|
||||
|
||||
// Based on WavingCube example:
|
||||
// https://github.com/raylib-cs/raylib-cs/blob/master/Examples/Models/WavingCubes.cs
|
||||
|
||||
// Initialize the camera
|
||||
var camera = new Camera3D();
|
||||
camera.Position = new Vector3(30.0f, 20.0f, 30.0f);
|
||||
camera.Target = new Vector3(0.0f, 0.0f, 0.0f);
|
||||
camera.Up = new Vector3(0.0f, 1.0f, 0.0f);
|
||||
camera.FovY = 70.0f;
|
||||
camera.Projection = CameraProjection.Perspective;
|
||||
|
||||
const int numBlocks = 15;
|
||||
|
||||
Raylib.SetTargetFPS(60);
|
||||
|
||||
// Main game loop
|
||||
while (!Raylib.WindowShouldClose())
|
||||
/// <summary>
|
||||
/// Retrieves configuration settings from a TOML file if it exists; otherwise, returns a default configuration.
|
||||
/// </summary>
|
||||
/// <param name="file">The name of the configuration file (defaults to "config.toml").</param>
|
||||
/// <returns>A Config object populated with values from the file, or a default Config instance if the file is not found.</returns>
|
||||
static Config ReadConfig(string file)
|
||||
{
|
||||
var time = Raylib.GetTime();
|
||||
var scale = (2.0f + (float)Math.Sin(time)) * 0.7f;
|
||||
var cfgPath = Path.Combine(Tracer.AppDirectory, file);
|
||||
|
||||
var cameraTime = time * 0.3;
|
||||
|
||||
camera.Position.X = (float)Math.Cos(cameraTime) * 40.0f;
|
||||
camera.Position.Z = (float)Math.Cos(cameraTime) * 40.0f;
|
||||
|
||||
Raylib.BeginDrawing();
|
||||
Raylib.ClearBackground(Color.RayWhite);
|
||||
|
||||
Raylib.BeginMode3D(camera);
|
||||
|
||||
Raylib.DrawGrid(10, 5.0f);
|
||||
|
||||
for (int x = 0; x < numBlocks; x++)
|
||||
if (!File.Exists(cfgPath))
|
||||
{
|
||||
for (int y = 0; y < numBlocks; y++)
|
||||
Tracer.WriteLine("Config file not found. Switching to defaults.");
|
||||
var config = new Config
|
||||
{
|
||||
for (int z = 0; z < numBlocks; z++)
|
||||
{
|
||||
var blockScale = (x + y + z) / 30.0f;
|
||||
var scatter = (float)Math.Sin(blockScale * 20.0f + (float)(time * 4.0f));
|
||||
Width = 600,
|
||||
Height = 450
|
||||
};
|
||||
|
||||
var cubePos = new Vector3(
|
||||
(float)(x - numBlocks / 2) * (scale * 3.0f) + scatter,
|
||||
(float)(x - numBlocks / 2) * (scale * 2.0f) + scatter,
|
||||
(float)(x - numBlocks / 2) * (scale * 3.0f) + scatter
|
||||
);
|
||||
|
||||
var cubeColor = Raylib.ColorFromHSV((float)(((x + y + z) * 18) % 360), 0.75f, 0.9f);
|
||||
|
||||
var cubeSize = (2.4f - scale) * blockScale;
|
||||
|
||||
Raylib.DrawCube(cubePos, cubeSize, cubeSize, cubeSize, cubeColor);
|
||||
}
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
Raylib.EndMode3D();
|
||||
Tracer.WriteLine($"Discovered config file: {cfgPath}");
|
||||
var toml = File.ReadAllText(cfgPath);
|
||||
var model = Toml.ToModel<Config>(toml);
|
||||
|
||||
Raylib.DrawFPS(10, 10);
|
||||
|
||||
Raylib.EndDrawing();
|
||||
return model;
|
||||
}
|
||||
|
||||
Raylib.CloseWindow();
|
||||
void Init(int screenWidth, int screenHeight, int fps)
|
||||
{
|
||||
var pos = new Vector3(0.2f, 0.4f, 0.2f);
|
||||
var target = new Vector3(0.0f, 0.0f, 0.0f);
|
||||
var up = new Vector3(0.0f, 1.0f, 0.0f);
|
||||
|
||||
InitWindow(screenWidth, screenHeight, $"PlayBark");
|
||||
World3D.InitCamera(pos, target, up, CameraProjection.Perspective);
|
||||
SetTargetFPS(fps);
|
||||
}
|
||||
|
||||
int GameLoop()
|
||||
{
|
||||
var config = ReadConfig("config.toml");
|
||||
Init(config.Width, config.Height, 60);
|
||||
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
BeginDrawing();
|
||||
ClearBackground(Color.White);
|
||||
|
||||
DrawFPS(10, 10);
|
||||
|
||||
EndDrawing();
|
||||
}
|
||||
|
||||
CloseWindow();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
GameLoop();
|
||||
|
|
66
Tracer.cs
Normal file
66
Tracer.cs
Normal file
|
@ -0,0 +1,66 @@
|
|||
// I hereby waive this project under the public domain - see UNLICENSE for details.
|
||||
namespace PlayBark;
|
||||
|
||||
/// <summary>
|
||||
/// Provides debug-only console output methods.
|
||||
/// These methods are only executed when the application is compiled in DEBUG mode.
|
||||
/// </summary>
|
||||
internal static class Tracer
|
||||
{
|
||||
/// <summary>
|
||||
/// Writes a line of text to the console, but only when in DEBUG mode.
|
||||
/// </summary>
|
||||
/// <param name="content">The text to write to the console.</param>
|
||||
[Conditional("DEBUG")]
|
||||
internal static void WriteLine(string content) =>
|
||||
Console.WriteLine(content);
|
||||
|
||||
/// <summary>
|
||||
/// Writes text to the console without a newline, but only when in DEBUG mode.
|
||||
/// </summary>
|
||||
/// <param name="content">The text to write to the console.</param>
|
||||
[Conditional("DEBUG")]
|
||||
internal static void Write(string content) =>
|
||||
Console.Write(content);
|
||||
|
||||
/// <summary>
|
||||
/// Writes multiple lines of text to the console, but only when in DEBUG mode.
|
||||
/// </summary>
|
||||
/// <param name="contents">A collection of text lines to write to the console.</param>
|
||||
[Conditional("DEBUG")]
|
||||
internal static void WriteLine(IEnumerable<string> contents)
|
||||
{
|
||||
foreach (var content in contents)
|
||||
{
|
||||
Console.WriteLine(content);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes multiple text entries to the console without newlines, but only when in DEBUG mode.
|
||||
/// </summary>
|
||||
/// <param name="contents">A collection of text entries to write to the console.</param>
|
||||
[Conditional("DEBUG")]
|
||||
internal static void Write(IEnumerable<string> contents)
|
||||
{
|
||||
foreach (var content in contents)
|
||||
{
|
||||
Console.Write(content);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the current working directory in DEBUG mode or the application's base directory in release mode.
|
||||
/// </summary>
|
||||
internal static string AppDirectory
|
||||
{
|
||||
get
|
||||
{
|
||||
#if DEBUG
|
||||
return Directory.GetCurrentDirectory();
|
||||
#else
|
||||
return AppDomain.CurrentDomain.BaseDirectory;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
17
World3D.cs
Normal file
17
World3D.cs
Normal file
|
@ -0,0 +1,17 @@
|
|||
// I hereby waive this project under the public domain - see UNLICENSE for details.
|
||||
namespace PlayBark;
|
||||
|
||||
internal static class World3D
|
||||
{
|
||||
public static Camera3D InitCamera(Vector3 pos, Vector3 target, Vector3 up, CameraProjection projection)
|
||||
{
|
||||
Camera3D camera = new();
|
||||
camera.Position = pos;
|
||||
camera.Target = target;
|
||||
camera.Up = up;
|
||||
camera.FovY = 45.0f;
|
||||
camera.Projection = projection;
|
||||
|
||||
return camera;
|
||||
}
|
||||
}
|
BIN
resources/cubicmap.png
Normal file
BIN
resources/cubicmap.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 164 B |
Loading…
Add table
Reference in a new issue