Scriptname FXBirdFleeSCRIPT extends ObjectReference  
{Triggers birds to fly away}

; Eddoursul 2024.01.21: Use states and updates, added OnReset

sound property BirdFlockSound auto
sound property BirdFlockFleeSound auto
bool property PlayerTriggered = false auto
{check if player is only actor that can trigger - Default FALSE}

int instanceID00

;*************************************************

auto state Init
	Event OnCellAttach()
		RegisterForSingleUpdate(0.5)
	endEvent

	event OnUpdate()
		instanceID00 = BirdFlockSound.Play(Self)
		GotoState("Waiting")
	endevent
endstate

;*************************************************

State Waiting
	Event onTriggerEnter(ObjectReference akActionRef)
		if ! PlayerTriggered || (akActionRef as Actor && akActionRef == Game.GetForm(0x14))
			GotoState("Triggered")
			PlayAnimation("PlayAnim01")
			BirdFlockFleeSound.Play(Self)
			RegisterForSingleUpdate(0.5)
		endif
	endEvent
endState

;*************************************************

State Triggered
	event OnUpdate()
		GotoState("WaitForReset")
		Sound.StopInstance(instanceID00)
		instanceID00 = 0
	endevent
endState

;*************************************************

State WaitForReset
	event OnReset()
		GotoState("Init")
		; OnReset fires after onCellAttach()
		if Is3DLoaded()
			RegisterForSingleUpdate(0.5)
		endif
	endevent
endState

;*************************************************