mirror of
https://github.com/tonytins/playbark.git
synced 2025-09-22 20:26:54 -04:00
Compare commits
No commits in common. "57736192c8882e8fbf4d2584d1a1234d8881b8c6" and "b8cd12302e00f661d9d5a37a0a0393b630ce8603" have entirely different histories.
57736192c8
...
b8cd12302e
4 changed files with 14 additions and 120 deletions
|
@ -4,7 +4,6 @@
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
|
105
Program.cs
105
Program.cs
|
@ -5,7 +5,7 @@
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="file">The name of the configuration file (defaults to "config.toml").</param>
|
/// <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>
|
/// <returns>A Config object populated with values from the file, or a default Config instance if the file is not found.</returns>
|
||||||
static Config Settings(string file)
|
static Config ReadConfig(string file)
|
||||||
{
|
{
|
||||||
var cfgPath = Path.Combine(Tracer.AppDirectory, file);
|
var cfgPath = Path.Combine(Tracer.AppDirectory, file);
|
||||||
|
|
||||||
|
@ -28,116 +28,35 @@ static Config Settings(string file)
|
||||||
return model;
|
return model;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update and Draw
|
void Init(int screenWidth, int screenHeight, int fps)
|
||||||
unsafe int Game()
|
|
||||||
{
|
{
|
||||||
var config = Settings("config.toml");
|
|
||||||
InitWindow(config.Width, config.Height, "PlayBark");
|
|
||||||
|
|
||||||
var pos = new Vector3(0.2f, 0.4f, 0.2f);
|
var pos = new Vector3(0.2f, 0.4f, 0.2f);
|
||||||
var target = new Vector3(0.0f, 0.0f, 0.0f);
|
var target = new Vector3(0.0f, 0.0f, 0.0f);
|
||||||
var up = new Vector3(0.0f, 1.0f, 0.0f);
|
var up = new Vector3(0.0f, 1.0f, 0.0f);
|
||||||
var camera = World3D.Camera(pos, target, up, CameraProjection.Perspective);
|
|
||||||
|
|
||||||
var imMap = LoadImage("resources/cubicmap.png");
|
InitWindow(screenWidth, screenHeight, $"PlayBark");
|
||||||
var cubicmap = LoadTextureFromImage(imMap);
|
World3D.InitCamera(pos, target, up, CameraProjection.Perspective);
|
||||||
var model = World3D.CubicMap(imMap);
|
SetTargetFPS(fps);
|
||||||
|
}
|
||||||
|
|
||||||
var texture = LoadTexture("resources/cubicmap_atlas.png");
|
int GameLoop()
|
||||||
|
{
|
||||||
// Set map diffuse texture
|
var config = ReadConfig("config.toml");
|
||||||
Raylib.SetMaterialTexture(ref model, 0, MaterialMapIndex.Albedo, ref texture);
|
Init(config.Width, config.Height, 60);
|
||||||
|
|
||||||
// Get image map data to be used for collission
|
|
||||||
var mapPixels = LoadImageColors(imMap);
|
|
||||||
UnloadImage(imMap);
|
|
||||||
|
|
||||||
var mapPosition = new Vector3(-16.0f, 0.0f, -8.0f);
|
|
||||||
var playerPosition = camera.Position;
|
|
||||||
|
|
||||||
SetTargetFPS(60);
|
|
||||||
|
|
||||||
while (!WindowShouldClose())
|
while (!WindowShouldClose())
|
||||||
{
|
{
|
||||||
// Update
|
|
||||||
var oldCamPos = camera.Position;
|
|
||||||
UpdateCamera(ref camera, CameraMode.FirstPerson);
|
|
||||||
|
|
||||||
var playerPos = new Vector2(camera.Position.X, camera.Position.Z);
|
|
||||||
|
|
||||||
var playerRadius = 0.1f;
|
|
||||||
|
|
||||||
var playerCellX = (int)(playerPos.X - mapPosition.X + 0.5f);
|
|
||||||
var playerCellY = (int)(playerPos.Y - mapPosition.Z + 0.5f);
|
|
||||||
|
|
||||||
// Out-of-limits security check
|
|
||||||
if (playerCellX < 0)
|
|
||||||
{
|
|
||||||
playerCellX = 0;
|
|
||||||
}
|
|
||||||
else if (playerCellX >= cubicmap.Width)
|
|
||||||
{
|
|
||||||
playerCellX = cubicmap.Width - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (playerCellY < 0)
|
|
||||||
{
|
|
||||||
playerCellY = 0;
|
|
||||||
}
|
|
||||||
else if (playerCellY >= cubicmap.Height)
|
|
||||||
{
|
|
||||||
playerCellY = cubicmap.Height - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (var y = 0; y < cubicmap.Height; y++)
|
|
||||||
{
|
|
||||||
for (var x = 0; y < cubicmap.Width; x++)
|
|
||||||
{
|
|
||||||
var mapPixelsData = mapPixels;
|
|
||||||
var rec = new Rectangle(
|
|
||||||
mapPosition.X - x - 0.5f + x * 1.0f,
|
|
||||||
mapPosition.Y - y - 0.5f + x * 1.0f,
|
|
||||||
1.0f,
|
|
||||||
1.0f
|
|
||||||
);
|
|
||||||
|
|
||||||
var collision = CheckCollisionCircleRec(playerPos, playerRadius, rec);
|
|
||||||
if ((mapPixelsData[y * cubicmap.Width + x].R == 255) && collision)
|
|
||||||
{
|
|
||||||
// Collision detected, reset camera position
|
|
||||||
camera.Position = oldCamPos;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
ClearBackground(Color.RayWhite);
|
ClearBackground(Color.White);
|
||||||
BeginMode3D(camera);
|
|
||||||
DrawModel(model, mapPosition, 1.0f, Color.White);
|
|
||||||
EndMode3D();
|
|
||||||
|
|
||||||
DrawTextureEx(cubicmap, new Vector2(GetScreenWidth() - cubicmap.Width * 4 - 20, 20), 0.0f, 4.0f, Color.White);
|
|
||||||
DrawRectangleLines(GetScreenWidth() - cubicmap.Width * 4 - 20, 20, cubicmap.Width * 4, cubicmap.Height * 4, Color.Green);
|
|
||||||
|
|
||||||
// Draw player position radar
|
|
||||||
DrawRectangle(GetScreenWidth() - cubicmap.Width * 4 - 20 + playerCellX * 4, 20 + playerCellY * 4, 4, 4, Color.Red);
|
|
||||||
|
|
||||||
|
|
||||||
DrawFPS(10, 10);
|
DrawFPS(10, 10);
|
||||||
|
|
||||||
EndDrawing();
|
EndDrawing();
|
||||||
}
|
}
|
||||||
|
|
||||||
UnloadImageColors(mapPixels);
|
|
||||||
|
|
||||||
UnloadTexture(cubicmap);
|
|
||||||
UnloadTexture(texture);
|
|
||||||
UnloadModel(model);
|
|
||||||
|
|
||||||
CloseWindow();
|
CloseWindow();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Entry point
|
GameLoop();
|
||||||
Game();
|
|
||||||
|
|
28
World3D.cs
28
World3D.cs
|
@ -1,22 +1,11 @@
|
||||||
// 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.
|
||||||
namespace PlayBark;
|
namespace PlayBark;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Provides utilities for creating and managing 3D world elements, including camera setup and map generation.
|
|
||||||
/// </summary>
|
|
||||||
internal static class World3D
|
internal static class World3D
|
||||||
{
|
{
|
||||||
/// <summary>
|
public static Camera3D InitCamera(Vector3 pos, Vector3 target, Vector3 up, CameraProjection projection)
|
||||||
/// Creates and returns a configured Camera3D instance.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="pos">The position of the camera in 3D space.</param>
|
|
||||||
/// <param name="target">The point the camera is looking at.</param>
|
|
||||||
/// <param name="up">The up direction for the camera.</param>
|
|
||||||
/// <param name="projection">The projection type of the camera.</param>
|
|
||||||
/// <returns>A configured Camera3D instance.</returns>
|
|
||||||
public static Camera3D Camera(Vector3 pos, Vector3 target, Vector3 up, CameraProjection projection)
|
|
||||||
{
|
{
|
||||||
var camera = new Camera3D();
|
Camera3D camera = new();
|
||||||
camera.Position = pos;
|
camera.Position = pos;
|
||||||
camera.Target = target;
|
camera.Target = target;
|
||||||
camera.Up = up;
|
camera.Up = up;
|
||||||
|
@ -25,17 +14,4 @@ internal static class World3D
|
||||||
|
|
||||||
return camera;
|
return camera;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Generates a 3D cubic map model from a given image map.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="imMap">The image used to generate the cubic map.</param>
|
|
||||||
/// <returns>A Model representing the cubic map.</returns>
|
|
||||||
public static Model CubicMap(Image imMap)
|
|
||||||
{
|
|
||||||
var mesh = GenMeshCubicmap(imMap, new Vector3(1.0f, 1.0f, 1.0f));
|
|
||||||
var model = LoadModelFromMesh(mesh);
|
|
||||||
|
|
||||||
return model;
|
|
||||||
}
|
|
||||||
}
|
}
|
Binary file not shown.
Before Width: | Height: | Size: 36 KiB |
Loading…
Add table
Add a link
Reference in a new issue