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

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

private alias object.value0 : object.startPos.x

// Player Aliases
private alias object.state 		: player.state
private alias object.xpos 		: player.xpos
private alias object.animation 	: player.animation

private alias object.value1  : player.timer
private alias object.value17 : debugMode.currentSelection

// Path ID Aliases
private alias 0 : PATH_A


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

reserve function CogPlatform_DebugDraw
reserve function CogPlatform_DebugSpawn


// ========================
// Static Values
// ========================

private value CogPlatform_startDebugID = 0


// ========================
// Function Definitions
// ========================

private function CogPlatform_DebugDraw
	DrawSprite(0)
end function


private function CogPlatform_DebugSpawn
	temp0 = debugMode[0].currentSelection
	temp0 -= CogPlatform_startDebugID
	CreateTempObject(TypeName[Cog Platform], temp0, object.xpos, object.ypos)
	object[tempObjectPos].direction = object.direction
end function


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

event ObjectUpdate
	temp0 = object.xpos
	temp0 &= 0xFFFF0000

	object.xpos += object.xvel
	if object.direction == FLIP_NONE
		ObjectTileCollision(CSIDE_LWALL,  64, 0, PATH_A)
	else
		ObjectTileCollision(CSIDE_RWALL, -64, 0, PATH_A)
	end if

	if checkResult == true
		if object.propertyValue == 0
			object.xvel 	  = 0
			MPZSetup_cogTimer = 0
			MPZSetup_cogFrame = 0
		else
			object.direction ^= FLIP_X
			FlipSign(object.xvel)
			FlipSign(MPZSetup_cogFrame)
		end if
	end if

	temp4 = object.xpos
	temp2 = object.xpos
	temp2 &= 0xFFFF0000
	temp2 -= temp0

	object.xpos = temp0
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_SOLID, object.entityPos, -64, -12, 64, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		switch checkResult
		case COL_TOP
			if object.xvel == 0
				object.priority = PRIORITY_ACTIVE
				if object.direction == FLIP_NONE
					object.xvel = 0x20000
					MPZSetup_cogTimer = -1
				else
					object.xvel = -0x20000
					MPZSetup_cogTimer = 1
				end if
			end if
			player[currentPlayer].xpos += temp2
			break

		case COL_LEFT
		case COL_RIGHT
			if player[currentPlayer].state == Player_State_Climb
				player[currentPlayer].timer 	= 0
				player[currentPlayer].animation = ANI_GLIDING_DROP
				player[currentPlayer].state 	= Player_State_GlideDrop
			end if
			break
			
		end switch
	next
	
	object.xpos = temp4

	if object.outOfBounds == true
		if object.startPos.x == 0 // It will only be 0 if the Platform was spawned via Debug Mode
			object.type = TypeName[Blank Object]
		else
			object.xpos = object.startPos.x
			if object.outOfBounds == false
				object.xpos = temp4
			else
				object.priority 	= PRIORITY_BOUNDS
				object.xvel 		= 0
				MPZSetup_cogTimer 	= 0
				MPZSetup_cogFrame 	= 0
			end if
		end if
	end if
end event


event ObjectDraw
	DrawSprite(0)
end event


event ObjectStartup
	LoadSpriteSheet("MPZ/Objects.gif")
	SpriteFrame(-64, -12, 128, 24, 350, 182)

	// Nothing too notable but normally setup foreaches are before the Debug Mode table additions, but here they're switched around in order

	temp0 = 0
	CogPlatform_startDebugID = DebugMode_ObjCount
	while temp0 < 2
		SetTableValue(TypeName[Cog Platform], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(CogPlatform_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(CogPlatform_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop

	foreach (TypeName[Cog Platform], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.x = object[arrayPos0].xpos
	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
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("MPZ/Objects.gif")
	SpriteFrame(-64, -12, 128, 24, 350, 182)
	
	AddEditorVariable("type")
	SetActiveVariable("type")
	AddEnumVariable("Stop On Contact", 0)
	AddEnumVariable("Swap Direction On Contact", 1)
end event
