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

//-------Aliases-------//
private alias 43 : TYPE_ORBINAUT

// Function declarations
reserve function Orbinaut_DebugDraw
reserve function Orbinaut_DebugSpawn
reserve function Orbinaut_Function107
reserve function Orbinaut_Function108
reserve function Orbinaut_Function109
reserve function Orbinaut_Mighty

// Static Values
public value Orbinaut_value32 = 0;

// Tables

function Orbinaut_DebugDraw
	temp0 = object.value17
	temp0 -= Orbinaut_value32
	temp0--
	temp0 ^= 1
	temp0 <<= 1
	DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
end function


function Orbinaut_DebugSpawn
	temp0 = object.value17
	temp0 -= Orbinaut_value32
	CreateTempObject(TypeName[Orbinaut], temp0, object.xpos, object.ypos)
	object[tempObjectPos].value2 = object[tempObjectPos].xpos
	object[tempObjectPos].value1 = 15
	if object[tempObjectPos].propertyValue > 1
		object[tempObjectPos].value3 = 0
		object[tempObjectPos].propertyValue &= 1
		GetBit(temp0, object.direction, 0)
		if temp0 == 0
			object[tempObjectPos].direction = FLIP_NONE
			object[tempObjectPos].xvel = -0x4000
		else
			object[tempObjectPos].direction = FLIP_X
			object[tempObjectPos].xvel = 0x4000
		end if
	else
		object[tempObjectPos].value3 = 1
		object[tempObjectPos].xvel = 0
		GetBit(temp0, object.direction, 0)
		if temp0 == 0
			object[tempObjectPos].direction = FLIP_NONE
		else
			object[tempObjectPos].direction = FLIP_X
		end if
	end if
end function


function Orbinaut_Mighty
	if stage.playerListPos != 7
		checkResult = false
	else
		CheckEqual(object[currentPlayer].state, PlayerObject_Hurt)
		temp4  = checkResult
		CheckEqual(object[currentPlayer].state, PlayerObject_Death)
		temp4 |= checkResult
		CheckEqual(object[currentPlayer].state, PlayerObject_Drown)
		temp4 |= checkResult
		CheckNotEqual(object[currentPlayer].value7, 0)
		temp4 |= checkResult
		CheckGreater(object[currentPlayer].value8, 0)
		temp4 |= checkResult
		if temp4 == true
			checkResult = false
		else
			CheckEqual(object[currentPlayer].animation, ANI_JUMPING)
			temp4  = checkResult
			CheckEqual(object[currentPlayer].animation, ANI_SPINDASH)
			temp4 |= checkResult
			CheckEqual(object[currentPlayer].animation, ANI_HAMMER_DROP)
			temp4 |= checkResult
			if temp4 == true
			
				temp4  = object[currentPlayer].xpos
				temp4 -= object.xpos
				temp5  = object[currentPlayer].ypos
				temp5 -= object.ypos
				ATan2(player.intAngle, temp4, temp5)
				
				if object[currentPlayer].animation != ANI_UNSPIN
					Cos256(temp4, player.intAngle)
					temp4 *= player.bounceDistance
					object[currentPlayer].xVel  = temp4
					object[currentPlayer].speed = temp4
				endif
				
				Sin256(temp4, player.IntAngle)
				temp4 *= player.BounceDistance
				object[currentPlayer].yVel = temp4
				object[currentPlayer].yVel = -0x48000
				
				temp4  = object[currentPlayer].direction
				temp4 &= FLIP_X
				if temp4 == false
					object[currentPlayer].xVel = -0x28000
				else	
					object[currentPlayer].xVel = 0x28000
				end if

				if object[currentPlayer].value25 == 0x1000
					object[currentPlayer].xVel >>= 1
					object[currentPlayer].yVel   = -0x24000
				end if

				object[currentPlayer].value8 = 121
				object[currentPlayer].visible = true
				object[currentPlayer].animation = ANI_UNSPIN
				PlaySfx(sfxName[MightyUnspin], 0)
				object[currentPlayer].gravity = GRAVITY_AIR
				object[currentPlayer].value35 = 0
				object[currentPlayer].state = PlayerObject_HandleMightyUnspin
				object[currentPlayer].speed = object[currentPlayer].xvel
			end if
		end if
	end if
end function

