[Interactions] Make combinable object pickup-able and swappable

This commit is contained in:
Michal Pikulski
2025-09-08 14:56:59 +02:00
parent a1b1d7475f
commit f4a183d524
7 changed files with 138 additions and 30 deletions

View File

@@ -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
}
}