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

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

private alias object.propertyValue : object.id

private alias object.value0  : object.ballAngle
private alias object.value1  : object.ballPos.x
private alias object.value2  : object.ballPos.y
private alias object.value3  : object.starTimer	
private alias object.value4  : object.starType	
private alias object.value5  : object.starAngleX	
private alias object.value6  : object.starAngleY
private alias object.value7  : object.starRadius
private alias object.value8  : object.starFrame
private alias object.value9  : object.timer
private alias object.value10 : object.targetPlayer

private alias 0 : STARPOST_AWAITPLAYER
private alias 1 : STARPOST_SPUN
private alias 2 : STARPOST_SHOWINGSTARS
private alias 3 : STARPOST_TOSPECIALSTAGE
private alias 4 : STARPOST_AWAITOTHERPLAYER

private alias 0 : STARPOST_ANI_IDLE
private alias 1 : STARPOST_ANI_SPINNING
private alias 2 : STARPOST_ANI_SPUN

// Player Aliases
private alias object.xpos    : player.xpos
private alias object.ypos    : player.ypos
private alias object.ixpos   : player.ixpos
private alias object.iypos   : player.iypos
private alias object.controlMode : player.controlMode
private alias object.value0  : player.rings
private alias object.value16 : player.isSidekick

// Game Modes
private alias 2 : MODE_TIMEATTACK

// Tracks
private alias 1 : TRACK_ACTFINISH


// ========================
// Function Declarations
// ========================

reserve function StarPost_DebugDraw
reserve function StarPost_DebugSpawn


// ========================
// Function Definitions
// ========================

private function StarPost_DebugDraw
	DrawSprite(2)
end function


private function StarPost_DebugSpawn
	CreateTempObject(TypeName[Star Post], 0, object.xpos, object.ypos)
end function


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

