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

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

private alias object.value0 : object.timer

// States
private alias 0 : CONTINUE_INIT
private alias 1 : CONTINUE_FADEIN
private alias 2 : CONTINUE_DISPLAY
private alias 3 : CONTINUE_REVUP
private alias 4 : CONTINUE_RUN
private alias 5 : CONTINUE_ACCEPTED
private alias 6 : CONTINUE_TIMEDOUT

// Player aliases
private alias object.type		 : player.type
private alias object.state		 : player.state
private alias object.animation	 : player.animation
private alias object.xpos		 : player.xpos
private alias object.ypos		 : player.ypos
private alias object.xvel		 : player.xvel
private alias object.speed		 : player.speed
private alias object.controlMode : player.controlMode


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

event ObjectUpdate
	switch object.state
	case CONTINUE_INIT
		object.timer = 320
		SetScreenFade(0x00, 0x00, 0x00, object.timer)
		object.state++
		break

	case CONTINUE_FADEIN
		if object.timer > 0
			object.timer -= 8
		else
			PlayMusic(0)
			object.state++

			// Tell the Continue Screen object to start counting
			object[-1].state++
		end if
		SetScreenFade(0x00, 0x00, 0x00, object.timer)
		break

	case CONTINUE_DISPLAY
		CheckTouchRect(0, 0, screen.xsize, screen.ysize)
		if checkResult > -1
			keyPress[0].start = true
		end if

		if keyPress[0].start == true
			player[0].animation = ANI_CONTINUE_UP
			player[0].speed = 0
			PlaySfx(SfxName[Charge], false)
			object.state++

			// Tell the Continue Screen object to stop counting
			object[-1].state++
		end if
		break

	case CONTINUE_REVUP
		if music.volume > 0
			// Fade out music
			music.volume--
		end if

		player[0].speed += 0x2000
		if player[0].speed == 0x90000
			player[0].xvel = 0x100000
			if player[1].type == TypeName[Blank Object]
				object.state++
			end if
		end if

		if player[1].type != TypeName[Blank Object]
			if player[0].speed == 0x20000
				player[1].animation = ANI_CONTINUE_UP
				player[1].speed = 0
				PlaySfx(SfxName[Charge], false)
			end if

			if player[0].speed > 0x20000
				player[1].speed += 0x2000
				if player[1].speed == 0x90000
					player[1].xvel = 0x100000
					object.state++
				end if
			end if
		end if
		break

	case CONTINUE_RUN
		if music.volume > 0
			music.volume--
		end if

		temp0 = screen.xsize
		temp0 += 32
		if player[1].type == TypeName[Blank Object]
			temp1 = player[0].xpos
		else
			temp1 = player[1].xpos
		end if

		temp1 >>= 16
		if temp1 >= temp0
			object.state++
		end if
		break

	case CONTINUE_ACCEPTED
		if music.volume > 0
			music.volume--
		end if

		object.timer += 4

		SetScreenFade(0x00, 0x00, 0x00, object.timer)

		if object.timer == 384
			player.continues--
			player.lives 		= 3
			fadeColor 			= 0x000000
			object.timer 		= 0
			starPostID 			= 0
			object.direction 	= FLIP_NONE // this object's direction doesn't even matter...
			stage.activeList 	= recStageList
			stage.listPos 		= recStage
			LoadStage()
		end if
		break

	case CONTINUE_TIMEDOUT
		object.timer += 4
		SetScreenFade(0x00, 0x00, 0x00, object.timer)
		if object.timer == 384
			fadeColor 			= 0x000000
			object.timer 		= 0
			starPostID 			= 0
			object.direction 	= FLIP_NONE // if you really wanna, ig
			engine.state 		= 8
		end if
		break
		
	end switch

	// Hide touch controls
	options.touchControls = false
end event


event ObjectDraw
	// Clear all old frames
	// (This level doesn't have a background so this is needed to prevent old frame smear)
	ClearScreen(1)
end event


event ObjectStartup
	foreach (TypeName[Continue Setup], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].drawOrder = 0	// Draw Order 0 so we draw before everything else
	next

	// Put the player in the middle of the screen
	player[0].xpos = screen.xcenter
	player[0].xpos <<= 16

	// Set the player's animation, although it gets set to ANI_CONTINUE a few lines below again anyways
	player[0].animation = ANI_FANROTATE

	player[0].state = Player_State_ContinueRun

	// Different starting offsets based on P1
	switch stage.playerListPos
	case PLAYER_SONIC_ALONE
	case PLAYER_SONIC_AND_TAILS
		// 172 pixels
		player[0].ypos = 0xAC0000
		break

	case PLAYER_TAILS_ALONE
		// Also 172 pixels, still separate from the above for whatever reason
		player[0].ypos = 0xAC0000
		break

	case PLAYER_KNUX_ALONE
		// 176 pixels
		player[0].ypos = 0xB00000
		break
		
	end switch

	// Set the proper P1 animation here
	player[0].animation = ANI_CONTINUE

	if player[1].type != TypeName[Blank Object]
		// Move P2 to the center of the screen too
		player[1].xpos = screen.xcenter
		player[1].xpos <<= 16

		// Set P2's animation, and then...
		player[1].animation = ANI_FANROTATE

		player[1].state = Player_State_ContinueRun

		// Move the players a bit to make room for two
		player[0].xpos -= 0x100000
		player[1].xpos += 0x100000
		player[1].ypos = player[0].ypos
		player[1].ypos += 0x40000

		// ...now set P2's animation *again*...?
		player[1].animation = ANI_BORED
	end if

	stage.newXBoundary2 = screen.xsize
	stage.newYBoundary2 = screen.ysize

	SetMusicTrack("Continue.ogg", 0, false)
	
	// Do note - this doesn't quite work when physical input is being used since the player doesn't check against this variable before pausing the game
	stage.pauseEnabled = false
	
#platform: USE_DECOMP
	// Not in the original game, but this is for the above to fixe an issue where
	// pressing start to continue also pauses the entire game
	player[0].controlMode = CONTROLMODE_NONE
#endplatform
	
	if player.continues == 0
		// Player was most likely brought here from the dev menu, give them three free continues
		player.continues = 3
	end if

	options.touchControls = false
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
