Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: RetroRaycaster CI

on: [push, pull_request]

permissions:
contents: read

jobs:
verify:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.0.x'
- name: Restore
run: dotnet restore RetroRaycaster.sln
- name: Build
run: dotnet build RetroRaycaster.sln -c Release --no-restore
- name: Test
run: dotnet test RetroRaycaster.sln -c Release --no-build
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/src/**/bin/
/src/**/obj/
/tests/**/bin/
/tests/**/obj/
/tests/**/TestResults/
19 changes: 19 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# RetroRaycaster

## Struttura

- `RetroRaycaster.sln` raccoglie i tre progetti C# con target `net8.0`.
- `src/RetroRaycaster.Core/` contiene la libreria autonoma.
- `src/RetroRaycaster.Desktop/` contiene l'eseguibile e fa riferimento a Core.
- `tests/RetroRaycaster.Core.Tests/` contiene i test xUnit di Core.
- `WOLFSRC/` conserva i sorgenti DOS come riferimento storico. Mantieni questa directory intatta.

## Verifica

Usa un SDK .NET 8 compatibile con `global.json`. Dalla radice del repository esegui gli stessi comandi del workflow GitHub Actions.

```sh
dotnet restore RetroRaycaster.sln
dotnet build RetroRaycaster.sln -c Release --no-restore
dotnet test RetroRaycaster.sln -c Release --no-build
```
43 changes: 43 additions & 0 deletions RetroRaycaster.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{2AFFA176-7E1D-4831-907B-72375D383F0C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RetroRaycaster.Core", "src\RetroRaycaster.Core\RetroRaycaster.Core.csproj", "{D2A87438-1502-4120-91AD-CE93DEDB52CF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RetroRaycaster.Desktop", "src\RetroRaycaster.Desktop\RetroRaycaster.Desktop.csproj", "{52828A9A-BD11-44AF-9B88-668BB58ACE52}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{006F083A-7131-4F81-92A7-17B79D5698B5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RetroRaycaster.Core.Tests", "tests\RetroRaycaster.Core.Tests\RetroRaycaster.Core.Tests.csproj", "{E6DB9596-F818-46D2-A20A-BD67ED4E8B5D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D2A87438-1502-4120-91AD-CE93DEDB52CF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D2A87438-1502-4120-91AD-CE93DEDB52CF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D2A87438-1502-4120-91AD-CE93DEDB52CF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D2A87438-1502-4120-91AD-CE93DEDB52CF}.Release|Any CPU.Build.0 = Release|Any CPU
{52828A9A-BD11-44AF-9B88-668BB58ACE52}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{52828A9A-BD11-44AF-9B88-668BB58ACE52}.Debug|Any CPU.Build.0 = Debug|Any CPU
{52828A9A-BD11-44AF-9B88-668BB58ACE52}.Release|Any CPU.ActiveCfg = Release|Any CPU
{52828A9A-BD11-44AF-9B88-668BB58ACE52}.Release|Any CPU.Build.0 = Release|Any CPU
{E6DB9596-F818-46D2-A20A-BD67ED4E8B5D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E6DB9596-F818-46D2-A20A-BD67ED4E8B5D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E6DB9596-F818-46D2-A20A-BD67ED4E8B5D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E6DB9596-F818-46D2-A20A-BD67ED4E8B5D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{D2A87438-1502-4120-91AD-CE93DEDB52CF} = {2AFFA176-7E1D-4831-907B-72375D383F0C}
{52828A9A-BD11-44AF-9B88-668BB58ACE52} = {2AFFA176-7E1D-4831-907B-72375D383F0C}
{E6DB9596-F818-46D2-A20A-BD67ED4E8B5D} = {006F083A-7131-4F81-92A7-17B79D5698B5}
EndGlobalSection
EndGlobal
7 changes: 7 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"sdk": {
"version": "8.0.100",
"rollForward": "latestFeature",
"allowPrerelease": false
}
}
78 changes: 78 additions & 0 deletions src/RetroRaycaster.Core/GridMap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;

namespace RetroRaycaster.Core;

public enum WallSide { Vertical, Horizontal }

/// <summary>The entry point into the wall is origin + direction * T.</summary>
public readonly record struct RayHit(int CellX, int CellY, WallSide Side, double T);

/// <summary>A grid of unit cells. X increases rightward and Y downward.</summary>
public sealed class GridMap
{
private readonly bool[,] solid;
public int Width => solid.GetLength(1);
public int Height => solid.GetLength(0);

/// <param name="solidCells">Rows indexed by [y, x]. True denotes a wall.</param>
public GridMap(bool[,] solidCells)
{
ArgumentNullException.ThrowIfNull(solidCells);
if (solidCells.GetLength(0) == 0 || solidCells.GetLength(1) == 0)
throw new ArgumentException("The grid must contain at least one row and column.", nameof(solidCells));

solid = (bool[,])solidCells.Clone();
}

/// <summary>
/// Finds the first wall entered, or returns null for an outside origin, a zero
/// direction, or a ray that leaves the map. Directions need not have unit length.
/// The origin belongs to cell (floor(x), floor(y)) and must be empty if inside.
/// At a corner the ray enters only the diagonal cell and reports its vertical side.
/// </summary>
public RayHit? CastRay(double originX, double originY, double directionX, double directionY)
{
if (!double.IsFinite(originX) || !double.IsFinite(originY) ||
!double.IsFinite(directionX) || !double.IsFinite(directionY))
throw new ArgumentException("Ray coordinates and direction must be finite.");

if (originX < 0 || originX >= Width || originY < 0 || originY >= Height ||
(directionX == 0 && directionY == 0))
return null;

int x = (int)originX;
int y = (int)originY;
if (solid[y, x])
throw new ArgumentException("A moving ray must start in an empty cell.");

int stepX = Math.Sign(directionX);
int stepY = Math.Sign(directionY);
double nextX = CrossingTime(x, originX, directionX);
double nextY = CrossingTime(y, originY, directionY);

while (true)
{
bool crossX = stepX != 0 && (stepY == 0 || nextX <= nextY);
bool crossY = stepY != 0 && (stepX == 0 || nextY <= nextX);
double t = crossX ? nextX : nextY;

if (crossX) x += stepX;
if (crossY) y += stepY;
if (x < 0 || x >= Width || y < 0 || y >= Height)
return null;
if (solid[y, x])
return new RayHit(x, y, crossX ? WallSide.Vertical : WallSide.Horizontal, t);

// Compute from the original ray to avoid accumulating rounding at corners.
if (crossX) nextX = CrossingTime(x, originX, directionX);
if (crossY) nextY = CrossingTime(y, originY, directionY);
}
}

private static double CrossingTime(int cell, double origin, double direction)
{
if (direction == 0) return double.PositiveInfinity;
int boundary = direction > 0 ? cell + 1 : cell;
return (boundary - origin) / direction;
}
}
6 changes: 6 additions & 0 deletions src/RetroRaycaster.Core/RetroRaycaster.Core.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
1 change: 1 addition & 0 deletions src/RetroRaycaster.Desktop/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return 0;
10 changes: 10 additions & 0 deletions src/RetroRaycaster.Desktop/RetroRaycaster.Desktop.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="../RetroRaycaster.Core/RetroRaycaster.Core.csproj" />
</ItemGroup>
</Project>
15 changes: 15 additions & 0 deletions tests/RetroRaycaster.Core.Tests/CoreAssemblyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Reflection;
using Xunit;

namespace RetroRaycaster.Core.Tests;

public class CoreAssemblyTests
{
[Fact]
public void CoreLoadsAsLibrary()
{
var core = Assembly.Load("RetroRaycaster.Core");

Assert.Null(core.EntryPoint);
}
}
54 changes: 54 additions & 0 deletions tests/RetroRaycaster.Core.Tests/GridMapTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using Xunit;

namespace RetroRaycaster.Core.Tests;

public class GridMapTests
{
[Theory]
[InlineData(1.5, 2.5, 1, 0, 4, 2, WallSide.Vertical, 2.5)]
[InlineData(1.5, 2.5, 2, 0, 4, 2, WallSide.Vertical, 1.25)]
[InlineData(4.5, 2.5, -1, 0, 1, 2, WallSide.Vertical, 2.5)]
[InlineData(3.5, 0.5, 0, 1, 3, 2, WallSide.Horizontal, 1.5)]
[InlineData(3.5, 2.5, 0, -1, 3, 0, WallSide.Horizontal, 1.5)]
[InlineData(0.75, 0.25, 2, 1, 2, 1, WallSide.Horizontal, 0.75)]
[InlineData(3, 1, -1, 0, 2, 1, WallSide.Vertical, 0)]
public void HitsFirstWall(double x, double y, double dx, double dy,
int wallX, int wallY, WallSide side, double t)
{
var cells = new bool[3, 6];
cells[wallY, wallX] = true;
cells[2, 5] = true;
Assert.Equal(new RayHit(wallX, wallY, side, t), new GridMap(cells).CastRay(x, y, dx, dy));
}

[Fact]
public void CornerCrossingSkipsWallsTouchedOnlyAtTheirVertex()
{
var cells = new bool[,] { { false, true }, { true, false } };
Assert.Null(new GridMap(cells).CastRay(0.5, 0.5, 1, 1));

cells[1, 1] = true;
Assert.Equal(new RayHit(1, 1, WallSide.Vertical, 0.5), new GridMap(cells).CastRay(0.5, 0.5, 1, 1));
}

[Theory]
[InlineData(0.5, 0.5, 1, 0)]
[InlineData(0.5, 0.5, 0, 0)]
[InlineData(-0.5, 1.5, 1, 0)]
[InlineData(4, 1.5, -1, 0)]
[InlineData(2.5, 2, 0, -1)]
public void ReturnsNoHit(double x, double y, double dx, double dy)
{
var cells = new bool[2, 4];
cells[1, 2] = true;
Assert.Null(new GridMap(cells).CastRay(x, y, dx, dy));
}

[Fact]
public void RejectsMovingRayStartingInsideWall()
{
var map = new GridMap(new bool[,] { { true } });
Assert.Throws<ArgumentException>(() => map.CastRay(0.5, 0.5, 1, 0));
}
}
14 changes: 14 additions & 0 deletions tests/RetroRaycaster.Core.Tests/RetroRaycaster.Core.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" PrivateAssets="all" />
<ProjectReference Include="../../src/RetroRaycaster.Core/RetroRaycaster.Core.csproj" />
</ItemGroup>
</Project>