Scriptname _00E_MQ11c_MinigameShootingSphere extends ObjectReference  

Import Utility

;=====================================================================================
;              							 STATES                  					 
;=====================================================================================

;-----------------------------AUTO STATE-------------------

Auto State WaitingForFirstActivation 									; State the sphere will automatically be set to once initialized

Event OnActivate(ObjectReference akActionRef)
	if (akActionRef == SphereButton01) && (MQ11c.GetCurrentStageID() == 150)		; Have the sphere levitate upwards a little and play a cool sound once Rynéus hits the switch!
		NPCDwarvenSphereOpen.Play(Self)
		Move(0.0, 0.0, 170, 65, 50)	
		SphereInactiveLoop = QSTDA02ShrineActivateLPMMarker.Play(Self)
		Wait(3)
		MQ11c.SetCurrentStageID(155)
		goToState("Pending")											; Go to the pending state and wait for input																		
	EndIf
EndEvent

EndState

;------------------------------PENDING STATE----------------

State Pending

Event OnActivate(ObjectReference akActionRef)

	if (GameRunning == False) && (akActionRef == SphereButton02)
		PlayerREF.EquipItem(_00E_MQ11c_SphereArrow, False, True)		; Have the player equip a bow and draw it
		PlayerREF.EquipItem(_00E_MQ11c_SphereBow, False, True)
		PlayerREF.DrawWeapon()
		Sound.StopInstance(SphereInactiveLoop)
		Self.PlayAnimation("Open")											; Play opening animation and sound								
		_00E_NPCDwarvenSphereScanM.Play(Self)
		MakeRyneusLookAtSphere()
		Wait(3)
		MakeRyneusLookAtSphere()
		SphereLoop = _00E_NPCDwarvenSphereConsciousLPM.Play(Self)
		_00E_MQ11c_Sphere_GameRunning.SetValue(1)
		SphereAngleX = Self.GetAngleX()
		SphereAngleY = Self.GetAngleY()
		SphereAngleZ = Self.GetAngleZ()
		SphereOffsetX = Self.GetPositionX()
		SphereOffsetY = Self.GetPositionY()
		SphereOffsetZ = Self.GetPositionZ()	
		Phase = 0
		GameRunning = True
		if StartRound == 1
			GoToState("RoundOne")												; Determine which movement pattern to follow
			; Debug.Trace("Moving to round 1 state")
		ElseIf StartRound == 2
			GoToState("RoundTwo")
			; Debug.Trace("Moving to round 2 state")
		ElseIf StartRound == 3
			GoToState("RoundThree")
			; Debug.Trace("Moving to round 3 state")
		EndIf
	ElseIf GameRunning == True
		if StartRound == 1
			GoToState("RoundOne")
			; Debug.Trace("Moving to round 1 state")
		ElseIf StartRound == 2
			GoToState("RoundTwo")
			; Debug.Trace("Moving to round 2 state")
		ElseIf StartRound == 3
			GoToState("RoundThree")
			; Debug.Trace("Moving to round 3 state")
		EndIf
		If MQ11c.GetCurrentStageID() < 175
			MakeRyneusLookAtSphere()
		EndIf
	EndIf

EndEvent

EndState

;----------------------------RESTART STATE-----------------------------------------------

State Restart					

Event OnBeginState()												; Make sure we still want to run the game - If it is, have Rynéus hit the switch again and thus raise the sphere level, if not, have Rynéus forcegreet the player to initiate the end-minigame dialogue

	Actor akRyneus = Ryneus.GetActorReference()

	_00E_MQ11c_Sphere_NextStaqge.SetValue(_00E_MQ11c_Sphere_NextStaqge.GetValue() + 1)
	if (_00E_MQ11c_Sphere_NextStaqge.GetValueInt() <= 2)
		akRyneus.ClearLookAt()
		MQ11c_SC7_RyneusRaiseSphereLevel.Start()
	ElseIf (_00E_MQ11c_Sphere_NextStaqge.GetValueInt() == 3)
		akRyneus.ClearLookAt()
		akRyneus.EvaluatePackage()
	EndIf
EndEvent

