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

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

private alias object.value1 : object.left.angle
private alias object.value2 : object.right.angle
private alias object.value3 : object.left.x
private alias object.value4 : object.right.x

private alias 0 : HDROPBRIDGE_CLOSED
private alias 1 : HDROPBRIDGE_OPENING
private alias 2 : HDROPBRIDGE_OPENED

// Activator Switch Aliases
private alias object.propertyValue : pullSwitch.pulled


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

reserve function HDropBridge_DebugDraw
reserve function HDropBridge_DebugSpawn


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

private function HDropBridge_DebugDraw
	temp0 = object.xpos
	temp1 = 0
	temp0 -= 0x380000
	while temp1 < 8
		DrawSpriteXY(0, temp0, object.ypos)
		temp0 += 0x100000
		temp1++
	loop
end function


private function HDropBridge_DebugSpawn
	CreateTempObject(TypeName[H Drop Bridge], 0, object.xpos, object.ypos)
	object[tempObjectPos].drawOrder = 4
	object[tempObjectPos].left.angle = 0
	object[tempObjectPos].right.angle = 256
	object[tempObjectPos].left.x = object[tempObjectPos].xpos
	object[tempObjectPos].left.x -= 0x380000
	object[tempObjectPos].right.x = object[tempObjectPos].xpos
	object[tempObjectPos].right.x += 0x380000
end function


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

event ObjectUpdate
	switch object.state
	case HDROPBRIDGE_CLOSED
		// object[-1] is a switch of either the normal or vine variety
		if pullSwitch[-1].pulled == true
			object.state++
			PlaySfx(SfxName[Flapping Door], false)
			if object.priority != PRIORITY_XBOUNDS_DESTROY
				object.priority = PRIORITY_ACTIVE
			end if
		end if

		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_SOLID, object.entityPos, -64, -8, 64, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		next
		break

	case HDROPBRIDGE_OPENING
		if object.left.angle < 128
			object.left.angle += 8
			object.right.angle -= 8
		else
			object.state++
		end if
		// [Fallthrough]
	case HDROPBRIDGE_OPENED
		temp6 = object.xpos
		temp7 = object.ypos

		temp0 = 0
		temp1 = 0
		while temp0 < 4
			Cos(object.xpos, object.left.angle)
			object.xpos *= temp1
			object.xpos += object.left.x
			Sin(object.ypos, object.left.angle)
			object.ypos *= temp1
			object.ypos += temp7
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				BoxCollisionTest(C_SOLID, object.entityPos, -8, -8, 8, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			next
			temp0++
			temp1 += 0x800
		loop

		temp0 = 0
		temp1 = 0
		while temp0 < 4
			Cos(object.xpos, object.right.angle)
			object.xpos *= temp1
			object.xpos += object.right.x
			Sin(object.ypos, object.right.angle)
			object.ypos *= temp1
			object.ypos += temp7
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				BoxCollisionTest(C_SOLID, object.entityPos, -8, -8, 8, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			next
			temp0++
			temp1 += 0x800
		loop
		object.xpos = temp6
		object.ypos = temp7
		break

	end switch

	if object.outOfBounds == true
		if object.priority != PRIORITY_XBOUNDS_DESTROY
			object.left.angle = 0
			object.right.angle = 256
			object.state = HDROPBRIDGE_CLOSED
		end if
	end if
end event


event ObjectDraw
	// Draw the left 4 pieces first
	temp0 = 0
	temp1 = 0
	while temp0 < 4
		Cos(temp2, object.left.angle)
		temp2 *= temp1
		temp2 += object.left.x
		Sin(temp3, object.left.angle)
		temp3 *= temp1
		temp3 += object.ypos
		DrawSpriteXY(0, temp2, temp3)
		temp0++
		temp1 += 0x800
	loop

	// Then draw the right 4 pieces afterwards
	temp0 = 0
	temp1 = 0
	while temp0 < 4
		Cos(temp2, object.right.angle)
		temp2 *= temp1
		temp2 += object.right.x
		Sin(temp3, object.right.angle)
		temp3 *= temp1
		temp3 += object.ypos
		DrawSpriteXY(0, temp2, temp3)
		temp0++
		temp1 += 0x800
	loop
end event


event ObjectStartup
	LoadSpriteSheet("MCZ/Objects.gif")

	SpriteFrame(-8, -8, 16, 16, 135, 131)

	foreach (TypeName[H Drop Bridge], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].drawOrder = 4
		object[arrayPos0].left.angle = 0
		object[arrayPos0].right.angle = 256
		object[arrayPos0].left.x = object[arrayPos0].xpos
		object[arrayPos0].left.x -= 0x380000
		object[arrayPos0].right.x = object[arrayPos0].xpos
		object[arrayPos0].right.x += 0x380000
	next

	// Add this object to the debug item list
	SetTableValue(TypeName[H Drop Bridge], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(HDropBridge_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(HDropBridge_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

event RSDKDraw
	temp0 = 0
	temp1 = 0
	while temp0 < 4
		Cos(temp2, 0)
		temp2 *= temp1
		temp2 += object.xpos
		temp2 -= 0x380000
		Sin(temp3, 0)
		temp3 *= temp1
		temp3 += object.ypos
		DrawSpriteXY(0, temp2, temp3)
		temp0++
		temp1 += 0x800
	loop

	temp0 = 0
	temp1 = 0
	while temp0 < 4
		Cos(temp2, 256)
		temp2 *= temp1
		temp2 += object.xpos
		temp2 += 0x380000
		Sin(temp3, 256)
		temp3 *= temp1
		temp3 += object.ypos
		DrawSpriteXY(0, temp2, temp3)
		temp0++
		temp1 += 0x800
	loop

	if editor.showGizmos == true
		editor.drawingOverlay = true

		// Draw a semi-transparent version of how the bridge will look when opened

		object.inkEffect = INK_BLEND

		temp0 = 0
		temp1 = 0
		while temp0 < 4
			Cos(temp2, 128)
			temp2 *= temp1
			temp2 += object.xpos
			temp2 -= 0x380000
			Sin(temp3, 128)
			temp3 *= temp1
			temp3 += object.ypos
			DrawSpriteXY(0, FX_INK, temp2, temp3)
			temp0++
			temp1 += 0x800
		loop

		temp0 = 0
		temp1 = 0
		while temp0 < 4
			Cos(temp2, 128)
			temp2 *= temp1
			temp2 += object.xpos
			temp2 += 0x380000
			Sin(temp3, 128)
			temp3 *= temp1
			temp3 += object.ypos
			DrawSpriteXY(0, FX_INK, temp2, temp3)
			temp0++
			temp1 += 0x800
		loop

		editor.drawingOverlay = false
	end if
end event


event RSDKLoad
	LoadSpriteSheet("MCZ/Objects.gif")
	SpriteFrame(-8, -8, 16, 16, 135, 131)

	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
