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

//-------Aliases-------//
private alias 66 : TYPE_BUBBLERSMOTHER

// Function declarations
reserve function BubblersMother_Function153
reserve function BubblersMother_DebugDraw
reserve function BubblersMother_DebugSpawn

// Static Values

// Tables

function BubblersMother_Function153
	object[arrayPos0].direction = object[arrayPos0].propertyValue
	if object[arrayPos0].direction == FLIP_NONE
		object[arrayPos0].xvel = -0x4000
	else
		object[arrayPos0].xvel = 0x4000
	end if
	object[arrayPos0].yvel = 0
	object[arrayPos0].value0 = 512
	object[arrayPos0].value1 = 0
	object[arrayPos0].frame = 1
	object[arrayPos0].animationTimer = 0
	object[arrayPos0].priority = PRIORITY_ACTIVE_BOUNDS
	object[arrayPos0].state = 0
end function


function BubblersMother_DebugDraw
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end function


function BubblersMother_DebugSpawn
	CreateTempObject(TypeName[Bubblers Mother], object.direction, object.xpos, object.ypos)
	object[tempObjectPos].value2 = object.xpos
	object[tempObjectPos].value3 = object.ypos
	arrayPos0 = object[tempObjectPos].entityPos
	CallFunction(BubblersMother_Function153)
end function


event ObjectMain
	switch object.state
	case 0
		object.priority = PRIORITY_ACTIVE
		object.xpos += object.xvel
		object.animationTimer++
		object.animationTimer &= 3
		object.frame = object.animationTimer
		object.frame >>= 1
		object.frame++
		object.value0--
		if object.value0 == 0
			object.direction ^= FLIP_X
			FlipSign(object.xvel)
			object.value0 = 512
		end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object.state == 0
				if object.direction == FLIP_NONE
					BoxCollisionTest(C_TOUCH, object.entityPos, -160, 12, 16, 96, currentPlayer, -1, -1, 1, 1)
				else
					BoxCollisionTest(C_TOUCH, object.entityPos, 16, 12, 160, 96, currentPlayer, -1, -1, 1, 1)
				end if
				if checkResult == 1
					object.state = 1
					object.frame = 2
					object.value0 = 16
					if object.direction == FLIP_NONE
						object.xvel = -0x28000
					else
						object.xvel = 0x28000
					end if
				end if
			end if
		next
		break
	case 1
		object.value0--
		if object.value0 < 0
			object.state = 2
		end if
		object.animationTimer++
		object.animationTimer &= 3
		object.frame = object.animationTimer
		object.frame >>= 1
		object.frame++
		break
	case 2
		object.xpos += object.xvel
		object.ypos += object.yvel
		object.value1++
		if object.value1 >= 20
			object.value1 = 0
			temp0 = object.xpos
			if object.direction == FLIP_NONE
				temp0 += 0x60000
			else
				temp0 -= 0x60000
			end if
			temp1 = object.ypos
			temp1 += 0xA0000
			CreateTempObject(TypeName[Bubbler], 0, temp0, temp1)
		end if
		object.animationTimer++
		if object.animationTimer >= 3
			object.animationTimer = 0
		end if
		object.frame = object.animationTimer
		object.frame++
		break
	end switch
	if object.outOfBounds == 1
		temp0 = object.xpos
		temp1 = object.ypos
		object.xpos = object.value2
		object.ypos = object.value3
		if object.outOfBounds == 1
			arrayPos0 = object.entityPos
			CallFunction(BubblersMother_Function153)
		else
			object.xpos = temp0
			object.ypos = temp1
		end if
	end if
	CallFunction(PlayerObject_EnemyPop)
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -16, -12, 16, 12, currentPlayer, object[currentPlayer].value40, object[currentPlayer].value38, object[currentPlayer].value41, object[currentPlayer].value39)
		if checkResult == 1
			CallFunction(PlayerObject_BadnikBreak)
		end if
	next
end event


event ObjectDraw
	DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone02")
	if checkResult == 1
		LoadSpriteSheet("CPZ/Objects.gif")
		SpriteFrame(-16, -12, 32, 28, 117, 102)
		SpriteFrame(0, 0, 1, 1, 117, 102)
		SpriteFrame(15, -3, 7, 13, 150, 111)
		SpriteFrame(15, -3, 8, 13, 158, 111)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-16, -12, 32, 28, 117, 102)
	end if
	foreach (TypeName[Bubblers Mother], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].value2 = object[arrayPos0].xpos
		CallFunction(BubblersMother_Function153)
	next
	SetTableValue(TypeName[Bubblers Mother], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(BubblersMother_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(BubblersMother_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