Event OnActivate(ObjectReference akActionRef)						; Play the opening animation, refresh ammo, et cetera

	if MQ11c.GetCurrentStageID() == 170
		if akActionRef == SphereButton02
			if (_00E_MQ11c_Sphere_NextStaqge.GetValueInt() == 0)
				Phase = 0
				StartRound = 1
			EndIf
			PlayerREF.RemoveItem(_00E_MQ11c_SphereArrow, 50, True)
			PlayerREF.AddItem(_00E_MQ11c_SphereArrow, 50, True)
			PlayerREF.EquipItem(_00E_MQ11c_SphereArrow, False, True)
			Self.PlayAnimation("Open")
			_00E_NPCDwarvenSphereScanM.Play(Self)
			Wait(1)
			MakeRyneusLookAtSphere()
			Wait(3)
			SphereLoop = _00E_NPCDwarvenSphereConsciousLPM.Play(Self)
			RegisterForSingleUpdate(5.0)
			_00E_MQ11c_Sphere_GameRunning.SetValue(1)
			SphereAngleX = Self.GetAngleX()
			SphereAngleY = Self.GetAngleY()
			SphereAngleZ = Self.GetAngleZ()
			SphereOffsetX = Self.GetPositionX()
			SphereOffsetY = Self.GetPositionY()
			SphereOffsetZ = Self.GetPositionZ()	
			Phase = 0
			GameRunning = True
			if StartRound == 1
				GoToState("RoundOne")
				; Debug.Trace("Moving to round 1 state")
			ElseIf StartRound == 2
				GoToState("RoundTwo")
				; Debug.Trace("Moving to round 2 state")
			ElseIf StartRound == 3
				GoToState("RoundThree")
				; Debug.Trace("Moving to round 3 state")
			EndIf
		EndIf
	ElseIf MQ11c.GetCurrentStageID() == 175										; If the minigame has ended, return to origin
		Move(0.0, 0.0, -170.0, 65, 50)	
		SphereInactiveLoop = QSTDA02ShrineActivateLPMMarker.Play(Self)
		Wait(3)
		Sound.StopInstance(SphereInactiveLoop)
		NPCDwarvenSphereClose.Play(Self)
	EndIf

EndEvent

EndState

;----------------------------TRANSLATING STATE-----------------------------------------------
State Translating

Event OnTranslationAlmostComplete()					

	; Debug.Trace("Translation complete") 					 		; Once the sphere has almost finished translating, return to pending state to determine the next movement
	Phase = (Phase + 1)
	GoToState("Pending")
	Self.Activate(Self)
	
EndEvent

EndState

State ReturningToBase

Event OnTranslationAlmostComplete()									; Add one to the start round integer to have the sphere enter the next round after re-activating itself
	StartRound = (StartRound +1)
	Phase = 0
	Sound.StopInstance(SphereLoop)
	Wait(5.5)
	; Debug.Trace("This should have worked!")
	GoToState("Restart")
	GameRunning == False
	Self.Activate(Self)
EndEvent

EndState

;----------------------------SPHERE ROUND #1-----------------------------------------------

State RoundOne

Event OnBeginState()

	; Debug.Trace("Entering" + Phase as Int)
	
	if Phase == 0
		Move(0.0, 0.0, 300, SphereRound01, 50)			
	ElseIf Phase == 1
		Move(- 300.0, 0.0, 0.0, SphereRound01, 50)	
	ElseIf Phase == 2
		Move(600, 0.0, 0.0, SphereRound01, 50)
	ElseIf Phase == 3
		Move(0.0, 300, 0.0, SphereRound01, 50)
	ElseIf Phase == 4
		Move(- 300, 0.0, 0, SphereRound01, 50)		
	ElseIf Phase == 5
		Move(0.0, -600, 0, SphereRound01, 50)
	ElseIf Phase == 6
		Move(300, 0.0, 0.0, SphereRound01, 50)
	ElseIf Phase == 7
		Move(0.0, 300, -300.0, SphereRound01, 50)
	ElseIf Phase == 8
		Wait(1.5)
		CloseAndReturnToBase()
	EndIf

EndEvent

EndState

;----------------------------SPHERE ROUND #2-----------------------------------------------

State RoundTwo

Event OnBeginState()

	; Debug.Trace("Entering" + Phase as Int)
	
	if Phase == 0
		Move(250, 0.0, 250, SphereRound02, 50)			
	ElseIf Phase == 1
		Move(- 200, 300, 0.0, SphereRound02, 40)	
	ElseIf Phase == 2
		Move(0.0, - 200, 150, SphereRound02, 40)
	ElseIf Phase == 3
		Move(200, 200, 0.0, SphereRound02, 40)
	ElseIf Phase == 4
		Move(- 50, - 300, - 150, SphereRound02, 40)		
	ElseIf Phase == 5
		Move(250, 250, 250, SphereRound02, 40)
	ElseIf Phase == 6
		Move(- 150, - 150, - 150, SphereRound02, 40)
	ElseIf Phase == 7
		Move(100, -100, -100, SphereRound02, 40)
	ElseIf Phase == 8
		Move(- 200, 100, 50, SphereRound02, 40)
	ElseIf Phase == 9
		Wait(1.5)
		CloseAndReturnToBase()
	EndIf

