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

// ========================
// Aliases
// ========================
// Used in Death Egg Type
private alias object.value0 : object.sparkTimer
private alias object.value1 : object.dustPuff1Timer
private alias object.value2 : object.dustPuff3Timer
private alias object.value4 : object.smallDustPuffTimer

// Used for animating in other types
private alias object.value0 : object.timer

// Types
private alias 0  : DEATHEGGFALL_BG
private alias 1  : DEATHEGGFALL_DEATHEGG
private alias 2  : DEATHEGGFALL_ANGELISLAND
private alias 3  : DEATHEGGFALL_CLOUD_WIDE
private alias 4  : DEATHEGGFALL_CLOUD_SMALL
private alias 5  : DEATHEGGFALL_DUSTPUFF1_1
private alias 6  : DEATHEGGFALL_DUSTPUFF1_2
private alias 7  : DEATHEGGFALL_DUSTPUFF1_3
private alias 8  : DEATHEGGFALL_DUSTPUFF1_4
private alias 9  : DEATHEGGFALL_DUSTPUFF2_1
private alias 10 : DEATHEGGFALL_DUSTPUFF2_2
private alias 11 : DEATHEGGFALL_DUSTPUFF2_3
private alias 12 : DEATHEGGFALL_DUSTPUFF2_4
private alias 13 : DEATHEGGFALL_SMALLDUSTPUFF1_1
private alias 14 : DEATHEGGFALL_SMALLDUSTPUFF1_2
private alias 15 : DEATHEGGFALL_SMALLDUSTPUFF1_3
private alias 16 : DEATHEGGFALL_SMALLDUSTPUFF1_4
private alias 17 : DEATHEGGFALL_SMALLDUSTPUFF2_1
private alias 18 : DEATHEGGFALL_SMALLDUSTPUFF2_2
private alias 19 : DEATHEGGFALL_SMALLDUSTPUFF2_3
private alias 20 : DEATHEGGFALL_SMALLDUSTPUFF2_4
private alias 21 : DEATHEGGFALL_SPARK_1
private alias 22 : DEATHEGGFALL_SPARK_2
private alias 23 : DEATHEGGFALL_SPARK_3
private alias 24 : DEATHEGGFALL_SPARK_4
private alias 25 : DEATHEGGFALL_SPARK_5
private alias 26 : DEATHEGGFALL_SPARK_6
private alias 27 : DEATHEGGFALL_DUSTPUFF3_1

