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

//-------Aliases-------//
private alias 76 : TYPE_MPLATFORM

// Function declarations
reserve function MPlatform_Function163
reserve function MPlatform_DebugDraw
reserve function MPlatform_DebugSpawn

// Static Values

// Tables
public table MPlatform_table49
	0, 0x40000, 0, -0x40000
end table

public table MPlatform_table50
	0x40000, -0x20000, 0x40000, -0x20000
end table

public table MPlatform_table51
	16, 32, 16, 32
end table


function MPlatform_Function163
	object[arrayPos0].priority = PRIORITY_ACTIVE
	object[arrayPos0].value0 = 0
	object[arrayPos0].value1 = 0
	object[arrayPos0].xpos = object[arrayPos0].value2
	object[arrayPos0].ypos = object[arrayPos0].value3
	object[arrayPos0].ypos += object[arrayPos0].value31
	object[arrayPos0].value4 = 0
end function


function MPlatform_DebugDraw
	DrawSprite(0)
end function


function MPlatform_DebugSpawn
	CreateTempObject(TypeName[M Platform], 0, object.xpos, object.ypos)
	arrayPos0 = object[tempObjectPos].entityPos
	object[arrayPos0].value2 = object.xpos
	object[arrayPos0].value3 = object.ypos
	CallFunction(MPlatform_Function163)
end function


event ObjectMain
	object.xpos += object.xvel
	object.ypos += object.yvel
	temp0 = 0
	temp1 = 0
	temp2 = 0
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		GetBit(temp3, object.value4, temp2)
		if temp3 == 1
			object[currentPlayer].xpos += object.xvel
			object[currentPlayer].ypos += object.yvel
			temp1 = 1
		end if
		SetBit(object.value4, temp2, 0)
		BoxCollisionTest(C_BOX, object.entityPos, -32, -12, 32, 12, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
		if checkResult == 1
			temp0 = 1
			SetBit(object.value4, temp2, 1)
		end if
		temp2++
	next
	if object.value0 == 0
		if temp0 == 0
			if temp1 == 1
				GetTableValue(object.xvel, object.value1, MPlatform_table49)
				GetTableValue(object.yvel, object.value1, MPlatform_table50)
				GetTableValue(object.value0, object.value1, MPlatform_table51)
				object.value1++
				object.value1 &= 3
			end if
		end if
	else
		object.value0--
		if object.value0 == 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].value2 = object[arrayPos0].xpos
		object[arrayPos0].value3 = object[arrayPos0].ypos
		CallFunction(MPlatform_Function163)
	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

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
