// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Y Bound Adjust Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

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

private alias object.value0 : object.leftBoundary

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


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

event ObjectUpdate
	if object.propertyValue == 0
		temp0 = player[0].xpos
		temp0 -= object.xpos
		Abs(temp0)
		if temp0 < 0x200000
			stage.newYBoundary1 = 0
			stage.newYBoundary2 = object.iypos
			stage.deathBoundary = object.ypos
		end if
	else
		if player[0].ypos < object.ypos
			if player[0].ypos > object[+1].ypos
				temp0 = player[0].xpos
				temp0 -= object.xpos
				Abs(temp0)
				if temp0 < 0x200000
					stage.newYBoundary2 = object.iypos
					stage.deathBoundary = object.ypos
					if object.leftBoundary > 0
						stage.newXBoundary1 = object.leftBoundary
						object.leftBoundary = 0
					end if
				end if
			end if
		end if
	end if
end event


event ObjectStartup
	foreach (TypeName[Y Bound Adjust], arrayPos0, ALL_ENTITIES)
		if options.vsMode == false
			if object[arrayPos0].propertyValue == 0
				object[arrayPos0].priority = PRIORITY_XBOUNDS
				temp0 = player[0].xpos
				temp0 -= object[arrayPos0].xpos
				Abs(temp0)
				if temp0 < 0x200000
					stage.curYBoundary2 = object[arrayPos0].iypos
					stage.deathBoundary = object[arrayPos0].ypos
				end if
			else
				object[arrayPos0].priority = PRIORITY_XBOUNDS
				arrayPos1 = arrayPos0
				arrayPos1++
				if player[0].ypos < object[arrayPos0].ypos
					if player[0].ypos > object[arrayPos1].ypos
						temp0 = player[0].xpos
						temp0 -= object[arrayPos0].xpos
						Abs(temp0)
						if temp0 < 0x200000
							stage.curYBoundary2 = object[arrayPos0].iypos
							stage.deathBoundary = object[arrayPos0].ypos
						end if
					end if
				end if
			end if
		else
			object[arrayPos0].type = TypeName[Blank Object]
		end if
	next
end event


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

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


event RSDKDraw
	if editor.showGizmos == true
		temp0 = object.xpos
		temp0 -= 0x180000
		temp1 = object.xpos
		temp1 += 0x180000
		DrawLine(temp0, object.ypos, temp1, object.ypos, 0xFF, 0xFF, 0x00) // No specific reason for using yellow, I just think it looks nice
	end if

	// Draw the Y sprite ontop of the line
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-8, -8, 16, 16, 134, 18)

	AddEditorVariable("type")
	SetActiveVariable("type")
	AddEnumVariable("Regular Adjust", 0) // adjusts to this YPos
	AddEnumVariable("Box Adjust", 1) // adjusts to this YPos, if the player is below this YPos and above the next object slots' YPos
	
	SetVariableAlias(ALIAS_VAR_VAL0, "leftBoundary") // only works on box adjust types
end event
