2025-09-01 22:51:52 +02:00
|
|
|
|
using System;
|
2025-10-14 04:56:00 +00:00
|
|
|
|
using AppleHills.Core.Settings;
|
|
|
|
|
|
using Core;
|
2025-09-11 13:00:26 +02:00
|
|
|
|
using Input;
|
|
|
|
|
|
using Interactions;
|
2025-10-28 14:31:17 +01:00
|
|
|
|
using System.Threading.Tasks;
|
2025-09-01 22:51:52 +02:00
|
|
|
|
using UnityEngine;
|
2025-09-01 16:14:21 +02:00
|
|
|
|
|
2025-10-28 14:31:17 +01:00
|
|
|
|
namespace Levels
|
2025-09-01 16:14:21 +02:00
|
|
|
|
{
|
2025-09-06 21:01:54 +02:00
|
|
|
|
/// <summary>
|
2025-10-14 04:56:00 +00:00
|
|
|
|
/// Handles level switching when interacted with. Applies switch data and triggers scene transitions.
|
2025-09-06 21:01:54 +02:00
|
|
|
|
/// </summary>
|
Refactoring of the interaction system and preliminary integration of save/load functionality across the game. (#44)
### Interactables Architecture Refactor
- Converted composition to inheritance, moved from component-based to class-based interactables. No more requirement for chain of "Interactable -> Item" etc.
- Created `InteractableBase` abstract base class with common functionality that replaces the old component
- Specialized child classes: `Pickup`, `ItemSlot`, `LevelSwitch`, `MinigameSwitch`, `CombinationItem`, `OneClickInteraction` are now children classes
- Light updates to the interactable inspector, moved some things arround, added collapsible inspector sections in the UI for better editor experience
### State Machine Integration
- Custom `AppleMachine` inheritong from Pixelplacement's StateMachine which implements our own interface for saving, easy place for future improvements
- Replaced all previous StateMachines by `AppleMachine`
- Custom `AppleState` extends from default `State`. Added serialization, split state logic into "EnterState", "RestoreState", "ExitState" allowing for separate logic when triggering in-game vs loading game
- Restores directly to target state without triggering transitional logic
- Migration tool converts existing instances
### Prefab Organization
- Saved changes from scenes into prefabs
- Cleaned up duplicated components, confusing prefabs hierarchies
- Created prefab variants where possible
- Consolidated Environment prefabs and moved them out of Placeholders subfolder into main Environment folder
- Organized item prefabs from PrefabsPLACEHOLDER into proper Items folder
- Updated prefab references - All scene references updated to new locations
- Removed placeholder files from Characters, Levels, UI, and Minigames folders
### Scene Updates
- Quarry scene with major updates
- Saved multiple working versions (Quarry, Quarry_Fixed, Quarry_OLD)
- Added proper lighting data
- Updated all interactable components to new architecture
### Minor editor tools
- New tool for testing cards from an editor window (no in-scene object required)
- Updated Interactable Inspector
- New debug option to opt in-and-out of the save/load system
- Tooling for easier migration
Co-authored-by: Michal Pikulski <michal.a.pikulski@gmail.com>
Reviewed-on: https://homelab.tailf7f81b.ts.net/tschesky/AppleHillsProduction/pulls/44
2025-11-03 10:12:51 +00:00
|
|
|
|
public class LevelSwitch : InteractableBase
|
2025-10-14 04:56:00 +00:00
|
|
|
|
{
|
|
|
|
|
|
public LevelSwitchData switchData;
|
|
|
|
|
|
private SpriteRenderer _iconRenderer;
|
|
|
|
|
|
private IInteractionSettings _interactionSettings;
|
2025-11-07 15:38:31 +00:00
|
|
|
|
private GameObject _menuObjectRef;
|
2025-09-06 21:01:54 +02:00
|
|
|
|
|
2025-10-14 04:56:00 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Unity Awake callback. Sets up icon, interactable, and event handlers.
|
|
|
|
|
|
/// </summary>
|
2025-11-07 15:38:31 +00:00
|
|
|
|
protected override void Awake()
|
2025-09-01 16:14:21 +02:00
|
|
|
|
{
|
2025-11-07 15:38:31 +00:00
|
|
|
|
base.Awake();
|
|
|
|
|
|
|
2025-11-10 13:03:36 +01:00
|
|
|
|
Logging.Debug($"[LevelSwitch] Awake called for {gameObject.name} in scene {gameObject.scene.name}");
|
2025-11-07 15:38:31 +00:00
|
|
|
|
|
2025-10-14 04:56:00 +00:00
|
|
|
|
if (_iconRenderer == null)
|
|
|
|
|
|
_iconRenderer = GetComponent<SpriteRenderer>();
|
2025-10-07 10:44:26 +02:00
|
|
|
|
|
2025-10-14 04:56:00 +00:00
|
|
|
|
// Initialize settings reference
|
|
|
|
|
|
_interactionSettings = GameManager.GetSettingsObject<IInteractionSettings>();
|
2025-10-07 10:44:26 +02:00
|
|
|
|
|
2025-10-14 04:56:00 +00:00
|
|
|
|
ApplySwitchData();
|
|
|
|
|
|
}
|
2025-09-01 16:14:21 +02:00
|
|
|
|
|
2025-11-07 15:38:31 +00:00
|
|
|
|
protected override void OnManagedAwake()
|
|
|
|
|
|
{
|
2025-11-10 13:03:36 +01:00
|
|
|
|
Logging.Debug($"[LevelSwitch] OnManagedAwake called for {gameObject.name}");
|
2025-11-07 15:38:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void OnSceneReady()
|
|
|
|
|
|
{
|
2025-11-10 13:03:36 +01:00
|
|
|
|
Logging.Debug($"[LevelSwitch] OnSceneReady called for {gameObject.name}");
|
2025-11-07 15:38:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-01 16:14:21 +02:00
|
|
|
|
#if UNITY_EDITOR
|
2025-10-14 04:56:00 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Unity OnValidate callback. Ensures icon and data are up to date in editor.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
void OnValidate()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_iconRenderer == null)
|
|
|
|
|
|
_iconRenderer = GetComponent<SpriteRenderer>();
|
|
|
|
|
|
ApplySwitchData();
|
|
|
|
|
|
}
|
2025-09-01 16:14:21 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
|
2025-10-14 04:56:00 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Applies the switch data to the level switch (icon, name, etc).
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void ApplySwitchData()
|
2025-09-01 16:14:21 +02:00
|
|
|
|
{
|
2025-10-14 04:56:00 +00:00
|
|
|
|
if (switchData != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_iconRenderer != null)
|
|
|
|
|
|
_iconRenderer.sprite = switchData.mapSprite;
|
|
|
|
|
|
gameObject.name = switchData.targetLevelSceneName;
|
|
|
|
|
|
// Optionally update other fields, e.g. description
|
|
|
|
|
|
}
|
2025-09-01 16:14:21 +02:00
|
|
|
|
}
|
2025-11-07 15:38:31 +00:00
|
|
|
|
|
2025-10-14 04:56:00 +00:00
|
|
|
|
/// <summary>
|
2025-11-07 15:38:31 +00:00
|
|
|
|
/// Main interaction logic: Spawn menu and switch input mode.
|
2025-10-14 04:56:00 +00:00
|
|
|
|
/// </summary>
|
2025-11-07 15:38:31 +00:00
|
|
|
|
protected override bool DoInteraction()
|
2025-09-01 22:51:52 +02:00
|
|
|
|
{
|
2025-11-07 15:38:31 +00:00
|
|
|
|
if (switchData == null || string.IsNullOrEmpty(switchData.targetLevelSceneName))
|
|
|
|
|
|
{
|
2025-11-10 13:03:36 +01:00
|
|
|
|
Logging.Warning("LevelSwitch has no valid switchData!");
|
2025-11-07 15:38:31 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2025-10-14 04:56:00 +00:00
|
|
|
|
|
|
|
|
|
|
var menuPrefab = _interactionSettings?.LevelSwitchMenuPrefab;
|
|
|
|
|
|
if (menuPrefab == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogError("LevelSwitchMenu prefab not assigned in InteractionSettings!");
|
2025-11-07 15:38:31 +00:00
|
|
|
|
return false;
|
2025-10-14 04:56:00 +00:00
|
|
|
|
}
|
2025-11-07 15:38:31 +00:00
|
|
|
|
|
|
|
|
|
|
// Spawn the menu overlay
|
|
|
|
|
|
_menuObjectRef = Instantiate(menuPrefab);
|
|
|
|
|
|
var menu = _menuObjectRef.GetComponent<LevelSwitchMenu>();
|
2025-10-14 04:56:00 +00:00
|
|
|
|
if (menu == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Debug.LogError("LevelSwitchMenu component missing on prefab!");
|
2025-11-07 15:38:31 +00:00
|
|
|
|
Destroy(_menuObjectRef);
|
|
|
|
|
|
return false;
|
2025-10-14 04:56:00 +00:00
|
|
|
|
}
|
2025-11-07 15:38:31 +00:00
|
|
|
|
|
2025-10-14 04:56:00 +00:00
|
|
|
|
// Setup menu with data and callbacks
|
2025-10-28 14:31:17 +01:00
|
|
|
|
menu.Setup(switchData, OnLevelSelectedWrapper, OnMinigameSelected, OnMenuCancel, OnRestartSelected);
|
2025-11-07 15:38:31 +00:00
|
|
|
|
|
2025-10-14 04:56:00 +00:00
|
|
|
|
// Switch input mode to UI only
|
|
|
|
|
|
InputManager.Instance.SetInputMode(InputMode.UI);
|
2025-11-07 15:38:31 +00:00
|
|
|
|
|
|
|
|
|
|
return true; // Menu spawned successfully
|
2025-09-01 22:51:52 +02:00
|
|
|
|
}
|
2025-10-14 04:56:00 +00:00
|
|
|
|
|
2025-10-28 14:31:17 +01:00
|
|
|
|
private void OnLevelSelectedWrapper()
|
|
|
|
|
|
{
|
|
|
|
|
|
_ = OnLevelSelected();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task OnLevelSelected()
|
2025-09-08 14:56:59 +02:00
|
|
|
|
{
|
2025-10-14 15:53:58 +02:00
|
|
|
|
var progress = new Progress<float>(p => Logging.Debug($"Loading progress: {p * 100:F0}%"));
|
2025-10-14 04:56:00 +00:00
|
|
|
|
await SceneManagerService.Instance.SwitchSceneAsync(switchData.targetLevelSceneName, progress);
|
2025-09-08 14:56:59 +02:00
|
|
|
|
}
|
2025-10-28 14:31:17 +01:00
|
|
|
|
|
|
|
|
|
|
private async void OnMinigameSelected()
|
|
|
|
|
|
{
|
|
|
|
|
|
var progress = new Progress<float>(p => Logging.Debug($"Loading progress: {p * 100:F0}%"));
|
|
|
|
|
|
await SceneManagerService.Instance.SwitchSceneAsync(switchData.targetMinigameSceneName, progress);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async void OnRestartSelected()
|
|
|
|
|
|
{
|
2025-11-07 15:38:31 +00:00
|
|
|
|
// Clear all save data for the target level before reloading
|
|
|
|
|
|
if (Core.SaveLoad.SaveLoadManager.Instance != null && !string.IsNullOrEmpty(switchData?.targetLevelSceneName))
|
|
|
|
|
|
{
|
|
|
|
|
|
Core.SaveLoad.SaveLoadManager.Instance.ClearLevelData(switchData.targetLevelSceneName);
|
|
|
|
|
|
Logging.Debug($"[LevelSwitch] Cleared save data for level: {switchData.targetLevelSceneName}");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Now reload the level with fresh state - skipSave=true prevents re-saving cleared data
|
|
|
|
|
|
var progress = new Progress<float>(p => Logging.Debug($"Loading progress: {p * 100:F0}%"));
|
|
|
|
|
|
await SceneManagerService.Instance.SwitchSceneAsync(switchData.targetLevelSceneName, progress, autoHideLoadingScreen: true, skipSave: true);
|
2025-10-28 14:31:17 +01:00
|
|
|
|
}
|
2025-09-08 14:56:59 +02:00
|
|
|
|
|
2025-10-14 04:56:00 +00:00
|
|
|
|
private void OnMenuCancel()
|
|
|
|
|
|
{
|
2025-10-14 07:09:16 +00:00
|
|
|
|
InputManager.Instance.SetInputMode(InputMode.GameAndUI);
|
2025-10-14 04:56:00 +00:00
|
|
|
|
}
|
2025-09-01 16:14:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|