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

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

private alias object.value0  : object.timer
private alias object.value1  : object.tablePos
private alias object.value2  : object.centerPos.x
private alias object.value3  : object.centerPos.y
private alias object.value4  : object.stoodPlayers
private alias object.value31 : object.wrapOffset.y

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


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

reserve function MPlatform_Setup
reserve function MPlatform_DebugDraw
reserve function MPlatform_DebugSpawn


// ========================
// Tables
// ========================

// Tables
// ========================

private table MPlatform_XVelTable
	0, 0x40000, 0, -0x40000
end table

private table MPlatform_YVelTable
	0x40000, -0x20000, 0x40000, -0x20000
end table

private table MPlatform_durationTable
	16, 32, 16, 32
end table


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

private function MPlatform_Setup
	object[arrayPos0].priority  		= PRIORITY_ACTIVE
	object[arrayPos0].timer 	 		= 0
	object[arrayPos0].tablePos  		= 0
	object[arrayPos0].xpos 		 		= object[arrayPos0].centerPos.x
	object[arrayPos0].ypos 		 		= object[arrayPos0].centerPos.y
	object[arrayPos0].ypos 	    	   += object[arrayPos0].wrapOffset.y
	object[arrayPos0].stoodPlayers 	= 0
end function


private function MPlatform_DebugDraw
	DrawSprite(0)
end function


private function MPlatform_DebugSpawn
	CreateTempObject(TypeName[M Platform], 0, object.xpos, object.ypos)
	arrayPos0 = object[tempObjectPos].entityPos
	object[arrayPos0].centerPos.x = object.xpos
	object[arrayPos0].centerPos.y = object.ypos
	CallFunction(MPlatform_Setup)
end function


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

event ObjectUpdate
	object.xpos += object.xvel
	object.ypos += object.yvel

	temp0 = 0
	temp1 = 0
	temp2 = 0
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		GetBit(temp3, object.stoodPlayers, temp2)
		if temp3 == true
			player[currentPlayer].xpos += object.xvel
			player[currentPlayer].ypos += object.yvel
			temp1 = true
		end if

		SetBit(object.stoodPlayers, temp2, false)
		BoxCollisionTest(C_SOLID, object.entityPos, -32, -12, 32, 12, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		if checkResult == COL_TOP
			temp0 = true
			SetBit(object.stoodPlayers, temp2, true)
		end if

		temp2++
	next

	if object.timer == 0
		if temp0 == false
			if temp1 == true
				GetTableValue(object.xvel, object.tablePos, MPlatform_XVelTable)
				GetTableValue(object.yvel, object.tablePos, MPlatform_YVelTable)
				GetTableValue(object.timer, object.tablePos, MPlatform_durationTable)
				object.tablePos++
				object.tablePos &= 3
			end if
		end if
	else
		object.timer--
		if object.timer == 0
			object.xvel = 0
			object.yvel = 0
		end if
	end if
end event


event ObjectDraw
	DrawSprite(0)
end event


event ObjectStartup
	LoadSpriteSheet("MPZ/Objects.gif")
	SpriteFrame(-32, -12, 64, 24, 383, 207)

	foreach (TypeName[M Platform], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].centerPos.x = object[arrayPos0].xpos
		object[arrayPos0].centerPos.y = object[arrayPos0].ypos
		CallFunction(MPlatform_Setup)
	next

	SetTableValue(TypeName[M Platform], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(MPlatform_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(MPlatform_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("MPZ/Objects.gif")
	SpriteFrame(-32, -12, 64, 24, 383, 207)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
