First MVP of sorting minigame (#60)

Co-authored-by: Michal Pikulski <michal.a.pikulski@gmail.com>
Co-authored-by: Michal Pikulski <michal@foolhardyhorizons.com>
Reviewed-on: #60
This commit is contained in:
2025-11-19 13:56:10 +00:00
parent f878521dab
commit fee5515bbd
91 changed files with 9205 additions and 178 deletions

View File

@@ -631,6 +631,25 @@ namespace Data.CardSystem
return _pendingRevealCards.Count;
}
/// <summary>
/// Gets a random card definition of the specified rarity.
/// Used by minigames to spawn cards without affecting player's collection.
/// </summary>
public CardDefinition GetRandomCardDefinitionByRarity(CardRarity targetRarity)
{
// Filter available cards by rarity
var matchingCards = availableCards.Where(c => c.Rarity == targetRarity).ToList();
if (matchingCards.Count == 0)
{
Debug.LogWarning($"[CardSystemManager] No card definitions found for rarity {targetRarity}");
return null;
}
// Return random card from matching rarity
return matchingCards[UnityEngine.Random.Range(0, matchingCards.Count)];
}
#endregion
/// <summary>

View File

@@ -401,6 +401,56 @@ namespace UI.CardSystem.StateMachine
}
#endregion
#region Color/Tint Animations
private TweenBase _activeBlinkTween;
private Color _originalColor;
/// <summary>
/// Blink an image red repeatedly (for fell-off-conveyor state)
/// </summary>
public void BlinkRed(UnityEngine.UI.Image image, float blinkSpeed = 0.25f)
{
if (image == null) return;
// Stop any existing blink
StopBlinking();
// Store original color
_originalColor = image.color;
// Start blinking red loop
BlinkLoop(image, blinkSpeed);
}
private void BlinkLoop(UnityEngine.UI.Image image, float blinkSpeed)
{
if (image == null) return;
// Tween to red
_activeBlinkTween = Tween.Color(image, Color.red, blinkSpeed, 0f, Tween.EaseInOut,
completeCallback: () =>
{
// Tween back to original
_activeBlinkTween = Tween.Color(image, _originalColor, blinkSpeed, 0f, Tween.EaseInOut,
completeCallback: () => BlinkLoop(image, blinkSpeed)); // Loop
});
}
/// <summary>
/// Stop blinking animation and restore original color
/// </summary>
public void StopBlinking()
{
if (_activeBlinkTween != null)
{
_activeBlinkTween.Stop();
_activeBlinkTween = null;
}
}
#endregion
}
}

View File

@@ -170,6 +170,7 @@ namespace Core
var interactionSettings = SettingsProvider.Instance.LoadSettingsSynchronous<InteractionSettings>();
var minigameSettings = SettingsProvider.Instance.LoadSettingsSynchronous<DivingMinigameSettings>();
var cardSystemSettings = SettingsProvider.Instance.LoadSettingsSynchronous<CardSystemSettings>();
var sortingGameSettings = SettingsProvider.Instance.LoadSettingsSynchronous<CardSortingSettings>();
// Register settings with service locator
if (playerSettings != null)
@@ -211,6 +212,16 @@ namespace Core
{
Debug.LogError("Failed to load CardSystemSettings");
}
if (sortingGameSettings != null)
{
ServiceLocator.Register<ICardSortingSettings>(sortingGameSettings);
Logging.Debug("CardSortingSettings registered successfully");
}
else
{
Debug.LogError("Failed to load CardSystemSettings");
}
// Log success
_settingsLoaded = playerSettings != null && interactionSettings != null && minigameSettings != null && cardSystemSettings != null;

View File

