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

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

// Variables
// Just like the normal Sign Post, value0 is unused
private alias object.value1 : object.spinCount
private alias object.value2 : object.spinTimer
// value3 is kinda unused - it's reset by Eggman SignPost, but doesn't see any use beyond that
// TODO: double check
private alias object.value4 : object.signpostCharYPos

// States
private alias 0 : FALLSIGNPOST_AWAITMISSIONCLEAR
private alias 1 : FALLSIGNPOST_FALLTOGROUND
private alias 2 : FALLSIGNPOST_SPINNINGONGROUND
private alias 3 : FALLSIGNPOST_RESETSIGNPOST
private alias 4 : FALLSIGNPOST_LANDONGROUND

// Path ID Aliases
private alias 0 : PATH_A


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

event ObjectUpdate
	switch object.state
	case FALLSIGNPOST_AWAITMISSIONCLEAR
		if game.missionCondition == MISSION_CONDITION_CLEAR
			game.missionCondition = 0
			object.xpos = screen.xoffset
			object.xpos += screen.xcenter
			object.xpos <<= 16
			object.ypos = screen.yoffset
			object.ypos += 24
			object.ypos <<= 16
			screen.cameraStyle = CAMERASTYLE_HLOCKED // Huh? They lock the camera like this?
			object.state = FALLSIGNPOST_FALLTOGROUND
		end if
		break
		
	case FALLSIGNPOST_FALLTOGROUND
		// Fall down
		object.xpos += object.xvel
		object.ypos += object.yvel
		object.yvel += 0xC00
		
		// Check if we hit the ground and stop falling if we did
		ObjectTileCollision(CSIDE_FLOOR, 0, 32, PATH_A)
		if checkResult == true
			PlaySfx(SfxName[Sliding], false)
			object.spinCount = 4
			object.state = FALLSIGNPOST_SPINNINGONGROUND
		end if
		
		// Spin!!
		object.spinTimer++
		if object.spinTimer == 2
			object.spinTimer = 0
			object.frame++
			if object.frame == 5
				Rand(temp0, 48)
				temp0 <<= 16
				temp0 += object.xpos
				temp0 -= 0x180000
				Rand(temp1, 32)
				temp1 <<= 16
				temp1 += object.ypos
				temp1 -= 0x140000
				CreateTempObject(TypeName[Ring Sparkle], 0, temp0, temp1)
			end if
			
			if object.frame == 9
				object.frame = 1
				Rand(temp0, 48)
				temp0 <<= 16
				temp0 += object.xpos
				temp0 -= 0x180000
				Rand(temp1, 32)
				temp1 <<= 16
				temp1 += object.ypos
				temp1 -= 0x140000
				
				object.signpostCharYPos += 33
				if object.signpostCharYPos > 67
					object.signpostCharYPos = 1
				end if
				EditFrame(5, -24, -16, 48, 32, 34, object.signpostCharYPos)
				
				CreateTempObject(TypeName[Ring Sparkle], 0, temp0, temp1)
			end if
		end if
		break
		
	case FALLSIGNPOST_SPINNINGONGROUND
		music.volume -= 2
		object.spinTimer++
		if object.spinTimer == 2
			object.spinTimer = 0
			
			object.frame++
			if object.frame == 5
				Rand(temp0, 48)
				temp0 <<= 16
				temp0 += object.xpos
				temp0 -= 0x180000
				Rand(temp1, 32)
				temp1 <<= 16
				temp1 += object.ypos
				temp1 -= 0x140000
				CreateTempObject(TypeName[Ring Sparkle], 0, temp0, temp1)
			end if
			
			if object.frame == 9
				Rand(temp0, 48)
				temp0 <<= 16
				temp0 += object.xpos
				temp0 -= 0x180000
				Rand(temp1, 32)
				temp1 <<= 16
				temp1 += object.ypos
				temp1 -= 0x140000
				
				object.signpostCharYPos += 33
				if object.signpostCharYPos > 67
					object.signpostCharYPos = 1
				end if
				EditFrame(5, -24, -16, 48, 32, 34, object.signpostCharYPos)
				
				CreateTempObject(TypeName[Ring Sparkle], 0, temp0, temp1)
				
				object.spinCount--
				if object.spinCount == 0
					object.frame = 5
					object.state = 3
				else
					object.frame = 1
				end if
			end if
		end if
		break
		
	case FALLSIGNPOST_RESETSIGNPOST
		ResetObjectEntity(object.entityPos, TypeName[Sign Post], 0, object.xpos, object.ypos) // Become a real Sign Post!
		object.frame = 1
		object.spinCount = 16
		object.priority = PRIORITY_XBOUNDS
		break
		
	case FALLSIGNPOST_LANDONGROUND // Only set by other objects such as Eggman Sign Post
		PlaySfx(SfxName[Sliding], false)
		object.spinCount = 4
		object.state = FALLSIGNPOST_SPINNINGONGROUND
		break
		
	end switch
end event


event ObjectDraw
	if object.state > FALLSIGNPOST_AWAITMISSIONCLEAR
		DrawSprite(0)
		if object.spinCount > 8
			DrawSprite(object.frame)
		else
			temp0 = object.frame
			temp0 += 4
			DrawSprite(temp0)
		end if
	end if
end event


event ObjectStartup
	LoadSpriteSheet("Global/Items2.gif")
	SpriteFrame(-24, 16, 48, 16, 34, 132)
	SpriteFrame(-24, -16, 48, 32, 34, 100)
	SpriteFrame(-16, -16, 32, 32, 149, 99)
	SpriteFrame(-4, -16, 8, 32, 247, 2)
	SpriteFrame(-16, -16, 32, 32, 116, 99)
	SpriteFrame(-24, -16, 48, 32, 34, 1)
	SpriteFrame(-16, -16, 32, 32, 149, 99)
	SpriteFrame(-4, -16, 8, 32, 247, 2)
	SpriteFrame(-16, -16, 32, 32, 116, 99)
	
	foreach (TypeName[FallSignPost], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].frame = 1
		object[arrayPos0].spinCount = 16
		object[arrayPos0].priority = PRIORITY_ACTIVE
	next
end event


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

event RSDKDraw
	DrawSprite(0)
	DrawSprite(1)
end event


event RSDKLoad
	LoadSpriteSheet("Global/Items2.gif")
	SpriteFrame(-24, 16, 48, 16, 34, 132)
	SpriteFrame(-24, -16, 48, 32, 34, 100)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
