// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Plane Sw H Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

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

private alias object.value0 : object.extendTop
private alias object.value1 : object.extendBottom
private alias object.value2 : object.planeR
private alias object.value3 : object.planeL
private alias object.value4 : object.drawOrderR
private alias object.value5 : object.drawOrderL
private alias object.value6 : object.onGround

// Player Aliases
private alias object.type : player.type
private alias object.state : player.state
private alias object.yvel : player.yvel
private alias object.gravity : player.gravity
private alias object.collisionPlane : player.collisionPlane
private alias object.value18 :player.sortedDrawOrder


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

event ObjectUpdate
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		CheckEqual(object.onGround, false)
		temp0 = checkResult
		CheckEqual(player[currentPlayer].gravity, GRAVITY_GROUND)
		temp0 |= checkResult
		CheckEqual(player[currentPlayer].state, Player_State_Climb)
		temp0 |= checkResult
		
		if temp0 == true
			BoxCollisionTest(C_TOUCH, object.entityPos, object.extendTop, -12, object.extendBottom, 12, currentPlayer, 0, 0, 0, 0)
			if checkResult == true
				if player[currentPlayer].yvel > 0
					GetBit(temp0, object.propertyValue, 6)
					if temp0 == false
						player[currentPlayer].collisionPlane = object.planeL
					end if
					player[currentPlayer].sortedDrawOrder = object.drawOrderL
				else
					GetBit(temp0, object.propertyValue, 6)
					if temp0 == false
						player[currentPlayer].collisionPlane = object.planeR
					end if
					player[currentPlayer].sortedDrawOrder = object.drawOrderR
				end if
			end if
		end if
	next
end event


event ObjectStartup
	LoadSpriteSheet("Global/Display.gif")

	SpriteFrame(-8, -8, 16, 16, 42, 130)
	SpriteFrame(-8, -8, 16, 16, 59, 130)
	SpriteFrame(-8, -8, 16, 16, 76, 130)
	SpriteFrame(-8, -8, 16, 16, 93, 130)
	SpriteFrame(-8, -8, 16, 16, 110, 130)
	SpriteFrame(-8, -8, 16, 16, 127, 130)
	SpriteFrame(-8, -8, 16, 16, 144, 130)
	SpriteFrame(-8, -8, 16, 16, 161, 130)
	SpriteFrame(-8, -8, 16, 16, 42, 147)
	SpriteFrame(-8, -8, 16, 16, 59, 147)
	SpriteFrame(-8, -8, 16, 16, 76, 147)
	SpriteFrame(-8, -8, 16, 16, 93, 147)
	SpriteFrame(-8, -8, 16, 16, 110, 147)
	SpriteFrame(-8, -8, 16, 16, 127, 147)
	SpriteFrame(-8, -8, 16, 16, 144, 147)
	SpriteFrame(-8, -8, 16, 16, 161, 147)
	SpriteFrame(-8, -8, 16, 16, 42, 164)
	SpriteFrame(-8, -8, 16, 16, 59, 164)
	SpriteFrame(-8, -8, 16, 16, 76, 164)
	SpriteFrame(-8, -8, 16, 16, 93, 164)
	SpriteFrame(-8, -8, 16, 16, 93, 113)

	foreach (TypeName[Plane Sw H], arrayPos0, ALL_ENTITIES)
		temp0 = object[arrayPos0].propertyValue
		temp0 &= 3
		temp0 += 2
		object[arrayPos0].extendTop = 1
		object[arrayPos0].extendTop <<= temp0
		object[arrayPos0].extendTop <<= 3
		object[arrayPos0].extendBottom = object[arrayPos0].extendTop
		FlipSign(object[arrayPos0].extendTop)
		GetBit(object[arrayPos0].planeR, object[arrayPos0].propertyValue, 2)
		GetBit(object[arrayPos0].planeL, object[arrayPos0].propertyValue, 3)

		GetBit(object[arrayPos0].drawOrderR, object[arrayPos0].propertyValue, 4)
		object[arrayPos0].drawOrderR += 4
		GetBit(object[arrayPos0].drawOrderL, object[arrayPos0].propertyValue, 5)
		object[arrayPos0].drawOrderL += 4

		GetBit(object[arrayPos0].onGround, object[arrayPos0].propertyValue, 7)
	next
end event

// This Object's kinda weird... its Draw comes after its Startup for some reason
event ObjectDraw
	if player[0].type == TypeName[Debug Mode]
		temp1 = object.propertyValue
		temp1 &= 3
		temp1 += 2
		temp0 = 1
		temp0 <<= temp1
		temp1 = temp0
		temp0 <<= 19
		FlipSign(temp0)
		temp0 += object.xpos
		temp0 += 0x80000
		GetBit(temp2, object.propertyValue, 7)
		temp3 = object.propertyValue
		temp3 >>= 2
		temp3 &= 15
		GetBit(temp4, object.propertyValue, 6)
		if temp4 == true
			temp3 >>= 2
			temp3 += 16
		end if
		
		temp4 = 0
		while temp4 < temp1
			if temp2 == true
				DrawSpriteXY(20, temp0, object.ypos)
			end if
			DrawSpriteXY(temp3, temp0, object.ypos)
			temp0 += 0x100000
			temp4++
		loop

		if temp1 == 0
			DrawSprite(temp3)
		end if
	end if
end event


// ========================
// Editor Events & Aliases
// ========================

private alias 0 : VAR_ID_SIZE
private alias 1 : VAR_ID_PLANEL
private alias 2 : VAR_ID_PLANER
private alias 3 : VAR_ID_DRAWL
private alias 4 : VAR_ID_DRAWR
private alias 5 : VAR_ID_ONLYDRAWORDER
private alias 6 : VAR_ID_ONGROUND


event RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			checkResult = object.propertyValue
			break
			
		case VAR_ID_SIZE // size
			checkResult = object.propertyValue
			checkResult &= 3
			break
			
		case VAR_ID_PLANEL // collisionPlaneL
			GetBit(checkResult, object.propertyValue, 2)
			break
			
		case VAR_ID_PLANER // collisionPlaneR
			GetBit(checkResult, object.propertyValue, 3)
			break
			
		case VAR_ID_DRAWL // drawOrderL
			GetBit(checkResult, object.propertyValue, 4)
			break
			
		case VAR_ID_DRAWR // drawOrderR
			GetBit(checkResult, object.propertyValue, 5)
			break
			
		case VAR_ID_ONLYDRAWORDER // onlyDrawOrder
			GetBit(checkResult, object.propertyValue, 6)
			break
			
		case VAR_ID_ONGROUND // onGround
			GetBit(checkResult, object.propertyValue, 7)
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case VAR_ID_SIZE // size
			temp1 = 3
			Not(temp1)
			object.propertyValue &= temp1

			temp0 = editor.variableValue
			temp0 &= 3
			object.propertyValue |= temp0
			break
			
		case VAR_ID_PLANEL // collisionPlaneL
			CheckNotEqual(editor.variableValue, false)
			SetBit(object.propertyValue, 2, checkResult)
			break
			
		case VAR_ID_PLANER // collisionPlaneR
			CheckNotEqual(editor.variableValue, false)
			SetBit(object.propertyValue, 3, checkResult)
			break
			
		case VAR_ID_DRAWL // drawOrderL
			CheckNotEqual(editor.variableValue, false)
			SetBit(object.propertyValue, 4, checkResult)
			break
			
		case VAR_ID_DRAWR // drawOrderR
			CheckNotEqual(editor.variableValue, false)
			SetBit(object.propertyValue, 5, checkResult)
			break
			
		case VAR_ID_ONLYDRAWORDER // onlyDrawOrder
			CheckNotEqual(editor.variableValue, false)
			SetBit(object.propertyValue, 6, checkResult)
			break
			
		case VAR_ID_ONGROUND // onGround
			CheckNotEqual(editor.variableValue, false)
			SetBit(object.propertyValue, 7, checkResult)
			break
			
		end switch
	end if
end event


event RSDKDraw
	temp1 = object.propertyValue
	temp1 &= 3
	temp1 += 2
	temp0 = 1
	temp0 <<= temp1
	temp1 = temp0
	temp0 <<= 19
	FlipSign(temp0)
	temp0 += object.xpos
	temp0 += 0x80000
	GetBit(temp2, object.propertyValue, 7)
	temp3 = object.propertyValue
	temp3 >>= 2
	temp3 &= 15
	GetBit(temp4, object.propertyValue, 6)
	if temp4 == true
		temp3 >>= 2
		temp3 += 16
	end if

	temp4 = 0
	while temp4 < temp1
		if temp2 == true
			DrawSpriteXY(20, temp0, object.ypos)
		end if
		DrawSpriteXY(temp3, temp0, object.ypos)
		temp0 += 0x100000
		temp4++
	loop

	if temp1 == 0
		DrawSprite(temp3)
	end if
end event


event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	
	temp0 = 0
	temp1 = 42
	while temp0 < 8
		SpriteFrame(-8, -8, 16, 16, temp1, 130)
		temp0++
		temp1 += 17
	loop
	
	temp0 = 0
	temp1 = 42
	while temp0 < 8
		SpriteFrame(-8, -8, 16, 16, temp1, 147)
		temp0++
		temp1 += 17
	loop

	SpriteFrame(-8, -8, 16, 16, 42, 164) // #16 - LL
	SpriteFrame(-8, -8, 16, 16, 59, 164) // #17 - LH
	SpriteFrame(-8, -8, 16, 16, 76, 164) // #18 - HL
	SpriteFrame(-8, -8, 16, 16, 93, 164) // #19 - HH
	
	SpriteFrame(-8, -8, 16, 16, 93, 113) // #20 - back

	AddEditorVariable("size")
	SetActiveVariable("size")
	AddEnumVariable("4 Nodes", 0)
	AddEnumVariable("8 Nodes", 1)
	AddEnumVariable("16 Nodes", 2)
	AddEnumVariable("32 Nodes", 3)

	AddEditorVariable("collisionPlaneL")
	SetActiveVariable("collisionPlaneL")
	AddEnumVariable("Plane A", 0)
	AddEnumVariable("Plane B", 1)

	AddEditorVariable("collisionPlaneR")
	SetActiveVariable("collisionPlaneR")
	AddEnumVariable("Plane A", 0)
	AddEnumVariable("Plane B", 1)

	AddEditorVariable("drawOrderL")
	SetActiveVariable("drawOrderL")
	AddEnumVariable("Low Layer", 0)
	AddEnumVariable("High Layer", 1)

	AddEditorVariable("drawOrderR")
	SetActiveVariable("drawOrderR")
	AddEnumVariable("Low Layer", 0)
	AddEnumVariable("High Layer", 1)

	AddEditorVariable("onlyDrawOrder")
	SetActiveVariable("onlyDrawOrder")
	AddEnumVariable("false", false)
	AddEnumVariable("true", true)

	AddEditorVariable("onGround")
	SetActiveVariable("onGround")
	AddEnumVariable("false", false)
	AddEnumVariable("true", true)
end event
