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

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

private alias object.value0 : object.timer
private alias object.value1 : object.duration
private alias object.value2 : object.frameTablePos

// Player Aliases
private alias 2 : PLAYER_KNUX_ALONE


// ========================
// Tables
// ========================

private table CreditsEggman_durationTable
	7, 113, 7, 113
end table

private table CreditsEggman_frameTable
	4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5, 4, 5, 6, 5
end table


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

event ObjectUpdate
	switch object.propertyValue
	case 0 // Eggman Juggle
		GetTableValue(object.duration, object.frame, CreditsEggman_durationTable)
		object.timer++
		if object.timer == object.duration
			object.timer = 0
			object.frame++
			object.frame &= 3
		end if

		switch object.frame
		case 1
			if object.timer == 0
				object[+1].angle = 0x20
				object[+2].angle = 0x20
				object[+3].angle = 0x20
				object[+4].angle = 0x20
				object[+5].angle = 0x20
				object[+6].angle = 0x20
				object[+7].angle = 0x20
			else
				arrayPos0 = 1
				while arrayPos0 < 8
					if object.timer > object[+arrayPos0].timer
						object[+arrayPos0].angle += 4
						if object[+arrayPos0].angle > 256
							object[+arrayPos0].angle = 256
						end if
					end if
					arrayPos0++
				loop
			end if
			break
			
		case 3
			if object.timer == 0
				object[+1].angle = 224
				object[+2].angle = 224
				object[+3].angle = 224
				object[+4].angle = 224
				object[+5].angle = 224
				object[+6].angle = 224
				object[+7].angle = 224
			else
				arrayPos0 = 1
				while arrayPos0 < 8
					if object.timer > object[+arrayPos0].timer
						object[+arrayPos0].angle -= 4
						if object[+arrayPos0].angle < 0
							object[+arrayPos0].angle = 0
						end if
					end if
					arrayPos0++
				loop
			end if
			break
			
		end switch

		arrayPos0 = 1
		while arrayPos0 < 8
			Cos(object[+arrayPos0].xpos, object[+arrayPos0].angle)
			object[+arrayPos0].xpos *= -0xE00
			Sin(object[+arrayPos0].ypos, object[+arrayPos0].angle)
			object[+arrayPos0].ypos *= -0xE00
			object[+arrayPos0].ypos -= 0x80000
			object[+arrayPos0].xpos += object.xpos
			object[+arrayPos0].ypos += object.ypos
			arrayPos0++
		loop
		break

	case 1 // Eggman Tantrum
		object.timer++
		if object.timer == 8
			object.timer = 0

			GetTableValue(object.frame, object.frameTablePos, CreditsEggman_frameTable)
			object.frameTablePos++
			object.frameTablePos &= 15

			if object.frame == 4
				// If this is the frame where Eggman lands after a jump, then spawn some dust
				
				temp1 = object.ypos
				temp1 += 0x140000
				temp0 = object.xpos
				temp0 -= 0x150000
				CreateTempObject(TypeName[Credits Eggman], 10, temp0, temp1)
				object[tempObjectPos].xvel = -0x10000
				object[tempObjectPos].frame = 16
				
				temp0 = object.xpos
				temp0 += 0x150000
				CreateTempObject(TypeName[Credits Eggman], 10, temp0, temp1)
				object[tempObjectPos].xvel = 0x10000
				object[tempObjectPos].frame = 16
			end if
		end if
		break

	case 9 // Knux Stinger
		if stage.playerListPos != PLAYER_KNUX_ALONE
			if object.timer < 166
				object.timer++
			else
				if object.alpha < 0x100
					object.alpha += 2
				end if
			end if
		end if
		break
		
	case 10 // Landing Dust
		object.animationTimer++
		if object.animationTimer >= 4
			object.animationTimer = 0
			object.frame++
			if object.frame >= 20
				object.type = TypeName[Blank Object]
			end if
		end if

		object.xpos += object.xvel
		object.ypos -= 0x10000
		if object.xvel != 0
			if object.xvel > 0
				object.xvel -= 0x1000
			else
				object.xvel += 0x1000
			end if
		end if
		break
		
	end switch
end event


