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

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

private alias object.value0 : object.ballAngle
private alias object.value1 : object.ballPos.x
private alias object.value2 : object.ballPos.y

private alias 0 : LAMPPOST_INACTIVE
private alias 1 : LAMPPOST_SPINNING
private alias 2 : LAMPPOST_ACTIVE

// 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.value0 : player.rings

private alias 0 : SLOT_PLAYER1


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

reserve function LampPost_DebugDraw
reserve function LampPost_DebugSpawn


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

private function LampPost_DebugDraw
	DrawSprite(0)
end function


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


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

event ObjectUpdate
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		if object.state == LAMPPOST_INACTIVE
			BoxCollisionTest(C_TOUCH, object.entityPos, -8, -44, 8, 20, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == true
				lampPostID = object.entityPos
#platform: USE_ORIGINS
				recScore = player.score
				recRing = player[0].rings
#endplatform
				recMilliSeconds = stage.milliSeconds
				recSeconds = stage.seconds
				recMinutes = stage.minutes
				recWaterState = stage.waterState
				recWaterLevel = stage.waterLevel
#platform: USE_ORIGINS
				StatsUsabilityParam1 = StageStatsUsabilityParam1
				StatsUsabilityParam2 = StageStatsUsabilityParam2
				StatsUsabilityParam3 = StageStatsUsabilityParam3
				StatsUsabilityParam4 = StageStatsUsabilityParam4
				StatsUsabilityParam5 = StageStatsUsabilityParam5
#endplatform
				object.state = LAMPPOST_SPINNING
				object.ballAngle = 0x180
				if object.priority != PRIORITY_XBOUNDS_DESTROY
					object.priority = PRIORITY_ACTIVE
				end if
				PlaySfx(SfxName[Lamp Post], false)
#platform: USE_ORIGINS
				if options.attractMode == false
					// That little autosave icon, I sure hope you like it...
					CallNativeFunction2(NotifyCallback, NOTIFY_TOUCH_CHECKPOINT, 0)
				end if
#endplatform
			end if
		end if
	next

	if object.state == LAMPPOST_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 += 0x20
		if object.ballAngle > 0x580
			object.ballAngle = 0
			object.state = LAMPPOST_ACTIVE
			if object.priority != PRIORITY_XBOUNDS_DESTROY
				object.priority = PRIORITY_BOUNDS
			end if
		end if
	end if
end event


event ObjectDraw
	switch object.state
	case LAMPPOST_INACTIVE
		DrawSprite(0)
		break

	case LAMPPOST_SPINNING
		DrawSprite(1)
		DrawSpriteXY(3, object.ballPos.x, object.ballPos.y)
		break

	case LAMPPOST_ACTIVE
		DrawSprite(1)
		object.ballAngle++
		if object.ballAngle == 8
			object.ballAngle = 0
		end if

		temp0 = object.ypos
		temp0 -= 0x240000
		DrawSpriteXY(3, object.xpos, temp0)
		break
		
	end switch
end event


event ObjectStartup
	LoadSpriteSheet("Global/Items.gif")

	SpriteFrame(-8, -44, 16, 64, 1, 137) // 0 - Full checkpoint sprite
	SpriteFrame(-8, -28, 16, 48, 1, 153) // 1 - Checkpoint body sprite
	SpriteFrame(-8, -8, 16, 16, 1, 137)  // 2 - Blue ball sprite
	SpriteFrame(-8, -8, 16, 16, 1, 236)  // 3 - Red ball sprite
	// (Weird green ball next to these on the sheet is unused)

	SetTableValue(TypeName[Lamp Post], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(LampPost_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(LampPost_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++

	if lampPostID > 31
		arrayPos0 = lampPostID

		// Move all Players to the Lamp Post
		currentPlayer = SLOT_PLAYER1
		while currentPlayer < playerCount
			player[currentPlayer].xpos = object[arrayPos0].xpos
			player[currentPlayer].ypos = object[arrayPos0].ypos
			currentPlayer++
		loop

#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

		camera[0].xpos = player[0].ixpos
		camera[0].ypos = player[0].iypos
		
		object[arrayPos0].state = LAMPPOST_ACTIVE
		
		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)
	
	// unused, BUT there are values in the editor which suggest it's supposed to be "lampPostID"
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
