// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Arrow Launcher Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.value0 : object.timer

// States
private alias 0 : ARROWLAUNCHER_AWAITPLAYER
private alias 1 : ARROWLAUNCHER_ACTIVATE
private alias 2 : ARROWLAUNCHER_FIREARROW

// Arrow Aliases
private alias object.value0 : arrow.timer
private alias object.value2 : arrow.drawPos.x


// ========================
// Function Declarations
// ========================

reserve function ArrowLauncher_DebugDraw
reserve function ArrowLauncher_DebugSpawn


// ========================
// Function Definitions
// ========================

private function ArrowLauncher_DebugDraw
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end function


private function ArrowLauncher_DebugSpawn
	CreateTempObject(TypeName[Arrow Launcher], 0, object.xpos, object.ypos)
	object[tempObjectPos].direction = object.direction
end function


// ========================
// Events
// ========================

event ObjectUpdate
	switch object.state
	case ARROWLAUNCHER_AWAITPLAYER
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -64, -256, 64, 256, currentPlayer, -1, -1, 1, 1)
			if checkResult == true
				object.state = ARROWLAUNCHER_ACTIVATE
			end if
		next
		break

	case ARROWLAUNCHER_ACTIVATE
		object.timer++
		object.timer &= 7
		object.frame = object.timer
		object.frame >>= 2

		temp0 = false
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -64, -256, 64, 256, currentPlayer, -1, -1, 1, 1)
			temp0 += checkResult
		next

		if temp0 == false
			object.state = ARROWLAUNCHER_FIREARROW
			object.timer = 0
			object.frame = 2
			PlaySfx(SfxName[Swish], false)
		end if
		break

	case ARROWLAUNCHER_FIREARROW
		object.timer++
		switch object.timer
		case 8
			object.frame = 3
			CreateTempObject(TypeName[Arrow], 0, object.xpos, object.ypos)
			if object.direction == FLIP_X
				object[tempObjectPos].xvel = -0x40000
			else
				object[tempObjectPos].xvel = 0x40000
			end if
			object[tempObjectPos].drawOrder = 1
			arrow[tempObjectPos].timer = 24
			arrow[tempObjectPos].drawPos.x = object.xpos
			object[tempObjectPos].direction = object.direction
			break

		case 24
			object.frame = 2
			break

		case 32
			object.timer = 0
			object.frame = 0
			object.state = ARROWLAUNCHER_AWAITPLAYER
			break
			
		end switch
		break
		
	end switch
end event


event ObjectDraw
	DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
end event


event ObjectStartup
	LoadSpriteSheet("ARZ/Objects.gif")
	SpriteFrame(-16, -8, 32, 16, 1, 1)
	SpriteFrame(-16, -8, 32, 16, 1, 18)
	SpriteFrame(-16, -8, 32, 16, 1, 35)
	SpriteFrame(-16, -8, 32, 16, 1, 52)

	SetTableValue(TypeName[Arrow Launcher], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(ArrowLauncher_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(ArrowLauncher_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
	
#platform: USE_ORIGINS
	if game.playMode == BOOT_PLAYMODE_MISSION
		CheckCurrentStageFolder("DLC_ZONE09") // Yeah, not really sure why it's all caps...
		if checkResult != false
			foreach (TypeName[Arrow Launcher], arrayPos0, ALL_ENTITIES)
				object[arrayPos0].drawOrder = 5
			next
		end if
	end if
#endplatform
end event


// ========================
// Editor Events
// ========================

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("ARZ/Objects.gif")
	SpriteFrame(-16, -8, 32, 16, 1, 1)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
