Skip to content

Commit

Permalink
chore: singleton BiomeFactory, adding biomes into nodes in map grid
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilz12 committed Nov 26, 2024
1 parent c97c017 commit dae37f1
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 46 deletions.
15 changes: 10 additions & 5 deletions Server/Modules/MapGenerator/Models/Node.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
using Fracture.Server.Modules.Pathfinding.Models;
using System.ComponentModel.DataAnnotations;
using Fracture.Server.Modules.Pathfinding.Models;

namespace Fracture.Server.Modules.MapGenerator.Models;

public class Node : IPathfindingNode
{
[Required]
public int XId { get; set; }

[Required]
public int YId { get; set; }
public Biome? Biome { get; set; }

public string? Color { get; set; }
public float NoiseValue { get; set; }
[Required]
public Biome Biome { get; set; }

[Required]
public float NoiseValue { get; set; }
public int GCost { get; set; }
public int HCost { get; set; }
public int FCost => GCost + HCost;
public bool Walkable { get; set; }
public IPathfindingNode? PreviousNode { get; set; }

public Node(int xId, int yId, Biome? biome)
public Node(int xId, int yId, Biome biome)
{
XId = xId;
YId = yId;
Expand Down
51 changes: 51 additions & 0 deletions Server/Modules/MapGenerator/Services/BiomeFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Drawing;
using Fracture.Server.Modules.MapGenerator.Models;

namespace Fracture.Server.Modules.MapGenerator.Services;

public sealed class BiomeFactory
{
private BiomeFactory() { }

private static List<Biome> biomes = new List<Biome>
{
CreateBiome(BiomeType.DeepOcean, "#21618C", 0.0f, 0.02f),
CreateBiome(BiomeType.ShallowWater, "#2E86C1", 0.02f, 0.2f),
CreateBiome(BiomeType.Beach, "#F9E79F", 0.2f, 0.4f),
CreateBiome(BiomeType.Grassland, "#28B463", 0.4f, 0.55f),
CreateBiome(BiomeType.Forest, "#1D8348", 0.55f, 0.7f),
CreateBiome(BiomeType.Mountains, "#616A6B", 0.7f, 0.85f),
CreateBiome(BiomeType.HighMountains, "#515A5A", 0.85f, 1.0f),
};
private static BiomeFactory _biomeFactory;

Check warning on line 20 in Server/Modules/MapGenerator/Services/BiomeFactory.cs

View workflow job for this annotation

GitHub Actions / Checks

Non-nullable field '_biomeFactory' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

public static BiomeFactory GetBiomeFactory()
{
if (_biomeFactory == null)
{
_biomeFactory = new BiomeFactory();
}
return _biomeFactory;
}

public static List<Biome> GetBiomes()
{
return biomes;
}

private static Biome CreateBiome(
BiomeType type,
string colorHex,
float minHeight,
float maxHeight
)
{
return new Biome
{
BiomeType = type,
Color = ColorTranslator.FromHtml(colorHex),
MinHeight = minHeight,
MaxHeight = maxHeight,
};
}
}
51 changes: 14 additions & 37 deletions Server/Modules/MapGenerator/Services/MapGeneratorService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Numerics;
using System.Drawing;
using System.Numerics;
using Fracture.Server.Modules.MapGenerator.Models;
using Fracture.Server.Modules.NoiseGenerator.Models;
using Fracture.Server.Modules.NoiseGenerator.Services;
Expand Down Expand Up @@ -64,6 +65,7 @@ private MapData GenerateMap(NoiseParameters noiseParameters)
Vector2.Zero
);

var biomes = BiomeFactory.GetBiomes();
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
Expand All @@ -87,45 +89,20 @@ private MapData GenerateMap(NoiseParameters noiseParameters)
);
}

grid[x, y] = new Node(x, y, null);
grid[x, y].NoiseValue = heightMap[x, y];
grid[x, y].Walkable = grid[x, y].NoiseValue > 0.2f && grid[x, y].NoiseValue < 0.7f;
HeightToColor(grid, heightMap, y, x);
var biome = biomes.FirstOrDefault(b =>
heightMap[x, y] >= b.MinHeight && heightMap[x, y] < b.MaxHeight
); //type biome in perlin range

grid[x, y] = new Node(x, y, biome)

Check warning on line 96 in Server/Modules/MapGenerator/Services/MapGeneratorService.cs

View workflow job for this annotation

GitHub Actions / Checks

Possible null reference argument for parameter 'biome' in 'Node.Node(int xId, int yId, Biome biome)'.
{
NoiseValue = heightMap[x, y],
Walkable = !(biome.BiomeType is BiomeType.DeepOcean or BiomeType.ShallowWater),
};

//grid[x, y].Biome.Color = biome != null ? grid[x, y].Biome.Color = biome.Color : biome.Color = Color.White;
}
}

return new MapData(grid);
}

private static void HeightToColor(Node[,] grid, float[,] heightMap, int y, int x)
{
if (heightMap[x, y] < 0.02f)
{
grid[x, y].Color = "#21618C";
}
else if (heightMap[x, y] < 0.2f)
{
grid[x, y].Color = "#2E86C1";
}
else if (heightMap[x, y] < 0.40f)
{
grid[x, y].Color = "#F9E79F";
}
else if (heightMap[x, y] < 0.55f)
{
grid[x, y].Color = "#28B463";
}
else if (heightMap[x, y] < 0.7f)
{
grid[x, y].Color = "#1D8348";
}
else if (heightMap[x, y] < 0.85f)
{
grid[x, y].Color = "#616A6B";
}
else
{
grid[x, y].Color = "#515A5A";
}
}
}
10 changes: 6 additions & 4 deletions Server/Modules/MapGenerator/UI/MapView.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@using Fracture.Server.Modules.MapGenerator.Models
@using System.Drawing
@using Fracture.Server.Modules.MapGenerator.Models
@using Fracture.Server.Modules.MapGenerator.UI.Models
@using Fracture.Server.Modules.Pathfinding.Models

Expand Down Expand Up @@ -56,9 +57,10 @@

private string GetTileColor(Node node)
{
if (MapDisplayData!.ShowColorMap)
{
return node.Color ?? string.Empty;
if (MapDisplayData!.ShowColorMap) {
Color color = node.Biome.Color;
string hex = $"#{color.R:X2}{color.G:X2}{color.B:X2}"; // rgb -> hex
return hex ?? string.Empty;
}

string hexValue = string.Format("{0:x}", (int)(node.NoiseValue * 255));
Expand Down

0 comments on commit dae37f1

Please sign in to comment.