Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Assets/_Project/Prefabs/Pet/Pet_Angel.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
_config: {fileID: 0}
_personality: {fileID: 0}
_personality: {fileID: 11400000, guid: 1ecede15733b42244a1305f7645463bd, type: 2}
_movementController: {fileID: 9100000, guid: ba46f117e79c2504888c585f53d1ecf8, type: 2}
_sideFramesFaceLeft: 1
_movementBounds: {fileID: 0}
Expand Down
2 changes: 1 addition & 1 deletion Assets/_Project/Prefabs/Pet/Pet_Devil.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
_config: {fileID: 0}
_personality: {fileID: 0}
_personality: {fileID: 11400000, guid: 844d776109915fc4092eeee43b3d825a, type: 2}
_movementController: {fileID: 9100000, guid: 49dff8841cae4cc7ae3247d5c623213b, type: 2}
_sideFramesFaceLeft: 1
_movementBounds: {fileID: 0}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,12 @@ public interface IPersonalityEvolutionService
/// 重复调用会覆盖。
/// </summary>
void SetInitialMatrix(PetId petId, PersonalityVector initial);

/// <summary>
/// 补种初始值:仅当该宠物尚无任何写入(含存档恢复)时才设置,否则忽略。
/// 用于编辑器 Play 流程中 Boot 场景晚于宠物场景加载、初始化事件被错过的兜底。
/// </summary>
/// <returns>true 表示本次实际写入了初始值;false 表示已存在(被跳过)。</returns>
bool SeedInitialMatrixIfAbsent(PetId petId, PersonalityMatrixSO? matrix);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,32 @@ private void Awake()
}

Debug.Log("[PersonalityEvolutionBootstrap] PersonalityEvolutionService registered.");
SeedInitialMatricesFromActivePets();
}

/// <summary>
/// 启动时补种初始矩阵。编辑器 Play 流程里 Boot 场景可能晚于宠物所在场景加载
/// (EditorBootSceneLoader 用 delayCall 附加加载 Boot),宠物 Awake 已发布的
/// <see cref="PetControllerInitializedEvent"/> 会在服务订阅之前被错过,导致
/// GetMatrix 一直返回全 0。此处扫描当前已激活的宠物补种;已通过正常事件路径
/// 或存档恢复写入的值会被 ContainsKey 守卫跳过,不会覆盖。
/// </summary>
private void SeedInitialMatricesFromActivePets()
{
if (_service == null) return;

var controllers = FindObjectsOfType<PetController>();
int seeded = 0;
foreach (var controller in controllers)
{
if (controller.InitialPersonalityMatrix is not { } matrix) continue;
if (_service.SeedInitialMatrixIfAbsent(controller.PetId, matrix)) seeded++;
}

if (seeded > 0)
{
Debug.Log($"[PersonalityEvolutionBootstrap] 启动补种初始性格: {seeded} 只宠物 (Boot 晚于宠物场景加载,事件已被错过)");
}
}

private void OnDestroy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ public void SetInitialMatrix(PetId petId, PersonalityVector initial)
MatrixChanged?.Invoke(petId, _matrices[petId]);
}

public bool SeedInitialMatrixIfAbsent(PetId petId, PersonalityMatrixSO? matrix)
{
// 存档恢复若已写入则不覆盖(与 OnPetControllerInitialized 的守卫一致)
if (_matrices.ContainsKey(petId)) return false;
SetInitialMatrix(petId, PersonalityVector.FromSO(matrix));
return true;
}

public void Dispose()
{
_tarotSub?.Dispose();
Expand Down Expand Up @@ -163,7 +171,7 @@ public string CaptureJson()
{
entries[i++] = new Entry { petId = (int)kv.Key, vector = kv.Value };
}
return JsonUtility.ToJson(new SavePayload { version = 1, entries = entries });
return JsonUtility.ToJson(new SavePayload { version = 2, entries = entries });
}

public bool RestoreJson(string json)
Expand All @@ -172,6 +180,18 @@ public bool RestoreJson(string json)
try
{
var payload = JsonUtility.FromJson<SavePayload>(json);

// v1 存档(初始矩阵接入修复前)是基于全 0 初始矩阵累计的:当时编辑器
// Play 时序让 PetControllerInitializedEvent 被错过,初始矩阵从未写入,
// 演化增量全部叠在 0 基数上,恢复它会覆盖启动补种写入的真实初始矩阵
// (雷达又变全 0 正多边形)。v1 一律忽略,由启动补种/宠物初始化事件
// 以真实 PersonalityMatrixSO 为基数重新建立。
if (payload.version < 2)
{
Debug.Log("[PersonalityEvolutionService] 忽略旧版性格存档(v1,0 基数累计,已由真实初始矩阵取代)");
return true;
}

_matrices.Clear();
if (payload.entries != null)
{
Expand Down
3 changes: 3 additions & 0 deletions Assets/_Project/Scripts/Modules/Pet/PetController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public sealed class PetController : MonoBehaviour

public PetId PetId => _petId;

/// <summary>初始性格矩阵(Inspector 绑定;可为空)。启动时由 PersonalityEvolutionBootstrap 补种读取。</summary>
public PersonalityMatrixSO? InitialPersonalityMatrix => _personality;

private PetContext? _context;
private StateMachine<PetContext>? _stateMachine;
private StatTickService? _tickService;
Expand Down
Loading