173 lines
5.0 KiB
C#
173 lines
5.0 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 BasicUpgrade Requirement;
|
|
public ItemType[] Ingredients;
|
|
public ItemType Product;
|
|
|
|
public Recipe(BasicUpgrade requirement, ItemType[] ingredients, ItemType product)
|
|
{
|
|
Requirement = requirement;
|
|
Ingredients = ingredients;
|
|
Product = product;
|
|
}
|
|
}
|
|
|
|
private static Recipe[] Recipes =
|
|
{
|
|
new
|
|
(
|
|
BasicUpgrade.DriedFruitSelection,
|
|
new[]
|
|
{
|
|
ItemType.DriedAppleSlices,
|
|
ItemType.Raisins,
|
|
ItemType.DriedBananaSlices
|
|
},
|
|
ItemType.DriedFruitSelection
|
|
),
|
|
|
|
new
|
|
(
|
|
BasicUpgrade.AppleMangoJuice,
|
|
new[]
|
|
{
|
|
ItemType.AppleJuice,
|
|
ItemType.MangoJuice
|
|
},
|
|
ItemType.AppleAndMangoJuice
|
|
),
|
|
|
|
new
|
|
(
|
|
BasicUpgrade.SpicedBananaIceCream,
|
|
new[]
|
|
{
|
|
ItemType.BananaIceCream,
|
|
ItemType.DurainPowder
|
|
},
|
|
ItemType.SpicedBananaIceCream
|
|
),
|
|
|
|
new
|
|
(
|
|
BasicUpgrade.ExoticFruitSelection,
|
|
new[]
|
|
{
|
|
ItemType.SlicedKiwi,
|
|
ItemType.MangoSlices,
|
|
ItemType.DurianSlices,
|
|
ItemType.BuddhasHandSlices
|
|
},
|
|
ItemType.ExoticFruitSelection
|
|
),
|
|
|
|
new
|
|
(
|
|
BasicUpgrade.SpicedPitayaIceCream,
|
|
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)
|
|
{
|
|
if (UpgradeManager.Is(recipe.Requirement))
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|