structure du projet, ajout de la lib hexagonal

This commit is contained in:
2025-03-11 23:37:12 +01:00
parent 7213e43d67
commit a43df23e89
52 changed files with 2783 additions and 2 deletions

View File

@@ -0,0 +1,57 @@
using HexagonalLib.Coordinates;
using NUnit.Framework;
namespace HexagonalLib.Tests
{
[TestFixture]
public class HexagonalGridTests
{
private float InscribedRadius => 0.5f;
[Test(Author = "Ivan Murashka", Description = "Check coordinate conversion from Offset to Unity vectors")]
public void Vector2ConversionTest(
[Values] HexagonalGridType type,
[Values(-13, -8, 0, 15, 22)] int offsetX,
[Values(-13, -8, 0, 15, 22)] int offsetY)
{
var grid = new HexagonalGrid(type, InscribedRadius);
var offset = new Offset(offsetX, offsetY);
var axial = grid.ToAxial(offset);
var cubic = grid.ToCubic(offset);
var fromOffset = grid.ToVector2(offset);
var fromAxial = grid.ToVector2(axial);
var fromCubic = grid.ToVector2(cubic);
Assert.IsTrue(fromOffset.SimilarTo(fromAxial), $"Expected: {fromAxial}; Actual: {fromOffset}");
Assert.IsTrue(fromOffset.SimilarTo(fromCubic), $"Expected: {fromCubic}; Actual: {fromOffset}");
Assert.AreEqual(offset, grid.ToOffset(fromOffset));
Assert.AreEqual(axial, grid.ToAxial(fromAxial));
Assert.AreEqual(cubic, grid.ToCubic(fromCubic));
}
[Test(Author = "Ivan Murashka", Description = "Check coordinate conversion from Offset to Unity vectors")]
public void Vector3ConversionTest(
[Values] HexagonalGridType type,
[Values(-13, -8, 0, 15, 22)] int offsetX,
[Values(-13, -8, 0, 15, 22)] int offsetY)
{
var grid = new HexagonalGrid(type, InscribedRadius);
var offset = new Offset(offsetX, offsetY);
var axial = grid.ToAxial(offset);
var cubic = grid.ToCubic(offset);
var fromOffset = grid.ToVector3(offset);
var fromAxial = grid.ToVector3(axial);
var fromCubic = grid.ToVector3(cubic);
Assert.IsTrue(fromOffset.SimilarTo(fromAxial), $"Expected: {fromAxial}; Actual: {fromOffset}");
Assert.IsTrue(fromOffset.SimilarTo(fromCubic), $"Expected: {fromCubic}; Actual: {fromOffset}");
Assert.AreEqual(offset, grid.ToOffset(fromOffset));
Assert.AreEqual(axial, grid.ToAxial(fromAxial));
Assert.AreEqual(cubic, grid.ToCubic(fromCubic));
}
}
}

View File