EndEvent

EndState

;----------------------------SPHERE ROUND #3-----------------------------------------------

State RoundThree

Event OnBeginState()

	; Debug.Trace("Entering" + Phase as Int)
	
	if Phase == 0
		Move(250, 0.0, 250, SphereRound03, 450)			
	ElseIf Phase == 1
		Move(-400, - 350, 0.0, SphereRound03, 480)	
	ElseIf Phase == 2
		Move(250, 250, - 150, SphereRound03, 460)
	ElseIf Phase == 3
		Move(0.0, 200, 100, SphereRound03, 460)
	ElseIf Phase == 4
		Move(- 350, 350, - 150, SphereRound03, 480)		
	ElseIf Phase == 5
		Move(400, - 350, 350, SphereRound03, 470)
	ElseIf Phase == 6
		Move(- 250, - 200, - 100, SphereRound03, 460)
	ElseIf Phase == 7
		Move(100.0, 0.0, 450, SphereRound03, 490)
	ElseIf Phase == 8
		Move(-200, 200, - 250, SphereRound03, 460)
	ElseIf Phase == 9
		Wait(1.5)
		CloseAndReturnToBase()
	EndIf

EndEvent

EndState

;=====================================================================================
;              							 FUNKTIONEN                 					 
;=====================================================================================


;x-value: + = Right, - Left, y-value: + = Backward, - = Forward

Function Move(float MoveX, float MoveY, float MoveZ, float SphereSpeed, float SphereSpline)

	_00E_MQ11c_SpherePrepareM.Play(Self)
	;Wait(0.7)
	Self.SplineTranslateTo((Self.GetPositionX() + MoveX), (Self.GetPositionY() + MoveY), (Self.GetPositionZ() + MoveZ),  (Self.GetAngleX() + 47), SphereAngleY, SphereAngleZ, (15.0 + SphereSpline), (SphereSpeed + GSM), 30.0)	
	_00E_MQ11c_SphereMoveM.Play(Self)
	GoToState("Translating")

EndFunction

Function CloseAndReturnToBase()

	Closing = True
	_00E_MQ11c_SphereClose.Play(Self)
	_00E_MQ11c_Sphere_GameRunning.SetValue(0)
	Self.SplineTranslateTo(SphereOffsetX, SphereOffsetY, SphereOffsetZ, SphereAngleX, SphereAngleY, SphereAngleZ, 55.0, 150, 60.0)
	Self.PlayAnimation("Close")
	GoToState("ReturningToBase")

EndFunction

Function MakeRyneusLookAtSphere()
	Ryneus.GetActorReference().SetLookAt(Self)
EndFunction


;=====================================================================================
;              							 PROPERTIES                 					 
;=====================================================================================


Float SphereAngleX
Float SphereAngleY
Float SphereAngleZ
Float SphereOffsetX
Float SphereOffsetY
Float SphereOffsetZ

int Phase
int SphereLoop
int SphereInactiveLoop

Float Property SphereRound01 Auto
Float Property SphereRound02 Auto
Float Property SphereRound03 Auto
Float Property GSM Auto
Float Property RyneusDialogueChance Auto

Bool Property Closing = False Auto Hidden
Bool Property GameRunning = False Auto Hidden
Bool Property TranslationInProgress = False Auto Hidden

int Property StartRound = 1 Auto

Actor Property PlayerREF Auto

Quest Property MQ11C Auto

ReferenceAlias Property Ryneus Auto

Sound Property _00E_NPCDwarvenSphereScanM Auto
Sound Property _00E_NPCDwarvenSphereConsciousLPM Auto
Sound Property _00E_MQ11c_SpherePrepareM Auto
Sound Property _00E_MQ11c_SphereMoveM Auto
Sound Property _00E_MQ11c_SphereClose Auto
Sound Property NPCDwarvenSphereOpen Auto
Sound Property NPCDwarvenSphereClose Auto
Sound Property QSTDA02ShrineActivateLPMMarker Auto

ObjectReference Property SphereButton01 Auto
ObjectReference Property SphereButton02 Auto

GlobalVariable Property _00E_MQ11c_Sphere_GameRunning Auto
GlobalVariable Property _00E_MQ11c_Sphere_NextStaqge Auto

Ammo Property _00E_MQ11c_SphereArrow Auto

Weapon Property _00E_MQ11c_SphereBow Auto

Scene Property MQ11c_SC7_RyneusRaiseSphereLevel Auto