// ----------------------------------
// RSDK Project: Sonic 1
// 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.delay
private alias object.value2 : object.frameID

// Priority Aliases
private alias 6 : PRIORITY_BOUNDS_SMALL


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

private table CreditsEggman_delayTable
	7, 113, 7, 113
end table

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


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

event ObjectUpdate
	switch object.propertyValue
	case 0
		GetTableValue(object.delay, object.frame, CreditsEggman_delayTable)
		object.timer++
		if object.timer == object.delay
			object.timer = 0
			object.frame++
			object.frame &= 3
		end if

		switch object.frame
		case 1
			if object.timer == 0
				object[+1].angle = 32
				object[+2].angle = 32
				object[+3].angle = 32
				object[+4].angle = 32
				object[+5].angle = 32
				object[+6].angle = 32
				object[+7].angle = 32
			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
		object.timer++
		if object.timer == 8
			object.timer = 0
			GetTableValue(object.frame, object.frameID, CreditsEggman_frameTable)
			object.frameID++
			object.frameID &= 15
		end if
		break
		
	end switch
end event


event ObjectDraw
	DrawSprite(object.frame)
end event


event ObjectStartup
	LoadSpriteSheet("Ending/Credits.gif")
	
	// 0-3 - Eggman Juggle Frames
	SpriteFrame(-29, -23, 60, 50, 1, 1)
	SpriteFrame(-29, -24, 58, 51, 62, 1)
	SpriteFrame(-31, -23, 60, 50, 121, 1)
	SpriteFrame(-29, -24, 58, 51, 182, 1)
	
	// 4-7 - Eggman Tantrum Frames
	SpriteFrame(-29, -19, 58, 55, 1, 53)
	SpriteFrame(-29, -46, 58, 82, 62, 53)
	SpriteFrame(-29, -60, 58, 96, 121, 53)
	SpriteFrame(-32, -12, 64, 48, 182, 53)
	
	// 8 - Emerald Flash Frame
	SpriteFrame(-6, -6, 12, 12, 1, 109)
	
	// 9-15 - Emerald Frames
	SpriteFrame(-6, -6, 12, 12, 14, 109) // Blue
	SpriteFrame(-6, -6, 12, 12, 27, 109) // Orange
	if options.superStates == false
		SpriteFrame(-6, -6, 12, 12, 40, 109) // Pink
	else
		SpriteFrame(-6, -6, 12, 12, 14, 135) // Purple
	end if
	SpriteFrame(-6, -6, 12, 12, 1, 122)  // Green
	SpriteFrame(-6, -6, 12, 12, 14, 122) // Red
	SpriteFrame(-6, -6, 12, 12, 27, 122) // Grey
	SpriteFrame(-6, -6, 12, 12, 1, 135)  // Light blue
	
	// (The dark orange one at 27, 135 is unused)

	foreach (TypeName[Credits Eggman], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].priority = PRIORITY_BOUNDS_SMALL
		switch object[arrayPos0].propertyValue
		case 0 // Eggman - Tease
			arrayPos1 = arrayPos0
			arrayPos1 += 7
			temp0 = 0
			temp2 = 0
			while temp0 < 7
				GetBit(temp1, specialStage.emeralds, temp0)
				if temp0 == 6
					if options.superStates == false
						temp1 = true
					end if
				end if

				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 - Angry
			object[arrayPos0].frame = 4
			break
			
		case 2 // Emeralds, each type gets its own Property Value
		case 3
		case 4
		case 5
		case 6
		case 7
		case 8
			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
			break
			
		end switch
	end if
end event


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

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

	// Emeralds
	case 2
	case 3
	case 4
	case 5
	case 6
	case 7
	case 8
		object.frame = object.propertyValue
		object.frame += 7
		break
		
	end switch

	DrawSprite(object.frame)
end event


event RSDKLoad
	LoadSpriteSheet("Ending/Credits.gif")
	SpriteFrame(-29, -23, 60, 50, 1, 1)
	SpriteFrame(-29, -24, 58, 51, 62, 1)
	SpriteFrame(-31, -23, 60, 50, 121, 1)
	SpriteFrame(-29, -24, 58, 51, 182, 1)
	SpriteFrame(-29, -19, 58, 55, 1, 53)
	SpriteFrame(-29, -46, 58, 82, 62, 53)
	SpriteFrame(-29, -60, 58, 96, 121, 53)
	SpriteFrame(-32, -12, 64, 48, 182, 53)
	SpriteFrame(-6, -6, 12, 12, 1, 109)
	SpriteFrame(-6, -6, 12, 12, 14, 109)
	SpriteFrame(-6, -6, 12, 12, 27, 109)
	SpriteFrame(-6, -6, 12, 12, 40, 109)
	SpriteFrame(-6, -6, 12, 12, 1, 122)
	SpriteFrame(-6, -6, 12, 12, 14, 122)
	SpriteFrame(-6, -6, 12, 12, 27, 122)
	SpriteFrame(-6, -6, 12, 12, 1, 135)

	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)
end event
