100 lines
2.9 KiB
C#
100 lines
2.9 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 Recipe[1]
|
|
{
|
|
new
|
|
(
|
|
new[] { ItemType.Apple, ItemType.Grape },
|
|
ItemType.Banana
|
|
),
|
|
};
|
|
|
|
[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);
|
|
|
|
item.TriggerDestruction(false);
|
|
StoredItems[type] = typeCount + 1;
|
|
StoredItemCount++;
|
|
}
|
|
}, 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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|