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

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

// Variables
private alias object.value0 : object.spinCount
private alias object.value1 : object.spinTimer
private alias object.value2 : object.clearTimer
private alias object.value3 : object.signPostLanded

// States
private alias 0 : FALLSIGNPOST_AWAITMISSIONCLEAR
private alias 1 : FALLSIGNPOST_FALLTOGROUND
private alias 2 : FALLSIGNPOST_SPINNINGONGROUND
private alias 3 : FALLSIGNPOST_LOCKPLAYERCONTROL

// Player Aliases
private alias object.xpos			: player.xpos
private alias object.ixpos			: player.ixpos
private alias object.outOfBounds	: player.outOfBounds
private alias object.state			: player.state
private alias object.controlMode	: player.controlMode
private alias object.interaction	: player.interaction
private alias object.up				: player.up
private alias object.down			: player.down
private alias object.left			: player.left
private alias object.right			: player.right
private alias object.jumpHold		: player.jumpHold
private alias object.jumpPress		: player.jumpPress
private alias object.xvel			: player.xvel
private alias object.yvel			: player.yvel
private alias object.speed			: player.speed
private alias object.animation		: player.animation
private alias object.value3			: player.drownTimer
private alias object.value7			: player.invincibleTimer

// Track Aliases
private alias 1 : TRACK_ACTFINISH

// Reserved Object Slot Aliases
private alias 30 : SLOT_ACTFINISH

// 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 = player[0].xpos
			object.xpos -= 0x80000
			object.ypos = screen.yoffset
			object.ypos += 24
			object.ypos <<= 16
			
			screen.cameraStyle = CAMERASTYLE_HLOCKED
			
			ObjectTileCollision(CSIDE_FLOOR, 0, 32, PATH_A)
			if checkResult == true
				object.signPostLanded = true
			end if
			
			object.state = FALLSIGNPOST_FALLTOGROUND
			PlaySfx(SfxName[Sign Post], false)
		end if
		break
		
	case FALLSIGNPOST_FALLTOGROUND
		object.clearTimer++
		object.ypos += object.yvel
		object.yvel += 0xC00
		
		ObjectTileCollision(CSIDE_FLOOR, 0, 32, PATH_A)
		if checkResult == true
			if object.signPostLanded == false
				object.spinCount = 4
				object.state = FALLSIGNPOST_SPINNINGONGROUND
			end if
		else
			object.signPostLanded = false
		end if
		
		if object.outOfBounds == true
			object.spinCount = 4
			object.state = FALLSIGNPOST_SPINNINGONGROUND
		end if
		
		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
				CreateTempObject(TypeName[Ring Sparkle], 0, temp0, temp1)
			end if
		end if
		break
		
	case FALLSIGNPOST_SPINNINGONGROUND
		object.clearTimer++
		if stage.timeEnabled == true
			music.volume -= 2
		end if
		
		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
				CreateTempObject(TypeName[Ring Sparkle], 0, temp0, temp1)
				object.spinCount--
				if object.spinCount == 0
					object.frame = 5
					object.state = 3
				end if
			end if
		end if
		break
		
	case 3
		player[0].controlMode = CONTROLMODE_NONE
		player[0].interaction = false
		player[0].up = false
		player[0].down = false
		player[0].left = false
		player[0].right = false
		player[0].jumpHold = false
		player[0].jumpPress = false
		
		CheckEqual(player[0].state, Player_State_Ground)
		temp0 = checkResult
		CheckEqual(player[0].state, Player_State_Static)
		temp0 |= checkResult
		CheckEqual(player[0].outOfBounds, true)
		temp0 |= checkResult
		temp1 = screen.xoffset
		temp1 += screen.xsize
		temp1 += 32
		CheckGreater(player[0].ixpos, temp1)
		temp0 |= checkResult
		if temp0 == true
			player[0].state = Player_State_Static
			player[0].controlMode = CONTROLMODE_NONE
			player[0].interaction = false
			player[0].up = false
			player[0].down = false
			player[0].left = false
			player[0].right = false
			player[0].jumpHold = false
			player[0].jumpPress = false
			player[0].xvel = 0
			player[0].yvel = 0
			player[0].speed = 0
			player[0].animation = ANI_WAITING
			screen.cameraEnabled = false
		end if
		break
		
	end switch
	
	if object.clearTimer > 0
		player[0].drownTimer = 0
		player[0].invincibleTimer = 80
	end if
	
	CheckGreater(object.clearTimer, 60)
	temp0 = checkResult
	CheckEqual(object[SLOT_ACTFINISH].type, TypeName[Blank Object])
	temp0 &= checkResult
	if temp0 == true
		CallNativeFunction2(NotifyCallback, NOTIFY_TOUCH_SIGNPOST, 0)
		stage.timeEnabled = false
		PlayMusic(TRACK_ACTFINISH)
		object[SLOT_ACTFINISH].type = TypeName[Act Finish]
		object[SLOT_ACTFINISH].drawOrder = 6
		object[SLOT_ACTFINISH].priority = PRIORITY_ACTIVE
	end if
end event


event ObjectDraw
	if object.state > FALLSIGNPOST_AWAITMISSIONCLEAR
		DrawSprite(0)
		DrawSprite(object.frame)
	end if
end event


event ObjectStartup
	LoadSpriteSheet("Global/Items2.gif")
	SpriteFrame(-24, 16, 48, 16, 34, 214)
	SpriteFrame(-24, -16, 48, 32, 34, 182)
	SpriteFrame(-16, -16, 32, 32, 1, 150)
	SpriteFrame(-4, -16, 8, 32, 189, 131)
	SpriteFrame(-16, -16, 32, 32, 1, 183)
	SpriteFrame(-24, -16, 48, 32, 34, 83)
	SpriteFrame(-16, -16, 32, 32, 1, 150)
	SpriteFrame(-4, -16, 8, 32, 189, 131)
	SpriteFrame(-16, -16, 32, 32, 1, 183)
	
	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)
end event


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