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

//-------Aliases-------//
private alias 48 : TYPE_LARGEGEAR

// Function declarations
reserve function LargeGear_DebugDraw
reserve function LargeGear_DebugSpawn

// Static Values

// Tables
public table LargeGear_table40
	0, 0, 0, 0, 0, 0, 0, 0
end table

public table LargeGear_table41
	0, 0, 0, 0, 0, 0, 0, 0
end table


function LargeGear_DebugDraw
	temp0 = 0
	temp1 = object.rotation
	object.rotation = 0
	while temp0 < 8
		DrawSpriteFX(0, FX_ROTATE, object.xpos, object.ypos)
		object.rotation += 64
		temp0++
	loop
	temp0 = 0
	while temp0 < 8
		DrawSpriteFX(2, FX_ROTATE, object.xpos, object.ypos)
		object.rotation += 64
		temp0++
	loop
	object.rotation = temp1
	Cos(temp0, 0)
	temp0 *= -28
	temp0 <<= 7
	temp0 += object.xpos
	Sin(temp1, 0)
	temp1 *= -28
	temp1 <<= 7
	temp1 += object.ypos
	DrawSpriteXY(1, temp0, temp1)
end function


function LargeGear_DebugSpawn
	CreateTempObject(TypeName[Large Gear], object.direction, object.xpos, object.ypos)
	object[tempObjectPos].drawOrder = 2
end function


event ObjectMain
	object.rotation = oscillation
	object.rotation &= 63
	object.value0 = oscillation
	if object.propertyValue == 1
		FlipSign(object.rotation)
		object.rotation += 104
		object.rotation &= 63
		FlipSign(object.value0)
		object.value0 += 552
		object.value0 &= 511
	end if
	temp0 = 0
	temp1 = object.value0
	while temp0 < 8
		Cos(temp2, temp1)
		temp2 *= -75
		temp2 >>= 9
		SetTableValue(temp2, temp0, LargeGear_table40)
		Sin(temp2, temp1)
		temp2 *= -75
		temp2 >>= 9
		SetTableValue(temp2, temp0, LargeGear_table41)
		temp0++
		temp1 += 64
	loop
	temp7 = 0
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		GetBit(temp6, object.value2, temp7)
		if temp6 == 1
			object[currentPlayer].ypos += 0x40000
		end if
		SetBit(object.value2, temp7, 0)
		temp0 = 0
		temp5 = 9
		while temp0 < 8
			GetTableValue(temp1, temp0, LargeGear_table40)
			GetTableValue(temp2, temp0, LargeGear_table41)
			temp3 = temp1
			temp4 = temp2
			temp1 -= 10
			temp2 -= 10
			temp3 += 10
			temp4 += 10
			if temp6 == 1
				BoxCollisionTest(C_PLATFORM, object.entityPos, temp1, temp2, temp3, temp4, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			else
				BoxCollisionTest(C_BOX, object.entityPos, temp1, temp2, temp3, temp4, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			end if
			if checkResult == 1
				object[currentPlayer].gravity = object.entityPos
				SetBit(object.value2, temp7, 1)
				temp5 = temp0
			end if
			temp0++
		loop
		if temp5 < 8
			GetTableValue(temp0, temp5, LargeGear_table40)
			switch temp5
			case 0
				temp0 -= object.value3
				break
			case 1
				temp0 -= object.value4
				break
			case 2
				temp0 -= object.value5
				break
			case 3
				temp0 -= object.value6
				break
			case 4
				temp0 -= object.value7
				break
			case 5
				temp0 -= object.value8
				break
			case 6
				temp0 -= object.value9
				break
			case 7
				temp0 -= object.value10
				break
			end switch
			temp0 <<= 16
			temp1 = temp0
			temp1 >>= 2
			temp0 += temp1
			object[currentPlayer].xpos += temp0
		else
			if temp6 == 1
				object[currentPlayer].ypos -= 0x40000
			end if
		end if
		arrayPos0 = stage.entityPos
		stage.entityPos = currentPlayer
		if object.xpos > object[arrayPos0].xpos
			ObjectTileCollision(CSIDE_RWALL, object.collisionLeft, 0, 0)
		else
			ObjectTileCollision(CSIDE_LWALL, object.collisionRight, 0, 0)
		end if
		ObjectTileCollision(CSIDE_FLOOR, 0, object.collisionBottom, 0)
		stage.entityPos = arrayPos0
		BoxCollisionTest(C_BOX, object.entityPos, -64, -8, 64, 8, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
		temp7++
	next
	GetTableValue(object.value3, 0, LargeGear_table40)
	GetTableValue(object.value4, 1, LargeGear_table40)
	GetTableValue(object.value5, 2, LargeGear_table40)
	GetTableValue(object.value6, 3, LargeGear_table40)
	GetTableValue(object.value7, 4, LargeGear_table40)
	GetTableValue(object.value8, 5, LargeGear_table40)
	GetTableValue(object.value9, 6, LargeGear_table40)
	GetTableValue(object.value10, 7, LargeGear_table40)
end event


event ObjectDraw
	if object.value1 == 0
		temp0 = 0
		temp1 = object.rotation
		while temp0 < 8
			DrawSpriteFX(0, FX_ROTATE, object.xpos, object.ypos)
			object.rotation += 64
			temp0++
		loop
		object.rotation = temp1
		AddDrawListEntityRef(4, object.entityPos)
	else
		temp0 = 0
		temp1 = object.rotation
		while temp0 < 8
			DrawSpriteFX(2, FX_ROTATE, object.xpos, object.ypos)
			object.rotation += 64
			temp0++
		loop
		object.rotation = temp1
		Cos(temp0, object.value0)
		temp0 *= -28
		temp0 <<= 7
		temp0 += object.xpos
		Sin(temp1, object.value0)
		temp1 *= -28
		temp1 <<= 7
		temp1 += object.ypos
		DrawSpriteXY(1, temp0, temp1)
	end if
	object.value1 ^= 1
end event


event ObjectStartup
	LoadSpriteSheet("MPZ/Objects.gif")
	SpriteFrame(-14, -88, 28, 32, 256, 1)
	SpriteFrame(-12, -12, 24, 24, 487, 157)
	SpriteFrame(-2, -14, 4, 4, 40, 34)
	foreach (TypeName[Large Gear], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].drawOrder = 2
	next
	SetTableValue(TypeName[Large Gear], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(LargeGear_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(LargeGear_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
