Started implementing mixers

This commit is contained in:
Pasha Bibko
2026-04-28 10:08:27 +01:00
parent ba9b895f03
commit d53b760546
4 changed files with 125 additions and 23 deletions

View File

@@ -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);
}
}
}
}
}

View File

@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using Fruitomation.Game.Items;
using Fruitomation.Global;
using UnityEngine;
using Random = UnityEngine.Random;
namespace Fruitomation.Game
{
@@ -27,9 +29,14 @@ namespace Fruitomation.Game
[SerializeField] private GameObject KiwiPrefab;
[SerializeField] private GameObject BuddhasHandPrefab;
private static FruitSpawner Instance;
private float TimeUntilNextSpawn;
private int CurrentItemCount => FruitSpawnParent.childCount;
private void Awake()
=> Instance = this;
private void Update()
{
@@ -70,13 +77,25 @@ namespace Fruitomation.Game
if (UpgradeManager.Is(BasicUpgrade.Pitayas))
unlocked.Add(ItemType.Pitaya);
SpawnItem(unlocked[Random.Range(0, unlocked.Count)]);
}
public static ItemBehaviour SpawnItem(ItemType item, Vector3? pos = null)
=> Instance.SpawnItemInternal(item, pos);
private ItemBehaviour SpawnItemInternal(ItemType item, Vector3? pos = null)
{
GameObject parent = Instantiate(BaseItemPrefab, FruitSpawnParent);
ItemType type = unlocked[Random.Range(0, unlocked.Count)];
ItemBehaviour behaviour = parent.GetComponent<ItemBehaviour>();
Debug.Assert(behaviour is not null, "Could not find ItemBehaviour");
if (pos != null)
{
parent.transform.position = (Vector3)pos;
}
behaviour.InitBehaviour(GameCanvas, type);
behaviour.InitBehaviour(GameCanvas, item);
return behaviour;
}
}
}

View File

@@ -36,17 +36,25 @@ namespace Fruitomation.Game.Items
private IEnumerator SendToGhostRealmInternal()
{
CurrentChild.SetActive(false);
Body2D.Sleep();
Sleep();
yield return new WaitForSeconds(0.5f);
CurrentChild.SetActive(true);
Body2D.WakeUp();
WakeUp();
}
public void SendToTheGhostRealm() => StartCoroutine(SendToGhostRealmInternal());
public void Sleep()
{
CurrentChild.SetActive(false);
Body2D.Sleep();
}
public void WakeUp()
{
CurrentChild.SetActive(true);
Body2D.WakeUp();
}
public void InitBehaviour(Canvas canvas, ItemType startType)
{
RectTransform = transform.GetComponent<RectTransform>();