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

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

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


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

event ObjectUpdate
	if object.propertyValue == 0
		// Only check if the player's within a 64 pixel range of the object, in terms of X
		temp0 = player[0].xpos
		temp0 -= object.xpos
		Abs(temp0)
		if temp0 < 0x200000
			stage.newYBoundary2 = object.iypos
			stage.deathBoundary = object.ypos
		end if
	else
		// Check if the player's between this object and the next, in terms of Y
		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
				end if
			end if
		end if
	end if
end event


event ObjectStartup
	foreach (TypeName[Y Bound Adjust], arrayPos0, ALL_ENTITIES)
		// Set the initial camera bounds based on where the player spawned
		// (This object comes after Lamp Posts in the object order, which is why this can be done already)

		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
	next
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
	// The object's tiny, draw a small line to show where the camera's gonna be aligned to
	// No specific reason for using yellow, I just think it looks nice
	temp0 = object.xpos
	temp0 -= 0x1C0000
	temp1 = object.xpos
	temp1 += 0x1C0000
	DrawLine(temp0, object.ypos, temp1, object.ypos, 0xFF, 0xFF, 0x00)

	// Draw the Y icon ontop of the line
	DrawSprite(0)

	editor.drawingOverlay = true

	CheckEqual(object.propertyValue, 1)
	checkResult &= editor.showGizmos
	if temp0 == true
		// Draw arrows between the next object too, since that's where the other side of the bounds adjust is
		// Using yellow again cuz why not
		DrawArrow(object.xpos, object.ypos, object[+1].xpos, object[+1].ypos, 0xFF, 0xFF, 0x00)
		DrawArrow(object[+1].xpos, object[+1].ypos, object.xpos, object.ypos, 0xFF, 0xFF, 0x00)
	end if

	editor.drawingOverlay = false
end event


event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-8, -8, 16, 16, 205, 239) // "Y" (Bounds Adjust)

	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
end event