event ObjectUpdate
	switch object.propertyValue
	case DEATHEGGFALL_BG
		break

	case DEATHEGGFALL_DEATHEGG
		object.xpos += 0x2000
		object.ypos += 0x2000

		object.sparkTimer++
		if object.sparkTimer >= 6
			object.sparkTimer = 0
			Rand(temp0, 36)
			temp0 -= 18
			temp0 <<= 16
			temp0 += object.xpos
			Rand(temp1, 36)
			temp1 -= 18
			temp1 <<= 16
			temp1 += object.ypos
			CreateTempObject(TypeName[Death Egg Fall], DEATHEGGFALL_SPARK_1, temp0, temp1)
			object[tempObjectPos].drawOrder = 1
		end if

		object.dustPuff1Timer++
		if object.dustPuff1Timer >= 24
			object.dustPuff1Timer = 0
			Rand(temp0, 36)
			temp0 -= 18
			temp0 <<= 16
			temp0 += object.xpos
			Rand(temp1, 36)
			temp1 -= 18
			temp1 <<= 16
			temp1 += object.ypos
			CreateTempObject(TypeName[Death Egg Fall], DEATHEGGFALL_DUSTPUFF1_1, temp0, temp1)
			object[tempObjectPos].drawOrder = 1

			Rand(temp0, 4)
			if temp0 == 2
				object[tempObjectPos].propertyValue = DEATHEGGFALL_DUSTPUFF2_1
			end if
		end if

		object.dustPuff3Timer++
		if object.dustPuff3Timer >= 32
			object.dustPuff3Timer = 0
			Rand(temp0, 36)
			temp0 -= 18
			temp0 <<= 16
			temp0 += object.xpos
			Rand(temp1, 36)
			temp1 -= 18
			temp1 <<= 16
			temp1 += object.ypos
			CreateTempObject(TypeName[Death Egg Fall], DEATHEGGFALL_DUSTPUFF3_1, temp0, temp1)
			object[tempObjectPos].drawOrder = 1
		end if

		object.smallDustPuffTimer++
		if object.smallDustPuffTimer >= 28
			object.smallDustPuffTimer = 0
			Rand(temp0, 36)
			temp0 -= 18
			temp0 <<= 16
			temp0 += object.xpos
			temp1 = object.ypos
			temp1 -= 0x120000
			CreateTempObject(TypeName[Death Egg Fall], DEATHEGGFALL_SMALLDUSTPUFF1_1, temp0, temp1)
			object[tempObjectPos].drawOrder = 1

			Rand(temp0, 4)
			if temp0 == 2
				object[tempObjectPos].propertyValue = DEATHEGGFALL_SMALLDUSTPUFF2_1
			end if
		end if
		break

	case DEATHEGGFALL_ANGELISLAND
		object.xpos -= 0x2000
		object.ypos -= 0x2000
		break

	case DEATHEGGFALL_CLOUD_WIDE
		object.xpos -= 0x4000
		object.ypos -= 0x4000
		break

	case DEATHEGGFALL_CLOUD_SMALL
		object.xpos -= 0x1000
		object.ypos -= 0x1000
		break

	// Dust Puffs
	case DEATHEGGFALL_DUSTPUFF1_1
	case DEATHEGGFALL_DUSTPUFF1_2
	case DEATHEGGFALL_DUSTPUFF1_3
	case DEATHEGGFALL_DUSTPUFF1_4

	case DEATHEGGFALL_DUSTPUFF2_1
	case DEATHEGGFALL_DUSTPUFF2_2
	case DEATHEGGFALL_DUSTPUFF2_3
	case DEATHEGGFALL_DUSTPUFF2_4

	case DEATHEGGFALL_SMALLDUSTPUFF1_1
	case DEATHEGGFALL_SMALLDUSTPUFF1_2
	case DEATHEGGFALL_SMALLDUSTPUFF1_3
	case DEATHEGGFALL_SMALLDUSTPUFF1_4

	case DEATHEGGFALL_SMALLDUSTPUFF2_1
	case DEATHEGGFALL_SMALLDUSTPUFF2_2
	case DEATHEGGFALL_SMALLDUSTPUFF2_3
	case DEATHEGGFALL_SMALLDUSTPUFF2_4
		object.animationTimer++
		if object.animationTimer >= 4
			object.animationTimer = 0
			object.propertyValue++
			object.timer++
			if object.timer >= 4
				object.type = TypeName[Blank Object]
			end if
		end if
		object.xpos -= 0x6000
		object.ypos -= 0x6000
		break

	// Sparks
	case DEATHEGGFALL_SPARK_1
	case DEATHEGGFALL_SPARK_2
	case DEATHEGGFALL_SPARK_3
	case DEATHEGGFALL_SPARK_4
	case DEATHEGGFALL_SPARK_5
	case DEATHEGGFALL_SPARK_6
		object.animationTimer ^= 1
		if object.animationTimer == 0
			object.propertyValue++
			object.timer++
			if object.timer >= 6
				object.type = TypeName[Blank Object]
			end if
		end if
		object.xpos += 0x1000
		object.ypos += 0x1000
		break

	// Dust Puff 3
	case DEATHEGGFALL_DUSTPUFF3_1
		object.animationTimer++
		object.animationTimer &= 15
		object.direction = object.animationTimer
		object.direction >>= 2
		object.xpos -= 0x6000
		object.ypos -= 0x6000
		break
		
	end switch
end event


event ObjectDraw
	DrawSprite(object.propertyValue)
end event


