using System.Collections.Generic;
using ProjectM.Simulation;
using Unity.Collections;
using Unity.Entities;
using UnityEngine;
namespace ProjectM.Authoring
{
///
/// Bakes the designer-authored item definitions into a single ItemDatabase blob singleton (immutable,
/// shared, Burst-fast), mirroring AbilityDatabaseAuthoring. Place ONE in the gameplay subscene; it streams
/// identically into the client and server worlds (config, not replicated). DependsOn each definition so a
/// value change re-bakes the blob. Runtime lookup is ID-keyed (ItemDatabaseBlob.TryGetItem), so the list
/// order here does not matter and inserting an item never renumbers existing ids.
///
public class ItemDatabaseAuthoring : MonoBehaviour
{
[Tooltip("All item definitions in the game (resources + tools/gear). Looked up at runtime by ItemId.")]
public List Items = new List();
private class DatabaseBaker : Baker
{
public override void Bake(ItemDatabaseAuthoring authoring)
{
var entity = GetEntity(TransformUsageFlags.None);
int count = authoring.Items != null ? authoring.Items.Count : 0;
var builder = new BlobBuilder(Allocator.Temp);
ref var root = ref builder.ConstructRoot();
var arr = builder.Allocate(ref root.Items, count);
for (int i = 0; i < count; i++)
{
var def = authoring.Items[i];
if (def == null) { arr[i] = default; continue; }
DependsOn(def);
arr[i] = new ItemDefBlob
{
ItemId = (ushort)def.ItemId,
Category = def.Category,
Tier = def.Tier,
StackMax = def.StackMax,
Name = def.DisplayName,
};
}
var blob = builder.CreateBlobAssetReference(Allocator.Persistent);
builder.Dispose();
AddBlobAsset(ref blob, out _);
AddComponent(entity, new ItemDatabase { Value = blob });
}
}
}
}