Big project cleanup with overhaul of file responsibilities (KISS) and code (DRY, YAGNI)
This commit is contained in:
@@ -1,40 +1,40 @@
|
||||
using System.Collections.Generic;
|
||||
using Godot;
|
||||
|
||||
public class ResourceDistributor
|
||||
public static class ResourceDistributor
|
||||
{
|
||||
public static Dictionary<string, Texture2D> resources = ResourceLoader.LoadResourceSymbols();
|
||||
public static Dictionary<string, float[]> weights = ResourceLoader.LoadResourceWeights();
|
||||
public static Dictionary<string, Texture2D> resources = ResourceLoader.LoadResourceSymbols();
|
||||
public static Dictionary<string, float[]> weights = ResourceLoader.LoadResourceWeights();
|
||||
|
||||
public static string GetResource(int layer)
|
||||
{
|
||||
return ChooseWeighted(layer);
|
||||
}
|
||||
public static string GetResource(int layer)
|
||||
{
|
||||
return ChooseWeighted(layer);
|
||||
}
|
||||
|
||||
private static string ChooseWeighted(int layer)
|
||||
private static string ChooseWeighted(int layer)
|
||||
{
|
||||
float totalWeight = 0f;
|
||||
float weightLerp;
|
||||
|
||||
foreach (string resource in resources.Keys)
|
||||
{
|
||||
weightLerp = Mathf.Lerp(weights[resource][0], weights[resource][1], layer);
|
||||
totalWeight += weightLerp;
|
||||
totalWeight += GetWeight(resource, layer);
|
||||
}
|
||||
|
||||
float r = (float)(GameData.rand.NextDouble() * totalWeight);
|
||||
|
||||
float randomWeight = (float)(GameData.rand.NextDouble() * totalWeight);
|
||||
float cumulative = 0f;
|
||||
|
||||
foreach (string resource in resources.Keys)
|
||||
{
|
||||
weightLerp = Mathf.Lerp(weights[resource][0], weights[resource][1], layer);
|
||||
cumulative += weightLerp;
|
||||
cumulative += GetWeight(resource, layer);
|
||||
|
||||
if (r <= cumulative)
|
||||
return resource;
|
||||
if (randomWeight <= cumulative) return resource;
|
||||
}
|
||||
|
||||
return "stone";
|
||||
}
|
||||
|
||||
private static float GetWeight(string resource, int layer)
|
||||
{
|
||||
return Mathf.Lerp(weights[resource][0], weights[resource][1], layer);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user