// ----------------------------------
// RSDK Project: Sonic 1
// 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.xpos : player.xpos
private alias object.xvel : player.xvel


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

reserve function OneWayDoor_DebugDraw
reserve function OneWayDoor_DebugSpawn


// ========================
// Tables
// ========================

private function OneWayDoor_DebugDraw
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
	DrawSpriteFX(1, 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 -= 0x40000
		end if
	else
		if object.movePos < 0x200000
			object.movePos += 0x40000
		end if
	end if

	object.ypos -= object.movePos
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_SOLID, object.entityPos, -8, -32, 8, 0, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
	next
	
	object.ypos += object.movePos
	object.ypos += object.movePos
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_SOLID, object.entityPos, -8, 0, 8, 32, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
	next

	object.ypos -= object.movePos
	object.isOpen = 0
	if object.propertyValue == 2
		temp0 = object.xpos
		temp0 += 0x200000
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if player[currentPlayer].xpos < temp0
				object.isOpen |= true
			end if
		next

		if object.isOpen == false
			object.propertyValue = 0
		end if
	else
		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 if
end event


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

	temp0 = object.movePos
	temp0 += object.ypos
	DrawSpriteFX(1, FX_FLIP, object.xpos, temp0)
end event


event ObjectStartup
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-8, -32, 16, 32, 449, 50)
	SpriteFrame(-8, 0, 16, 32, 449, 82)
	foreach (TypeName[One Way Door], arrayPos0, ALL_ENTITIES)
		if object[arrayPos0].propertyValue == 1
			object[arrayPos0].direction = FACING_LEFT
		end if
	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
	object.direction = FACING_RIGHT
	if object.propertyValue == 1
		object.direction = FACING_LEFT
	end if

	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
	DrawSpriteFX(1, FX_FLIP, object.xpos, object.ypos)
end event


event RSDKLoad
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-8, -32, 16, 32, 449, 50)
	SpriteFrame(-8, 0, 16, 32, 449, 82)

	AddEditorVariable("type")
	SetActiveVariable("type")
	AddEnumVariable("Left Only", 0)
	AddEnumVariable("Right Only", 1)
	AddEnumVariable("Open Once (Left Only)", 2)
end event
