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

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

private alias 0 : VDROPBRIDGE_CLOSED
private alias 1 : VDROPBRIDGE_OPENING_L
private alias 2 : VDROPBRIDGE_OPENING_R
private alias 3 : VDROPBRIDGE_OPEN_L
private alias 4 : VDROPBRIDGE_OPEN_R

// Activator switch alias
private alias object.propertyValue : pullSwitch.pulled


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

reserve function VDropBridge_DebugDraw
reserve function VDropBridge_DebugSpawn


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

private function VDropBridge_DebugDraw
	DrawSprite(0)
end function


private function VDropBridge_DebugSpawn
	CreateTempObject(TypeName[V Drop Bridge], 0, object.xpos, object.ypos)
	object[tempObjectPos].direction = object.direction
	object[tempObjectPos].angle = 256
end function


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

event ObjectUpdate
	switch object.state
	case VDROPBRIDGE_CLOSED
		if pullSwitch[-1].pulled == true
			if object.direction == FLIP_NONE
				object.state = VDROPBRIDGE_OPENING_R
			else
				object.state = VDROPBRIDGE_OPENING_L
			end if

			if object.priority != PRIORITY_XBOUNDS_DESTROY
				object.priority = PRIORITY_ACTIVE
			end if

			PlaySfx(SfxName[Drop Bridge], false)
		end if

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

	case VDROPBRIDGE_OPENING_L
		if object.angle < 384
			object.angle += 2
		else
			object.state = VDROPBRIDGE_OPEN_L
			PlaySfx(SfxName[Drop Bridge 2], false)
		end if

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

	case VDROPBRIDGE_OPENING_R
		if object.angle > 128
			object.angle -= 2
		else
			object.state = VDROPBRIDGE_OPEN_R
			PlaySfx(SfxName[Drop Bridge 2], false)
		end if

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

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

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

	end switch

	if object.outOfBounds == true
		if object.priority != PRIORITY_XBOUNDS_DESTROY
			// If offscreen, reset this object
			object.angle = 256
			object.state = VDROPBRIDGE_CLOSED
		end if
	end if
end event


event ObjectDraw
	// Draw all the bridge segments with a loop
	temp0 = 0
	temp1 = 0x800
	while temp0 < 8
		Sin(temp2, object.angle)
		temp2 *= temp1
		temp2 += object.xpos

		Cos(temp3, object.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[V Drop Bridge], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].angle = 256
		object[arrayPos0].direction = object[arrayPos0].propertyValue
		object[arrayPos0].direction &= FLIP_X
	next

	SetTableValue(TypeName[V Drop Bridge], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(VDropBridge_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(VDropBridge_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

event RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			checkResult = object.propertyValue
			break
			
		case 0 // openDir
			checkResult = object.propertyValue
			checkResult &= 1
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // openDir
			object.propertyValue = editor.variableValue
			break
			
		end switch
	end if
end event


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

	if editor.showGizmos == true
		editor.drawingOverlay = true
		
		// Draw semi-transparent version of how the bridge will be when lowered

		if object.propertyValue == 0
			temp4 = 128
		else
			temp4 = 384
		end if

		object.inkEffect = INK_BLEND
		
		temp0 = 0
		temp1 = 0x800
		while temp0 < 8
			Sin(temp2, temp4)
			temp2 *= temp1
			temp2 += object.xpos
			Cos(temp3, temp4)
			temp3 *= temp1
			temp3 += object.ypos
			DrawSpriteFX(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)

	AddEditorVariable("openDir")
	SetActiveVariable("openDir")
	AddEnumVariable("Right", 0)
	AddEnumVariable("Left",  1)
end event
