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

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

private alias object.frame  : object.sunFlowerFrame
private alias object.value0 : object.sunFlowerTimer
private alias object.value1 : object.extendFlowerTimer
private alias object.value2 : object.extendFlowerFrame
private alias object.value3 : object.bubbleFlowerTimer
private alias object.value4 : object.bubbleFlowerFrame
private alias object.value5 : object.bubbleFlowerTimer2 // tile has a BG behind it
private alias object.value6 : object.bubbleFlowerFrame2 // tile has a BG behind it
private alias object.value7 : object.palRotateTimer

// Reserved object slots
private alias 10 : SLOT_ZONESETUP


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

event ObjectUpdate
	// This object's state never changes...
	// (This check is in the original GHZSetup script too, which this object is copied from, and this check is equally as confusing there...)
	if object.state < 5
		object.palRotateTimer++
		if object.palRotateTimer == 6
			object.palRotateTimer = 0
			RotatePalette(0, 168, 171, true)
		end if
	end if

	if object.sunFlowerTimer == 0
		object.sunFlowerTimer = 20
		temp0 = 768
		temp0 += object.sunFlowerFrame
		Copy16x16Tile(427, temp0)
		temp0++
		Copy16x16Tile(428, temp0)
		temp0++
		Copy16x16Tile(429, temp0)
		temp0++
		Copy16x16Tile(430, temp0)

		temp0 = 796
		temp0 += object.sunFlowerFrame
		Copy16x16Tile(421, temp0)
		temp0++
		Copy16x16Tile(422, temp0)
		temp0++
		Copy16x16Tile(423, temp0)
		temp0++
		Copy16x16Tile(424, temp0)
		object.sunFlowerFrame += 4
		object.sunFlowerFrame &= 7
	else
		object.sunFlowerTimer--
	end if

	if object.extendFlowerTimer == 0
		switch object.extendFlowerFrame
		case 0
			object.extendFlowerTimer = 160
			temp0 = 0
			break

		case 1
			object.extendFlowerTimer = 10
			temp0 = 6
			break

		case 2
			object.extendFlowerTimer = 160
			temp0 = 12
			break

		case 3
			object.extendFlowerTimer = 10
			temp0 = 6
			break
			
		end switch

		temp0 += 776
		Copy16x16Tile(431, temp0)
		temp0++
		Copy16x16Tile(432, temp0)
		temp0++
		Copy16x16Tile(433, temp0)
		temp0++
		Copy16x16Tile(434, temp0)
		temp0++
		Copy16x16Tile(435, temp0)
		temp0++
		Copy16x16Tile(436, temp0)
		object.extendFlowerFrame++
		object.extendFlowerFrame &= 3
	else
		object.extendFlowerTimer--
	end if

	if object.bubbleFlowerTimer == 0
		switch object.bubbleFlowerFrame
		case 0
			object.bubbleFlowerTimer = 16
			temp0 = 0
			break

		case 1
			object.bubbleFlowerTimer = 16
			temp0 = 6
			break

		case 2
			object.bubbleFlowerTimer = 16
			temp0 = 12
			break

		case 3
			object.bubbleFlowerTimer = 16
			temp0 = 6
			break
			
		end switch
		
		temp0 += 816
		Copy16x16Tile(411, temp0)
		temp0++
		Copy16x16Tile(412, temp0)
		temp0++
		Copy16x16Tile(413, temp0)
		temp0++
		Copy16x16Tile(414, temp0)
		temp0++
		Copy16x16Tile(415, temp0)
		temp0++
		Copy16x16Tile(416, temp0)
		object.bubbleFlowerFrame++
		object.bubbleFlowerFrame &= 3
	else
		object.bubbleFlowerTimer--
	end if

	if object.bubbleFlowerTimer2 == 0
		switch object.bubbleFlowerFrame2
		case 0
			object.bubbleFlowerTimer2 = 16
			temp0 = 0
			break

		case 1
			object.bubbleFlowerTimer2 = 16
			temp0 = 4
			break

		case 2
			object.bubbleFlowerTimer2 = 16
			temp0 = 8
			break

		case 3
			object.bubbleFlowerTimer2 = 16
			temp0 = 4
			break
			
		end switch

		temp0 += 804
		Copy16x16Tile(417, temp0)
		temp0++
		Copy16x16Tile(418, temp0)
		temp0++
		Copy16x16Tile(419, temp0)
		temp0++
		Copy16x16Tile(420, temp0)
		object.bubbleFlowerFrame2++
		object.bubbleFlowerFrame2 &= 3
	else
		object.bubbleFlowerTimer2--
	end if
end event


event ObjectStartup
	SetMusicTrack("Ending.ogg", 0, false)

	animalType1 = TypeName[Pocky]
	animalType2 = TypeName[Flicky]

	object[SLOT_ZONESETUP].type 				= TypeName[Ending Setup]
	object[SLOT_ZONESETUP].priority 			= PRIORITY_ACTIVE
	object[SLOT_ZONESETUP].bubbleFlowerFrame2 	= 8
	
	temp0 = 424
	temp0 -= screen.xcenter
	stage.newXBoundary1 = temp0

	if specialStage.emeralds < 0x3F
		temp0 = tileLayer[0].xsize
		temp0 <<= 7
		temp0 -= 256
		stage.curXBoundary2 = temp0
	else
		stage.curXBoundary2 = 0x700
	end if

	stage.curYBoundary1 = 264
	stage.curYBoundary2 = 504

	// try to unlock these bad boys
	// despite tails and knuckles being playable at the start, this code is still in Origins LOL
	ReadSaveRAM()
	if options.stageSelectFlag == false
		saveRAM[43] |= true // tails unlock flag
		if specialStage.emeralds >= 0x3F
			saveRAM[44] |= true // knuckles unlock flag
		end if
		
		// open up all levels for time attack
		saveRAM[45] = 20
	end if

#platform: USE_ORIGINS	
	if specialStage.emeralds >= 0x3F
		CallNativeFunction2(NotifyCallback, NOTIFY_GOOD_ENDING, 0)
	end if
#endplatform

	WriteSaveRAM()
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
