41 lines
956 B
C#
41 lines
956 B
C#
using System.Collections.Generic;
|
|
using Godot;
|
|
|
|
public static class ResourceDistributor
|
|
{
|
|
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);
|
|
}
|
|
|
|
private static string ChooseWeighted(int layer)
|
|
{
|
|
float totalWeight = 0f;
|
|
|
|
foreach (string resource in resources.Keys)
|
|
{
|
|
totalWeight += GetWeight(resource, layer);
|
|
}
|
|
|
|
float randomWeight = (float)(GameData.rand.NextDouble() * totalWeight);
|
|
float cumulative = 0f;
|
|
|
|
foreach (string resource in resources.Keys)
|
|
{
|
|
cumulative += GetWeight(resource, layer);
|
|
|
|
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);
|
|
}
|
|
}
|