@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\..\.packages\NUnit.3.12.0\build\NUnit.props" Condition="Exists('..\..\.packages\NUnit.3.12.0\build\NUnit.props')" />
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{1E807875-014C-4745-86AB-E1E35916F8B3}</ProjectGuid>
<ProjectTypeGuids>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>HexagonalLib.Tests</RootNamespace>
<AssemblyName>HexagonalLib.Unity.Tests</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\..\.bin\HexagonalLib.Unity.Tests\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\..\.bin\HexagonalLib.Unity.Tests\Debug\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="nunit.framework, Version=3.12.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb">
<HintPath>..\..\.packages\NUnit.3.12.0\lib\net45\nunit.framework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\..\.packages\Unity3D.UnityEngine.2018.3.5.1\lib\UnityEngine.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="HexagonalGridTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HexagonalLib.Unity\HexagonalLib.Unity.csproj">
<Project>{ee497232-ac7a-48d0-9059-53b152c6bf01}</Project>
<Name>HexagonalLib.Unity</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\..\.packages\NUnit.3.12.0\build\NUnit.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\.packages\NUnit.3.12.0\build\NUnit.props'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@@ -0,0 +1,35 @@
using System.Reflection;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("HexagonalLib.Unity.Tests")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("HexagonalLib.Unity.Tests")]
[assembly: AssemblyCopyright("Copyright © 2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("1E807875-014C-4745-86AB-E1E35916F8B3")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NUnit" version="3.12.0" targetFramework="net472" />
<package id="Unity3D.UnityEngine" version="2018.3.5.1" targetFramework="net472" />
</packages>

View File

@@ -0,0 +1,7 @@
namespace HexagonalLib.Coordinates
{
public partial struct Axial
{
}
}

View File

@@ -0,0 +1,6 @@
namespace HexagonalLib.Coordinates
{
public readonly partial struct Cubic
{
}
}

View File

@@ -0,0 +1,6 @@
namespace HexagonalLib.Coordinates
{
public readonly partial struct Offset
{
}
}

View File

@@ -0,0 +1,171 @@
using System;
using System.Collections.Generic;
using HexagonalLib.Coordinates;
using HexagonalLib.Utility;
using UnityEngine;
namespace HexagonalLib
{
public partial struct HexagonalGrid
{
#region ToOffset
/// <summary>
/// Convert point to offset coordinate
/// </summary>
public Offset ToOffset(Vector2 vector)
{
return ToOffset(vector.AsTuple());
}
/// <summary>
/// Convert point to offset coordinate
/// </summary>
public Offset ToOffset(Vector3 vector)
{
return ToOffset(vector.AsTuple());
}
#endregion
#region ToAxial
/// <summary>
/// Convert point to axial coordinate
/// </summary>
public Axial ToAxial(Vector2 vector)
{
return ToAxial(vector.AsTuple());
}
/// <summary>
/// Convert point to axial coordinate
/// </summary>
public Axial ToAxial(Vector3 vector)
{
return ToAxial(vector.AsTuple());
}
#endregion
#region ToCubic
/// <summary>
/// Convert point to cubic coordinate
/// </summary>
public Cubic ToCubic(Vector2 vector)
{
return ToCubic(vector.AsTuple());
}
/// <summary>
/// Convert point to cubic coordinate
/// </summary>
public Cubic ToCubic(Vector3 vector)
{
return ToCubic(vector.AsTuple());
}
#endregion
#region ToVector
/// <summary>
/// Convert hex based on its offset coordinate to it center position in 2d space
/// </summary>
public Vector2 ToVector2(Offset coord)
{
return ToPoint2(coord).AsVector2();
}
/// <summary>
/// Convert hex based on its axial coordinate to it center position in 2d space
/// </summary>
public Vector2 ToVector2(Axial coord)
{
return ToPoint2(coord).AsVector2();
}
/// <summary>
/// Convert hex based on its cubic coordinate to it center position in 2d space
/// </summary>
public Vector2 ToVector2(Cubic coord)
{
return ToPoint2(coord).AsVector2();
}
/// <summary>
/// Convert hex based on its offset coordinate to it center position in 3d space OZ
/// </summary>
public Vector3 ToVector3(Offset coord, float y = 0)
{
return ToPoint2(coord).AsVector3(y);
}
/// <summary>
/// Convert hex based on its axial coordinate to it center position in 3d space OZ
/// </summary>
public Vector3 ToVector3(Axial coord, float y = 0)
{
return ToPoint2(coord).AsVector3(y);
}
/// <summary>
/// Convert hex based on its cubic coordinate to it center position in 3d space OZ
/// </summary>
public Vector3 ToVector3(Cubic coord, float y = 0)
{
return ToPoint2(coord).AsVector3(y);
}
#endregion
#region CreateMesh
/// <summary>
/// Generates a mesh for list of hex. The generation algorithm is taken from the site:
/// </summary>
public Mesh CreateMesh(IReadOnlyList<Offset> hexes, int subdivide)
{
return CreateMesh(hexes, subdivide, CreateMesh);
}
/// <summary>
/// Generates a mesh for list of hex. The generation algorithm is taken from the site:
/// </summary>
public Mesh CreateMesh(IReadOnlyList<Axial> hexes, int subdivide)
{
return CreateMesh(hexes, subdivide, CreateMesh);
}
/// <summary>
/// Generates a mesh for list of hex. The generation algorithm is taken from the site:
/// </summary>
public Mesh CreateMesh(IReadOnlyList<Cubic> hexes, int subdivide)
{
return CreateMesh(hexes, subdivide, CreateMesh);
}
private Mesh CreateMesh<T>(IReadOnlyList<T> hexes, int subdivide, Action<IEnumerable<T>, int, Action<int, (float X, float Y)>, Action<int, int>> createMesh)
{
var meshData = GetMeshData(hexes.Count, subdivide);
var vertices = new Vector3[meshData.VerticesCount];
var triangles = new int[meshData.IndicesCount];
createMesh(hexes, subdivide,
(i, point) => vertices[i] = new Vector3(point.X, 0, point.Y),
(i, index) => triangles[i] = index);
var mesh = new Mesh
{
vertices = vertices,
triangles = triangles
};
mesh.RecalculateNormals();
return mesh;
}
#endregion
}
}

View File

@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{EE497232-AC7A-48D0-9059-53B152C6BF01}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>HexagonalLib</RootNamespace>
<AssemblyName>HexagonalLib.Unity</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\..\.bin\HexagonalLib.Unity\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\..\.bin\HexagonalLib.Unity\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\..\.packages\Unity3D.UnityEngine.2018.3.5.1\lib\UnityEngine.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Coordinates\Axial.cs" />
<Compile Include="Coordinates\Cubic.cs" />
<Compile Include="Coordinates\Offset.cs" />
<Compile Include="HexagonalGrid.cs" />
<Compile Include="HexagonalMath.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utility\VectorUtility.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\..\HexagonalLib\HexagonalLib.projitems" Label="Shared" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@@ -0,0 +1,23 @@
using UnityEngine;
namespace HexagonalLib
{
public static partial class HexagonalMath
{
/// <summary>
/// Compares two vectors and returns true if they are similar.
/// </summary>
public static bool SimilarTo(this in Vector2 a, in Vector2 b)
{
return a.x.SimilarTo(b.x) && a.y.SimilarTo(b.y);
}
/// <summary>
/// Compares two vectors and returns true if they are similar.
/// </summary>
public static bool SimilarTo(this in Vector3 a, in Vector3 b)
{
return a.x.SimilarTo(b.x) && a.y.SimilarTo(b.y) && a.z.SimilarTo(b.z);
}
}
}

View File

@@ -0,0 +1,35 @@
using System.Reflection;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("HexagonalLib.Unity")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("HexagonalLib.Unity")]
[assembly: AssemblyCopyright("Copyright © 2020")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("EE497232-AC7A-48D0-9059-53B152C6BF01")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@@ -0,0 +1,27 @@
using UnityEngine;
namespace HexagonalLib.Utility
{
internal static class VectorUtility
{
internal static (float x, float y) AsTuple(this Vector2 vector)
{
return (vector.x, vector.y);
}
internal static (float x, float y) AsTuple(this Vector3 vector)
{
return (vector.x, vector.z);
}
internal static Vector2 AsVector2(this (float x, float y) tuple)
{
return new Vector2(tuple.x, tuple.y);
}
internal static Vector3 AsVector3(this (float x, float y) tuple, float y = 0.0f)
{
return new Vector3(tuple.x, y, tuple.y);
}
}
}

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Unity3D.UnityEngine" version="2018.3.5.1" targetFramework="net472" />
</packages>