145 lines
4.4 KiB
C#
145 lines
4.4 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Sirenix.OdinInspector;
|
|
using FullSerializer;
|
|
using UnityEngine;
|
|
using Random = System.Random;
|
|
|
|
namespace Beyond
|
|
{
|
|
public class ResourceGrid : ObjectGrid<SerializedStreamedItem, ResourceGrid>
|
|
{
|
|
//public ItemDefinitionsDB ItemDefDB => m_db;
|
|
//used to map item definition type to spawn slots
|
|
//used to find Resource DB by type
|
|
|
|
[Serializable]
|
|
public class CellStats
|
|
{
|
|
public int numObjects = 0;
|
|
}
|
|
|
|
private CellStats[,] m_cellStatsArray;
|
|
//private Dictionary<Cell, CellStats> m_cellStats = new Dictionary<Cell, CellStats>();
|
|
|
|
public ResourceSpawnerDB[] m_resourceSpawnerDbs;
|
|
//public CustomItemPickup.SerializedItem testItem;
|
|
|
|
float CalculateChance(ResourceSpawnerDB db)
|
|
{
|
|
float chance = db.density;
|
|
float brightness = (float)Player.Instance.GetAttribute("BrightnessPoints").AttributeCurrentValue;
|
|
float maxBrightness = (float)Player.Instance.GetAttribute("BrightnessPoints").AttributeMaxValue;
|
|
float halfB = maxBrightness / 2f;
|
|
|
|
if (brightness > halfB)
|
|
{
|
|
brightness -= halfB;
|
|
chance *= (1f + (brightness / halfB) * db.positiveBrightnessFactor);
|
|
}
|
|
else
|
|
{
|
|
chance *= (1f - ((halfB -brightness) / halfB) * db.negativeBrightnessFactor);
|
|
}
|
|
|
|
float maturity = Player.Instance.GetAttribute("Maturity").AttributeCurrentValue;
|
|
chance *= (1f + db.maturityFactor*maturity);
|
|
chance += db.offset;
|
|
chance = Mathf.Clamp01(chance);
|
|
//db.negativeBrightnessFactor
|
|
return chance;
|
|
}
|
|
|
|
protected override void PrepareGrid()
|
|
{
|
|
base.PrepareGrid();
|
|
|
|
m_cellStatsArray = new CellStats[this.m_xCount, this.m_zCount];
|
|
for (int i = 0; i < m_xCount; i++)
|
|
{
|
|
for (int j = 0; j < m_xCount; j++)
|
|
{
|
|
m_cellStatsArray[i, j] = new CellStats();
|
|
}
|
|
}
|
|
foreach (var db in m_resourceSpawnerDbs)
|
|
{
|
|
if (db.Items.Count > 0)
|
|
{
|
|
//m_itemDef2DB[db.Items[0].itemDefAmount.ItemDefinition] = db;
|
|
}
|
|
|
|
float chance = CalculateChance(db);
|
|
foreach (var p in db.Items)
|
|
{
|
|
p.isAvailable = UnityEngine.Random.Range(0f, 1f) < chance;
|
|
AddItem(p);
|
|
}
|
|
}
|
|
}
|
|
|
|
//checks if enough of time has passes and new object should be spawned
|
|
protected void CheckIfShouldSpawnNewObjects()
|
|
{
|
|
|
|
}
|
|
|
|
protected override void CheckObjectsInCell(int x, int z, bool activate)
|
|
{
|
|
if (activate)
|
|
{
|
|
CheckIfShouldSpawnNewObjects();
|
|
foreach (var ob in m_ItemGrid[x,z].guidToItem)
|
|
{
|
|
if (!ob.Value.IsSpawned() && ob.Value.isAvailable)
|
|
{
|
|
ob.Value.Spawn();
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
foreach (var ob in m_ItemGrid[x,z].guidToItem)
|
|
{
|
|
if (ob.Value.IsSpawned())
|
|
{
|
|
ob.Value.Despawn();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void OnCollected(SerializedStreamedItem item)
|
|
{
|
|
item.isAvailable = false;
|
|
}
|
|
|
|
public void InitializeSerializer()
|
|
{
|
|
JSONSerializer.Instance.Initialize();
|
|
|
|
}
|
|
[Button]
|
|
public override string SerializeToJSON()
|
|
{
|
|
InitializeSerializer();
|
|
|
|
return JSONSerializer.Instance.Serialize(typeof(Cell[,]), m_ItemGrid);
|
|
}
|
|
[Button]
|
|
|
|
public override void DeserializeFromJSON(string js)
|
|
{
|
|
InitializeSerializer();
|
|
fsData data = fsJsonParser.Parse(js);
|
|
if (data == null)
|
|
return;
|
|
|
|
m_ItemGrid = (Cell[,])JSONSerializer.Instance.Deserialize(typeof(Cell[,]), js);
|
|
|
|
}
|
|
}
|
|
}
|
|
|