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

//-------Aliases-------//
private alias 53 : TYPE_TIPPINGFLOOR

// Function declarations
reserve function TippingFloor_DebugDraw
reserve function TippingFloor_DebugSpawn

// Static Values
public value TippingFloor_value44 = 0;

// Tables

function TippingFloor_DebugDraw
	DrawSprite(0)
end function


function TippingFloor_DebugSpawn
	CreateTempObject(TypeName[Tipping Floor], 0, object.xpos, object.ypos)
	object[tempObjectPos].value1 = 127
	object[tempObjectPos].value0 = 127
	object[tempObjectPos].value2 = object[0].value17
	object[tempObjectPos].value2 -= TippingFloor_value44
	object[tempObjectPos].value2 <<= 4
	object[tempObjectPos].value2 += 16
	object[tempObjectPos].value2--
end function


event ObjectMain
	if object.state == 0
		temp0 = oscillation
		temp0 += object.value2
		temp0 &= 255
		if temp0 == 0
			object.state = 1
			object.priority = PRIORITY_ACTIVE
		end if
	end if
	if object.state == 1
		object.value0--
		if object.value0 < 0
			if object.animation == 0
				object.value0 = object.value1
			else
				object.value0 = 127
			end if
			object.animation ^= 1
		end if
		if object.animation == 0
			if object.frame < 4
				object.animationTimer++
				if object.animationTimer == 8
					object.animationTimer = 0
					object.frame++
				end if
			end if
		else
			if object.frame > 0
				object.animationTimer++
				if object.animationTimer == 8
					object.animationTimer = 0
					object.frame--
				end if
			end if
		end if
	end if
	if object.frame == 0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_PLATFORM, object.entityPos, -16, -16, 16, -8, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
		next
	end if
	if options.vsMode == 0
		temp0 = screen.cameraX
		temp0 -= object.ixpos
		Abs(temp0)
		temp0 -= 128
		if temp0 > screen.xcenter
			object.state = 0
			object.value0 = object.value1
			object.priority = PRIORITY_XBOUNDS
			object.animation = 0
			object.frame = 0
			object.animationTimer = 0
		end if
	end if
end event


event ObjectDraw
	DrawSprite(object.frame)
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone02")
	if checkResult == 1
		LoadSpriteSheet("CPZ/Objects.gif")
		SpriteFrame(-16, -16, 32, 32, 1, 222)
		SpriteFrame(-16, -21, 32, 37, 34, 218)
		SpriteFrame(-16, -12, 32, 32, 67, 223)
		SpriteFrame(-16, -8, 32, 37, 99, 218)
		SpriteFrame(-16, -8, 32, 32, 132, 223)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-16, -16, 32, 32, 34, 692)
		SpriteFrame(-16, -21, 32, 37, 34, 725)
		SpriteFrame(-16, -12, 32, 32, 34, 763)
		SpriteFrame(-16, -8, 32, 37, 34, 796)
		SpriteFrame(-16, -8, 32, 32, 34, 834)
	end if
	foreach (TypeName[Tipping Floor], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].priority = PRIORITY_XBOUNDS
		CheckGreater(object[arrayPos0].propertyValue, 127)
		temp0 = checkResult
		CheckEqual(options.vsMode, 1)
		temp0 &= checkResult
		if temp0 == 1
			object[arrayPos0].state = 2
		else
			object[arrayPos0].value2 = object[arrayPos0].propertyValue
			object[arrayPos0].value2 &= 15
			object[arrayPos0].value2++
			object[arrayPos0].value2 <<= 4
			object[arrayPos0].value1 = object[arrayPos0].propertyValue
			object[arrayPos0].value1 &= 112
			object[arrayPos0].value1 += 16
			object[arrayPos0].value1--
			object[arrayPos0].value0 = object[arrayPos0].value1
		end if
	next
	temp0 = 0
	TippingFloor_value44 = DebugMode_ObjCount
	while temp0 < 16
		SetTableValue(TypeName[Tipping Floor], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(TippingFloor_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(TippingFloor_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
end event

event RSDKDraw
	DrawSprite(0)
end event

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