159 lines
4.5 KiB
C#
159 lines
4.5 KiB
C#
using UnityEngine;
|
|
using System;
|
|
using System.Collections;
|
|
using Core;
|
|
using Core.Lifecycle;
|
|
using Core.SaveLoad;
|
|
using UnityEngine.Audio;
|
|
|
|
public class PicnicBehaviour : ManagedBehaviour
|
|
{
|
|
private AppleMachine stateMachine;
|
|
private Animator animator;
|
|
|
|
[Header("The FakeChocolate to destroy!")]
|
|
[SerializeField] private GameObject fakeChocolate;
|
|
[SerializeField] private GameObject realChocolate;
|
|
|
|
private AppleAudioSource _audioSource;
|
|
public AudioResource distractedAudioClips;
|
|
public AudioResource angryAudioClips;
|
|
public AudioResource feederClips;
|
|
public AudioResource moanerClips;
|
|
|
|
// Save system configuration
|
|
public override bool AutoRegisterForSave => true;
|
|
|
|
// Runtime state tracking
|
|
private bool _fakeChocolateDestroyed;
|
|
private bool _isDistracted; // track current explicit state so it can be saved
|
|
|
|
internal override void OnManagedAwake()
|
|
{
|
|
stateMachine = GetComponent<AppleMachine>();
|
|
animator = GetComponent<Animator>();
|
|
_audioSource = GetComponent<AppleAudioSource>();
|
|
}
|
|
|
|
internal override void OnSceneRestoreCompleted()
|
|
{
|
|
if (_fakeChocolateDestroyed)
|
|
{
|
|
DestroyChocolateObjects();
|
|
}
|
|
else
|
|
{
|
|
// Restore the saved state (distracted or chilling) and wait for external control to change states.
|
|
if (_isDistracted)
|
|
EnterDistractedState();
|
|
else
|
|
EnterChillingState();
|
|
}
|
|
}
|
|
|
|
// Manual state control methods (replaces automatic timer-based switching)
|
|
public void EnterDistractedState()
|
|
{
|
|
if (stateMachine == null) stateMachine = GetComponent<AppleMachine>();
|
|
if (animator == null) animator = GetComponent<Animator>();
|
|
if (_audioSource == null) _audioSource = GetComponent<AppleAudioSource>();
|
|
|
|
_audioSource.Stop();
|
|
stateMachine.ChangeState("Picnic PPL Distracted");
|
|
animator.SetBool("theyDistracted", true);
|
|
_isDistracted = true;
|
|
}
|
|
|
|
public void EnterChillingState()
|
|
{
|
|
if (stateMachine == null) stateMachine = GetComponent<AppleMachine>();
|
|
if (animator == null) animator = GetComponent<Animator>();
|
|
if (_audioSource == null) _audioSource = GetComponent<AppleAudioSource>();
|
|
|
|
_audioSource.Stop();
|
|
stateMachine.ChangeState("Picnic PPL Chilling");
|
|
animator.SetBool("theyDistracted", false);
|
|
_isDistracted = false;
|
|
}
|
|
|
|
public void triedToStealChocolate()
|
|
{
|
|
_audioSource.Stop();
|
|
animator.SetTrigger("theyAngry");
|
|
Logging.Debug("Hey! Don't steal my chocolate!");
|
|
_audioSource.audioSource.resource = angryAudioClips;
|
|
_audioSource.Play(0);
|
|
}
|
|
|
|
public void destroyFakeChocolate()
|
|
{
|
|
_fakeChocolateDestroyed = true;
|
|
Destroy(fakeChocolate);
|
|
Destroy(realChocolate);
|
|
}
|
|
|
|
private void DestroyChocolateObjects()
|
|
{
|
|
if (fakeChocolate != null)
|
|
{
|
|
Destroy(fakeChocolate);
|
|
fakeChocolate = null;
|
|
}
|
|
|
|
if (realChocolate != null)
|
|
{
|
|
realChocolate.SetActive(true);
|
|
}
|
|
}
|
|
|
|
public void PlayFeederAudio()
|
|
{
|
|
_audioSource.audioSource.resource = feederClips;
|
|
_audioSource.Play(0);
|
|
}
|
|
|
|
public void PlayMoanerAudio()
|
|
{
|
|
_audioSource.audioSource.resource = moanerClips;
|
|
_audioSource.Play(0);
|
|
}
|
|
|
|
public void PlayDistractedAudio()
|
|
{
|
|
_audioSource.audioSource.resource = distractedAudioClips;
|
|
_audioSource.Play(0);
|
|
}
|
|
|
|
internal override string OnSceneSaveRequested()
|
|
{
|
|
var state = new PicnicBehaviourState { fakeChocolateDestroyed = _fakeChocolateDestroyed, isDistracted = _isDistracted };
|
|
return JsonUtility.ToJson(state);
|
|
}
|
|
|
|
internal override void OnSceneRestoreRequested(string serializedData)
|
|
{
|
|
if (string.IsNullOrEmpty(serializedData)) return;
|
|
|
|
try
|
|
{
|
|
var state = JsonUtility.FromJson<PicnicBehaviourState>(serializedData);
|
|
if (state != null)
|
|
{
|
|
_fakeChocolateDestroyed = state.fakeChocolateDestroyed;
|
|
_isDistracted = state.isDistracted;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.LogWarning($"[PicnicBehaviour] Failed to restore state: {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
|
|
[Serializable]
|
|
public class PicnicBehaviourState
|
|
{
|
|
public bool fakeChocolateDestroyed;
|
|
public bool isDistracted;
|
|
}
|