Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
955c9a6
feat: add economy runtime folder
pes7 Jul 27, 2026
3cf7db3
feat: add economy runtime assembly
pes7 Jul 27, 2026
420a530
chore: add economy assembly meta
pes7 Jul 27, 2026
736ff1b
feat: add resource node definition
pes7 Jul 27, 2026
7fc2f26
chore: add resource definition meta
pes7 Jul 27, 2026
b9ca289
feat: add production recipe definition
pes7 Jul 27, 2026
9006e80
chore: add production recipe meta
pes7 Jul 27, 2026
6a9544a
feat: add harvest production and worker job states
pes7 Jul 27, 2026
55495d2
chore: add economy states meta
pes7 Jul 27, 2026
40a1161
feat: add persistent resource node
pes7 Jul 27, 2026
5070ad4
chore: add resource node meta
pes7 Jul 27, 2026
a2a1b6e
feat: add persistent production facility
pes7 Jul 27, 2026
0724b9f
fix: use inventory atomic consume API
pes7 Jul 27, 2026
7f219d5
chore: add production facility meta
pes7 Jul 27, 2026
0dbc69b
feat: add worker job agent
pes7 Jul 27, 2026
7102940
chore: add worker job agent meta
pes7 Jul 27, 2026
f9adf6c
fix: block production when output storage is full
pes7 Jul 27, 2026
c3df86e
chore: reference economy assembly from editor validation
pes7 Jul 27, 2026
a93a28e
feat: add economy content validation
pes7 Jul 27, 2026
cb82f14
chore: add economy validator meta
pes7 Jul 27, 2026
ddfc103
chore: reference economy assembly from editmode tests
pes7 Jul 27, 2026
070bd73
test: add economy state coverage
pes7 Jul 27, 2026
6052c94
chore: add economy tests meta
pes7 Jul 27, 2026
c5fe1a3
docs: add resource jobs and production guide
pes7 Jul 27, 2026
408a9ff
fix: use inventory entry fields
pes7 Jul 27, 2026
0e1cd6f
fix: use inventory entry fields in worker delivery
pes7 Jul 27, 2026
6bf5321
chore: add economy documentation meta
pes7 Jul 27, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions Assets/Script/ProductionFacility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using ZoneUA.Economy;
using ZoneUA.Inventory;
using ZoneUA.Persistence;

[RequireComponent(typeof(PersistentIdentity))]
public sealed class ProductionFacility : MonoBehaviour, IPersistentSaveParticipant
{
[Serializable]
private sealed class Payload
{
public List<ProductionQueueEntry> entries = new List<ProductionQueueEntry>();
}

[SerializeField] private InventoryComponent inputInventory;
[SerializeField] private InventoryComponent outputInventory;
[SerializeField] private List<ProductionRecipe> allowedRecipes = new List<ProductionRecipe>();
[SerializeField] private bool processAutomatically = true;

private readonly ProductionQueueState queue = new ProductionQueueState();

public string ParticipantKey => "production-facility";
public int ParticipantVersion => 1;
public IReadOnlyList<ProductionQueueEntry> Queue => queue.entries;

private void Awake()
{
inputInventory ??= GetComponent<InventoryComponent>();
outputInventory ??= inputInventory;
}

private void Update()
{
if (processAutomatically) Tick(Time.deltaTime);
}

public bool Enqueue(ProductionRecipe recipe, int cycles = 1)
{
if (recipe == null || cycles <= 0 || !allowedRecipes.Contains(recipe) || inputInventory == null) return false;
List<InventoryEntry> totalInputs = recipe.BuildInputs()
.Select(entry => new InventoryEntry(entry.itemId, entry.amount * cycles))
.ToList();
if (!inputInventory.TryConsume(totalInputs)) return false;
queue.Enqueue(recipe.RecipeId, cycles);
return true;
}

public void Tick(float deltaTime)
{
if (queue.entries.Count == 0) return;
ProductionRecipe recipe = FindRecipe(queue.entries[0].recipeId);
if (recipe == null)
{
queue.entries.RemoveAt(0);
return;
}

InventoryComponent destination = outputInventory != null ? outputInventory : inputInventory;
if (destination == null) return;
List<InventoryEntry> outputs = recipe.BuildOutputs();
int outputCount = outputs.Sum(entry => entry.amount);
if (destination.Capacity > 0 && destination.TotalItemCount + outputCount > destination.Capacity) return;

if (!queue.TryAdvance(deltaTime, recipe.DurationSeconds, out _)) return;
foreach (InventoryEntry output in outputs) destination.Add(output.itemId, output.amount);
}

public string CaptureState()
{
queue.Normalize();
return JsonUtility.ToJson(new Payload { entries = queue.entries });
}

public void RestoreState(string payload, int version)
{
Payload restored = JsonUtility.FromJson<Payload>(payload ?? string.Empty);
queue.entries = restored?.entries ?? new List<ProductionQueueEntry>();
queue.Normalize();
}

private ProductionRecipe FindRecipe(string recipeId) => allowedRecipes.FirstOrDefault(recipe =>
recipe != null && string.Equals(recipe.RecipeId, recipeId, StringComparison.Ordinal));
}
11 changes: 11 additions & 0 deletions Assets/Script/ProductionFacility.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