event ObjectStartup
	LoadSpriteSheet("Ending/Fall.gif")

	// Death Egg frames
	SpriteFrame(-48, -48, 96, 96, 1, 1)  	// Sky BG 							- #0
	SpriteFrame(-21, -21, 42, 42, 163, 1)	// Death Egg 						- #1
	SpriteFrame(-32, -32, 64, 64, 98, 1)	// Angel Island 					- #2
	SpriteFrame(-24, -4, 48, 8, 206, 1)		// Wide Cloud 						- #3
	SpriteFrame(-8, -4, 16, 8, 206, 10)		// Small Cloud						- #4

	SpriteFrame(-8, -7, 16, 13, 98, 66)		// Dust Puff 1 - Frame 1 			- #5
	SpriteFrame(-8, -7, 15, 14, 115, 66)	// Dust Puff 1 - Frame 2 			- #6
	SpriteFrame(-8, -7, 15, 14, 131, 66)	// Dust Puff 1 - Frame 3 			- #7
	SpriteFrame(-6, -6, 12, 12, 147, 66)	// Dust Puff 1 - Frame 4 			- #8

	SpriteFrame(-8, -7, 16, 13, 98, 81)		// Dust Puff 2 - Frame 1 			- #9
	SpriteFrame(-8, -7, 15, 14, 115, 81)	// Dust Puff 2 - Frame 2 			- #10
	SpriteFrame(-8, -7, 15, 14, 131, 81)	// Dust Puff 2 - Frame 3 			- #11
	SpriteFrame(-6, -6, 12, 12, 131, 96)	// Dust Puff 2 - Frame 4 			- #12

	SpriteFrame(-4, -4, 8, 7, 163, 44)		// Dust Puff 1 (Small) - Frame 1 	- #13
	SpriteFrame(-4, -4, 8, 7, 172, 44)		// Dust Puff 1 (Small) - Frame 2 	- #14
	SpriteFrame(-4, -4, 8, 7, 181, 44)		// Dust Puff 1 (Small) - Frame 3 	- #15
	SpriteFrame(-3, -3, 6, 6, 190, 44)		// Dust Puff 1 (Small) - Frame 4 	- #16

	SpriteFrame(-4, -4, 8, 7, 163, 52)		// Dust Puff 2 (Small) - Frame 1 	- #17
	SpriteFrame(-4, -4, 8, 7, 172, 52)		// Dust Puff 2 (Small) - Frame 2 	- #18
	SpriteFrame(-4, -4, 8, 7, 181, 52)		// Dust Puff 2 (Small) - Frame 3 	- #19
	SpriteFrame(-3, -3, 6, 6, 190, 52)		// Dust Puff 2 (Small) - Frame 4 	- #20

	SpriteFrame(0, 0, 1, 1, 163, 60)		// Sparks - Frame 1 				- #21
	SpriteFrame(0, -1, 1, 2, 165, 60)		// Sparks - Frame 2 				- #22
	SpriteFrame(-1, -1, 2, 2, 167, 60)		// Sparks - Frame 3 				- #23
	SpriteFrame(-2, -2, 4, 4, 170, 60)		// Sparks - Frame 4 				- #24
	SpriteFrame(-2, -2, 4, 4, 175, 60)		// Sparks - Frame 5 				- #25
	SpriteFrame(-2, -2, 4, 4, 180, 60)		// Sparks - Frame 6 				- #26
	
	SpriteFrame(-3, -3, 6, 6, 223, 10)		// Dust Puff 3 - Frame 1 			- #27
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 // type
			checkResult = object.propertyValue
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // type
			object.propertyValue = editor.variableValue
			break
			
		end switch
	end if
end event


event RSDKDraw
	DrawSprite(object.propertyValue)
end event


