Compare commits

...

2 commits

Author SHA1 Message Date
b8cd12302e Renamed World to World3D
- Renamed World3D's Camera to InitCamera
2025-03-21 00:43:27 -04:00
f1141d836c WIP First Person Maze example 2025-03-21 00:35:49 -04:00
7 changed files with 147 additions and 61 deletions

8
Config.cs Normal file
View 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; }
}

View file

@ -1,2 +1,7 @@
global using System.Diagnostics;
global using System.Numerics; global using System.Numerics;
global using Tomlyn;
global using Tomlyn.Model;
global using static Raylib_cs.Raylib;
global using Raylib_cs; global using Raylib_cs;
global using PlayBark;

View file

@ -9,6 +9,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Raylib-cs" Version="7.0.1" /> <PackageReference Include="Raylib-cs" Version="7.0.1" />
<PackageReference Include="Tomlyn" Version="0.19.0" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View file

@ -1,73 +1,62 @@
// I hereby waive this project under the public domain - see UNLICENSE for details. // I hereby waive this project under the public domain - see UNLICENSE for details.
// Initialization /// <summary>
//-------------------------------------------------------------------------------------- /// Retrieves configuration settings from a TOML file if it exists; otherwise, returns a default configuration.
const int screenWidth = 600; /// </summary>
const int screenHeight = 450; /// <param name="file">The name of the configuration file (defaults to "config.toml").</param>
Raylib.InitWindow(screenWidth, screenHeight, "PlayBark"); /// <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)
// 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())
{ {
var time = Raylib.GetTime(); var cfgPath = Path.Combine(Tracer.AppDirectory, file);
var scale = (2.0f + (float)Math.Sin(time)) * 0.7f;
var cameraTime = time * 0.3; if (!File.Exists(cfgPath))
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++)
{ {
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++) Width = 600,
{ Height = 450
var blockScale = (x + y + z) / 30.0f; };
var scatter = (float)Math.Sin(blockScale * 20.0f + (float)(time * 4.0f));
var cubePos = new Vector3( return config;
(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);
}
}
} }
Raylib.EndMode3D(); Tracer.WriteLine($"Discovered config file: {cfgPath}");
var toml = File.ReadAllText(cfgPath);
var model = Toml.ToModel<Config>(toml);
Raylib.DrawFPS(10, 10); return model;
Raylib.EndDrawing();
} }
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
View 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
View 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

Binary file not shown.

After

Width:  |  Height:  |  Size: 164 B