123 lines
3.7 KiB
C#
123 lines
3.7 KiB
C#
using System;
|
|
using Input;
|
|
using Interactions;
|
|
using UnityEngine;
|
|
using AppleHills.Core.Settings; // Added for IInteractionSettings
|
|
|
|
/// <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, OnMenuConfirm, OnMenuCancel);
|
|
_isActive = false; // Prevent re-triggering until menu is closed
|
|
|
|
// Switch input mode to UI only
|
|
InputManager.Instance.SetInputMode(InputMode.UI);
|
|
}
|
|
|
|
private async void OnMenuConfirm()
|
|
{
|
|
var progress = new Progress<float>(p => Debug.Log($"Loading progress: {p * 100:F0}%"));
|
|
await SceneManagerService.Instance.SwitchSceneAsync(switchData.targetLevelSceneName, progress);
|
|
}
|
|
|
|
private void OnMenuCancel()
|
|
{
|
|
_isActive = true; // Allow interaction again if cancelled
|
|
}
|
|
}
|