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

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

private alias object.value0 : object.startPos.x
private alias object.value1 : object.endPos.x
private alias object.value2 : object.velIncrease
private alias object.value3 : object.stoodPlayers

// States
private alias 0 : LAUNCHER_IDLE
private alias 1 : LAUNCHER_LAUNCHING
private alias 2 : LAUNCHER_RETRACTING

// Player Aliases
private alias object.state : player.state
private alias object.xpos : player.xpos
private alias object.xvel : player.xvel
private alias object.yvel : player.yvel
private alias object.speed : player.speed
private alias object.direction : player.direction
private alias object.gravity : player.gravity
private alias object.animation : player.animation

private alias object.value17 : debugMode.currentSelection


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

reserve function Launcher_DebugDraw
reserve function Launcher_DebugSpawn


// ========================
// Static Values
// ========================

private value Launcher_startDebugID = 0


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

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


private function Launcher_DebugSpawn
	temp0 = debugMode[0].currentSelection
	temp0 -= Launcher_startDebugID
	temp0++

	temp0 <<= 2
	CreateTempObject(TypeName[Launcher], temp0, object.xpos, object.ypos)

	object[tempObjectPos].startPos.x = object.xpos
	object[tempObjectPos].endPos.x = object.xpos
	object[tempObjectPos].direction = object.direction
	temp0 <<= 20
	if object.direction == FACING_RIGHT
		object[tempObjectPos].endPos.x -= temp0
		object[tempObjectPos].velIncrease = -0x8000
	else
		object[tempObjectPos].endPos.x += temp0
		object[tempObjectPos].velIncrease = 0x8000
	end if
end function


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

event ObjectUpdate
	switch object.state
	case LAUNCHER_IDLE
		break

	case LAUNCHER_LAUNCHING
		object.xvel += object.velIncrease
		object.xpos += object.xvel

		temp0 = false
		if object.direction == FLIP_NONE
			if object.xpos <= object.endPos.x
				temp0 = true
			end if
		else
			if object.xpos >= object.endPos.x
				temp0 = true
			end if
		end if

		if temp0 == true
			temp2 = 0
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				GetBit(temp0, object.stoodPlayers, temp2)
				if temp0 == true
					if player[currentPlayer].yvel >= 0
						player[currentPlayer].xvel 		= object.xvel
						player[currentPlayer].speed 	= object.xvel
						player[currentPlayer].yvel 		= -0x40000
						player[currentPlayer].gravity 	= 1
						player[currentPlayer].state 	= Player_State_Air
						player[currentPlayer].direction = object.direction
						player[currentPlayer].direction ^= FACING_LEFT
					end if
				end if
				temp2++
			next
			object.state = LAUNCHER_RETRACTING
		end if
		break

	case LAUNCHER_RETRACTING
		if object.direction == FLIP_NONE
			object.xpos += 0x40000
			if object.xpos >= object.startPos.x
				object.xpos = object.startPos.x
				object.priority = PRIORITY_BOUNDS
				object.state = LAUNCHER_IDLE
			end if
		else
			object.xpos -= 0x40000
			if object.xpos <= object.startPos.x
				object.xpos = object.startPos.x
				object.priority = PRIORITY_BOUNDS
				object.state = LAUNCHER_IDLE
			end if
		end if
		break
		
	end switch

	temp2 = 0
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		GetBit(temp0, object.stoodPlayers, temp2)
		if temp0 == true
			if player[currentPlayer].yvel >= 0
				player[currentPlayer].xpos 		= object.xpos
				player[currentPlayer].xvel 		= 0
				player[currentPlayer].speed 	= 0
				player[currentPlayer].animation = ANI_STOPPED
				player[currentPlayer].state 	= Player_State_Ground
				player[currentPlayer].direction = object.direction
				player[currentPlayer].direction ^= FACING_LEFT
			end if
		end if

		SetBit(object.stoodPlayers, temp2, false)
		BoxCollisionTest(C_PLATFORM, object.entityPos, -16, -16, 16, -8, currentPlayer, -1, C_BOX, 1, C_BOX)
		if checkResult == true
			SetBit(object.stoodPlayers, temp2, true)
			if object.state == LAUNCHER_IDLE
				PlaySfx(SfxName[Launch], false)
				if object.direction == FLIP_NONE
					object.xvel = -0xC0000
				else
					object.xvel = 0xC0000
				end if
				object.priority = PRIORITY_ACTIVE
				object.state 	= LAUNCHER_LAUNCHING
			end if
		end if
		temp2++
	next
end event


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


event ObjectStartup
	CheckCurrentStageFolder("Zone11")
	if checkResult == true
		LoadSpriteSheet("SCZ/Objects.gif")
		SpriteFrame(-16, -31, 32, 16, 152, 74)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-16, -31, 32, 16, 445, 403)
	end if

	foreach (TypeName[Launcher], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.x 	= object[arrayPos0].xpos
		object[arrayPos0].endPos.x 		= object[arrayPos0].xpos

		temp0 = object[arrayPos0].propertyValue
		temp0 <<= 20
		if object[arrayPos0].direction == FLIP_NONE
			object[arrayPos0].endPos.x -= temp0
			object[arrayPos0].velIncrease = -0x8000
		else
			object[arrayPos0].endPos.x += temp0
			object[arrayPos0].velIncrease = 0x8000
		end if
	next

	temp0 = 0
	Launcher_startDebugID = DebugMode_ObjCount
	while temp0 < 4
		SetTableValue(TypeName[Launcher], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(Launcher_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(Launcher_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
end event


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

event RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			checkResult = object.propertyValue
			break
			
		case 0 // distance
			checkResult = object.propertyValue
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // distance
			object.propertyValue = editor.variableValue
			break
			
		end switch
	end if
end event


event RSDKDraw
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)

	if editor.showGizmos == true
		temp0 = object.propertyValue
		temp0 <<= 20
		
		object.endPos.x = object.xpos
		if object.direction == FLIP_NONE
			object.endPos.x -= temp0
		else
			object.endPos.x += temp0
		end if

		DrawArrow(object.xpos, object.ypos, object.endPos.x, object.ypos, 0xFF, 0xFF, 0x00)
	end if
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone11")
	if checkResult == true
		LoadSpriteSheet("SCZ/Objects.gif")
		SpriteFrame(-16, -31, 32, 16, 152, 74)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-16, -31, 32, 16, 445, 403)
	end if

	AddEditorVariable("distance")
	SetActiveVariable("distance")
end event
