[Interactions] Make combinable object pickup-able and swappable
This commit is contained in:
@@ -10,11 +10,8 @@ public class LevelSwitch : MonoBehaviour
|
||||
/// Data for this level switch (target scene, icon, etc).
|
||||
/// </summary>
|
||||
public LevelSwitchData switchData;
|
||||
/// <summary>
|
||||
/// Renderer for the switch icon.
|
||||
/// </summary>
|
||||
public SpriteRenderer iconRenderer;
|
||||
private Interactable interactable;
|
||||
private SpriteRenderer _iconRenderer;
|
||||
private Interactable _interactable;
|
||||
|
||||
private bool _isActive = true;
|
||||
|
||||
@@ -24,12 +21,12 @@ public class LevelSwitch : MonoBehaviour
|
||||
void Awake()
|
||||
{
|
||||
_isActive = true;
|
||||
if (iconRenderer == null)
|
||||
iconRenderer = GetComponent<SpriteRenderer>();
|
||||
interactable = GetComponent<Interactable>();
|
||||
if (interactable != null)
|
||||
if (_iconRenderer == null)
|
||||
_iconRenderer = GetComponent<SpriteRenderer>();
|
||||
_interactable = GetComponent<Interactable>();
|
||||
if (_interactable != null)
|
||||
{
|
||||
interactable.StartedInteraction += OnStartedInteraction;
|
||||
_interactable.StartedInteraction += OnStartedInteraction;
|
||||
}
|
||||
ApplySwitchData();
|
||||
}
|
||||
@@ -39,9 +36,9 @@ public class LevelSwitch : MonoBehaviour
|
||||
/// </summary>
|
||||
void OnDestroy()
|
||||
{
|
||||
if (interactable != null)
|
||||
if (_interactable != null)
|
||||
{
|
||||
interactable.StartedInteraction -= OnStartedInteraction;
|
||||
_interactable.StartedInteraction -= OnStartedInteraction;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,8 +48,8 @@ public class LevelSwitch : MonoBehaviour
|
||||
/// </summary>
|
||||
void OnValidate()
|
||||
{
|
||||
if (iconRenderer == null)
|
||||
iconRenderer = GetComponent<SpriteRenderer>();
|
||||
if (_iconRenderer == null)
|
||||
_iconRenderer = GetComponent<SpriteRenderer>();
|
||||
ApplySwitchData();
|
||||
}
|
||||
#endif
|
||||
@@ -64,26 +61,49 @@ public class LevelSwitch : MonoBehaviour
|
||||
{
|
||||
if (switchData != null)
|
||||
{
|
||||
if (iconRenderer != null)
|
||||
iconRenderer.sprite = switchData.mapSprite;
|
||||
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 (switches the level if active).
|
||||
/// Handles the start of an interaction (shows confirmation menu and switches the level if confirmed).
|
||||
/// </summary>
|
||||
private async void OnStartedInteraction()
|
||||
private void OnStartedInteraction()
|
||||
{
|
||||
Debug.Log($"LevelSwitch.OnInteracted: Switching to level {switchData?.targetLevelSceneName}");
|
||||
if (switchData != null && !string.IsNullOrEmpty(switchData.targetLevelSceneName) && _isActive)
|
||||
if (switchData == null || string.IsNullOrEmpty(switchData.targetLevelSceneName) || !_isActive)
|
||||
return;
|
||||
|
||||
var menuPrefab = GameManager.Instance.LevelSwitchMenuPrefab;
|
||||
if (menuPrefab == null)
|
||||
{
|
||||
// Optionally: show loading UI here
|
||||
var progress = new Progress<float>(p => Debug.Log($"Loading progress: {p * 100:F0}%"));
|
||||
await SceneManagerService.Instance.SwitchSceneAsync(switchData.targetLevelSceneName, progress);
|
||||
_isActive = false;
|
||||
// Optionally: hide loading UI here
|
||||
Debug.LogError("LevelSwitchMenu prefab not assigned in GameSettings!");
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
55
Assets/Scripts/LevelS/LevelSwitchMenu.cs
Normal file
55
Assets/Scripts/LevelS/LevelSwitchMenu.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using TMPro;
|
||||
using System;
|
||||
|
||||
/// <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 TMP_Text descriptionText;
|
||||
public Button confirmButton;
|
||||
public Button cancelButton;
|
||||
|
||||
private Action _onConfirm;
|
||||
private Action _onCancel;
|
||||
private LevelSwitchData _switchData;
|
||||
|
||||
/// <summary>
|
||||
/// Initialize the menu with data and callbacks.
|
||||
/// </summary>
|
||||
public void Setup(LevelSwitchData switchData, Action onConfirm, Action onCancel)
|
||||
{
|
||||
_switchData = switchData;
|
||||
_onConfirm = onConfirm;
|
||||
_onCancel = onCancel;
|
||||
if (iconImage) iconImage.sprite = switchData?.mapSprite;
|
||||
if (levelNameText) levelNameText.text = switchData?.targetLevelSceneName ?? "";
|
||||
if (descriptionText) descriptionText.text = switchData?.description ?? "";
|
||||
if (confirmButton) confirmButton.onClick.AddListener(OnConfirmClicked);
|
||||
if (cancelButton) cancelButton.onClick.AddListener(OnCancelClicked);
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (confirmButton) confirmButton.onClick.RemoveListener(OnConfirmClicked);
|
||||
if (cancelButton) cancelButton.onClick.RemoveListener(OnCancelClicked);
|
||||
}
|
||||
|
||||
private void OnConfirmClicked()
|
||||
{
|
||||
_onConfirm?.Invoke();
|
||||
Destroy(gameObject);
|
||||
}
|
||||
|
||||
private void OnCancelClicked()
|
||||
{
|
||||
_onCancel?.Invoke();
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
3
Assets/Scripts/LevelS/LevelSwitchMenu.cs.meta
Normal file
3
Assets/Scripts/LevelS/LevelSwitchMenu.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 662ab024f9b9403f9184b71850bfce4f
|
||||
timeCreated: 1757335311
|
||||
Reference in New Issue
Block a user