// ----------------------------------
// RSDK Project: Sonic 1
// Script Description: Flapping 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.interval
private alias object.value2 : object.frameTimer
private alias object.value3 : object.displayedFrame

// States
private alias 0 : FLAPPINGDOOR_CLOSED
private alias 1 : FLAPPINGDOOR_OPENING
private alias 2 : FLAPPINGDOOR_OPENED

// Player Aliases
private alias object.xpos : player.xpos


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

reserve function FlappingDoor_DebugDraw
reserve function FlappingDoor_DebugSpawn


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

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


private function FlappingDoor_DebugSpawn
	CreateTempObject(TypeName[Flapping Door], 0, object.xpos, object.ypos)
	object[tempObjectPos].direction = object.direction
	object[tempObjectPos].interval = 56
	object[tempObjectPos].timer = 56
end function


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

event ObjectUpdate
	switch object.frame
	case FLAPPINGDOOR_CLOSED
		if player[0].xpos < object.xpos
			CurrentTunnel_activateDelay = 4
		end if
		// [Fallthrough]
	case FLAPPINGDOOR_OPENING
		object.timer--
		if object.timer == -1
			temp0 = camera[0].xpos
			temp0 -= object.ixpos
			temp0 -= 8
			Abs(temp0)
			if temp0 <= screen.xcenter
				temp0 = camera[0].ypos
				temp0 -= object.iypos
				temp0 -= 32
				Abs(temp0)
				if temp0 <= screen.ycenter
					PlaySfx(SfxName[Flapping Door], false)
				end if
			end if
		end if

		if object.timer == -5
			object.frameTimer = 0
			object.frame = 2
		end if
		break

	case FLAPPINGDOOR_OPENED
		object.frameTimer++
		if object.frameTimer == 4
			object.displayedFrame ^= 1
			object.frame = object.displayedFrame
			object.timer = object.interval
		end if
		break
		
	end switch

	if object.frame == FLAPPINGDOOR_CLOSED
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_SOLID, object.entityPos, -8, -32, 8, 32, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		next
	end if
end event


event ObjectDraw
	DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
end event


event ObjectStartup
	LoadSpriteSheet("LZ/Objects.gif")
	SpriteFrame(-8, -32, 16, 64, 1, 191)
	SpriteFrame(0, -40, 32, 80, 51, 175)
	SpriteFrame(-5, -38, 32, 76, 18, 179)

	foreach (TypeName[Flapping Door], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].interval = object[arrayPos0].propertyValue
		object[arrayPos0].interval *= 60
		object[arrayPos0].interval -= 4
		object[arrayPos0].timer = object[arrayPos0].interval
	next

	SetTableValue(TypeName[Flapping Door], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(FlappingDoor_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(FlappingDoor_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 // interval
			checkResult = object.propertyValue
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // interval
			object.propertyValue = editor.variableValue
			break
			
		end switch
	end if
end event


event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("LZ/Objects.gif")
	SpriteFrame(-8, -32, 16, 64, 1, 191)

	AddEditorVariable("interval")
	SetActiveVariable("interval")
end event
