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

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

private alias object.value0 : object.timer
private alias object.value1 : object.delay

private alias 0 : CUTBRIDGE_ACTIVE
private alias 1 : CUTBRIDGE_COLLAPSE
private alias 2 : CUTBRIDGE_COLLAPSED
private alias 3 : CUTBRIDGE_NONE
private alias 4 : CUTBRIDGE_TILEDELAY
private alias 5 : CUTBRIDGE_TILEFALL

// Player Aliases
private alias object.state : player.state
private alias object.gravity : player.gravity
private alias object.animation : player.animation


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

event ObjectUpdate
	stage.newXBoundary1 = 8660
	stage.newXBoundary1 -= screen.xcenter

	temp0 = 8660
	temp0 += screen.xcenter

	if stage.newXBoundary2 > temp0
		stage.newXBoundary2 = temp0
		temp0 -= screen.xcenter
		temp0 -= screen.xcenter
		stage.newXBoundary1 = temp0
	end if

	object.priority = PRIORITY_ACTIVE
	switch object.state
	case CUTBRIDGE_ACTIVE
		break

	case CUTBRIDGE_COLLAPSE
		if object.timer > 0
			object.timer--
		else
			temp0 = object.xpos
			temp0 -= 0x80000
			temp1 = object.ypos
			CreateTempObject(TypeName[Cutscene Bridge], 1, temp0, temp1)
			object[tempObjectPos].state = CUTBRIDGE_TILEDELAY
			object[tempObjectPos].delay = 2
			temp0 += 0x100000

			CreateTempObject(TypeName[Cutscene Bridge], 2, temp0, temp1)
			object[tempObjectPos].state = CUTBRIDGE_TILEDELAY
			object[tempObjectPos].delay = 3
			temp0 -= 0x100000
			temp1 += 0x100000

			CreateTempObject(TypeName[Cutscene Bridge], 3, temp0, temp1)
			object[tempObjectPos].state = CUTBRIDGE_TILEDELAY
			object[tempObjectPos].delay = 0
			temp0 += 0x100000

			CreateTempObject(TypeName[Cutscene Bridge], 4, temp0, temp1)
			object[tempObjectPos].state = CUTBRIDGE_TILEDELAY
			object[tempObjectPos].delay = 1

			PlaySfx(SfxName[Block Break], false)
			object.state++
		end if
		break

	case CUTBRIDGE_COLLAPSED
		if object.timer < 4
			object.timer++
		else
			object.timer = 0
			object.state++
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				BoxCollisionTest(C_TOUCH, object.entityPos, -16, -10, 16, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				if checkResult == true
					if player[currentPlayer].gravity == GRAVITY_AIR
						player[currentPlayer].animation = ANI_HURT
						player[currentPlayer].state = Player_State_Hurt
					end if
				end if
			next
		end if
		break

	case CUTBRIDGE_NONE
		break

	case CUTBRIDGE_TILEDELAY
		if object.timer < object.delay
			object.timer++
		else
			object.timer = 0
			object.state++
		end if
		break

	case CUTBRIDGE_TILEFALL
		object.ypos += object.yvel
		object.yvel += 0x4000
		if object.outOfBounds == true
			object.type = TypeName[Blank Object]
		end if
		break

	end switch

	if object.state < CUTBRIDGE_NONE
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_PLATFORM, object.entityPos, -16, -8, 16, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		next
	end if
end event


event ObjectDraw
	switch object.state
	default
	case CUTBRIDGE_COLLAPSED
	case CUTBRIDGE_NONE
		break

	case CUTBRIDGE_ACTIVE
	case CUTBRIDGE_COLLAPSE
		DrawSprite(0)
		break

	case CUTBRIDGE_TILEDELAY
	case CUTBRIDGE_TILEFALL
		DrawSprite(object.propertyValue)
		break
		
	end switch
end event


event ObjectStartup
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-16, -8, 32, 32, 457, 231)
	SpriteFrame(-8, -8, 16, 16, 457, 231)
	SpriteFrame(-8, -8, 16, 16, 473, 231)
	SpriteFrame(-8, -8, 16, 16, 457, 247)
	SpriteFrame(-8, -8, 16, 16, 473, 247)
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-16, -8, 32, 32, 457, 231)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
