Scriptname TW_Script_SwitchWeapon extends ObjectReference 
 
Import Game

;Properties
;------------------------------------------------------

WEAPON Property ThrowingWeapon Auto
WEAPON Property ThrowingWeaponHidden Auto
PROJECTILE Property EmbeddedProjectile Auto

;------------------------------------------------------

;Variables
;------------------------------------------------------

ObjectReference SelfRef
ObjectReference WeaponProjectile
Form FormProjectile

Float zOffset
Float SelfXPos
Float SelfYPos
Float SelfZPos
Float SelfXAngle
Float SelfYAngle
Float SelfZAngle
Float EmbedXPos
Float EmbedYPos
Float EmbedZPos
Float EmbedXAngle
Float EmbedYAngle
Float EmbedZAngle

;------------------------------------------------------

;Event is called when hidden weapon is loaded in the world
;--------------------------------------------------------------------------------------------------------------

Event OnLoad()
;Event OnInit()

	SelfRef = Self as Objectreference
	If SelfRef && SelfRef.Is3DLoaded()
		SelfRef.SetActorOwner(Game.GetPlayer().GetActorBase())
		SelfRef.SetMotionType(Motion_Keyframed)
		
		WeaponProjectile  = Game.FindClosestReferenceOfTypeFromRef(EmbeddedProjectile, SelfRef, 100.0)
		
		If (WeaponProjectile == None)
			SelfRef.Delete()
		EndIf
		
		If (SelfRef != None) && (WeaponProjectile != None)		
			Utility.Wait(0.5)			
			ChangePosition()				
		EndIf
	EndIf

EndEvent
;--------------------------------------------------------------------------------------------------------------

;Event is called if the embedded weapon position moves
;--------------------------------------------------------------------------------------------------------------
Event OnUpdate()
	If SelfRef && SelfRef.Is3DLoaded()
		If (SelfXAngle == EmbedXAngle) && (SelfYAngle == EmbedYAngle) && (SelfZAngle == EmbedZAngle)
			If(SelfXPos == EmbedXpos) && (SelfYPos == EmbedYPos) && (SelfZPos == EmbedZPos)
				Return
			Else
				ChangePosition()
			EndIf
		Else
			ChangePosition()
		EndIf
	EndIf
EndEvent
;--------------------------------------------------------------------------------------------------------------

;Events are called if the hidden weapon is picked up
;--------------------------------------------------------------------------------------------------------------
Event OnContainerChanged(ObjectReference akNewContainer, ObjectReference akOldContainer)
	If (SelfRef && SelfRef.Is3DLoaded() == False)
		If akNewContainer == Game.GetPlayer()
			Int ItemCount = Game.GetPlayer().GetItemCount(ThrowingWeaponHidden)
		
			Game.getPlayer().AddItem(ThrowingWeapon, ItemCount, true)
			Game.getPlayer().RemoveItem(ThrowingWeaponHidden, ItemCount, true)
		EndIf
	EndIf
EndEvent

Event OnActivate(ObjectReference akActionRef)
	Int ItemCount = Game.GetPlayer().GetItemCount(ThrowingWeaponHidden)		

	WeaponProjectile.Delete()
	WeaponProjectile = None
EndEvent
;--------------------------------------------------------------------------------------------------------------

;Function controls the setting of position and angle
;--------------------------------------------------------------------------------------------------------------
Function ChangePosition()
	If SelfRef && (WeaponProjectile != None)
		SelfRef.MoveTo(WeaponProjectile, abMatchRotation = true)
		PositionCheck()
		RegisterForSingleUpdate(0.5)
	EndIf
EndFunction
;--------------------------------------------------------------------------------------------------------------

;Function is called if the hidden weapons position needs to change
;--------------------------------------------------------------------------------------------------------------
Function PositionCheck()
	if (selfRef && !selfRef.IsDeleted())
		SelfXPos = selfRef.GetPositionX()
		SelfYPos = selfRef.GetPositionY()
		SelfZPos = selfRef.GetPositionZ()
	endif
	if (selfRef && WeaponProjectile)
		EmbedXPos = WeaponProjectile.GetPositionX()
		EmbedYPos = WeaponProjectile.GetPositionY()
		EmbedZPos = WeaponProjectile.GetPositionZ()
	endif

	if (selfRef && !selfRef.IsDeleted())
		SelfXAngle = selfRef.GetAngleX()
		SelfYAngle = selfRef.GetAngleY()
		SelfZAngle = selfRef.GetAngleZ()
	endif

	if (selfRef && WeaponProjectile)
		EmbedXAngle = WeaponProjectile.GetAngleX()
		EmbedYAngle = WeaponProjectile.GetAngleY()
		EmbedZAngle = WeaponProjectile.GetAngleZ()
	endif
EndFunction
;--------------------------------------------------------------------------------------------------------------