Rework of base interactables and managed behaviors

This commit is contained in:
Michal Pikulski
2025-11-04 11:11:27 +01:00
committed by Michal Pikulski
parent 00e1746ac4
commit f88bd0e2c9
60 changed files with 11175 additions and 1340 deletions

View File

@@ -6,8 +6,6 @@ using Interactions;
using System.Threading.Tasks;
using UnityEngine;
// Added for IInteractionSettings
namespace Levels
{
/// <summary>
@@ -15,40 +13,46 @@ namespace Levels
/// </summary>
public class LevelSwitch : InteractableBase
{
/// <summary>
/// Data for this level switch (target scene, icon, etc).
/// </summary>
public LevelSwitchData switchData;
private SpriteRenderer _iconRenderer;
// Settings reference
private IInteractionSettings _interactionSettings;
private bool switchActive = true;
private SpriteRenderer iconRenderer;
private IInteractionSettings interactionSettings;
/// <summary>
/// Unity Awake callback. Sets up icon, interactable, and event handlers.
/// </summary>
void Awake()
protected override void Awake()
{
switchActive = true;
if (_iconRenderer == null)
_iconRenderer = GetComponent<SpriteRenderer>();
base.Awake();
Debug.Log($"[LevelSwitch] Awake called for {gameObject.name} in scene {gameObject.scene.name}");
if (iconRenderer == null)
iconRenderer = GetComponent<SpriteRenderer>();
// Initialize settings reference
_interactionSettings = GameManager.GetSettingsObject<IInteractionSettings>();
interactionSettings = GameManager.GetSettingsObject<IInteractionSettings>();
ApplySwitchData();
}
protected override void OnManagedAwake()
{
Debug.Log($"[LevelSwitch] OnManagedAwake called for {gameObject.name}");
}
protected override void OnSceneReady()
{
Debug.Log($"[LevelSwitch] OnSceneReady called for {gameObject.name}");
}
#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>();
if (iconRenderer == null)
iconRenderer = GetComponent<SpriteRenderer>();
ApplySwitchData();
}
#endif
@@ -60,42 +64,48 @@ namespace Levels
{
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 (shows confirmation menu and switches the level if confirmed).
/// Main interaction logic: Spawn menu and switch input mode.
/// </summary>
protected override void OnCharacterArrived()
protected override bool DoInteraction()
{
if (switchData == null || string.IsNullOrEmpty(switchData.targetLevelSceneName) || !switchActive)
return;
if (switchData == null || string.IsNullOrEmpty(switchData.targetLevelSceneName))
{
Debug.LogWarning("LevelSwitch has no valid switchData!");
return false;
}
var menuPrefab = _interactionSettings?.LevelSwitchMenuPrefab;
var menuPrefab = interactionSettings?.LevelSwitchMenuPrefab;
if (menuPrefab == null)
{
Debug.LogError("LevelSwitchMenu prefab not assigned in InteractionSettings!");
return;
return false;
}
// Spawn the menu overlay (assume Canvas parent is handled in prefab setup)
// Spawn the menu overlay
var menuGo = Instantiate(menuPrefab);
var menu = menuGo.GetComponent<LevelSwitchMenu>();
if (menu == null)
{
Debug.LogError("LevelSwitchMenu component missing on prefab!");
Destroy(menuGo);
return;
return false;
}
// Setup menu with data and callbacks
menu.Setup(switchData, OnLevelSelectedWrapper, OnMinigameSelected, OnMenuCancel, OnRestartSelected);
switchActive = false; // Prevent re-triggering until menu is closed
// Switch input mode to UI only
InputManager.Instance.SetInputMode(InputMode.UI);
return true; // Menu spawned successfully
}
private void OnLevelSelectedWrapper()
@@ -123,7 +133,6 @@ namespace Levels
private void OnMenuCancel()
{
switchActive = true; // Allow interaction again if cancelled
InputManager.Instance.SetInputMode(InputMode.GameAndUI);
}
}