111 lines
2.5 KiB
C#
111 lines
2.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using UFS2.ScriptableObjects;
|
|
using UnityEngine;
|
|
|
|
[CreateAssetMenu(menuName = "Data/LevelFishData", fileName = "LevelFishData")]
|
|
public class LevelFishData : ScriptableObject
|
|
{
|
|
[Serializable]
|
|
public class FishDataHelper
|
|
{
|
|
public FishData FishData;
|
|
|
|
public Vector2 Weight;
|
|
|
|
public float spawnChance = 1f;
|
|
}
|
|
|
|
[Tooltip("Name of scene to which they refer")]
|
|
public string SceneName;
|
|
|
|
public List<FishDataHelper> FishesSetting;
|
|
|
|
public int StartCount;
|
|
|
|
public float CullDistance;
|
|
|
|
public float DrawDistance;
|
|
|
|
[Header("Only for DEBUG")]
|
|
[Tooltip("Put here all FishData there and click button Fish Settings")]
|
|
public List<FishData> AllFishes;
|
|
|
|
[Tooltip("Put CSV file to parse data, look in to code how it's parsed")]
|
|
public TextAsset FishWeightCSV;
|
|
|
|
private void FishesSettingSave()
|
|
{
|
|
string[] array = FishWeightCSV.text.Split('\n');
|
|
FishesSetting = new List<FishDataHelper>();
|
|
string[] array2 = array;
|
|
for (int i = 0; i < array2.Length; i++)
|
|
{
|
|
string[] array3 = array2[i].Split(',');
|
|
Debug.Log(array3[0] + ", " + array3[1] + ", " + array3[2]);
|
|
int index = int.Parse(array3[0], NumberStyles.Number);
|
|
float x = float.Parse(array3[1]);
|
|
float y = float.Parse(array3[2]);
|
|
FishesSetting.Add(new FishDataHelper
|
|
{
|
|
FishData = AllFishes[index],
|
|
Weight = new Vector2(x, y)
|
|
});
|
|
}
|
|
}
|
|
|
|
public FishDataHelper GetRandomFishDataToSpawn()
|
|
{
|
|
FishDataHelper result = null;
|
|
if (FishesSetting != null && FishesSetting.Count > 0)
|
|
{
|
|
float[] array = new float[FishesSetting.Count];
|
|
for (int i = 0; i < FishesSetting.Count; i++)
|
|
{
|
|
array[i] = FishesSetting[i].spawnChance;
|
|
}
|
|
int randomWeightedIndex = GetRandomWeightedIndex(array);
|
|
if (randomWeightedIndex >= 0)
|
|
{
|
|
result = FishesSetting[randomWeightedIndex];
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private int GetRandomWeightedIndex(float[] weights, System.Random rand = null, int weightsLength = -1)
|
|
{
|
|
weightsLength = ((weightsLength == -1) ? weights.Length : weightsLength);
|
|
float num = 0f;
|
|
for (int i = 0; i < weightsLength; i++)
|
|
{
|
|
num += weights[i];
|
|
}
|
|
if (num > 0f)
|
|
{
|
|
float num2;
|
|
if (rand == null)
|
|
{
|
|
num2 = UnityEngine.Random.value;
|
|
}
|
|
else
|
|
{
|
|
int num3 = 1000000;
|
|
num2 = (float)rand.Next(num3) / (float)(num3 - 1);
|
|
}
|
|
float num4 = 0f;
|
|
for (int j = 0; j < weightsLength; j++)
|
|
{
|
|
num4 += weights[j] / num;
|
|
if (num4 >= num2)
|
|
{
|
|
return j;
|
|
}
|
|
}
|
|
return weightsLength - 1;
|
|
}
|
|
return -1;
|
|
}
|
|
}
|