event RSDKLoad
	LoadSpriteSheet("Ending/Fall.gif")
	SpriteFrame(-48, -48, 96, 96, 1, 1)  	// Sky BG 							- #0
	SpriteFrame(-21, -21, 42, 42, 163, 1)	// Death Egg 						- #1
	SpriteFrame(-32, -32, 64, 64, 98, 1)	// Angel Island 					- #2
	SpriteFrame(-24, -4, 48, 8, 206, 1)		// Wide Cloud 						- #3
	SpriteFrame(-8, -4, 16, 8, 206, 10)		// Small Cloud						- #4

	SpriteFrame(-8, -7, 16, 13, 98, 66)		// Dust Puff 1 - Frame 1 			- #5
	SpriteFrame(-8, -7, 15, 14, 115, 66)	// Dust Puff 1 - Frame 2 			- #6
	SpriteFrame(-8, -7, 15, 14, 131, 66)	// Dust Puff 1 - Frame 3 			- #7
	SpriteFrame(-6, -6, 12, 12, 147, 66)	// Dust Puff 1 - Frame 4 			- #8

	SpriteFrame(-8, -7, 16, 13, 98, 81)		// Dust Puff 2 - Frame 1 			- #9
	SpriteFrame(-8, -7, 15, 14, 115, 81)	// Dust Puff 2 - Frame 2 			- #10
	SpriteFrame(-8, -7, 15, 14, 131, 81)	// Dust Puff 2 - Frame 3 			- #11
	SpriteFrame(-6, -6, 12, 12, 131, 96)	// Dust Puff 2 - Frame 4 			- #12

	SpriteFrame(-4, -4, 8, 7, 163, 44)		// Dust Puff 1 (Small) - Frame 1 	- #13
	SpriteFrame(-4, -4, 8, 7, 172, 44)		// Dust Puff 1 (Small) - Frame 2 	- #14
	SpriteFrame(-4, -4, 8, 7, 181, 44)		// Dust Puff 1 (Small) - Frame 3 	- #15
	SpriteFrame(-3, -3, 6, 6, 190, 44)		// Dust Puff 1 (Small) - Frame 4 	- #16

	SpriteFrame(-4, -4, 8, 7, 163, 52)		// Dust Puff 2 (Small) - Frame 1 	- #17
	SpriteFrame(-4, -4, 8, 7, 172, 52)		// Dust Puff 2 (Small) - Frame 2 	- #18
	SpriteFrame(-4, -4, 8, 7, 181, 52)		// Dust Puff 2 (Small) - Frame 3 	- #19
	SpriteFrame(-3, -3, 6, 6, 190, 52)		// Dust Puff 2 (Small) - Frame 4 	- #20

	SpriteFrame(0, 0, 1, 1, 163, 60)		// Sparks - Frame 1 				- #21
	SpriteFrame(0, -1, 1, 2, 165, 60)		// Sparks - Frame 2 				- #22
	SpriteFrame(-1, -1, 2, 2, 167, 60)		// Sparks - Frame 3 				- #23
	SpriteFrame(-2, -2, 4, 4, 170, 60)		// Sparks - Frame 4 				- #24
	SpriteFrame(-2, -2, 4, 4, 175, 60)		// Sparks - Frame 5 				- #25
	SpriteFrame(-2, -2, 4, 4, 180, 60)		// Sparks - Frame 6 				- #26
	
	SpriteFrame(-3, -3, 6, 6, 223, 10)		// Dust Puff 3 - Frame 1 			- #27
	
	AddEditorVariable("frameID")
	SetActiveVariable("frameID")
	AddEnumVariable("Sky BG", DEATHEGGFALL_BG)
	AddEnumVariable("Death Egg", DEATHEGGFALL_DEATHEGG)
	AddEnumVariable("Angel Island", DEATHEGGFALL_ANGELISLAND)
	AddEnumVariable("Cloud (Wide)", DEATHEGGFALL_CLOUD_WIDE)
	AddEnumVariable("Cloud (Small)", DEATHEGGFALL_CLOUD_SMALL)
	AddEnumVariable("Dust Puff 1 (Frame 1)", DEATHEGGFALL_DUSTPUFF1_1)
	AddEnumVariable("Dust Puff 1 (Frame 2)", DEATHEGGFALL_DUSTPUFF1_2)
	AddEnumVariable("Dust Puff 1 (Frame 3)", DEATHEGGFALL_DUSTPUFF1_3)
	AddEnumVariable("Dust Puff 1 (Frame 4)", DEATHEGGFALL_DUSTPUFF1_4)
	AddEnumVariable("Dust Puff 2 (Frame 1)", DEATHEGGFALL_DUSTPUFF2_1)
	AddEnumVariable("Dust Puff 2 (Frame 2)", DEATHEGGFALL_DUSTPUFF2_2)
	AddEnumVariable("Dust Puff 2 (Frame 3)", DEATHEGGFALL_DUSTPUFF2_3)
	AddEnumVariable("Dust Puff 2 (Frame 4)", DEATHEGGFALL_DUSTPUFF2_4)
	AddEnumVariable("Small Dust Puff 1 (Frame 1)", DEATHEGGFALL_SMALLDUSTPUFF1_1)
	AddEnumVariable("Small Dust Puff 1 (Frame 2)", DEATHEGGFALL_SMALLDUSTPUFF1_2)
	AddEnumVariable("Small Dust Puff 1 (Frame 3)", DEATHEGGFALL_SMALLDUSTPUFF1_3)
	AddEnumVariable("Small Dust Puff 1 (Frame 4)", DEATHEGGFALL_SMALLDUSTPUFF1_4)
	AddEnumVariable("Small Dust Puff 2 (Frame 1)", DEATHEGGFALL_SMALLDUSTPUFF2_1)
	AddEnumVariable("Small Dust Puff 2 (Frame 2)", DEATHEGGFALL_SMALLDUSTPUFF2_2)
	AddEnumVariable("Small Dust Puff 2 (Frame 3)", DEATHEGGFALL_SMALLDUSTPUFF2_3)
	AddEnumVariable("Small Dust Puff 2 (Frame 4)", DEATHEGGFALL_SMALLDUSTPUFF2_4)
	AddEnumVariable("Spark (Frame 1)", DEATHEGGFALL_SPARK_1)
	AddEnumVariable("Spark (Frame 2)", DEATHEGGFALL_SPARK_2)
	AddEnumVariable("Spark (Frame 3)", DEATHEGGFALL_SPARK_3)
	AddEnumVariable("Spark (Frame 4)", DEATHEGGFALL_SPARK_4)
	AddEnumVariable("Spark (Frame 5)", DEATHEGGFALL_SPARK_5)
	AddEnumVariable("Spark (Frame 6)", DEATHEGGFALL_SPARK_6)
	AddEnumVariable("Dust Puff 3", DEATHEGGFALL_DUSTPUFF3_1)
	
	// drawOrder is set from editor as well
end event
