Started implementing mixers
This commit is contained in:
@@ -1,13 +1,43 @@
|
||||
using Fruitomation.Game.Items;
|
||||
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()
|
||||
{
|
||||
@@ -18,11 +48,52 @@ namespace Fruitomation.Game
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
item.transform.position = OutputLocation.position;
|
||||
item.SendToTheGhostRealm();
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user