@@ -0,0 +1,92 @@
using AppleHills.Core.Settings;
using Minigames.CardSorting.Data;
using UnityEngine;
namespace Core.Settings
{
/// <summary>
/// Settings for Card Sorting minigame.
/// Follows DivingMinigameSettings pattern.
/// </summary>
[CreateAssetMenu(fileName = "CardSortingSettings", menuName = "AppleHills/Settings/CardSorting", order = 4)]
public class CardSortingSettings : BaseSettings, ICardSortingSettings
{
[Header("Timing")]
[Tooltip("Total game duration in seconds")]
[SerializeField] private float gameDuration = 120f;
[Tooltip("Distance between item spawns (units)")]
[SerializeField] private float spawnDistance = 50f;
[Header("Conveyor Speed")]
[Tooltip("Initial belt movement speed")]
[SerializeField] private float initialBeltSpeed = 1f;
[Tooltip("Maximum belt movement speed")]
[SerializeField] private float maxBeltSpeed = 3f;
[Tooltip("Curve for difficulty progression (X=time%, Y=speed multiplier)")]
[SerializeField] private AnimationCurve speedCurve = AnimationCurve.EaseInOut(0f, 0f, 1f, 1f);
[Header("Item Pools")]
[Tooltip("Garbage items that can spawn (banana peels, cans, receipts, etc.)")]
[SerializeField] private GarbageItemDefinition[] garbageItems = new GarbageItemDefinition[0];
[Header("Spawn Weights")]
[Tooltip("Weight for spawning normal rarity cards")]
[Range(0, 100)] [SerializeField] private float normalCardWeight = 40f;
[Tooltip("Weight for spawning rare rarity cards")]
[Range(0, 100)] [SerializeField] private float rareCardWeight = 30f;
[Tooltip("Weight for spawning legendary rarity cards")]
[Range(0, 100)] [SerializeField] private float legendCardWeight = 20f;
[Tooltip("Weight for spawning garbage items")]
[Range(0, 100)] [SerializeField] private float garbageWeight = 10f;
[Header("Scoring")]
[Tooltip("Points awarded for correct sort")]
[SerializeField] private int correctSortPoints = 10;
[Tooltip("Points deducted for incorrect sort")]
[SerializeField] private int incorrectSortPenalty = -5;
[Tooltip("Points deducted when item falls off belt")]
[SerializeField] private int missedItemPenalty = -3;
[Header("Rewards")]
[Tooltip("Booster packs awarded per correct sort")]
[SerializeField] private int boosterPacksPerCorrectItem = 1;
// Interface implementation
public float GameDuration => gameDuration;
public float SpawnDistance => spawnDistance;
public float InitialBeltSpeed => initialBeltSpeed;
public float MaxBeltSpeed => maxBeltSpeed;
public AnimationCurve SpeedCurve => speedCurve;
public GarbageItemDefinition[] GarbageItems => garbageItems;
public float NormalCardWeight => normalCardWeight;
public float RareCardWeight => rareCardWeight;
public float LegendCardWeight => legendCardWeight;
public float GarbageWeight => garbageWeight;
public int CorrectSortPoints => correctSortPoints;
public int IncorrectSortPenalty => incorrectSortPenalty;
public int MissedItemPenalty => missedItemPenalty;
public int BoosterPacksPerCorrectItem => boosterPacksPerCorrectItem;
public override void OnValidate()
{
base.OnValidate();
gameDuration = Mathf.Max(1f, gameDuration);
initialBeltSpeed = Mathf.Max(0.1f, initialBeltSpeed);
maxBeltSpeed = Mathf.Max(initialBeltSpeed, maxBeltSpeed);
correctSortPoints = Mathf.Max(0, correctSortPoints);
boosterPacksPerCorrectItem = Mathf.Max(0, boosterPacksPerCorrectItem);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a0fd5f8ab5d74b12968501dd4e3cc416
timeCreated: 1763461789

View File

@@ -0,0 +1,39 @@
using Minigames.CardSorting.Data;
using UnityEngine;
namespace Core.Settings
{
/// <summary>
/// Settings interface for Card Sorting minigame.
/// Accessed via GameManager.GetSettingsObject&lt;ICardSortingSettings&gt;()
/// </summary>
public interface ICardSortingSettings
{
// Timing
float GameDuration { get; }
float SpawnDistance { get; }
// Conveyor Speed
float InitialBeltSpeed { get; }
float MaxBeltSpeed { get; }
AnimationCurve SpeedCurve { get; }
// Item Pools
GarbageItemDefinition[] GarbageItems { get; }
// Spawn Weights
float NormalCardWeight { get; }
float RareCardWeight { get; }
float LegendCardWeight { get; }
float GarbageWeight { get; }
// Scoring
int CorrectSortPoints { get; }
int IncorrectSortPenalty { get; }
int MissedItemPenalty { get; }
// Rewards
int BoosterPacksPerCorrectItem { get; }
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 646b90dfa92640e59430f871037affea
timeCreated: 1763461774

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 1f5c5963e2ff4b33a086f5b97e648914
timeCreated: 1763461758

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d35b6d1850b94234bbb54e77b202861b
timeCreated: 1763469999

View File

@@ -0,0 +1,370 @@
using AppleHills.Data.CardSystem;
using Core.Settings;
using Data.CardSystem;
using Minigames.CardSorting.Core;
using Minigames.CardSorting.Data;
using System.Collections.Generic;
using UnityEngine;
namespace Minigames.CardSorting.Controllers
{
/// <summary>
/// Non-MonoBehaviour controller for conveyor belt logic.
/// Handles spawning, speed, item lifecycle.
/// Owns item tracking and emits events when items fall off or are sorted.
/// Parallel to CornerCardManager in card system.
/// </summary>
public class ConveyorBeltController
{
private readonly Transform spawnPoint;
private readonly Transform endPoint; // Visual end - scoring happens here
private readonly Transform despawnPoint; // Off-screen - destruction happens here
private readonly GameObject cardPrefab;
private readonly GameObject garbagePrefab;
private readonly ICardSortingSettings settings;
private List<SortableItem> activeItems = new List<SortableItem>();
private HashSet<SortableItem> missedItems = new HashSet<SortableItem>(); // Items past visual end, moving to despawn
private float currentSpeed;
private SortableItem lastSpawnedItem; // Track last spawned item for distance-based spawning
// Events - conveyor owns item lifecycle
public event System.Action<SortableItem> OnItemSpawned; // Fired when new item spawns
public event System.Action<SortableItem> OnItemFellOffBelt; // Fired at visual end (endPoint)
public event System.Action<SortableItem> OnItemDespawned; // Fired at despawn point (destruction)
public event System.Action<SortableItem, SortingBox, bool> OnItemSorted; // item, box, correct
public event System.Action<SortableItem> OnItemDroppedOnFloor; // Fired when dropped outside any box
public float CurrentSpeed => currentSpeed;
public int ActiveItemCount => activeItems.Count;
public ConveyorBeltController(
Transform spawnPoint,
Transform endPoint,
Transform despawnPoint,
GameObject cardPrefab,
GameObject garbagePrefab,
ICardSortingSettings settings)
{
this.spawnPoint = spawnPoint;
this.endPoint = endPoint;
this.despawnPoint = despawnPoint;
this.cardPrefab = cardPrefab;
this.garbagePrefab = garbagePrefab;
this.settings = settings;
this.currentSpeed = settings.InitialBeltSpeed;
this.lastSpawnedItem = null; // No items spawned yet
}
/// <summary>
/// Update belt speed, check for items falling off, and handle distance-based spawning.
/// </summary>
public void Update(float deltaTime, float gameProgress)
{
UpdateBeltSpeed(gameProgress);
CheckItemsOffBelt();
CheckDistanceBasedSpawn(gameProgress);
}
/// <summary>
/// Check if we should spawn a new item based on distance from last spawn.
/// Items spawn when last item has moved far enough from spawn point.
/// </summary>
private void CheckDistanceBasedSpawn(float gameProgress)
{
// If no items spawned yet, spawn immediately
if (lastSpawnedItem == null)
{
SpawnNewItem(gameProgress);
return;
}
// Check if last spawned item is far enough from spawn point
float distanceFromSpawn = Mathf.Abs(lastSpawnedItem.transform.position.x - spawnPoint.position.x);
if (distanceFromSpawn >= settings.SpawnDistance) // Using InitialSpawnInterval as distance threshold
{
SpawnNewItem(gameProgress);
}
}
/// <summary>
/// Spawn a new item at the spawn point.
/// </summary>
private SortableItem SpawnNewItem(float gameProgress)
{
// Weighted random: card or garbage?
float totalWeight = settings.NormalCardWeight + settings.RareCardWeight +
settings.LegendCardWeight + settings.GarbageWeight;
if (totalWeight <= 0f)
{
Debug.LogWarning("[ConveyorBeltController] Total spawn weight is 0, cannot spawn items!");
return null;
}
float roll = Random.Range(0f, totalWeight);
SortableItem item;
if (roll < settings.GarbageWeight)
{
// Spawn garbage
item = SpawnGarbageItem();
}
else
{
// Spawn card - determine rarity, get random card from CardSystemManager
CardRarity rarity = DetermineRarity(roll);
item = SpawnCardItem(rarity);
}
if (item != null)
{
// Set conveyor speed on item
item.Context.ConveyorSpeed = currentSpeed;
activeItems.Add(item);
lastSpawnedItem = item; // Track for distance-based spawning
// Emit spawn event
OnItemSpawned?.Invoke(item);
}
return item;
}
private SortableItem SpawnGarbageItem()
{
if (settings.GarbageItems == null || settings.GarbageItems.Length == 0)
{
Debug.LogWarning("[ConveyorBeltController] No garbage items configured!");
return null;
}
GarbageItemDefinition garbage = SelectRandomGarbage();
GameObject obj = Object.Instantiate(garbagePrefab, spawnPoint.position, Quaternion.identity);
SortableItem item = obj.GetComponent<SortableItem>();
if (item != null)
{
item.SetupAsGarbage(garbage);
// Subscribe to item events
item.OnItemDroppedInBox += HandleItemDroppedInBox;
item.OnItemReturnedToConveyor += HandleItemReturnedToConveyor;
}
else
{
Debug.LogError("[ConveyorBeltController] Garbage prefab missing SortableItem component!");
Object.Destroy(obj);
return null;
}
return item;
}
private SortableItem SpawnCardItem(CardRarity rarity)
{
// Get a random card of the specified rarity
CardData cardData = GetRandomCardDataByRarity(rarity);
if (cardData == null)
{
Debug.LogWarning($"[ConveyorBeltController] No card data found for rarity {rarity}");
return null;
}
GameObject obj = Object.Instantiate(cardPrefab, spawnPoint.position, Quaternion.identity);
SortableItem item = obj.GetComponent<SortableItem>();
if (item != null)
{
item.SetupAsCard(cardData);
// Subscribe to item events
item.OnItemDroppedInBox += HandleItemDroppedInBox;
item.OnItemReturnedToConveyor += HandleItemReturnedToConveyor;
}
else
{
Debug.LogError("[ConveyorBeltController] Card prefab missing SortableItem component!");
Object.Destroy(obj);
return null;
}
return item;
}
/// <summary>
/// Helper method to get a random card of a specific rarity.
/// Gets a CardDefinition from CardSystemManager and converts to CardData.
/// Does NOT affect player's collection or open boosters.
/// </summary>
private CardData GetRandomCardDataByRarity(CardRarity targetRarity)
{
// Get random card definition from manager
var definition = CardSystemManager.Instance.GetRandomCardDefinitionByRarity(targetRarity);
if (definition == null)
{
Debug.LogWarning($"[ConveyorBeltController] No card definition found for rarity {targetRarity}");
return null;
}
// Create CardData from definition using constructor
// This properly links the definition and sets all properties
return new CardData(definition);
}
private void UpdateBeltSpeed(float gameProgress)
{
// Evaluate speed curve
float speedMultiplier = settings.SpeedCurve.Evaluate(gameProgress);
currentSpeed = Mathf.Lerp(
settings.InitialBeltSpeed,
settings.MaxBeltSpeed,
speedMultiplier
);
// Update all active items (including missed items moving to despawn)
foreach (var item in activeItems)
{
if (item != null && item.Context.IsOnConveyor)
{
item.Context.ConveyorSpeed = currentSpeed;
}
}
}
private void CheckItemsOffBelt()
{
// Check active items for reaching visual end point
for (int i = activeItems.Count - 1; i >= 0; i--)
{
var item = activeItems[i];
if (item == null)
{
activeItems.RemoveAt(i);
continue;
}
// Check if past visual end point (not yet scored as missed)
if (item.transform.position.x > endPoint.position.x && !missedItems.Contains(item))
{
// Mark as missed and emit event for scoring
missedItems.Add(item);
// Transition item to FellOffConveyorState (will blink red)
item.ChangeState("FellOffConveyorState");
OnItemFellOffBelt?.Invoke(item);
// Item continues moving, stays in activeItems until despawn
}
}
// Check missed items for reaching despawn point
for (int i = activeItems.Count - 1; i >= 0; i--)
{
var item = activeItems[i];
if (item == null)
{
activeItems.RemoveAt(i);
continue;
}
// Check if past despawn point (time to destroy)
if (item.transform.position.x > despawnPoint.position.x && missedItems.Contains(item))
{
// Remove from tracking
activeItems.RemoveAt(i);
missedItems.Remove(item);
// Clear lastSpawnedItem reference if this was it
if (lastSpawnedItem == item)
{
lastSpawnedItem = null;
}
// Emit despawn event for destruction
OnItemDespawned?.Invoke(item);
}
}
}
/// <summary>
/// Handle when an item is dropped in a box (correct or incorrect).
/// </summary>
private void HandleItemDroppedInBox(SortableItem item, SortingBox box, bool correct)
{
// Remove from tracking and unsubscribe
if (activeItems.Remove(item))
{
// Also remove from missed items if it was there
missedItems.Remove(item);
// Clear lastSpawnedItem reference if this was it
if (lastSpawnedItem == item)
{
lastSpawnedItem = null;
}
item.OnItemDroppedInBox -= HandleItemDroppedInBox;
item.OnItemReturnedToConveyor -= HandleItemReturnedToConveyor;
// Emit event for game manager to handle scoring, passing box and correctness
OnItemSorted?.Invoke(item, box, correct);
}
}
/// <summary>
/// Handle when an item is returned to conveyor (dropped outside box).
/// Item transitions to DroppedOnFloorState and gets destroyed.
/// </summary>
private void HandleItemReturnedToConveyor(SortableItem item)
{
// Remove from tracking and unsubscribe (item will be destroyed)
if (activeItems.Remove(item))
{
missedItems.Remove(item);
if (lastSpawnedItem == item)
{
lastSpawnedItem = null;
}
item.OnItemDroppedInBox -= HandleItemDroppedInBox;
item.OnItemReturnedToConveyor -= HandleItemReturnedToConveyor;
// Emit event for scoring
OnItemDroppedOnFloor?.Invoke(item);
Debug.Log($"[ConveyorBeltController] Item dropped on floor: {item.CardData?.Name ?? item.GarbageItem?.DisplayName}");
}
}
private CardRarity DetermineRarity(float roll)
{
// Adjust roll to be relative to card weights only (subtract garbage weight)
float adjusted = roll - settings.GarbageWeight;
if (adjusted < settings.NormalCardWeight)
return CardRarity.Normal;
if (adjusted < settings.NormalCardWeight + settings.RareCardWeight)
return CardRarity.Rare;
return CardRarity.Legendary;
}
private GarbageItemDefinition SelectRandomGarbage()
{
return settings.GarbageItems[Random.Range(0, settings.GarbageItems.Length)];
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 681596c1ece04da18b2c3394441ebd4b
timeCreated: 1763469999

View File

@@ -0,0 +1,88 @@
using Core.Settings;
using System;
namespace Minigames.CardSorting.Controllers
{
/// <summary>
/// Non-MonoBehaviour controller for score tracking.
/// Handles scoring, accuracy calculation, and reward calculation.
/// </summary>
public class SortingScoreController
{
private readonly ICardSortingSettings settings;
private int totalScore;
private int correctSorts;
private int incorrectSorts;
private int missedItems;
public int TotalScore => totalScore;
public int CorrectSorts => correctSorts;
public int IncorrectSorts => incorrectSorts;
public int MissedItems => missedItems;
public int TotalAttempts => correctSorts + incorrectSorts;
public float Accuracy => TotalAttempts > 0 ? (float)correctSorts / TotalAttempts : 0f;
public event Action<int> OnScoreChanged;
public event Action<int> OnCorrectSort;
public event Action<int> OnIncorrectSort;
public SortingScoreController(ICardSortingSettings settings)
{
this.settings = settings;
}
/// <summary>
/// Record a correct sort.
/// </summary>
public void RecordCorrectSort()
{
correctSorts++;
totalScore += settings.CorrectSortPoints;
OnScoreChanged?.Invoke(totalScore);
OnCorrectSort?.Invoke(correctSorts);
}
/// <summary>
/// Record an incorrect sort.
/// </summary>
public void RecordIncorrectSort()
{
incorrectSorts++;
totalScore += settings.IncorrectSortPenalty; // This is a negative value
OnScoreChanged?.Invoke(totalScore);
OnIncorrectSort?.Invoke(incorrectSorts);
}
/// <summary>
/// Record a missed item (fell off belt).
/// </summary>
public void RecordMissedItem()
{
missedItems++;
totalScore += settings.MissedItemPenalty; // This is a negative value
OnScoreChanged?.Invoke(totalScore);
}
/// <summary>
/// Calculate booster pack reward based on performance.
/// </summary>
public int CalculateBoosterReward()
{
// Simple: 1 booster per correct sort (or use settings multiplier)
return correctSorts * settings.BoosterPacksPerCorrectItem;
}
/// <summary>
/// Reset all scores (for restarting game).
/// </summary>
public void Reset()
{
totalScore = 0;
correctSorts = 0;
incorrectSorts = 0;
missedItems = 0;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 280957c6e5b24e91b9fdc49ec685ed2f
timeCreated: 1763470011

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: fa35cf17256e403ab8bed2555352eaf5
timeCreated: 1763461824

View File

@@ -0,0 +1,36 @@
using UnityEngine;
using UnityEngine.UI;
namespace Minigames.CardSorting.Core
{
/// <summary>
/// Simple sprite renderer for garbage items.
/// Parallel to CardDisplay for cards.
/// </summary>
public class GarbageVisual : MonoBehaviour
{
[Header("Visual Components")]
[SerializeField] private Image spriteRenderer;
/// <summary>
/// Update the displayed sprite.
/// </summary>
public void UpdateDisplay(Sprite sprite)
{
if (spriteRenderer != null)
{
spriteRenderer.sprite = sprite;
}
}
private void Awake()
{
// Auto-find Image component if not assigned
if (spriteRenderer == null)
{
spriteRenderer = GetComponent<Image>();
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b707770fc3a6448ea0dcd1b2fbf41e00
timeCreated: 1763461824

View File

@@ -0,0 +1,230 @@
using AppleHills.Data.CardSystem;
using Core;
using Core.SaveLoad;
using Minigames.CardSorting.Data;
using UI.DragAndDrop.Core;
using UnityEngine;
namespace Minigames.CardSorting.Core
{
/// <summary>
/// Draggable sortable item on conveyor belt.
/// Uses state machine for behavior (OnConveyor → BeingDragged → Sorted).
/// Inherits from DraggableObject to reuse drag/drop system.
/// </summary>
public class SortableItem : DraggableObject
{
[Header("Components")]
[SerializeField] private SortableItemContext context;
[SerializeField] private AppleMachine stateMachine;
[Header("Configuration")]
[SerializeField] private string initialState = "OnConveyorState";
// Data tracking
private bool isGarbage;
private CardData cardData;
private GarbageItemDefinition garbageItem;
// Events - item emits notifications, conveyor subscribes
public event System.Action<SortableItem, SortingBox, bool> OnItemDroppedInBox;
public event System.Action<SortableItem> OnItemReturnedToConveyor;
// Public accessors
public SortableItemContext Context => context;
public AppleMachine StateMachine => stateMachine;
public bool IsGarbage => isGarbage;
public CardData CardData => cardData;
public GarbageItemDefinition GarbageItem => garbageItem;
/// <summary>
/// Get the correct box type for this item.
/// </summary>
public BoxType CorrectBox
{
get
{
if (isGarbage)
return BoxType.Trash;
return cardData.Rarity switch
{
CardRarity.Normal => BoxType.Normal,
CardRarity.Rare => BoxType.Rare,
CardRarity.Legendary => BoxType.Legend,
_ => BoxType.Trash
};
}
}
protected override void Initialize()
{
base.Initialize();
// Auto-find components if not assigned
if (context == null)
context = GetComponent<SortableItemContext>();
if (stateMachine == null)
stateMachine = GetComponentInChildren<AppleMachine>();
}
/// <summary>
/// Setup item as a card.
/// </summary>
public void SetupAsCard(CardData data)
{
isGarbage = false;
cardData = data;
garbageItem = null;
if (context != null)
{
context.SetupAsCard(data);
}
if (stateMachine != null && !string.IsNullOrEmpty(initialState))
{
stateMachine.ChangeState(initialState);
}
}
/// <summary>
/// Setup item as garbage.
/// </summary>
public void SetupAsGarbage(GarbageItemDefinition garbage)
{
isGarbage = true;
cardData = default;
garbageItem = garbage;
if (context != null)
{
context.SetupAsGarbage(garbage.Sprite);
}
if (stateMachine != null && !string.IsNullOrEmpty(initialState))
{
stateMachine.ChangeState(initialState);
}
}
protected override void OnDragStartedHook()
{
base.OnDragStartedHook();
// Check if current state wants to handle drag behavior
if (stateMachine?.currentState != null)
{
var dragHandler = stateMachine.currentState.GetComponent<ISortableItemDragHandler>();
if (dragHandler != null && dragHandler.OnDragStarted(context))
{
return; // State handled it
}
}
// Default behavior if state doesn't handle
Logging.Debug($"[SortableItem] Drag started on {(isGarbage ? garbageItem.DisplayName : cardData.Name)}");
}
// TODO: Fixed when base slot/draggable reworked
public override void OnDrag(UnityEngine.EventSystems.PointerEventData eventData)
{
base.OnDrag(eventData);
if (!IsDragging) return;
// Perform raycast to detect what's underneath the dragged card
DetectSlotUnderPointer(eventData);
}
protected override void OnDragEndedHook()
{
base.OnDragEndedHook();
// Validate drop on sorting box
if (CurrentSlot is SortingBox box)
{
bool correctSort = box.ValidateItem(this);
// Fire event IMMEDIATELY when card is released over bin
// This allows manager to update score/UI right away
OnItemDroppedInBox?.Invoke(this, box, correctSort);
// Transition to appropriate state based on correctness
// State will handle fall-into-bin animation and destruction
if (correctSort)
{
ChangeState("SortedCorrectlyState");
}
else
{
ChangeState("SortedIncorrectlyState");
}
}
else
{
// Dropped outside valid box - transition to dropped on floor state
Logging.Debug("[SortableItem] Dropped outside box, transitioning to floor state");
ChangeState("DroppedOnFloorState");
}
}
// TODO: Fixed when base slot/draggable reworked
/// <summary>
/// Detect which slot (if any) is under the pointer during drag.
/// Updates CurrentSlot for drop detection.
/// </summary>
private void DetectSlotUnderPointer(UnityEngine.EventSystems.PointerEventData eventData)
{
// Perform raycast at pointer position to find slots
var raycastResults = new System.Collections.Generic.List<UnityEngine.EventSystems.RaycastResult>();
UnityEngine.EventSystems.EventSystem.current.RaycastAll(eventData, raycastResults);
SortingBox hoveredBox = null;
// Find first SortingBox in raycast results
foreach (var result in raycastResults)
{
var box = result.gameObject.GetComponentInParent<SortingBox>();
if (box != null)
{
hoveredBox = box;
break;
}
}
// Update current slot (used in OnDragEndedHook)
if (hoveredBox != null && hoveredBox != CurrentSlot)
{
_currentSlot = hoveredBox;
Logging.Debug($"[SortableItem] Now hovering over {hoveredBox.BoxType} box");
}
else if (hoveredBox == null && CurrentSlot != null)
{
_currentSlot = null;
Logging.Debug("[SortableItem] No longer over any box");
}
}
/// <summary>
/// Change to a specific state.
/// </summary>
public void ChangeState(string stateName)
{
if (stateMachine != null)
{
stateMachine.ChangeState(stateName);
}
}
}
/// <summary>
/// Interface for states that handle drag behavior.
/// </summary>
public interface ISortableItemDragHandler
{
bool OnDragStarted(SortableItemContext context);
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 9b3db9ce867c4df884411fb3da8fd80a
timeCreated: 1763461867

View File

@@ -0,0 +1,139 @@
using AppleHills.Data.CardSystem;
using Core.SaveLoad;
using UI.CardSystem;
using UI.CardSystem.StateMachine;
using UnityEngine;
namespace Minigames.CardSorting.Core
{
/// <summary>
/// Shared context for sortable item states.
/// Provides access to common components and data that states need.
/// Routes data to appropriate visual component (CardDisplay for cards, GarbageVisual for garbage).
/// </summary>
public class SortableItemContext : MonoBehaviour
{
[Header("Visual Components (one or the other)")]
[SerializeField] private CardDisplay cardDisplay; // For cards
[SerializeField] private GarbageVisual garbageVisual; // For garbage
[Header("Shared Components")]
[SerializeField] private CardAnimator animator;
[SerializeField] private Transform visualTransform; // "Visual" GameObject
private AppleMachine stateMachine;
// Public accessors
public CardDisplay CardDisplay => cardDisplay;
public GarbageVisual GarbageVisual => garbageVisual;
public CardAnimator Animator => animator;
public Transform VisualTransform => visualTransform;
public AppleMachine StateMachine => stateMachine;
public Transform RootTransform => transform;
// Conveyor state
public bool IsOnConveyor { get; set; } = true;
public float ConveyorSpeed { get; set; } = 1f;
// Original transform data (captured on spawn for drag animations)
public Vector3 OriginalScale { get; private set; }
public Vector3 OriginalPosition { get; private set; }
public Quaternion OriginalRotation { get; private set; }
private void Awake()
{
// Auto-find components if not assigned
if (visualTransform == null)
{
visualTransform = transform.Find("Visual");
if (visualTransform == null)
{
Debug.LogWarning($"[SortableItemContext] 'Visual' child GameObject not found on {name}");
}
}
if (animator == null)
{
// CardAnimator should be on root GameObject (animates root transform with Canvas scale)
animator = GetComponent<CardAnimator>();
// Fallback: check Visual child (legacy setup)
if (animator == null && visualTransform != null)
{
animator = visualTransform.GetComponent<CardAnimator>();
}
}
if (cardDisplay == null && visualTransform != null)
{
cardDisplay = visualTransform.GetComponentInChildren<CardDisplay>();
}
if (garbageVisual == null && visualTransform != null)
{
garbageVisual = visualTransform.GetComponentInChildren<GarbageVisual>();
}
stateMachine = GetComponentInChildren<AppleMachine>();
}
/// <summary>
/// Setup as card item - CardDisplay handles all rendering.
/// </summary>
public void SetupAsCard(CardData cardData)
{
// Capture original root transform for drag animations
// This preserves the tiny world-space Canvas scale (e.g., 0.05)
var currentScale = transform.localScale;
if (currentScale.x < 0.01f && currentScale.y < 0.01f && currentScale.z < 0.01f)
{
OriginalScale = Vector3.one; // Fallback if scale is ~0
}
else
{
OriginalScale = currentScale;
}
OriginalPosition = transform.localPosition;
OriginalRotation = transform.localRotation;
if (cardDisplay != null)
{
cardDisplay.SetupCard(cardData);
}
else
{
Debug.LogError($"[SortableItemContext] CardDisplay not found on {name}");
}
}
/// <summary>
/// Setup as garbage item - simple sprite display.
/// </summary>
public void SetupAsGarbage(Sprite sprite)
{
// Capture original root transform for drag animations
// This preserves the tiny world-space Canvas scale (e.g., 0.05)
var currentScale = transform.localScale;
if (currentScale.x < 0.01f && currentScale.y < 0.01f && currentScale.z < 0.01f)
{
OriginalScale = Vector3.one; // Fallback if scale is ~0
}
else
{
OriginalScale = currentScale;
}
OriginalPosition = transform.localPosition;
OriginalRotation = transform.localRotation;
if (garbageVisual != null)
{
garbageVisual.UpdateDisplay(sprite);
}
else
{
Debug.LogError($"[SortableItemContext] GarbageVisual not found on {name}");
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 9a9c60767eef4a3090d8bf70ee87340f
timeCreated: 1763461839

View File

@@ -0,0 +1,40 @@
using Minigames.CardSorting.Data;
using UI.DragAndDrop.Core;
using UnityEngine;
namespace Minigames.CardSorting.Core
{
/// <summary>
/// Drop target for sortable items.
/// Validates if item belongs in this box.
/// </summary>
public class SortingBox : DraggableSlot
{
[Header("Box Configuration")]
[SerializeField] private BoxType boxType;
public BoxType BoxType => boxType;
/// <summary>
/// Check if item belongs in this box.
/// </summary>
public bool ValidateItem(SortableItem item)
{
if (item == null) return false;
BoxType correctBox = item.CorrectBox;
return correctBox == boxType;
}
/// <summary>
/// Check if this slot can accept a specific draggable type.
/// SortingBox accepts all SortableItems (validation happens on drop).
/// </summary>
public new bool CanAccept(DraggableObject draggable)
{
// Accept all sortable items (validation happens on drop)
return draggable is SortableItem;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a25b1c9a82b540c8ac0d6c016849f561
timeCreated: 1763461875

View File

@@ -0,0 +1,341 @@
using System;
using Core;
using Core.Lifecycle;
using Core.Settings;
using Data.CardSystem;
using Input;
using Minigames.CardSorting.Controllers;
using Minigames.CardSorting.Core;
using Unity.Cinemachine;
using UnityEngine;
namespace Minigames.CardSorting.Core
{
/// <summary>
/// Main manager for card sorting minigame.
/// Orchestrates game loop, timer, controllers, and integration with CardSystemManager.
/// Parallel to DivingGameManager.
/// </summary>
public class SortingGameManager : ManagedBehaviour
{
[Header("Scene References")]
[SerializeField] private Transform conveyorSpawnPoint;
[SerializeField] private Transform conveyorEndPoint; // Visual end - items scored as missed here
[SerializeField] private Transform conveyorDespawnPoint; // Off-screen - items destroyed here
[SerializeField] private GameObject sortableCardPrefab;
[SerializeField] private GameObject sortableGarbagePrefab;
[SerializeField] private SortingBox[] sortingBoxes;
[Header("Effects")]
[SerializeField] private CinemachineImpulseSource impulseSource; // Screen shake on incorrect sort
// Settings
private ICardSortingSettings _settings;
// Controllers (lazy init)
private ConveyorBeltController _conveyorController;
private ConveyorBeltController Conveyor => _conveyorController ??= new ConveyorBeltController(
conveyorSpawnPoint,
conveyorEndPoint,
conveyorDespawnPoint,
sortableCardPrefab,
sortableGarbagePrefab,
_settings
);
private SortingScoreController _scoreController;
private SortingScoreController Score => _scoreController ??= new SortingScoreController(_settings);
// Game state
private float gameTimer;
private bool isGameActive;
private bool isGameOver;
// Singleton
private static SortingGameManager _instance;
public static SortingGameManager Instance => _instance;
// Events
public event Action OnGameStarted;
public event Action OnGameEnded;
public event Action<SortableItem> OnItemSpawned;
public event Action<SortableItem, SortingBox, bool> OnItemSortedEvent;
public event Action<float> OnTimerUpdated; // Remaining time
// Global effect events
public event Action<SortableItem> OnItemSortedCorrectly;
public event Action<SortableItem> OnItemSortedIncorrectly;
public event Action<SortableItem> OnItemFellOffBelt;
internal override void OnManagedAwake()
{
_instance = this;
// Load settings
_settings = GameManager.GetSettingsObject<ICardSortingSettings>();
if (_settings == null)
{
Debug.LogError("[SortingGameManager] Failed to load CardSortingSettings!");
return;
}
Logging.Debug("[SortingGameManager] Initialized with settings");
}
internal override void OnManagedStart()
{
// Subscribe to score events
Score.OnScoreChanged += OnScoreChanged;
Score.OnCorrectSort += OnCorrectSort;
Score.OnIncorrectSort += OnIncorrectSort;
// Subscribe to conveyor events
Conveyor.OnItemSpawned += OnConveyorItemSpawned;
Conveyor.OnItemFellOffBelt += OnConveyorItemFellOff;
Conveyor.OnItemDespawned += OnConveyorItemDespawned;
Conveyor.OnItemSorted += OnConveyorItemSorted;
Conveyor.OnItemDroppedOnFloor += OnConveyorItemDroppedOnFloor;
// Start game automatically or wait for trigger
// For now, auto-start
StartGame();
}
private void OnDestroy()
{
if (_scoreController != null)
{
Score.OnScoreChanged -= OnScoreChanged;
Score.OnCorrectSort -= OnCorrectSort;
Score.OnIncorrectSort -= OnIncorrectSort;
}
if (_conveyorController != null)
{
Conveyor.OnItemSpawned -= OnConveyorItemSpawned;
Conveyor.OnItemFellOffBelt -= OnConveyorItemFellOff;
Conveyor.OnItemDespawned -= OnConveyorItemDespawned;
Conveyor.OnItemSorted -= OnConveyorItemSorted;
Conveyor.OnItemDroppedOnFloor -= OnConveyorItemDroppedOnFloor;
}
}
private void Update()
{
if (!isGameActive || isGameOver) return;
gameTimer += Time.deltaTime;
float remainingTime = _settings.GameDuration - gameTimer;
float gameProgress = gameTimer / _settings.GameDuration;
// Update timer
OnTimerUpdated?.Invoke(remainingTime);
// Check game over
if (remainingTime <= 0f)
{
EndGame();
return;
}
// Update conveyor (handles spawning, movement, and despawning internally)
Conveyor.Update(Time.deltaTime, gameProgress);
}
public void StartGame()
{
isGameActive = true;
isGameOver = false;
gameTimer = 0f;
// Reset score
Score.Reset();
OnGameStarted?.Invoke();
// Set input mode to game
if (InputManager.Instance != null)
{
InputManager.Instance.SetInputMode(InputMode.GameAndUI);
}
Logging.Debug("[SortingGameManager] Game started!");
}
public void EndGame()
{
if (isGameOver) return;
isGameOver = true;
isGameActive = false;
// Calculate rewards
int boosterReward = Score.CalculateBoosterReward();
Logging.Debug($"[SortingGameManager] Game ended! Score: {Score.TotalScore}, Boosters: {boosterReward}");
// Grant boosters
if (CardSystemManager.Instance != null)
{
CardSystemManager.Instance.AddBoosterPack(boosterReward);
}
OnGameEnded?.Invoke();
// Show results screen (handled by UI controller)
}
/// <summary>
/// Called when conveyor spawns a new item.
/// </summary>
private void OnConveyorItemSpawned(SortableItem item)
{
// Forward to public event for UI/other systems
OnItemSpawned?.Invoke(item);
Logging.Debug($"[SortingGameManager] Item spawned: {item.CardData?.Name ?? item.GarbageItem?.DisplayName}");
}
/// <summary>
/// Called when item reaches visual end of belt (via conveyor event).
/// Item continues moving off-screen until despawn point.
/// Scoring rules:
/// - Trash fell off: Negative score (penalty)
/// - Card fell off: Neutral (no score change)
/// </summary>
private void OnConveyorItemFellOff(SortableItem item)
{
// Only penalize TRASH items that fall off
// Cards falling off are neutral (no score change)
if (item.IsGarbage)
{
Score.RecordMissedItem();
Logging.Debug($"[SortingGameManager] Trash fell off belt! {item.GarbageItem?.DisplayName} - PENALTY");
}
else
{
Logging.Debug($"[SortingGameManager] Card fell off belt: {item.CardData?.Name} - no penalty");
}
// Fire global fell off belt event for effects
OnItemFellOffBelt?.Invoke(item);
// Visual feedback could go here (e.g., "MISS!" popup)
// Item will continue moving and be destroyed at despawn point
}
/// <summary>
/// Called when item is dropped on floor (via conveyor event).
/// Scoring rules:
/// - Trash dropped on floor: Negative score (penalty)
/// - Card dropped on floor: Neutral (no score change)
/// </summary>
private void OnConveyorItemDroppedOnFloor(SortableItem item)
{
// Only penalize TRASH items dropped on floor
// Cards dropped on floor are neutral (no score change)
if (item.IsGarbage)
{
Score.RecordIncorrectSort();
Logging.Debug($"[SortingGameManager] Trash dropped on floor! {item.GarbageItem?.DisplayName} - PENALTY");
// Trigger screen shake for trash dropped on floor
if (impulseSource != null)
{
impulseSource.GenerateImpulse();
}
}
else
{
Logging.Debug($"[SortingGameManager] Card dropped on floor: {item.CardData?.Name} - no penalty");
}
}
/// <summary>
/// Called when item reaches despawn point (via conveyor event).
/// Actually destroys the item.
/// </summary>
private void OnConveyorItemDespawned(SortableItem item)
{
Logging.Debug($"[SortingGameManager] Item despawned: {item.CardData?.Name ?? item.GarbageItem?.DisplayName}");
// Destroy the item
if (item != null)
Destroy(item.gameObject);
}
/// <summary>
/// Called when conveyor confirms item was sorted (via event).
/// Handles scoring only - the state (SortedCorrectlyState/SortedIncorrectlyState) handles animation and destruction.
/// Scoring rules:
/// - Correct sort: Positive score (cards or trash in correct box)
/// - Incorrect trash: Negative score (trash in wrong box)
/// - Incorrect card: Neutral (no score change)
/// </summary>
private void OnConveyorItemSorted(SortableItem item, SortingBox box, bool correct)
{
if (correct)
{
Score.RecordCorrectSort();
Logging.Debug($"[SortingGameManager] Correct sort! {item.CardData?.Name ?? item.GarbageItem?.DisplayName}");
// Fire global correct sort event for effects
OnItemSortedCorrectly?.Invoke(item);
}
else
{
// Only penalize incorrect sorting for TRASH items
// Cards incorrectly sorted are neutral (no score change)
if (item.IsGarbage)
{
Score.RecordIncorrectSort();
Logging.Debug($"[SortingGameManager] Incorrect trash sort! {item.GarbageItem?.DisplayName} - PENALTY");
// Fire global incorrect sort event for effects
OnItemSortedIncorrectly?.Invoke(item);
// Trigger screen shake
if (impulseSource != null)
{
impulseSource.GenerateImpulse();
}
}
else
{
Logging.Debug($"[SortingGameManager] Card sorted incorrectly: {item.CardData?.Name} - no penalty");
}
}
OnItemSortedEvent?.Invoke(item, box, correct);
// State handles animation and destruction - we just update score/UI here
}
private void OnScoreChanged(int newScore)
{
// UI will subscribe to this event
Logging.Debug($"[SortingGameManager] Score changed: {newScore}");
}
private void OnCorrectSort(int totalCorrect)
{
// Could play effects, update combo, etc.
}
private void OnIncorrectSort(int totalIncorrect)
{
// Could play error effects
}
// Public accessors for UI
public int CurrentScore => Score?.TotalScore ?? 0;
public int CorrectSorts => Score?.CorrectSorts ?? 0;
public int IncorrectSorts => Score?.IncorrectSorts ?? 0;
public int MissedItems => Score?.MissedItems ?? 0;
public float Accuracy => Score?.Accuracy ?? 0f;
public float RemainingTime => Mathf.Max(0f, _settings.GameDuration - gameTimer);
public bool IsGameActive => isGameActive && !isGameOver;
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 20acac8b97ca4d6397612679b3bbde50
timeCreated: 1763470372

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a450e2687ce14a66b1495e1f2db7d403
timeCreated: 1763461758

View File

@@ -0,0 +1,14 @@
namespace Minigames.CardSorting.Data
{
/// <summary>
/// Types of sorting boxes in the minigame.
/// </summary>
public enum BoxType
{
Normal, // Copper-rimmed box for Normal rarity cards
Rare, // Silver-rimmed box for Rare rarity cards
Legend, // Gold-rimmed box for Legendary rarity cards
Trash // Trash can for garbage items
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 7e238b51f7164867b519bf139fe22c01
timeCreated: 1763461758

View File

@@ -0,0 +1,38 @@
using UnityEngine;
namespace Minigames.CardSorting.Data
{
/// <summary>
/// Definition for garbage items (banana peels, cans, receipts, etc.).
/// Cards use existing CardDefinition from CardSystemManager.
/// </summary>
[CreateAssetMenu(fileName = "GarbageItem", menuName = "Minigames/CardSorting/GarbageItem", order = 0)]
public class GarbageItemDefinition : ScriptableObject
{
[Tooltip("Unique identifier for this garbage item")]
[SerializeField] private string itemId;
[Tooltip("Display name for debugging")]
[SerializeField] private string displayName;
[Tooltip("Sprite to display for this garbage item")]
[SerializeField] private Sprite sprite;
// Public accessors
public string ItemId => itemId;
public string DisplayName => displayName;
public Sprite Sprite => sprite;
#if UNITY_EDITOR
private void OnValidate()
{
// Auto-generate itemId from asset name if empty
if (string.IsNullOrEmpty(itemId))
{
itemId = name;
}
}
#endif
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 2e69a2167710437798b1980126d5a4f6
timeCreated: 1763461765

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 0197a7c7c3174a5fbf1ddd5b1445f24c
timeCreated: 1763461845

View File

@@ -0,0 +1,21 @@
using Core.SaveLoad;
namespace Minigames.CardSorting.StateMachine
{
/// <summary>
/// State machine for sortable items that opts out of save system.
/// Sorting minigame is session-only and doesn't persist between loads.
/// Follows CardStateMachine pattern.
/// </summary>
public class SortingStateMachine : AppleMachine
{
/// <summary>
/// Opt out of save/load system - sortable items are transient minigame objects.
/// </summary>
public override bool ShouldParticipateInSave()
{
return false;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 1b459f40574b45839aa32d5730627ca6
timeCreated: 1763461845

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 24973c8bb25d493885224ac6f099492d
timeCreated: 1763469762

View File

@@ -0,0 +1,49 @@
using Core;
using Core.SaveLoad;
using Minigames.CardSorting.Core;
using UnityEngine;
namespace Minigames.CardSorting.StateMachine.States
{
/// <summary>
/// Item is being dragged by the player.
/// Provides visual feedback (scale up).
/// Transitions to SortedState when dropped in box, or back to OnConveyorState if dropped elsewhere.
/// </summary>
public class BeingDraggedState : AppleState
{
private SortableItemContext _context;
private void Awake()
{
_context = GetComponentInParent<SortableItemContext>();
}
public override void OnEnterState()
{
if (_context == null) return;
_context.IsOnConveyor = false;
// Visual feedback: scale up root transform by 10%
// Use OriginalScale from context (captured at spawn, preserves world-space Canvas scale)
if (_context.RootTransform != null && _context.Animator != null)
{
Vector3 targetScale = _context.OriginalScale * 1.1f;
_context.Animator.AnimateScale(targetScale, 0.2f);
}
Logging.Debug("[BeingDraggedState] Item being dragged, scaled up for feedback");
}
private void OnDisable()
{
// Restore original root transform scale (e.g., 0.05 for world-space Canvas)
if (_context != null && _context.Animator != null)
{
_context.Animator.AnimateScale(_context.OriginalScale, 0.2f);
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 015c0740240748c8901c9304490cb80d
timeCreated: 1763469770

View File

@@ -0,0 +1,86 @@
using Core;
using Core.SaveLoad;
using Minigames.CardSorting.Core;
namespace Minigames.CardSorting.StateMachine.States
{
/// <summary>
/// Item was dropped outside any bin (on the floor).
/// Plays "disappear" animation then destroys the item.
/// </summary>
public class DroppedOnFloorState : AppleState
{
private SortableItemContext _context;
private SortableItem _item;
private void Awake()
{
_context = GetComponentInParent<SortableItemContext>();
_item = GetComponentInParent<SortableItem>();
}
public override void OnEnterState()
{
if (_context == null) return;
_context.IsOnConveyor = false;
Logging.Debug("[DroppedOnFloorState] Item dropped on floor, blinking red then disappearing");
// Blink red briefly, then play disappear animation
StartBlinkThenDisappear();
}
private void StartBlinkThenDisappear()
{
if (_context.Animator == null || _item == null) return;
// Get the image to blink
UnityEngine.UI.Image imageToBlink = null;
if (_context.CardDisplay != null)
{
imageToBlink = _context.CardDisplay.GetComponent<UnityEngine.UI.Image>();
}
else if (_context.GarbageVisual != null)
{
imageToBlink = _context.GarbageVisual.GetComponent<UnityEngine.UI.Image>();
}
if (imageToBlink != null)
{
// Blink red briefly (2-3 times), then stop and disappear
_context.Animator.BlinkRed(imageToBlink, 0.15f); // Fast blink
// After brief delay, stop blinking and play disappear animation
_context.Animator.AnimateScale(_context.RootTransform.localScale, 0.5f, () =>
{
_context.Animator.StopBlinking();
PlayDisappearAnimation();
});
}
else
{
// No image found, just disappear directly
PlayDisappearAnimation();
}
}
private void PlayDisappearAnimation()
{
if (_context.Animator == null || _item == null) return;
// Tween scale down to 0 (disappear)
// When complete, destroy the item
_context.Animator.PopOut(0.4f, () =>
{
if (_item != null)
{
Logging.Debug("[DroppedOnFloorState] Animation complete, destroying item");
Destroy(_item.gameObject);
}
});
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b384e4988bf549f2b6e70d1ff0fa4bcd
timeCreated: 1763557103

View File

@@ -0,0 +1,88 @@
using Core;
using Core.SaveLoad;
using Minigames.CardSorting.Core;
using UnityEngine;
namespace Minigames.CardSorting.StateMachine.States
{
/// <summary>
/// Item reached the visual end of conveyor without being sorted.
/// Becomes non-clickable and blinks red until despawn point.
/// </summary>
public class FellOffConveyorState : AppleState
{
private SortableItemContext _context;
private SortableItem _item;
private void Awake()
{
_context = GetComponentInParent<SortableItemContext>();
_item = GetComponentInParent<SortableItem>();
}
public override void OnEnterState()
{
if (_context == null) return;
// Keep IsOnConveyor = true so item continues moving to despawn point
// Item is no longer sortable but must continue moving off-screen
_context.IsOnConveyor = true;
Logging.Debug("[FellOffConveyorState] Item fell off conveyor, blinking red until despawn");
// Disable dragging - item can no longer be picked up
if (_item != null)
{
_item.SetDraggingEnabled(false);
}
// Start blinking red animation
StartBlinkingRed();
}
private void Update()
{
if (_context == null || !_context.IsOnConveyor) return;
// Continue moving item toward despawn point (same logic as OnConveyorState)
Vector3 movement = Vector3.right * _context.ConveyorSpeed * Time.deltaTime;
_context.RootTransform.position += movement;
}
private void StartBlinkingRed()
{
if (_context.Animator == null) return;
// Get the image to tint (CardDisplay or GarbageVisual)
UnityEngine.UI.Image imageToBlink = null;
if (_context.CardDisplay != null)
{
imageToBlink =
_context.CardDisplay.GetComponent<UnityEngine.UI.Image>()
?? _context.CardDisplay.GetComponentInChildren<UnityEngine.UI.Image>();
}
else if (_context.GarbageVisual != null)
{
imageToBlink =
_context.GarbageVisual.GetComponent<UnityEngine.UI.Image>()
?? _context.GarbageVisual.GetComponentInChildren<UnityEngine.UI.Image>();
}
if (imageToBlink != null)
{
_context.Animator.BlinkRed(imageToBlink);
}
}
private void OnDisable()
{
// Stop blinking when state exits (item despawned)
if (_context?.Animator != null)
{
_context.Animator.StopBlinking();
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 619a38624dcf48b19913bd4e1ac28625
timeCreated: 1763557115

View File

@@ -0,0 +1,62 @@
using Core;
using Core.SaveLoad;
using Minigames.CardSorting.Core;
using UnityEngine;
namespace Minigames.CardSorting.StateMachine.States
{
/// <summary>
/// Item is moving along the conveyor belt.
/// Transitions to BeingDraggedState when player drags the item.
/// </summary>
public class OnConveyorState : AppleState, ISortableItemDragHandler
{
private SortableItemContext _context;
private SortableItem _item;
private void Awake()
{
_context = GetComponentInParent<SortableItemContext>();
_item = GetComponentInParent<SortableItem>();
}
public override void OnEnterState()
{
if (_context == null) return;
_context.IsOnConveyor = true;
Logging.Debug($"[OnConveyorState] Item entered conveyor state");
}
private void OnDisable()
{
if (_context == null) return;
_context.IsOnConveyor = false;
}
private void Update()
{
if (_context == null || !_context.IsOnConveyor) return;
// Move item along conveyor (right direction)
Vector3 movement = Vector3.right * _context.ConveyorSpeed * Time.deltaTime;
_context.RootTransform.position += movement;
}
/// <summary>
/// Handle drag start - transition to BeingDraggedState.
/// </summary>
public bool OnDragStarted(SortableItemContext context)
{
Logging.Debug("[OnConveyorState] Drag started, transitioning to BeingDraggedState");
// Transition to dragging state
_item?.ChangeState("BeingDraggedState");
return true; // We handled it
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 17d2ba6f5aec4b698247b082734cad8f
timeCreated: 1763469762

View File

@@ -0,0 +1,73 @@
using Core;
using Core.SaveLoad;
using Minigames.CardSorting.Core;
namespace Minigames.CardSorting.StateMachine.States
{
/// <summary>
/// Item has been correctly sorted into the right box.
/// Plays "fall into bin" animation then destroys the card.
/// </summary>
public class SortedCorrectlyState : AppleState
{
private SortableItemContext _context;
private SortableItem _item;
private void Awake()
{
_context = GetComponentInParent<SortableItemContext>();
_item = GetComponentInParent<SortableItem>();
}
public override void OnEnterState()
{
if (_context == null) return;
_context.IsOnConveyor = false;
Logging.Debug("[SortedCorrectlyState] Item correctly sorted, tweening to box then falling in");
// First tween to box center, then play fall animation
TweenToBoxThenFall();
}
private void TweenToBoxThenFall()
{
if (_context.Animator == null || _item == null) return;
// Get the box position (if available from CurrentSlot)
var box = _item.CurrentSlot as SortingBox;
if (box != null)
{
// Tween position to box center
_context.Animator.AnimateLocalPosition(box.transform.position, 0.2f, () =>
{
// After reaching box, play fall animation
PlayFallIntoBinAnimation();
});
}
else
{
// No box found, just play fall animation
PlayFallIntoBinAnimation();
}
}
private void PlayFallIntoBinAnimation()
{
if (_context.Animator == null || _item == null) return;
// Tween scale down to 0 (looks like falling into bin)
// When complete, destroy the card
_context.Animator.PopOut(0.4f, () =>
{
if (_item != null)
{
Logging.Debug("[SortedCorrectlyState] Animation complete, destroying item");
Destroy(_item.gameObject);
}
});
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: f3ed2e6fb0814273926c33a178bdf42b
timeCreated: 1763555977

View File

@@ -0,0 +1,109 @@
using Core;
using Core.SaveLoad;
using Minigames.CardSorting.Core;
namespace Minigames.CardSorting.StateMachine.States
{
/// <summary>
/// Item has been incorrectly sorted into the wrong box.
/// Plays "fall into bin" animation then destroys the card.
/// Same animation as correct sort, but different state for tracking/events.
/// </summary>
public class SortedIncorrectlyState : AppleState
{
private SortableItemContext _context;
private SortableItem _item;
private void Awake()
{
_context = GetComponentInParent<SortableItemContext>();
_item = GetComponentInParent<SortableItem>();
}
public override void OnEnterState()
{
if (_context == null) return;
_context.IsOnConveyor = false;
Logging.Debug("[SortedIncorrectlyState] Item incorrectly sorted, blinking red then tweening to box");
// Start blinking red briefly, then tween to box
StartBlinkThenTween();
}
private void StartBlinkThenTween()
{
if (_context.Animator == null || _item == null) return;
// Get the image to blink
UnityEngine.UI.Image imageToBlink = null;
if (_context.CardDisplay != null)
{
imageToBlink = _context.CardDisplay.GetComponent<UnityEngine.UI.Image>();
}
else if (_context.GarbageVisual != null)
{
imageToBlink = _context.GarbageVisual.GetComponent<UnityEngine.UI.Image>();
}
if (imageToBlink != null)
{
// Blink red briefly (2-3 times), then stop and continue with tween
_context.Animator.BlinkRed(imageToBlink, 0.15f); // Fast blink
// After brief delay, stop blinking and tween to box
_context.Animator.AnimateScale(_context.RootTransform.localScale, 0.5f, () =>
{
_context.Animator.StopBlinking();
TweenToBoxThenFall();
});
}
else
{
// No image found, just tween directly
TweenToBoxThenFall();
}
}
private void TweenToBoxThenFall()
{
if (_context.Animator == null || _item == null) return;
// Get the box position (if available from CurrentSlot)
var box = _item.CurrentSlot as SortingBox;
if (box != null)
{
// Tween position to box center
_context.Animator.AnimateLocalPosition(box.transform.position, 0.2f, () =>
{
// After reaching box, play fall animation
PlayFallIntoBinAnimation();
});
}
else
{
// No box found, just play fall animation
PlayFallIntoBinAnimation();
}
}
private void PlayFallIntoBinAnimation()
{
if (_context.Animator == null || _item == null) return;
// Tween scale down to 0 (looks like falling into bin)
// When complete, destroy the card
_context.Animator.PopOut(0.4f, () =>
{
if (_item != null)
{
Logging.Debug("[SortedIncorrectlyState] Animation complete, destroying item");
Destroy(_item.gameObject);
}
});
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: edef0fb846be4fd99d396ea27dca1e4f
timeCreated: 1763555989

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 2194dbe8c2c4479f89f7307ce56cac5d
timeCreated: 1763470403

View File

@@ -0,0 +1,116 @@
using Core.Settings;
using Minigames.CardSorting.Core;
using TMPro;
using UnityEngine;
namespace Minigames.CardSorting.UI
{
/// <summary>
/// HUD display for card sorting minigame.
/// Shows timer and score during gameplay.
/// </summary>
public class SortingGameHUD : MonoBehaviour
{
[Header("UI Elements")]
[SerializeField] private TextMeshProUGUI timerText;
[SerializeField] private TextMeshProUGUI scoreText;
[SerializeField] private TextMeshProUGUI accuracyText;
private SortingGameManager gameManager;
private void Start()
{
gameManager = SortingGameManager.Instance;
if (gameManager == null)
{
Debug.LogError("[SortingGameHUD] SortingGameManager not found!");
return;
}
// Subscribe to events
gameManager.OnTimerUpdated += UpdateTimer;
if (gameManager != null)
{
var scoreController = typeof(SortingGameManager)
.GetProperty("Score", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
?.GetValue(gameManager);
if (scoreController != null)
{
var scoreChangedEvent = scoreController.GetType().GetEvent("OnScoreChanged");
if (scoreChangedEvent != null)
{
scoreChangedEvent.AddEventHandler(scoreController,
new System.Action<int>(UpdateScore));
}
}
}
// Initial display
UpdateScore(0);
UpdateTimer(120f); // Default timer display
}
private void OnDestroy()
{
if (gameManager != null)
{
gameManager.OnTimerUpdated -= UpdateTimer;
}
}
private void Update()
{
// Update accuracy every frame (could optimize to only update on score change)
if (gameManager != null && accuracyText != null)
{
float accuracy = gameManager.Accuracy;
accuracyText.text = $"Accuracy: {accuracy:P0}";
}
}
public void UpdateTimer(float remainingTime)
{
if (timerText == null) return;
int minutes = Mathf.FloorToInt(remainingTime / 60f);
int seconds = Mathf.FloorToInt(remainingTime % 60f);
timerText.text = $"{minutes:00}:{seconds:00}";
// Change color if time running out
if (remainingTime <= 10f)
{
timerText.color = Color.red;
}
else if (remainingTime <= 30f)
{
timerText.color = Color.yellow;
}
else
{
timerText.color = Color.white;
}
}
public void UpdateScore(int newScore)
{
if (scoreText == null) return;
scoreText.text = $"Score: {newScore}";
// Color based on positive/negative
if (newScore >= 0)
{
scoreText.color = Color.white;
}
else
{
scoreText.color = new Color(1f, 0.5f, 0.5f); // Light red
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: aa656e03d5384a9eae31fab73b6fe5e2
timeCreated: 1763470403

View File

@@ -0,0 +1,126 @@
using Minigames.CardSorting.Core;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace Minigames.CardSorting.UI
{
/// <summary>
/// Results screen shown at end of card sorting minigame.
/// Displays final score, accuracy, and boosters earned.
/// </summary>
public class SortingResultsScreen : MonoBehaviour
{
[Header("UI Elements")]
[SerializeField] private TextMeshProUGUI finalScoreText;
[SerializeField] private TextMeshProUGUI correctSortsText;
[SerializeField] private TextMeshProUGUI incorrectSortsText;
[SerializeField] private TextMeshProUGUI missedItemsText;
[SerializeField] private TextMeshProUGUI accuracyText;
[SerializeField] private TextMeshProUGUI boostersEarnedText;
[SerializeField] private Button closeButton;
[Header("Screen")]
[SerializeField] private CanvasGroup canvasGroup;
private void Awake()
{
// Hide initially
if (canvasGroup != null)
{
canvasGroup.alpha = 0f;
canvasGroup.interactable = false;
canvasGroup.blocksRaycasts = false;
}
else
{
gameObject.SetActive(false);
}
// Setup close button
if (closeButton != null)
{
closeButton.onClick.AddListener(OnCloseClicked);
}
}
private void Start()
{
var gameManager = SortingGameManager.Instance;
if (gameManager != null)
{
gameManager.OnGameEnded += ShowResults;
}
}
private void OnDestroy()
{
var gameManager = SortingGameManager.Instance;
if (gameManager != null)
{
gameManager.OnGameEnded -= ShowResults;
}
}
private void ShowResults()
{
var gameManager = SortingGameManager.Instance;
if (gameManager == null) return;
// Populate data
if (finalScoreText != null)
finalScoreText.text = $"Final Score: {gameManager.CurrentScore}";
if (correctSortsText != null)
correctSortsText.text = $"Correct: {gameManager.CorrectSorts}";
if (incorrectSortsText != null)
incorrectSortsText.text = $"Incorrect: {gameManager.IncorrectSorts}";
if (missedItemsText != null)
missedItemsText.text = $"Missed: {gameManager.MissedItems}";
if (accuracyText != null)
accuracyText.text = $"Accuracy: {gameManager.Accuracy:P0}";
// Calculate boosters (already granted by manager)
int boosters = gameManager.CorrectSorts; // Simple 1:1 ratio
if (boostersEarnedText != null)
boostersEarnedText.text = $"Boosters Earned: {boosters}";
// Show screen
if (canvasGroup != null)
{
canvasGroup.alpha = 1f;
canvasGroup.interactable = true;
canvasGroup.blocksRaycasts = true;
}
else
{
gameObject.SetActive(true);
}
}
private void OnCloseClicked()
{
// Hide screen
if (canvasGroup != null)
{
canvasGroup.alpha = 0f;
canvasGroup.interactable = false;
canvasGroup.blocksRaycasts = false;
}
else
{
gameObject.SetActive(false);
}
// Could also trigger scene transition, return to menu, etc.
Debug.Log("[SortingResultsScreen] Closed results screen");
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 03823b5ad80b482086569050fbb8bb40
timeCreated: 1763470418

View File

@@ -130,8 +130,9 @@ namespace UI.DragAndDrop.Core
SmoothMoveTowardPointer();
}
// Only clamp for non-overlay canvases (WorldSpace/ScreenSpaceCamera)
if (_canvas != null && _canvas.renderMode != RenderMode.ScreenSpaceOverlay)
// Only clamp when actively being dragged (prevent dragging off-screen)
// Don't clamp when just sitting idle - allow objects to move freely off-screen
if (_isDragging && _canvas != null && _canvas.renderMode != RenderMode.ScreenSpaceOverlay)
{
ClampToScreen();
}

View File

@@ -45,6 +45,10 @@ namespace UI.DragAndDrop.Core
public Vector3 WorldPosition => transform.position;
public RectTransform RectTransform => transform as RectTransform;
// Support for both UI and world-space slots
public bool IsUISlot => transform is RectTransform;
public Vector3 SlotPosition => transform.position; // Works for both Transform types
private void Start()
{
if (hideImageOnPlay)
@@ -78,6 +82,7 @@ namespace UI.DragAndDrop.Core
switch (occupantSizeMode)
{
case OccupantSizeMode.MatchSlotSize:
// Only works for UI slots with RectTransform
if (draggable.RectTransform != null && RectTransform != null)
{
Vector2 targetSize = RectTransform.sizeDelta;
@@ -85,6 +90,7 @@ namespace UI.DragAndDrop.Core
(val) => draggable.RectTransform.sizeDelta = val,
scaleTransitionDuration, 0f, Tween.EaseOutBack);
}
// World-space slots (no RectTransform) skip size matching
break;
case OccupantSizeMode.Scale:

View File

@@ -157,19 +157,24 @@ namespace UI
}
}
// Subscribe to scene load events to adjust HUD based on scene
if (SceneManagerService.Instance != null)
{
SceneManagerService.Instance.SceneLoadCompleted += NewSceneLoaded;
}
if (SceneManagerService.Instance.CurrentGameplayScene == "AppleHillsOverworld")
{
NewSceneLoaded("AppleHillsOverworld");
}
if (SceneManagerService.Instance.CurrentGameplayScene == "StartingScene")
{
// TODO: Hide all UI until cinematics have played
NewSceneLoaded("AppleHillsOverworld");
}
// If in editor - initialize HUD based on current scene
#if UNITY_EDITOR
if (SceneManagerService.Instance.CurrentGameplayScene == "StartingScene")
{
// TODO: Hide all UI until cinematics have played
NewSceneLoaded("AppleHillsOverworld");
}
else if (SceneManagerService.Instance.CurrentGameplayScene != null)
{
NewSceneLoaded(SceneManagerService.Instance.CurrentGameplayScene);
}
#endif
}
internal override void OnManagedDestroy()
@@ -229,7 +234,7 @@ namespace UI
case "Quarry":
currentUIMode = UIMode.Puzzle;
break;
case "DivingForPictures":
case "DivingForPictures" or "CardQualityControl":
currentUIMode = UIMode.Minigame;
break;
}