// ----------------------------------
// RSDK Project: Sonic 1
// Script Description: Trap Door 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 object.value17 : debugMode.currentSelection

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

reserve function TrapDoor_DebugDraw
reserve function TrapDoor_DebugSpawn

// ========================
// Static Values
// ========================

private value TrapDoor_startDebugID = 0


private function TrapDoor_DebugDraw
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)

	object.direction ^= 1
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
	
	object.direction ^= 1
end function


private function TrapDoor_DebugSpawn
	CreateTempObject(TypeName[Trap Door], 0, object.xpos, object.ypos)
	object[tempObjectPos].priority = PRIORITY_XBOUNDS
	object[tempObjectPos].delay = debugMode[0].currentSelection
	object[tempObjectPos].delay -= TrapDoor_startDebugID
	object[tempObjectPos].delay++
	object[tempObjectPos].delay *= 60
end function


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

event ObjectUpdate
	object.timer--
	if object.timer < 0
		temp0 = 1
		object.timer = object.delay
		object.animation ^= 1
		temp0 = camera[0].xpos
		temp0 -= object.ixpos
		Abs(temp0)
		temp0 -= 64
		if temp0 <= screen.xcenter
			temp0 = camera[0].ypos
			temp0 -= object.iypos
			Abs(temp0)
			temp0 -= 12
			if temp0 <= screen.ycenter
				PlaySfx(SfxName[Flapping Door], false)
			end if
		end if
	end if

	if object.animation == 0
		if object.rotation > 0
			object.rotation -= 16
		end if
	else
		if object.rotation < 128
			object.rotation += 16
		end if
	end if

	if object.rotation == 0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_SOLID, object.entityPos, -64, -12, 64, 12, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		next
	end if
end event


event ObjectDraw
	temp0 = object.xpos
	temp0 -= 0x400000

	// Frame is always 0 lol
	DrawSpriteFX(object.frame, FX_ROTATE, temp0, object.ypos)

	object.direction ^= 1
	object.rotation ^= 0x1FF
	object.rotation++
	object.rotation &= 0x1FF

	temp0 += 0x800000
	temp1 = object.ypos

	if object.rotation != 0
		temp1--
	end if

	DrawSpriteFX(object.frame, FX_ROTATE, temp0, temp1)

	object.direction ^= 1

	object.rotation--
	object.rotation ^= 0x1FF
	object.rotation &= 0x1FF
end event


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

	// Trap door frames
	SpriteFrame(0, -12, 64, 24, 318, 115)

	// Setup every trap door object
	foreach (TypeName[Trap Door], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].priority = PRIORITY_XBOUNDS

		object[arrayPos0].delay = object[arrayPos0].propertyValue
		object[arrayPos0].delay &= 0xF
		object[arrayPos0].delay *= 60
	next

	// Add the object to the debug mode object count
	temp0 = 0
	TrapDoor_startDebugID = DebugMode_ObjCount
	while temp0 < 4
		SetTableValue(TypeName[Trap Door], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(TrapDoor_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(TrapDoor_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
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 // delay
			checkResult = object.propertyValue
			checkResult &= 0xF
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // delay
			object.propertyValue &= 0xF0
			object.propertyValue |= editor.variableValue
			break
			
		end switch
	end if
end event


event RSDKDraw
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
	object.direction ^= 1
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
	object.direction ^= 1
end event


event RSDKLoad
	LoadSpriteSheet("SBZ/Objects.gif")

	// Trap door frames
	SpriteFrame(0, -12, 64, 24, 318, 115)

	AddEditorVariable("delay")
	SetActiveVariable("delay")
	AddEnumVariable("0 Seconds", 0)
	AddEnumVariable("1 Second", 1)
	AddEnumVariable("2 Seconds", 2)
	AddEnumVariable("3 Seconds", 3)
	AddEnumVariable("4 Seconds", 4)
	AddEnumVariable("5 Seconds", 5)
	AddEnumVariable("6 Seconds", 6)
	AddEnumVariable("7 Seconds", 7)
	AddEnumVariable("8 Seconds", 8)
	AddEnumVariable("9 Seconds", 9)
	AddEnumVariable("10 Seconds", 10)
	AddEnumVariable("11 Seconds", 11)
	AddEnumVariable("12 Seconds", 12)
	AddEnumVariable("13 Seconds", 13)
	AddEnumVariable("14 Seconds", 14)
	AddEnumVariable("15 Seconds", 15)
end event