98 changes: 98 additions & 0 deletions Assets/Script/ResourceNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using System;
using UnityEngine;
using ZoneUA.Economy;
using ZoneUA.Inventory;
using ZoneUA.Persistence;

[RequireComponent(typeof(PersistentIdentity))]
public sealed class ResourceNode : MonoBehaviour, IPersistentSaveParticipant
{
[Serializable]
private sealed class Payload
{
public string resourceId = string.Empty;
public int remainingUnits;
public float accumulatedWork;
public float respawnRemaining;
}

[SerializeField] private ResourceNodeDefinition definition;
[SerializeField] private GameObject availableRoot;
[SerializeField] private GameObject depletedRoot;

private HarvestState state;

public string ParticipantKey => "resource-node";
public int ParticipantVersion => 1;
public ResourceNodeDefinition Definition => definition;
public int RemainingUnits => state?.remainingUnits ?? 0;
public bool IsDepleted => state == null || state.IsDepleted;

private void Awake()
{
EnsureState();
RefreshPresentation();
}

private void Update()
{
if (state == null || !state.IsDepleted || definition == null || definition.RespawnSeconds <= 0f) return;
state.respawnRemaining = Mathf.Max(0f, state.respawnRemaining - Time.deltaTime);
if (state.respawnRemaining <= 0f)
{
state.Restore(definition.TotalUnits, 0f, 0f);
RefreshPresentation();
}
}

public int ApplyWork(float work, InventoryComponent destination)
{
EnsureState();
if (definition == null || destination == null || definition.YieldedItem == null) return 0;

int harvested = state.ApplyWork(work, definition.WorkPerHarvest, definition.UnitsPerHarvest);
if (harvested <= 0) return 0;

if (!destination.Add(definition.YieldedItem.ItemId, harvested))
{
state.remainingUnits += harvested;
return 0;
}

if (state.IsDepleted && definition.RespawnSeconds > 0f) state.respawnRemaining = definition.RespawnSeconds;
RefreshPresentation();
return harvested;
}

public string CaptureState()
{
EnsureState();
return JsonUtility.ToJson(new Payload
{
resourceId = definition != null ? definition.ResourceId : string.Empty,
remainingUnits = state.remainingUnits,
accumulatedWork = state.accumulatedWork,
respawnRemaining = state.respawnRemaining
});
}

public void RestoreState(string payload, int version)
{
EnsureState();
Payload restored = JsonUtility.FromJson<Payload>(payload ?? string.Empty);
if (restored == null) return;
state.Restore(restored.remainingUnits, restored.accumulatedWork, restored.respawnRemaining);
RefreshPresentation();
}

private void EnsureState()
{
if (state == null) state = new HarvestState(definition != null ? definition.TotalUnits : 0);
}

private void RefreshPresentation()
{
if (availableRoot != null) availableRoot.SetActive(!IsDepleted);
if (depletedRoot != null) depletedRoot.SetActive(IsDepleted);
}
}
11 changes: 11 additions & 0 deletions Assets/Script/ResourceNode.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 92 additions & 0 deletions Assets/Script/WorkerJobAgent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System;
using UnityEngine;
using ZoneUA.Economy;
using ZoneUA.Persistence;

