// ----------------------------------
// RSDK Project: Sonic 1
// Script Description: Plane Sw V 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.xvel : player.xvel
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
		if temp0 == true
			BoxCollisionTest(C_TOUCH, object.entityPos, -12, object.extendTop, 12, object.extendBottom, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == true
				if player[currentPlayer].xvel > 0
					player[currentPlayer].collisionPlane 	= object.planeL
					player[currentPlayer].sortedDrawOrder	= object.drawOrderL
				else
					player[currentPlayer].collisionPlane 	= object.planeR
					player[currentPlayer].sortedDrawOrder	= object.drawOrderR
				end if
			end if
		end if
	next
end event


event ObjectStartup
	foreach (TypeName[Plane Sw V], 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].extendTop += 8
		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 <<= 1
		object[arrayPos0].drawOrderR += 4
		GetBit(object[arrayPos0].drawOrderL, object[arrayPos0].propertyValue, 5)
		object[arrayPos0].drawOrderL <<= 1
		object[arrayPos0].drawOrderL += 4

		GetBit(object[arrayPos0].onGround, object[arrayPos0].propertyValue, 7)
	next
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_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_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_ONGROUND // onGround
			CheckNotEqual(editor.variableValue, false)
			SetBit(object.propertyValue, 7, checkResult)
			break
			
		end switch
	end if
end event


event RSDKDraw
	// Grabbed this from S2's debug view, since S1 doesn't have one, works the same though
	
	// size
	temp1 = object.propertyValue
	temp1 &= 3
	temp1 += 2

	temp0 = 1
	temp0 <<= temp1
	temp1 = temp0
	temp0 <<= 19
	FlipSign(temp0)
	temp0 += object.ypos
	temp0 += 0x80000

	// onGround
	GetBit(temp2, object.propertyValue, 7)
	temp3 = object.propertyValue
	temp3 >>= 2
	temp3 &= 15
	
	if editor.showGizmos == true
		// hitbox view goes here
	end if

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

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


event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	
	temp0 = 0
	temp1 = 46
	while temp0 < 8
		SpriteFrame(-8, -8, 16, 16, temp1, 158)
		temp0++
		temp1 += 17
	loop
	
	temp0 = 0
	temp1 = 46
	while temp0 < 8
		SpriteFrame(-8, -8, 16, 16, temp1, 175)
		temp0++
		temp1 += 17
	loop
	
	SpriteFrame(-8, -8, 16, 16, 182, 141) // #16 - 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("onGround")
	SetActiveVariable("onGround")
	AddEnumVariable("false", false)
	AddEnumVariable("true", true)
end event
