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

//-------Aliases-------//
private alias 59 : TYPE_BUMPINGPLATFORM

// Function declarations
reserve function BumpingPlatform_DebugDraw
reserve function BumpingPlatform_DebugSpawn

// Static Values

// Tables
public table BumpingPlatform_table51
	0, 1, 1, 0
end table

public table BumpingPlatform_table52
	0x680000, 0xA80000, 0xE80000, 0x680000
end table

public table BumpingPlatform_table53
	-0x680000, 0, -0xB00000, 0x400000, -0x780000, 0x800000, 0x670000, 0
end table


function BumpingPlatform_DebugDraw
	DrawSprite(0)
end function


function BumpingPlatform_DebugSpawn
	CreateTempObject(TypeName[Bumping Platform], 0, object.xpos, object.ypos)
	object[tempObjectPos].state = 0
	object[tempObjectPos].value2 = object.xpos
	object[tempObjectPos].value2 -= 0x680000
	object[tempObjectPos].value3 = object.xpos
	object[tempObjectPos].value3 += 0x680000
	object[tempObjectPos].value0 = object.xpos
	object[tempObjectPos].value0 += -0x680000
	object[tempObjectPos].value1 = object.xpos
	object[tempObjectPos].value1 += 0
end function


event ObjectMain
	temp6 = object.value0
	temp7 = object.value1
	GetBit(temp0, object.direction, 0)
	if temp0 == 0
		object.value0 += 0x10000
		if object.value0 >= object.value3
			SetBit(object.direction, 0, 1)
		end if
	else
		object.value0 -= 0x10000
		if object.value0 <= object.value2
			SetBit(object.direction, 0, 0)
		end if
	end if
	if object.state == 1
		if object.direction == FLIP_Y
			temp0 = object.value0
			temp0 += 0x180000
			temp1 = object.value1
			temp1 -= 0x180000
			if temp0 == temp1
				object.direction ^= FLIP_XY
			end if
		end if
		if object.direction == FLIP_X
			temp0 = object.value0
			temp0 -= 0x180000
			temp1 = object.value1
			temp1 += 0x180000
			if temp0 == temp1
				object.direction ^= FLIP_XY
			end if
		end if
		GetBit(temp0, object.direction, 1)
		if temp0 == 0
			object.value1 += 0x10000
			if object.value1 >= object.value3
				SetBit(object.direction, 1, 1)
			end if
		else
			object.value1 -= 0x10000
			if object.value1 <= object.value2
				SetBit(object.direction, 1, 0)
			end if
		end if
		if object.direction == FLIP_Y
			temp0 = object.value0
			temp0 += 0x180000
			temp1 = object.value1
			temp1 -= 0x180000
			if temp0 == temp1
				object.direction ^= FLIP_XY
			end if
		end if
		if object.direction == FLIP_X
			temp0 = object.value0
			temp0 -= 0x180000
			temp1 = object.value1
			temp1 += 0x180000
			if temp0 == temp1
				object.direction ^= FLIP_XY
			end if
		end if
	end if
	temp4 = object.value0
	temp5 = object.value1
	temp4 -= temp6
	temp5 -= temp7
	temp0 = object.xpos
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		object.xpos = temp6
		BoxCollisionTest(C_PLATFORM, object.entityPos, -24, -8, 24, 6, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
		if checkResult == 1
			object[currentPlayer].xpos += temp4
		end if
		if object.state == 1
			object.xpos = temp7
			BoxCollisionTest(C_PLATFORM, object.entityPos, -24, -8, 24, 6, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				object[currentPlayer].xpos += temp5
			end if
		end if
	next
	object.xpos = temp0
end event


event ObjectDraw
	DrawSpriteXY(0, object.value0, object.ypos)
	if object.state == 1
		DrawSpriteXY(0, object.value1, object.ypos)
	end if
end event


event ObjectStartup
	LoadSpriteSheet("CPZ/Objects.gif")
	SpriteFrame(-24, -8, 48, 14, 6, 204)
	foreach (TypeName[Bumping Platform], arrayPos0, ALL_ENTITIES)
		temp0 = object[arrayPos0].propertyValue
		GetTableValue(object[arrayPos0].state, temp0, BumpingPlatform_table51)
		GetTableValue(temp1, temp0, BumpingPlatform_table52)
		object[arrayPos0].value2 = object[arrayPos0].xpos
		object[arrayPos0].value2 -= temp1
		object[arrayPos0].value3 = object[arrayPos0].xpos
		object[arrayPos0].value3 += temp1
		temp0 <<= 1
		GetTableValue(temp1, temp0, BumpingPlatform_table53)
		object[arrayPos0].value0 = object[arrayPos0].xpos
		object[arrayPos0].value0 += temp1
		temp0++
		GetTableValue(temp1, temp0, BumpingPlatform_table53)
		object[arrayPos0].value1 = object[arrayPos0].xpos
		object[arrayPos0].value1 += temp1
		if object[arrayPos0].propertyValue == 2
			object[arrayPos0].direction = FLIP_X
		end if
		object[arrayPos0].priority = PRIORITY_ACTIVE
	next
	SetTableValue(TypeName[Bumping Platform], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(BumpingPlatform_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(BumpingPlatform_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