event ObjectDraw
	DrawSprite(object.frame)

	if object.propertyValue == 9
		DrawSpriteFX(21, FX_INK, object.xpos, object.ypos)
	end if
end event


event ObjectStartup
	LoadSpriteSheet("Ending/Credits.gif")
	SpriteFrame(-29, -23, 60, 50, 1, 1)			// Eggman Juggle - Frame 1 		- #0
	SpriteFrame(-29, -24, 58, 51, 62, 1)		// Eggman Juggle - Frame 2 		- #1
	SpriteFrame(-31, -23, 60, 50, 121, 1)		// Eggman Juggle - Frame 3 		- #2
	SpriteFrame(-29, -24, 58, 51, 182, 1)		// Eggman Juggle - Frame 4 		- #3
	SpriteFrame(-29, -19, 58, 39, 52, 53)		// Eggman Tantrum - Frame 1 	- #4
	SpriteFrame(-29, -46, 58, 62, 52, 93)		// Eggman Tantrum - Frame 2 	- #5
	SpriteFrame(-29, -60, 58, 42, 52, 156)		// Eggman Tantrum - Frame 3 	- #6
	SpriteFrame(-29, -19, 58, 39, 52, 53)		// Eggman Tantrum - Frame 4 	- #7
	SpriteFrame(-8, -8, 16, 16, 1, 52)			// Emerald 1 (Copy) 			- #8
	SpriteFrame(-8, -8, 16, 16, 1, 52)			// Emerald 1 					- #9
	SpriteFrame(-8, -8, 16, 16, 18, 52)			// Emerald 2 					- #10
	SpriteFrame(-8, -8, 16, 16, 35, 52)			// Emerald 3 					- #11
	SpriteFrame(-8, -8, 16, 16, 1, 69)			// Emerald 4 					- #12
	SpriteFrame(-8, -8, 16, 16, 18, 69)			// Emerald 5 					- #13
	SpriteFrame(-8, -8, 16, 16, 35, 69)			// Emerald 6 					- #14
	SpriteFrame(-8, -8, 16, 16, 1, 86)			// Emerald 7 					- #15
	SpriteFrame(-4, -4, 8, 7, 18, 86)			// Dust Puff - Frame 1 			- #16
	SpriteFrame(-4, -4, 8, 7, 27, 86)			// Dust Puff - Frame 2 			- #17
	SpriteFrame(-4, -4, 8, 7, 36, 86)			// Dust Puff - Frame 3 			- #18
	SpriteFrame(-3, -3, 6, 6, 45, 86)			// Dust Puff - Frame 4 			- #19
	SpriteFrame(-85, -22, 170, 44, 52, 211)		// Knux Shrubs 					- #20
	SpriteFrame(38, -5, 12, 7, 128, 203)		// Knux Eyes 					- #21

	foreach (TypeName[Credits Eggman], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].priority = PRIORITY_BOUNDS_SMALL
		switch object[arrayPos0].propertyValue
		case 0 // Eggman Juggle
			arrayPos1 = arrayPos0
			arrayPos1 += 7
			temp0 = 0
			temp2 = 0
			while temp0 < 7
				GetBit(temp1, specialStage.emeralds, temp0)
				if temp1 == false
					object[arrayPos1].propertyValue = temp0
					object[arrayPos1].propertyValue += 2
					object[arrayPos1].timer = temp2
					object[arrayPos1].timer *= 9
					arrayPos1--
					temp2++
				end if
				temp0++
			loop

			while arrayPos1 > arrayPos0
				object[arrayPos1].type = TypeName[Blank Object]
				arrayPos1--
			loop
			break

		case 1 // Eggman Tantrum
			object[arrayPos0].frame = 4
			break

		case 2 // Emerald 1
		case 3 // Emerald 2
		case 4 // Emerald 3
		case 5 // Emerald 4
		case 6 // Emerald 5
		case 7 // Emerald 6
		case 8 // Emerald 7
			object[arrayPos0].frame = object[arrayPos0].propertyValue
			object[arrayPos0].frame += 7
			break
			
		end switch
	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 // 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
			
			if editor.variableValue == 9
				// Knuckles uses object.frame
				object.frame = 20
			end if
			break
			
		end switch
	end if
