Big script cleanup. Remove the examples from Ropes' external package
This commit is contained in:
89
Assets/Scripts/LevelS/LevelSwitch.cs
Normal file
89
Assets/Scripts/LevelS/LevelSwitch.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
/// <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;
|
||||
/// <summary>
|
||||
/// Renderer for the switch icon.
|
||||
/// </summary>
|
||||
public SpriteRenderer iconRenderer;
|
||||
private Interactable interactable;
|
||||
|
||||
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.StartedInteraction += OnStartedInteraction;
|
||||
}
|
||||
ApplySwitchData();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unity OnDestroy callback. Cleans up event handlers.
|
||||
/// </summary>
|
||||
void OnDestroy()
|
||||
{
|
||||
if (interactable != null)
|
||||
{
|
||||
interactable.StartedInteraction -= OnStartedInteraction;
|
||||
}
|
||||
}
|
||||
|
||||
#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 (switches the level if active).
|
||||
/// </summary>
|
||||
private async void OnStartedInteraction()
|
||||
{
|
||||
Debug.Log($"LevelSwitch.OnInteracted: Switching to level {switchData?.targetLevelSceneName}");
|
||||
if (switchData != null && !string.IsNullOrEmpty(switchData.targetLevelSceneName) && _isActive)
|
||||
{
|
||||
// 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
|
||||
}
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/LevelS/LevelSwitch.cs.meta
Normal file
3
Assets/Scripts/LevelS/LevelSwitch.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 66cdab2e217c4c8388e2fc66da02f296
|
||||
timeCreated: 1756733777
|
||||
24
Assets/Scripts/LevelS/LevelSwitchData.cs
Normal file
24
Assets/Scripts/LevelS/LevelSwitchData.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// ScriptableObject holding data for a level switch (scene name, description, icon).
|
||||
/// </summary>
|
||||
[CreateAssetMenu(fileName = "LevelSwitchData", menuName = "Game/Level Switch Data")]
|
||||
public class LevelSwitchData : ScriptableObject
|
||||
{
|
||||
/// <summary>
|
||||
/// The name of the target scene to switch to.
|
||||
/// </summary>
|
||||
public string targetLevelSceneName;
|
||||
|
||||
/// <summary>
|
||||
/// Description of the level switch.
|
||||
/// </summary>
|
||||
[TextArea]
|
||||
public string description;
|
||||
|
||||
/// <summary>
|
||||
/// Icon to display for this level switch.
|
||||
/// </summary>
|
||||
public Sprite mapSprite;
|
||||
}
|
||||
3
Assets/Scripts/LevelS/LevelSwitchData.cs.meta
Normal file
3
Assets/Scripts/LevelS/LevelSwitchData.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 451d08ffb9ed459db0f0473f2275743b
|
||||
timeCreated: 1756733909
|
||||
Reference in New Issue
Block a user