[RequireComponent(typeof(PersistentIdentity))]
public sealed class WorkerJobAgent : MonoBehaviour, IPersistentSaveParticipant
{
[Serializable]
private sealed class Payload
{
public WorkerJobKind kind;
public string targetObjectId = string.Empty;
}

[SerializeField] private InventoryComponent carryInventory;
[SerializeField, Min(0.01f)] private float workPerSecond = 1f;
[SerializeField, Min(0.01f)] private float interactionRange = 1f;

private readonly WorkerJobState state = new WorkerJobState();
private ResourceNode resourceTarget;
private InventoryComponent deliveryTarget;

public string ParticipantKey => "worker-job";
public int ParticipantVersion => 1;
public WorkerJobKind CurrentJob => state.kind;

private void Awake() => carryInventory ??= GetComponent<InventoryComponent>();

private void Update()
{
if (state.kind != WorkerJobKind.Harvest || resourceTarget == null || carryInventory == null) return;
if (Vector2.Distance(transform.position, resourceTarget.transform.position) > interactionRange) return;

resourceTarget.ApplyWork(workPerSecond * Time.deltaTime, carryInventory);
if (resourceTarget.IsDepleted || carryInventory.Capacity > 0 && carryInventory.TotalItemCount >= carryInventory.Capacity)
{
if (deliveryTarget != null) DeliverAll(deliveryTarget);
ClearJob();
}
}

public void AssignHarvest(ResourceNode node, InventoryComponent deposit = null)
{
resourceTarget = node;
deliveryTarget = deposit;
PersistentIdentity identity = node != null ? node.GetComponent<PersistentIdentity>() : null;
state.Assign(WorkerJobKind.Harvest, identity != null ? identity.ObjectId : string.Empty);
}

public void ClearJob()
{
resourceTarget = null;
deliveryTarget = null;
state.Clear();
}

public int DeliverAll(InventoryComponent destination)
{
if (carryInventory == null || destination == null) return 0;
int delivered = 0;
var snapshot = new System.Collections.Generic.List<ZoneUA.Inventory.InventoryEntry>(carryInventory.Entries);
foreach (ZoneUA.Inventory.InventoryEntry entry in snapshot)
{
if (!destination.Add(entry.itemId, entry.amount)) continue;
carryInventory.Remove(entry.itemId, entry.amount);
delivered += entry.amount;
}
return delivered;
}

public string CaptureState() => JsonUtility.ToJson(new Payload
{
kind = state.kind,
targetObjectId = state.targetObjectId
});

public void RestoreState(string payload, int version)
{
Payload restored = JsonUtility.FromJson<Payload>(payload ?? string.Empty);
if (restored == null) return;
state.Assign(restored.kind, restored.targetObjectId);
resourceTarget = null;
deliveryTarget = null;
}

private void OnValidate()
{
workPerSecond = Mathf.Max(0.01f, workPerSecond);
interactionRange = Mathf.Max(0.01f, interactionRange);
}
}
11 changes: 11 additions & 0 deletions Assets/Script/WorkerJobAgent.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Assets/_ZoneUA/Editor/ZoneUA.EditorValidation.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"references": [
"ZoneUA.SceneManagement",
"ZoneUA.Persistence",
"ZoneUA.Inventory"
"ZoneUA.Inventory",
"ZoneUA.Economy"
],
"includePlatforms": [
"Editor"
Expand Down
Loading
Loading