252 lines
8.9 KiB
C#
252 lines
8.9 KiB
C#
using UnityEngine;
|
|
using AppleHills.Data.CardSystem;
|
|
using System.Collections.Generic;
|
|
using UnityEditor;
|
|
using Data.CardSystem;
|
|
using Core;
|
|
using UI.CardSystem;
|
|
using UnityEngine.UI;
|
|
|
|
namespace AppleHills.Tests
|
|
{
|
|
/// <summary>
|
|
/// Testing component for the Card System. Provides editor buttons to test core functionalities.
|
|
/// Place this in a test scene to easily test card system features without needing full game implementation.
|
|
/// </summary>
|
|
public class CardSystemTester : MonoBehaviour
|
|
{
|
|
[Header("References")]
|
|
[SerializeField] private CardAlbumUI cardAlbumUI;
|
|
|
|
[Header("Test Settings")]
|
|
[SerializeField] [Range(1, 10)] private int boosterPacksToAdd = 3;
|
|
[SerializeField] [Range(1, 100)] private int cardsToGenerate = 10;
|
|
[SerializeField] private bool autoOpenPacksWhenAdded = false;
|
|
|
|
[Header("Debug Info")]
|
|
[SerializeField] [ReadOnly] private int currentBoosterCount;
|
|
[SerializeField] [ReadOnly] private int totalCardsInCollection;
|
|
[SerializeField] [ReadOnly] private string lastActionMessage;
|
|
|
|
private void Awake()
|
|
{
|
|
// Auto-find references if needed
|
|
if (cardAlbumUI == null)
|
|
cardAlbumUI = FindAnyObjectByType<CardAlbumUI>();
|
|
|
|
// Log missing references
|
|
if (cardAlbumUI == null)
|
|
Debug.LogError("CardSystemTester: No CardAlbumUI found in the scene!");
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
RefreshDebugInfo();
|
|
}
|
|
|
|
// Refresh the debug information displayed in the inspector
|
|
private void RefreshDebugInfo()
|
|
{
|
|
// Access CardSystemManager through the singleton Instance
|
|
if (CardSystemManager.Instance != null)
|
|
{
|
|
currentBoosterCount = CardSystemManager.Instance.GetBoosterPackCount();
|
|
totalCardsInCollection = CardSystemManager.Instance.GetCardInventory().GetAllCards().Count;
|
|
}
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
// Custom editor buttons for testing
|
|
public void AddBoosterPacks()
|
|
{
|
|
// Access CardSystemManager through the singleton Instance
|
|
if (CardSystemManager.Instance != null)
|
|
{
|
|
CardSystemManager.Instance.AddBoosterPack(boosterPacksToAdd);
|
|
lastActionMessage = $"Added {boosterPacksToAdd} booster pack(s)";
|
|
Logging.Debug($"[CardSystemTester] {lastActionMessage}");
|
|
RefreshDebugInfo();
|
|
|
|
if (autoOpenPacksWhenAdded && cardAlbumUI != null)
|
|
{
|
|
SimulateBackpackClick();
|
|
cardAlbumUI.OpenBoosterPack();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SimulateBackpackClick()
|
|
{
|
|
if (cardAlbumUI != null)
|
|
{
|
|
// This will show the main card menu
|
|
// Manually trigger a click on the backpack icon
|
|
// Note: This relies on the backpack icon being correctly set up in the CardAlbumUI
|
|
if (cardAlbumUI.BackpackIcon != null)
|
|
{
|
|
Button backpackButton = cardAlbumUI.BackpackIcon.GetComponent<Button>();
|
|
if (backpackButton != null)
|
|
{
|
|
backpackButton.onClick.Invoke();
|
|
lastActionMessage = "Opened card menu via backpack click";
|
|
Logging.Debug($"[CardSystemTester] {lastActionMessage}");
|
|
}
|
|
else
|
|
{
|
|
lastActionMessage = "Failed to find Button component on backpack icon";
|
|
Logging.Warning($"[CardSystemTester] {lastActionMessage}");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
lastActionMessage = "BackpackIcon reference is null";
|
|
Logging.Warning($"[CardSystemTester] {lastActionMessage}");
|
|
}
|
|
}
|
|
}
|
|
|
|
public void OpenBoosterPack()
|
|
{
|
|
if (cardAlbumUI != null)
|
|
{
|
|
SimulateBackpackClick(); // First make sure we've opened the menu
|
|
cardAlbumUI.OpenBoosterPack();
|
|
lastActionMessage = "Opening booster pack";
|
|
Logging.Debug($"[CardSystemTester] {lastActionMessage}");
|
|
RefreshDebugInfo();
|
|
}
|
|
}
|
|
|
|
public void OpenAlbumView()
|
|
{
|
|
if (cardAlbumUI != null)
|
|
{
|
|
SimulateBackpackClick(); // First make sure we've opened the menu
|
|
cardAlbumUI.OpenAlbumView();
|
|
lastActionMessage = "Opening album view";
|
|
Logging.Debug($"[CardSystemTester] {lastActionMessage}");
|
|
}
|
|
}
|
|
|
|
public void GenerateRandomCards()
|
|
{
|
|
// Access CardSystemManager through the singleton Instance
|
|
if (CardSystemManager.Instance != null)
|
|
{
|
|
int cardsAdded = 0;
|
|
List<CardDefinition> allDefinitions = CardSystemManager.Instance.GetAllCardDefinitions();
|
|
|
|
if (allDefinitions.Count == 0)
|
|
{
|
|
lastActionMessage = "Error: No card definitions available";
|
|
Logging.Warning($"[CardSystemTester] {lastActionMessage}");
|
|
return;
|
|
}
|
|
|
|
for (int i = 0; i < cardsToGenerate; i++)
|
|
{
|
|
// Get a random card definition
|
|
CardDefinition randomDef = allDefinitions[Random.Range(0, allDefinitions.Count)];
|
|
|
|
// Create a card data instance and add it to inventory
|
|
CardData newCard = randomDef.CreateCardData();
|
|
CardSystemManager.Instance.GetCardInventory().AddCard(newCard);
|
|
cardsAdded++;
|
|
}
|
|
|
|
lastActionMessage = $"Generated {cardsAdded} random cards";
|
|
Logging.Debug($"[CardSystemTester] {lastActionMessage}");
|
|
RefreshDebugInfo();
|
|
}
|
|
}
|
|
|
|
public void ClearAllCards()
|
|
{
|
|
// Access CardSystemManager through the singleton Instance
|
|
if (CardSystemManager.Instance != null)
|
|
{
|
|
int count = CardSystemManager.Instance.GetCardInventory().GetAllCards().Count;
|
|
CardSystemManager.Instance.GetCardInventory().ClearAllCards();
|
|
lastActionMessage = $"Cleared {count} cards from inventory";
|
|
Logging.Debug($"[CardSystemTester] {lastActionMessage}");
|
|
RefreshDebugInfo();
|
|
}
|
|
}
|
|
#endif
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
[CustomEditor(typeof(CardSystemTester))]
|
|
public class CardSystemTesterEditor : Editor
|
|
{
|
|
public override void OnInspectorGUI()
|
|
{
|
|
DrawDefaultInspector();
|
|
|
|
CardSystemTester tester = (CardSystemTester)target;
|
|
|
|
EditorGUILayout.Space();
|
|
EditorGUILayout.LabelField("Test Actions", EditorStyles.boldLabel);
|
|
|
|
// Only enable buttons when in play mode
|
|
GUI.enabled = Application.isPlaying;
|
|
|
|
if (GUILayout.Button("Add Booster Packs"))
|
|
{
|
|
tester.AddBoosterPacks();
|
|
}
|
|
|
|
if (GUILayout.Button("Open Card Menu"))
|
|
{
|
|
tester.SimulateBackpackClick();
|
|
}
|
|
|
|
if (GUILayout.Button("Open Booster Pack"))
|
|
{
|
|
tester.OpenBoosterPack();
|
|
}
|
|
|
|
if (GUILayout.Button("Open Album View"))
|
|
{
|
|
tester.OpenAlbumView();
|
|
}
|
|
|
|
EditorGUILayout.Space();
|
|
|
|
if (GUILayout.Button("Generate Random Cards"))
|
|
{
|
|
tester.GenerateRandomCards();
|
|
}
|
|
|
|
if (GUILayout.Button("Clear All Cards"))
|
|
{
|
|
tester.ClearAllCards();
|
|
}
|
|
|
|
// If not in play mode, show a hint
|
|
if (!Application.isPlaying)
|
|
{
|
|
GUI.enabled = true;
|
|
EditorGUILayout.HelpBox("Enter Play Mode to use these testing functions.", MessageType.Info);
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
}
|
|
|
|
// Simple attribute to make fields read-only in the inspector
|
|
public class ReadOnlyAttribute : PropertyAttribute { }
|
|
|
|
#if UNITY_EDITOR
|
|
[CustomPropertyDrawer(typeof(ReadOnlyAttribute))]
|
|
public class ReadOnlyDrawer : PropertyDrawer
|
|
{
|
|
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
|
|
{
|
|
GUI.enabled = false;
|
|
EditorGUI.PropertyField(position, property, label, true);
|
|
GUI.enabled = true;
|
|
}
|
|
}
|
|
#endif
|