event ObjectUpdate
	switch object.state
	case STARPOST_AWAITPLAYER
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if player[currentPlayer].isSidekick == false
				BoxCollisionTest(C_TOUCH, object.entityPos, -8, -44, 8, 20, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				if checkResult == true
					if options.vsMode == true
						if currentPlayer == 0
							vs.restartX1 = object.xpos
							vs.restartY1 = object.ypos
							object.targetPlayer = 1
						else
							vs.restartX2 = object.xpos
							vs.restartY2 = object.ypos
							object.targetPlayer = 0
						end if

						object.state = STARPOST_AWAITOTHERPLAYER
					else
						starPostID = object.entityPos
						foreach (TypeName[Star Post], arrayPos0, ALL_ENTITIES)
							if object[arrayPos0].id < object.id
								object[arrayPos0].state = STARPOST_SPUN
								object[arrayPos0].animation = STARPOST_ANI_SPUN
								object[arrayPos0].ballAngle = 0
							end if
						next

						if specialStage.emeralds < 127
							if options.gameMode != MODE_TIMEATTACK
								if player[0].rings >= 50
									object.state = STARPOST_SHOWINGSTARS
									object.starType = 4
									object.starTimer = 0
									object.starAngleY = 0
									object.starAngleX = 0
									object.starRadius = 0
								else
									object.state = STARPOST_SPUN
								end if
							else
								object.state = STARPOST_SPUN
							end if
						else
							object.state = STARPOST_SPUN
						end if
					end if

#platform: USE_ORIGINS
					StatsUsabilityParam1 = StageStatsUsabilityParam1
					StatsUsabilityParam2 = StageStatsUsabilityParam2
					StatsUsabilityParam3 = StageStatsUsabilityParam3
					StatsUsabilityParam4 = StageStatsUsabilityParam4
					StatsUsabilityParam5 = StageStatsUsabilityParam5
					
					recScore = player.score
					recRing = player[0].rings
#endplatform
					recMilliSeconds = stage.milliSeconds
					recSeconds = stage.seconds
					recMinutes = stage.minutes
					recWaterState = stage.waterState
					recWaterLevel = stage.waterLevel
					
					object.ballAngle = 0x180

					if object.priority != PRIORITY_XBOUNDS_DESTROY
						object.priority = PRIORITY_ACTIVE
					end if

					object.animation = STARPOST_ANI_SPINNING
					PlaySfx(SfxName[Star Post], false)

#platform: USE_ORIGINS
					if options.attractMode == false
						CallNativeFunction2(NotifyCallback, NOTIFY_TOUCH_CHECKPOINT, 0)
					end if
#endplatform
				end if
			end if
		next
		break

	case STARPOST_SPUN
		object.animationTimer++
		object.animationTimer &= 7
		object.frame = object.animationTimer
		object.frame >>= 2
		break

	case STARPOST_SHOWINGSTARS
		object.starAngleX += 18
		object.starAngleX &= 0x1FF
		object.starAngleY += 4
		object.starAngleY &= 0x1FF

		if object.starTimer < 128
			object.starRadius++
		else
			if object.starTimer > 472
				object.starRadius--
			end if
		end if

		object.starTimer++
		if object.starTimer == 600
			object.state = STARPOST_SPUN
			object.starTimer = 0
			object.starType = 0
			if object.priority != PRIORITY_XBOUNDS_DESTROY
				object.priority = PRIORITY_BOUNDS
			end if
		end if

		object.animationTimer++
		object.animationTimer &= 7
		object.frame = object.animationTimer
		object.frame >>= 2
		temp0 = object.starRadius
		temp0 >>= 2
		FlipSign(temp0)
		temp1 = object.starRadius
		temp1 >>= 2

		if object.starTimer > 60
			BoxCollisionTest(C_TOUCH, object.entityPos, temp0, -64, temp1, -56, 0, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == true
				PlaySfx(SfxName[Warp], false)
				object.state = STARPOST_TOSPECIALSTAGE
				object.priority = PRIORITY_ALWAYS
				stage.state = STAGE_FROZEN
				player[0].controlMode = CONTROLMODE_NONE
				fadeColor = 208
				fadeColor <<= 16
				temp0 = 0xFF
				temp0 <<= 8
				fadeColor += temp0
				fadeColor += 0xE0
				object.timer = 0
			end if
		end if
		break

	case STARPOST_TOSPECIALSTAGE
		options.touchControls = false
		music.volume -= 2
		object.timer += 8
		SetScreenFade(208, 255, 224, object.timer)
		if object.timer == 0x400
			specialStage.nextZone = stage.listPos
			stage.activeList = SPECIAL_STAGE
			stage.listPos = specialStage.listPos
			LoadStage()
		end if
		break

	case STARPOST_AWAITOTHERPLAYER
		currentPlayer = object.targetPlayer
		BoxCollisionTest(C_TOUCH, object.entityPos, -8, -44, 8, 20, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		if checkResult == true
			if currentPlayer == 0
				vs.restartX1 = object.xpos
				vs.restartY1 = object.ypos
			else
				vs.restartX2 = object.xpos
				vs.restartY2 = object.ypos
			end if

			if object.animation != STARPOST_ANI_SPINNING
				object.ballAngle = 0x180
			end if

			if object.priority != PRIORITY_XBOUNDS_DESTROY
				object.priority = PRIORITY_ACTIVE
			end if
			object.animation = STARPOST_ANI_SPINNING
			object.state = STARPOST_SPUN
			PlaySfx(SfxName[Star Post], false)
		end if

		object.animationTimer++
		object.animationTimer &= 7
		object.frame = object.animationTimer
		object.frame >>= 2
		break
		
	end switch

	if object.animation == STARPOST_ANI_SPINNING
		Cos(object.ballPos.x, object.ballAngle)
		object.ballPos.x *= -0x500

		Sin(object.ballPos.y, object.ballAngle)
		object.ballPos.y *= 0x500

		object.ballPos.x += object.xpos
		object.ballPos.y += object.ypos
		object.ballPos.y -= 0x1A0000
		object.ballAngle += 32
		if object.ballAngle > 0x580
			object.ballAngle = 0
			object.animation = STARPOST_ANI_SPUN
			if object.priority != PRIORITY_XBOUNDS_DESTROY
				if object.starType == 0
					object.priority = PRIORITY_BOUNDS
				end if
			end if
		end if
	end if
end event


event ObjectDraw
	switch object.animation
	case STARPOST_ANI_IDLE
		DrawSprite(2)
		break

	case STARPOST_ANI_SPINNING
		DrawSprite(3)
		DrawSpriteXY(0, object.ballPos.x, object.ballPos.y)
		break

	case STARPOST_ANI_SPUN
		DrawSprite(3)
		temp0 = object.ypos
		temp0 -= 0x240000
		DrawSpriteXY(object.frame, object.xpos, temp0)
		break
		
	end switch

	if object.starType > 0
		temp0 = 0
		temp1 = object.starAngleX
		Sin(temp4, object.starAngleY)
		temp4 *= 3
		object.starFrame = object.starAngleY
		object.starFrame &= 15
		object.starFrame >>= 2
		object.starFrame += object.starType
		while temp0 < 4
			Sin(temp2, temp1)
			temp2 <<= 12
			Cos(temp3, temp1)
			temp3 <<= 10
			Sin(temp5, temp1)
			temp5 *= temp4
			temp3 += temp5
			temp2 *= object.starRadius
			temp2 >>= 7
			temp3 *= object.starRadius
			temp3 >>= 7
			temp2 += object.xpos
			temp3 += object.ypos
			temp3 -= 0x3C0000
			DrawSpriteXY(object.starFrame, temp2, temp3)
			temp0++
			temp1 += 128
		loop
	end if
end event


event ObjectStartup
	LoadSpriteSheet("Global/Items.gif")
	SpriteFrame(-8, -8, 16, 16, 1, 137)
	SpriteFrame(-8, -8, 16, 16, 1, 202)
	SpriteFrame(-8, -44, 16, 64, 1, 137)
	SpriteFrame(-8, -28, 16, 48, 1, 153)
	SpriteFrame(-1, -1, 3, 3, 76, 214)
	SpriteFrame(-3, -3, 7, 7, 68, 214)
	SpriteFrame(-7, -7, 15, 15, 52, 214)
	SpriteFrame(-3, -3, 7, 7, 68, 214)

	SetTableValue(TypeName[Star Post], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(StarPost_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(StarPost_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++

	if starPostID >= 32
		arrayPos0 = starPostID
		currentPlayer = 0
		while currentPlayer < playerCount
			player[currentPlayer].xpos = object[arrayPos0].xpos
			player[currentPlayer].ypos = object[arrayPos0].ypos
			currentPlayer++
		loop

		foreach (TypeName[Star Post], arrayPos1, ALL_ENTITIES)
			if object[arrayPos1].id < object[arrayPos0].id
				object[arrayPos1].state = STARPOST_SPUN
				object[arrayPos1].animation = STARPOST_ANI_SPUN
				object[arrayPos1].ballAngle = 0
			end if
		next

		camera[0].xpos = player[0].ixpos
		camera[0].ypos = player[0].iypos
		
		object[arrayPos0].state = STARPOST_SPUN
		object[arrayPos0].animation = STARPOST_ANI_SPUN
		
#platform: USE_ORIGINS
		player.score = recScore
		
		if player.deadResetRings == true
			player[0].rings = 0
			player.deadResetRings = false
		else
			player[0].rings = recRing
		end if
		
		ringExtraLife = 100
		ringExtraLife += player[0].rings
		temp0 = player[0].rings
		temp0 %= 100
		ringExtraLife -= temp0
		if ringExtraLife > 300
			ringExtraLife = 1000
		end if
#endplatform

		stage.milliSeconds = recMilliSeconds
		stage.seconds = recSeconds
		stage.minutes = recMinutes
		
#platform: USE_ORIGINS
		StageStatsUsabilityParam1 = StatsUsabilityParam1
		StageStatsUsabilityParam2 = StatsUsabilityParam2
		StageStatsUsabilityParam3 = StatsUsabilityParam3
		StageStatsUsabilityParam4 = StatsUsabilityParam4
		StageStatsUsabilityParam5 = StatsUsabilityParam5
	else
		game.timeOver = false
#endplatform
	end if
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("Global/Items.gif")
	SpriteFrame(-8, -44, 16, 64, 1, 137)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "id")
end event
