// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: One Way Door Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

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

private alias object.state  : object.isOpen
private alias object.value0 : object.movePos

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


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

reserve function OneWayDoor_DebugDraw
reserve function OneWayDoor_DebugSpawn


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

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


private function OneWayDoor_DebugSpawn
	CreateTempObject(TypeName[One Way Door], object.direction, object.xpos, object.ypos)
	object[tempObjectPos].direction = object.direction
end function


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

event ObjectUpdate
	if object.isOpen == false
		if object.movePos > 0
			object.movePos -= 0x80000
		end if
	else
		if object.movePos < 0x400000
			object.movePos += 0x80000
		end if
	end if

	object.ypos -= object.movePos
	if object.movePos == 0
		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
	else
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_SOLID, object.entityPos, -8, -32, 8, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		next
	end if

	object.ypos += object.movePos
	object.isOpen = false
	if object.propertyValue == 0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if player[currentPlayer].xvel < 0x60000
				BoxCollisionTest(C_TOUCH, object.entityPos, -64, -32, 8, 32, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			else
				BoxCollisionTest(C_TOUCH, object.entityPos, -88, -32, 8, 32, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			end if

			object.isOpen |= checkResult
		next
	else
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if player[currentPlayer].xvel > -0x60000
				BoxCollisionTest(C_TOUCH, object.entityPos, -8, -32, 64, 32, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			else
				BoxCollisionTest(C_TOUCH, object.entityPos, -8, -32, 88, 32, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			end if

			object.isOpen |= checkResult
		next
	end if
end event


event ObjectDraw
	temp0 = object.movePos
	FlipSign(temp0)
	temp0 += object.ypos
	DrawSpriteFX(0, FX_FLIP, object.xpos, temp0)
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone12")
	if checkResult == true
		// Check if in DEZ and then... load CPZ sprites...
		LoadSpriteSheet("CPZ/Objects.gif")
		SpriteFrame(-8, -32, 16, 64, 206, 142)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-8, -32, 16, 64, 93, 312)
	end if

	foreach (TypeName[One Way Door], arrayPos0, ALL_ENTITIES)
		if object[arrayPos0].propertyValue == 1
			object[arrayPos0].direction = FLIP_X
		end if

		object[arrayPos0].drawOrder = 4
	next

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


event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone12")
	if checkResult == true
		// Even for the editor, there's not really anything that can be done here since
		// the sprites aren't even present on the DEZ sheet at all...
		LoadSpriteSheet("CPZ/Objects.gif")
		SpriteFrame(-8, -32, 16, 64, 206, 142)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-8, -32, 16, 64, 93, 312)
	end if
	
	AddEditorVariable("type")
	SetActiveVariable("type")
	AddEnumVariable("Left Only", 0)
	AddEnumVariable("Right Only", 1)
end event