end event


event RSDKDraw
	switch object.propertyValue
	case 0 // Eggman Juggle
		object.frame = 0
		break

	case 1 // Eggman Tantrum
		object.frame = 4
		break

	case 2 // Emerald 1
	case 3 // Emerald 2
	case 4 // Emerald 3
	case 5 // Emerald 4
	case 6 // Emerald 5
	case 7 // Emerald 6
	case 8 // Emerald 7
		object.frame = object.propertyValue
		object.frame += 7
		break
		
	// Knuckles's frame should be set via object.frame attr
	
	end switch

	DrawSprite(object.frame)

	if object.propertyValue == 9
		DrawSprite(21)
	end if
end event


event RSDKLoad
	LoadSpriteSheet("Ending/Credits.gif")
	SpriteFrame(-29, -23, 60, 50, 1, 1)			// Eggman Juggle - Frame 1 		- #0
	SpriteFrame(-29, -24, 58, 51, 62, 1)		// Eggman Juggle - Frame 2 		- #1
	SpriteFrame(-31, -23, 60, 50, 121, 1)		// Eggman Juggle - Frame 3 		- #2
	SpriteFrame(-29, -24, 58, 51, 182, 1)		// Eggman Juggle - Frame 4 		- #3
	SpriteFrame(-29, -19, 58, 39, 52, 53)		// Eggman Tantrum - Frame 1 	- #4
	SpriteFrame(-29, -46, 58, 62, 52, 93)		// Eggman Tantrum - Frame 2 	- #5
	SpriteFrame(-29, -60, 58, 42, 52, 156)		// Eggman Tantrum - Frame 3 	- #6
	SpriteFrame(-29, -19, 58, 39, 52, 53)		// Eggman Tantrum - Frame 4 	- #7
	SpriteFrame(-8, -8, 16, 16, 1, 52)			// Emerald 1 (Copy) 			- #8
	SpriteFrame(-8, -8, 16, 16, 1, 52)			// Emerald 1 					- #9
	SpriteFrame(-8, -8, 16, 16, 18, 52)			// Emerald 2 					- #10
	SpriteFrame(-8, -8, 16, 16, 35, 52)			// Emerald 3 					- #11
	SpriteFrame(-8, -8, 16, 16, 1, 69)			// Emerald 4 					- #12
	SpriteFrame(-8, -8, 16, 16, 18, 69)			// Emerald 5 					- #13
	SpriteFrame(-8, -8, 16, 16, 35, 69)			// Emerald 6 					- #14
	SpriteFrame(-8, -8, 16, 16, 1, 86)			// Emerald 7 					- #15
	SpriteFrame(-4, -4, 8, 7, 18, 86)			// Dust Puff - Frame 1 			- #16
	SpriteFrame(-4, -4, 8, 7, 27, 86)			// Dust Puff - Frame 2 			- #17
	SpriteFrame(-4, -4, 8, 7, 36, 86)			// Dust Puff - Frame 3 			- #18
	SpriteFrame(-3, -3, 6, 6, 45, 86)			// Dust Puff - Frame 4 			- #19
	SpriteFrame(-85, -22, 170, 44, 52, 211)		// Knux Shrubs 					- #20
	SpriteFrame(38, -5, 12, 7, 128, 203)		// Knux Eyes 					- #21
	
	AddEditorVariable("type")
	SetActiveVariable("type")
	AddEnumVariable("Eggman (Juggling)", 0)
	AddEnumVariable("Eggman (Tantrum)", 1)
	AddEnumVariable("Chaos Emerald 1", 2)
	AddEnumVariable("Chaos Emerald 2", 3)
	AddEnumVariable("Chaos Emerald 3", 4)
	AddEnumVariable("Chaos Emerald 4", 5)
	AddEnumVariable("Chaos Emerald 5", 6)
	AddEnumVariable("Chaos Emerald 6", 7)
	AddEnumVariable("Chaos Emerald 7", 8)
	AddEnumVariable("Knux Teaser", 9)
	// 10 is skipped over on purpose, it's not to be set from the editor
	
	// frame should be set from editor as well
end event
