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

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

private alias object.value0 : object.width
private alias object.value1 : object.height
private alias object.value2 : object.timer

// Player Aliases
private alias 0 : SLOT_PLAYER1
private alias 1 : SLOT_PLAYER2

private alias object.type			 : player.type
private alias object.state			 : player.state
private alias object.xpos			 : player.xpos
private alias object.ypos			 : player.ypos
private alias object.xvel			 : player.xvel
private alias object.yvel			 : player.yvel
private alias object.speed			 : player.speed
private alias object.controlMode	 : player.controlMode
private alias object.interaction	 : player.interaction
private alias object.tileCollisions	 : player.tileCollisions
private alias object.value3			 : player.drownTimer
private alias object.value4			 : player.drownLevel


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

event ObjectUpdate
	temp0 = object.width
	FlipSign(temp0)
	temp1 = object.height
	FlipSign(temp1)
	if player[SLOT_PLAYER2].type == TypeName[Player 2 Object]
		BoxCollisionTest(C_TOUCH, object.entityPos, temp0, temp1, object.width, object.height, SLOT_PLAYER2, 4, 4, 4, 4)
		if checkResult == true
			object.timer++
			if object.timer >= 240
				player[SLOT_PLAYER2].state 			 = Player2_State_FlyToPlayer
				player[SLOT_PLAYER2].xpos 			 = player[SLOT_PLAYER1].xpos
				player[SLOT_PLAYER2].ypos 			 = screen.yoffset
				player[SLOT_PLAYER2].ypos 			 -= 128
				player[SLOT_PLAYER2].ypos 			 <<= 16
				player[SLOT_PLAYER2].xvel 			 = 0
				player[SLOT_PLAYER2].yvel 			 = 0
				player[SLOT_PLAYER2].speed 			 = 0
				player[SLOT_PLAYER2].tileCollisions	 = false
				player[SLOT_PLAYER2].interaction 	 = false
				player[SLOT_PLAYER2].controlMode 	 = CONTROLMODE_P2
				player[SLOT_PLAYER2].drownTimer 	 = 0
				player[SLOT_PLAYER2].drownLevel 	 = 0
			end if
		else
			object.timer = 0
		end if
	end if
end event


event ObjectDraw
	if player[SLOT_PLAYER1].type == TypeName[Debug Mode]
		temp0 = object.xpos
		temp1 = object.ypos
		temp2 = object.propertyValue
		temp4 = temp2
		temp4 &= 0xF0
		temp4 <<= 15
		temp2 >>= 4
		temp2++
		temp6 = temp2
		temp3 = object.propertyValue
		temp3 &= 0x0F
		temp5 = temp3
		temp5 <<= 19
		temp3++
		temp0 -= temp4
		temp4 = temp0
		temp1 -= temp5
		while temp3 > 0
			while temp2 > 0
				DrawSpriteXY(0, temp0, temp1)
				temp0 += 0x100000
				temp2--
			loop
			temp2 = temp6
			temp0 = temp4
			temp1 += 0x100000
			temp3--
		loop
	end if
end event


event ObjectStartup
	LoadSpriteSheet("Global/Display.gif")

	// Tails's life icon
	SpriteFrame(-8, -7, 16, 14, 60, 108)

	foreach (TypeName[Tails Stuck], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].drawOrder = 5
	
		object[arrayPos0].width = object[arrayPos0].propertyValue
		object[arrayPos0].width &= 0xF0
		object[arrayPos0].width += 16
		object[arrayPos0].width >>= 1
	
		object[arrayPos0].height = object[arrayPos0].propertyValue
		object[arrayPos0].height &= 0x0F
		object[arrayPos0].height++
		object[arrayPos0].height <<= 3
	next
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 // width
			checkResult = object.propertyValue
			checkResult >>= 4
			checkResult &= 0xF
			break

		case 1 // height
			checkResult = object.propertyValue
			checkResult &= 0xF
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // width
			temp0 = editor.variableValue
			temp0 &= 0xF
			temp0 <<= 4
			
			object.propertyValue &= 0x0F
			object.propertyValue |= temp0
			break

		case 1 // height
			temp0 = editor.variableValue
			temp0 &= 0xF

			object.propertyValue &= 0xF0
			object.propertyValue |= temp0
			break
			
		end switch
	end if
end event


event RSDKDraw
	temp0 = object.xpos
	temp1 = object.ypos
	temp2 = object.propertyValue
	temp4 = temp2
	temp4 &= 0xF0
	temp4 <<= 15
	temp2 >>= 4
	temp2++
	temp6 = temp2
	temp3 = object.propertyValue
	temp3 &= 0x0F
	temp5 = temp3
	temp5 <<= 19
	temp3++
	temp0 -= temp4
	temp4 = temp0
	temp1 -= temp5
	while temp3 > 0
		while temp2 > 0
			DrawSpriteXY(0, temp0, temp1)
			temp0 += 0x100000
			temp2--
		loop
		temp2 = temp6
		temp0 = temp4
		temp1 += 0x100000
		temp3--
	loop
end event


event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-8, -7, 16, 14, 60, 108)

	AddEditorVariable("width")
	AddEditorVariable("height")
end event