function Orbinaut_Function107
	temp0 = 0
	temp7 = object.angle
	temp1 = object.xpos
	temp2 = object.ypos
	while temp0 < 4
		GetBit(temp6, object.value1, temp0)
		if temp6 == 1
			Cos256(object.xpos, temp7)
			object.xpos <<= 12
			object.xpos += temp1
			Sin256(object.ypos, temp7)
			object.ypos <<= 12
			object.ypos += temp2
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				BoxCollisionTest(C_TOUCH, object.entityPos, -4, -4, 4, 4, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				if checkResult == 1
					Player.BounceDistance = 0x400
					Player.CheckHammerDrop = 0
					CallFunction(Orbinaut_Mighty)
					if checkResult == false
						CallFunction(PlayerObject_Hit)
					end if
				end if
			next
		end if
		temp7 += 64
		temp7 &= 255
		temp0++
	loop
	object.xpos = temp1
	object.ypos = temp2
	CallFunction(PlayerObject_EnemyPop)
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -8, -8, 8, 8, currentPlayer, object[currentPlayer].value40, object[currentPlayer].value38, object[currentPlayer].value41, object[currentPlayer].value39)
		if checkResult == 1
			CallFunction(PlayerObject_BadnikBreak)
		end if
	next
end function


function Orbinaut_Function108
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -4, -4, 4, 4, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
		if checkResult == 1
			Player.BounceDistance = 0x400
			Player.CheckHammerDrop = 0
			CallFunction(Orbinaut_Mighty)
			if checkResult == false
				CallFunction(PlayerObject_Hit)
			end if
		end if
	next
end function


function Orbinaut_Function109
	if object.outOfBounds == 1
		temp0 = object.xpos
		object.xpos = object.value2
		if object.outOfBounds == 1
			object.xpos = object.value2
			object.value1 = 15
			if object.value3 == 0
				if object.propertyValue == 0
					object.direction = FLIP_NONE
					object.xvel = -0x4000
				else
					object.direction = FLIP_X
					object.xvel = 0x4000
				end if
			else
				if object.propertyValue == 0
					object.direction = FLIP_NONE
				else
					object.direction = FLIP_X
				end if
				object.xvel = 0
			end if
			object.value0 = 0
			object.animationTimer = 0
			object.priority = PRIORITY_ACTIVE_BOUNDS
			object.state = 0
			object.angle = 0
		else
			object.xpos = temp0
		end if
	end if
end function


event ObjectMain
	switch object.state
	case 0
		object.priority = PRIORITY_ACTIVE
		object.xpos += object.xvel
		if object.direction == FLIP_NONE
			object.angle++
		else
			object.angle--
		end if
		object.angle &= 255
		CallFunction(Orbinaut_Function107)
		if object.value3 == 1
			object.value6 = 0x7FFFFFFF
			object.value4 = 0xFFFF
			object.value5 = 0
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				temp0 = object[currentPlayer].ypos
				temp0 -= object.ypos
				Abs(temp0)
				if temp0 < object.value6
					object.value6 = temp0
					object.value5 = currentPlayer
				end if
				if temp0 < 0x400000
					if object.value4 != 0xFFFF
						arrayPos0 = object.value4
						temp0 = object[currentPlayer].xpos
						temp0 -= object.xpos
						Abs(temp0)
						temp1 = object[arrayPos0].xpos
						temp1 -= object.xpos
						Abs(temp1)
						if temp0 < temp1
							object.value4 = currentPlayer
						end if
					else
						object.value4 = currentPlayer
					end if
				end if
			next
			if object.value4 != 0xFFFF
				arrayPos0 = object.value4
				temp0 = object.xpos
				temp0 -= object[arrayPos0].xpos
				Abs(temp0)
				if temp0 <= 0x800000
					object.state = 1
					object.frame = 1
				end if
			else
				arrayPos0 = object.value5
			end if
			if object[arrayPos0].xpos < object.xpos
				object.direction = FLIP_NONE
			else
				object.direction = FLIP_X
			end if
		end if
		CallFunction(Orbinaut_Function109)
		break
	case 1
		if object.direction == FLIP_NONE
			object.angle++
		else
			object.angle--
		end if
		object.angle &= 255
		temp0 = 0
		temp7 = object.angle
		while temp0 < 4
			if temp7 == 64
				GetBit(temp6, object.value1, temp0)
				if temp6 == 1
					Cos256(temp1, temp7)
					temp1 <<= 12
					temp1 += object.xpos
					Sin256(temp2, temp7)
					temp2 <<= 12
					temp2 += object.ypos
					SetBit(object.value1, temp0, 0)
					CreateTempObject(TypeName[Orbinaut], 0, temp1, temp2)
					object[tempObjectPos].state = 3
					if object.direction == FLIP_NONE
						object[tempObjectPos].xvel = -0x20000
					else
						object[tempObjectPos].xvel = 0x20000
					end if
				end if
			end if
			temp7 += 64
			temp7 &= 255
			temp0++
		loop
		CallFunction(Orbinaut_Function107)
		if object.value1 == 0
			object.state = 2
			if object.direction == FLIP_NONE
				object.xvel = -0x4000
			else
				object.xvel = 0x4000
			end if
		end if
		if object.animationTimer < 16
			object.animationTimer++
		else
			object.frame = 2
		end if
		CallFunction(Orbinaut_Function109)
		break
	case 2
		object.xpos += object.xvel
		CallFunction(Orbinaut_Function107)
		CallFunction(Orbinaut_Function109)
		break
	case 3
		object.xpos += object.xvel
		CallFunction(Orbinaut_Function108)
		if object.outOfBounds == 1
			object.type = TypeName[Blank Object]
		end if
		break
	end switch
