Cleanup compile warnings, cleanup logs, spruce up level selection menu

This commit is contained in:
Michal Pikulski
2025-10-28 14:31:17 +01:00
parent a5b1a4f8a0
commit 43779c560e
67 changed files with 4814 additions and 1050 deletions

View File

@@ -0,0 +1,147 @@
using System;
using AppleHills.Core.Settings;
using Core;
using Input;
using Interactions;
using System.Threading.Tasks;
using UnityEngine;
// Added for IInteractionSettings
namespace Levels
{
/// <summary>
/// Handles level switching when interacted with. Applies switch data and triggers scene transitions.
/// </summary>
public class LevelSwitch : MonoBehaviour
{
/// <summary>
/// Data for this level switch (target scene, icon, etc).
/// </summary>
public LevelSwitchData switchData;
private SpriteRenderer _iconRenderer;
private Interactable _interactable;
// Settings reference
private IInteractionSettings _interactionSettings;
private bool _isActive = true;
/// <summary>
/// Unity Awake callback. Sets up icon, interactable, and event handlers.
/// </summary>
void Awake()
{
_isActive = true;
if (_iconRenderer == null)
_iconRenderer = GetComponent<SpriteRenderer>();
_interactable = GetComponent<Interactable>();
if (_interactable != null)
{
_interactable.characterArrived.AddListener(OnCharacterArrived);
}
// Initialize settings reference
_interactionSettings = GameManager.GetSettingsObject<IInteractionSettings>();
ApplySwitchData();
}
/// <summary>
/// Unity OnDestroy callback. Cleans up event handlers.
/// </summary>
void OnDestroy()
{
if (_interactable != null)
{
_interactable.characterArrived.RemoveListener(OnCharacterArrived);
}
}
#if UNITY_EDITOR
/// <summary>
/// Unity OnValidate callback. Ensures icon and data are up to date in editor.
/// </summary>
void OnValidate()
{
if (_iconRenderer == null)
_iconRenderer = GetComponent<SpriteRenderer>();
ApplySwitchData();
}
#endif
/// <summary>
/// Applies the switch data to the level switch (icon, name, etc).
/// </summary>
public void ApplySwitchData()
{
if (switchData != null)
{
if (_iconRenderer != null)
_iconRenderer.sprite = switchData.mapSprite;
gameObject.name = switchData.targetLevelSceneName;
// Optionally update other fields, e.g. description
}
}
/// <summary>
/// Handles the start of an interaction (shows confirmation menu and switches the level if confirmed).
/// </summary>
private void OnCharacterArrived()
{
if (switchData == null || string.IsNullOrEmpty(switchData.targetLevelSceneName) || !_isActive)
return;
var menuPrefab = _interactionSettings?.LevelSwitchMenuPrefab;
if (menuPrefab == null)
{
Debug.LogError("LevelSwitchMenu prefab not assigned in InteractionSettings!");
return;
}
// Spawn the menu overlay (assume Canvas parent is handled in prefab setup)
var menuGo = Instantiate(menuPrefab);
var menu = menuGo.GetComponent<LevelSwitchMenu>();
if (menu == null)
{
Debug.LogError("LevelSwitchMenu component missing on prefab!");
Destroy(menuGo);
return;
}
// Setup menu with data and callbacks
menu.Setup(switchData, OnLevelSelectedWrapper, OnMinigameSelected, OnMenuCancel, OnRestartSelected);
_isActive = false; // Prevent re-triggering until menu is closed
// Switch input mode to UI only
InputManager.Instance.SetInputMode(InputMode.UI);
}
private void OnLevelSelectedWrapper()
{
_ = OnLevelSelected();
}
private async Task OnLevelSelected()
{
var progress = new Progress<float>(p => Logging.Debug($"Loading progress: {p * 100:F0}%"));
await SceneManagerService.Instance.SwitchSceneAsync(switchData.targetLevelSceneName, progress);
}
private async void OnMinigameSelected()
{
var progress = new Progress<float>(p => Logging.Debug($"Loading progress: {p * 100:F0}%"));
await SceneManagerService.Instance.SwitchSceneAsync(switchData.targetMinigameSceneName, progress);
}
private async void OnRestartSelected()
{
// TODO: Restart level here
await OnLevelSelected();
}
private void OnMenuCancel()
{
_isActive = true; // Allow interaction again if cancelled
InputManager.Instance.SetInputMode(InputMode.GameAndUI);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 66cdab2e217c4c8388e2fc66da02f296
timeCreated: 1756733777

View File

@@ -0,0 +1,32 @@
using UnityEngine;
namespace Levels
{
/// <summary>
/// ScriptableObject holding data for a level switch (scene name, description, icon).
/// </summary>
[CreateAssetMenu(fileName = "LevelSwitchData", menuName = "AppleHills/Items & Puzzles/Level Switch Data")]
public class LevelSwitchData : ScriptableObject
{
/// <summary>
/// The name of the target scene to switch to.
/// </summary>
public string targetLevelSceneName;
/// <summary>
/// The name of the minigame scene to switch to.
/// </summary>
public string targetMinigameSceneName;
/// <summary>
/// Description of the level switch.
/// </summary>
[TextArea]
public string description;
/// <summary>
/// Icon to display for this level switch.
/// </summary>
public Sprite mapSprite;
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 451d08ffb9ed459db0f0473f2275743b
timeCreated: 1756733909

View File

@@ -0,0 +1,110 @@
using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace Levels
{
/// <summary>
/// UI overlay for confirming a level switch. Displays level info and handles confirm/cancel actions.
/// </summary>
public class LevelSwitchMenu : MonoBehaviour
{
[Header("UI References")]
public Image iconImage;
public TMP_Text levelNameText;
public Button confirmButton;
public Button cancelButton;
public Button minigameButton;
public GameObject padlockIcon;
public Button restartButton;
public GameObject popupConfirmMenu;
public Button popupConfirmButton;
public Button popupCancelButton;
public Image tintTargetImage;
public Color popupTintColor = new Color(0.5f, 0.5f, 0.5f, 1f); // grey by default
private Color _originalTintColor;
private Action _onRestart;
private Action _onLevelConfirm;
private Action _onMinigameConfirm;
private Action _onCancel;
private LevelSwitchData _switchData;
/// <summary>
/// Initialize the menu with data and callbacks.
/// </summary>
public void Setup(LevelSwitchData switchData, Action onLevelConfirm, Action onMinigameConfirm, Action onCancel, Action onRestart = null)
{
_switchData = switchData;
_onLevelConfirm = onLevelConfirm;
_onMinigameConfirm = onMinigameConfirm;
_onCancel = onCancel;
_onRestart = onRestart;
if (iconImage) iconImage.sprite = switchData?.mapSprite;
if (levelNameText) levelNameText.text = switchData?.targetLevelSceneName ?? "";
if (confirmButton) confirmButton.onClick.AddListener(OnConfirmClicked);
if (cancelButton) cancelButton.onClick.AddListener(OnCancelClicked);
if (minigameButton)
{
minigameButton.onClick.AddListener(OnMinigameClicked);
bool minigameUnlocked = true;
minigameButton.interactable = !minigameUnlocked;
padlockIcon.SetActive(!minigameUnlocked);
}
if (restartButton) restartButton.onClick.AddListener(OnRestartClicked);
if (popupConfirmMenu) popupConfirmMenu.SetActive(false);
if (tintTargetImage) _originalTintColor = tintTargetImage.color;
if (popupConfirmButton) popupConfirmButton.onClick.AddListener(OnPopupConfirmClicked);
if (popupCancelButton) popupCancelButton.onClick.AddListener(OnPopupCancelClicked);
}
private void OnDestroy()
{
if (confirmButton) confirmButton.onClick.RemoveListener(OnConfirmClicked);
if (cancelButton) cancelButton.onClick.RemoveListener(OnCancelClicked);
if (minigameButton) minigameButton.onClick.RemoveListener(OnMinigameClicked);
if (restartButton) restartButton.onClick.RemoveListener(OnRestartClicked);
if (popupConfirmButton) popupConfirmButton.onClick.RemoveListener(OnPopupConfirmClicked);
if (popupCancelButton) popupCancelButton.onClick.RemoveListener(OnPopupCancelClicked);
}
private void OnConfirmClicked()
{
_onLevelConfirm?.Invoke();
Destroy(gameObject);
}
private void OnMinigameClicked()
{
_onMinigameConfirm?.Invoke();
Destroy(gameObject);
}
private void OnCancelClicked()
{
_onCancel?.Invoke();
Destroy(gameObject);
}
private void OnRestartClicked()
{
if (popupConfirmMenu) popupConfirmMenu.SetActive(true);
if (tintTargetImage) tintTargetImage.color = popupTintColor;
}
private void OnPopupCancelClicked()
{
if (popupConfirmMenu) popupConfirmMenu.SetActive(false);
if (tintTargetImage) tintTargetImage.color = _originalTintColor;
}
private void OnPopupConfirmClicked()
{
_onRestart?.Invoke();
if (popupConfirmMenu) popupConfirmMenu.SetActive(false);
if (tintTargetImage) tintTargetImage.color = _originalTintColor;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 662ab024f9b9403f9184b71850bfce4f
timeCreated: 1757335311