Files
Fruitomation/Assets/Scripts/Game/Buildings/Automation/MixerBuilding.cs
2026-04-28 11:05:02 +01:00

163 lines
4.5 KiB
C#

using PashaBibko.Pacore.Attributes;
using System.Collections.Generic;
using System.Linq;
using Fruitomation.Game.Items;
using Fruitomation.Global;
using UnityEngine;
namespace Fruitomation.Game
{
public class MixerBuilding : Building
{
private struct Recipe
{
public ItemType[] Ingredients;
public ItemType Product;
public Recipe(ItemType[] ingredients, ItemType product)
{
Ingredients = ingredients;
Product = product;
}
}
private static Recipe[] Recipes =
{
new
(
new[]
{
ItemType.DriedAppleSlices,
ItemType.Raisins,
ItemType.DriedBananaSlices
},
ItemType.DriedFruitSelection
),
new
(
new[]
{
ItemType.AppleJuice,
ItemType.MangoJuice
},
ItemType.AppleAndMangoJuice
),
new
(
new[]
{
ItemType.BananaIceCream,
ItemType.DurainPowder
},
ItemType.SpicedBananaIceCream
),
new
(
new[]
{
ItemType.SlicedKiwi,
ItemType.MangoSlices,
ItemType.DurianSlices,
ItemType.BuddhasHandSlices
},
ItemType.ExoticFruitSelection
),
new
(
new[]
{
ItemType.PitayaIceCream,
ItemType.DurainPowder
},
ItemType.SpicedPitayaIceCream
)
};
[Header("Mixer Specific")]
[SerializeField] private RectTransform OutputLocation;
[SerializeField] private TriggerDetector Trigger;
[Header("Read Only")]
[SerializeField, InspectorReadOnly] private int StoredItemCount;
private readonly Dictionary<ItemType, int> StoredItems = new();
private void Start()
{
Trigger.SetAction(other =>
{
bool isItem = other.transform.parent.TryGetComponent(out ItemBehaviour item);
if (!isItem)
{
return;
}
if (StoredItemCount < 50)
{
ItemType type = item.CurrentType;
int typeCount = StoredItems.GetValueOrDefault(type);
if (typeCount < 5)
{
item.TriggerDestruction(false);
StoredItems[type] = typeCount + 1;
StoredItemCount++;
}
else
{
item.transform.position = OutputLocation.position;
item.SendToTheGhostRealm();
}
}
else
{
item.transform.position = OutputLocation.position;
item.SendToTheGhostRealm();
}
}, TriggerType.Enter);
}
private void Update()
{
if (!GameStateController.Is(GameState.Simulation))
{
StoredItems.Clear();
return;
}
foreach (Recipe recipe in Recipes)
{
bool hasAllIngredients = recipe.Ingredients.Aggregate(true,
(current, ingredient)
=> current && StoredItems.ContainsKey(ingredient)
);
if (hasAllIngredients)
{
foreach (ItemType ingredient in recipe.Ingredients)
{
int count = StoredItems[ingredient] - 1;
if (count <= 0)
{
StoredItems.Remove(ingredient);
}
else
{
StoredItems[ingredient] = count;
}
}
FruitSpawner.SpawnItem(recipe.Product, OutputLocation.position);
}
}
}
}
}