end event


event ObjectDraw
	switch object.state
	case 0
	case 1
		temp0 = 0
		temp7 = object.angle
		while temp0 < 4
			GetBit(temp6, object.value1, temp0)
			if temp6 == 1
				Cos256(temp1, temp7)
				temp1 <<= 12
				temp1 += object.xpos
				Sin256(temp2, temp7)
				temp2 <<= 12
				temp2 += object.ypos
				DrawSpriteXY(3, temp1, temp2)
			end if
			temp7 += 64
			temp7 &= 255
			temp0++
		loop
		DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
		break
	case 2
		DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
		break
	case 3
		DrawSprite(3)
		break
	end switch
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone04")
	if checkResult == 1
		LoadSpriteSheet("LZ/Objects.gif")
		SpriteFrame(-10, -10, 20, 20, 50, 105)
		SpriteFrame(-10, -10, 20, 20, 71, 105)
		SpriteFrame(-10, -10, 20, 20, 50, 154)
		SpriteFrame(-8, -8, 16, 16, 107, 1)
	end if
	CheckCurrentStageFolder("Zone05")
	if checkResult == 1
		LoadSpriteSheet("SLZ/Objects.gif")
		SpriteFrame(-10, -10, 20, 20, 51, 1)
		SpriteFrame(-10, -10, 20, 20, 72, 1)
		SpriteFrame(-10, -10, 20, 20, 93, 1)
		SpriteFrame(-8, -8, 16, 16, 114, 1)
	end if
	CheckCurrentStageFolder("Zone06")
	if checkResult == 1
		LoadSpriteSheet("SBZ/Objects.gif")
		SpriteFrame(-10, -10, 20, 20, 1, 138)
		SpriteFrame(-10, -10, 20, 20, 22, 138)
		SpriteFrame(-10, -10, 20, 20, 43, 138)
		SpriteFrame(-8, -8, 16, 16, 64, 142)
	end if
	CheckCurrentStageFolder("BossRush")
	if checkResult == 1
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-10, -10, 20, 20, 119, 114)
		SpriteFrame(-10, -10, 20, 20, 140, 114)
		SpriteFrame(-10, -10, 20, 20, 119, 135)
		SpriteFrame(-8, -8, 16, 16, 140, 135)
	end if
	CheckCurrentStageFolder("Zone00")
	if checkResult == 1
		LoadSpriteSheet("GHZ/Objects3.gif")
		SpriteFrame(-10, -10, 20, 20, 119, 114)
		SpriteFrame(-10, -10, 20, 20, 140, 114)
		SpriteFrame(-10, -10, 20, 20, 119, 135)
		SpriteFrame(-8, -8, 16, 16, 140, 135)
	end if
	foreach (TypeName[Orbinaut], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].value2 = object[arrayPos0].xpos
		object[arrayPos0].value1 = 15
		if object[arrayPos0].propertyValue > 1
			object[arrayPos0].value3 = 0
			object[arrayPos0].propertyValue &= 1
			if object[arrayPos0].propertyValue == 0
				object[arrayPos0].direction = FLIP_NONE
				object[arrayPos0].xvel = -0x4000
			else
				object[arrayPos0].direction = FLIP_X
				object[arrayPos0].xvel = 0x4000
			end if
		else
			object[arrayPos0].value3 = 1
			object[arrayPos0].xvel = 0
			if object[arrayPos0].propertyValue == 0
				object[arrayPos0].direction = FLIP_NONE
			else
				object[arrayPos0].direction = FLIP_X
			end if
		end if
	next
	temp0 = 0
	Orbinaut_value32 = DebugMode_ObjCount
	Orbinaut_value32--
	while temp0 < 2
		SetTableValue(TypeName[Orbinaut], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(Orbinaut_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(Orbinaut_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
