Further updates to state machines

This commit is contained in:
Michal Pikulski
2025-11-05 13:48:25 +01:00
parent 15fe4bdce6
commit 210823344a
25 changed files with 2209 additions and 4382 deletions

View File

@@ -221,7 +221,10 @@ namespace Core.Lifecycle
{
LogDebug($"Broadcasting ManagedAwake to {managedAwakeList.Count} components");
foreach (var component in managedAwakeList)
// Create a copy to avoid collection modification during iteration
var componentsCopy = new List<ManagedBehaviour>(managedAwakeList);
foreach (var component in componentsCopy)
{
if (component == null) continue;
@@ -337,7 +340,10 @@ namespace Core.Lifecycle
ProcessBatchedSceneComponents();
}
foreach (var component in sceneReadyList)
// Create a copy to avoid collection modification during iteration
var componentsCopy = new List<ManagedBehaviour>(sceneReadyList);
foreach (var component in componentsCopy)
{
if (component == null) continue;
@@ -364,7 +370,10 @@ namespace Core.Lifecycle
{
var saveData = new Dictionary<string, string>();
foreach (var component in managedAwakeList)
// Create a copy to avoid collection modification during iteration
var componentsCopy = new List<ManagedBehaviour>(managedAwakeList);
foreach (var component in componentsCopy)
{
if (component == null || !component.AutoRegisterForSave) continue;
@@ -396,7 +405,10 @@ namespace Core.Lifecycle
{
var saveData = new Dictionary<string, string>();
foreach (var component in managedAwakeList)
// Create a copy to avoid collection modification during iteration
var componentsCopy = new List<ManagedBehaviour>(managedAwakeList);
foreach (var component in componentsCopy)
{
if (component == null || !component.AutoRegisterForSave) continue;
@@ -429,7 +441,12 @@ namespace Core.Lifecycle
if (saveData == null) return;
int restoredCount = 0;
foreach (var component in managedAwakeList)
// Create a copy to avoid collection modification during iteration
// (components might destroy themselves during restoration)
var componentsCopy = new List<ManagedBehaviour>(managedAwakeList);
foreach (var component in componentsCopy)
{
if (component == null || !component.AutoRegisterForSave) continue;
@@ -461,7 +478,11 @@ namespace Core.Lifecycle
if (saveData == null) return;
int restoredCount = 0;
foreach (var component in managedAwakeList)
// Create a copy to avoid collection modification during iteration
var componentsCopy = new List<ManagedBehaviour>(managedAwakeList);
foreach (var component in componentsCopy)
{
if (component == null || !component.AutoRegisterForSave) continue;
@@ -492,7 +513,10 @@ namespace Core.Lifecycle
{
LogDebug("Broadcasting GlobalLoadCompleted");
foreach (var component in managedAwakeList)
// Create a copy to avoid collection modification during iteration
var componentsCopy = new List<ManagedBehaviour>(managedAwakeList);
foreach (var component in componentsCopy)
{
if (component == null || !component.AutoRegisterForSave) continue;
@@ -516,7 +540,10 @@ namespace Core.Lifecycle
{
LogDebug("Broadcasting GlobalSaveStarted");
foreach (var component in managedAwakeList)
// Create a copy to avoid collection modification during iteration
var componentsCopy = new List<ManagedBehaviour>(managedAwakeList);
foreach (var component in componentsCopy)
{
if (component == null || !component.AutoRegisterForSave) continue;

View File

@@ -1,4 +1,4 @@
using System.Collections;
using System.Collections;
using AppleHills.Core.Settings;
using Core.Lifecycle;
using Settings;
@@ -36,6 +36,12 @@ namespace Core
protected override void OnManagedAwake()
{
// Subscribe to SceneManagerService to enforce orientation on every scene load
if (SceneManagerService.Instance != null)
{
SceneManagerService.Instance.SceneLoadCompleted += OnSceneLoadCompleted;
}
#if UNITY_EDITOR
// When playing in the editor, manually invoke orientation check for the currently active scene
if (Application.isPlaying)
@@ -47,11 +53,17 @@ namespace Core
protected override void OnSceneReady()
{
// Handle orientation when scene is ready
// Handle orientation when scene is ready (initial scene)
// Note: This fires for the scene that just loaded, LifecycleManager tracks which scene
string sceneName = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;
HandleSceneOrientation(sceneName);
}
private void OnSceneLoadCompleted(string sceneName)
{
// Enforce orientation every time a scene is loaded via SceneManagerService
HandleSceneOrientation(sceneName);
}
private void HandleSceneOrientation(string sceneName)
{
@@ -95,6 +107,12 @@ namespace Core
protected override void OnDestroy()
{
// Unsubscribe from events to prevent memory leaks
if (SceneManagerService.Instance != null)
{
SceneManagerService.Instance.SceneLoadCompleted -= OnSceneLoadCompleted;
}
base.OnDestroy(); // Important: call base
}

View File

@@ -1,5 +1,6 @@
using System.Collections.Generic;
using UnityEngine;
using Interactions;
namespace AppleHills.Core.Settings
{
@@ -88,5 +89,31 @@ namespace AppleHills.Core.Settings
}
return null;
}
/// <summary>
/// Finds a pickup prefab by its itemData.itemId.
/// Searches through combination rules to find result prefabs.
/// Used to spawn dynamically created items during save/load.
/// </summary>
public GameObject FindPickupPrefabByItemId(string itemId)
{
if (string.IsNullOrEmpty(itemId) || combinationRules == null)
return null;
// Search through combination rules to find a result prefab with matching itemId
foreach (var rule in combinationRules)
{
if (rule.resultPrefab != null)
{
var pickup = rule.resultPrefab.GetComponent<Pickup>();
if (pickup != null && pickup.itemData != null && pickup.itemData.itemId == itemId)
{
return rule.resultPrefab;
}
}
}
return null;
}
}
}

View File

@@ -57,6 +57,7 @@ namespace AppleHills.Core.Settings
// Methods to query item configurations
CombinationRule GetCombinationRule(PickupItemData item1, PickupItemData item2);
SlotItemConfig GetSlotItemConfig(PickupItemData slotItem);
GameObject FindPickupPrefabByItemId(string itemId);
}
/// <summary>