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

//-------Aliases-------//
private alias 56 : TYPE_BEE

// Function declarations
reserve function Bee_DebugDraw
reserve function Bee_DebugSpawn

// Static Values

// Tables

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


function Bee_DebugSpawn
	CreateTempObject(TypeName[Bee], 0, object.xpos, object.ypos)
	GetBit(temp0, object.direction, 0)
	if temp0 == 0
		object[tempObjectPos].direction = FLIP_NONE
		object[tempObjectPos].xvel = -0x10000
	else
		object[tempObjectPos].direction = FLIP_X
		object[tempObjectPos].xvel = 0x10000
	end if
end function


event ObjectMain
	switch object.state
	case 0
		object.priority = PRIORITY_ACTIVE
		if object.value0 < 256
			object.value0++
			object.xpos += object.xvel
		else
			object.value0 = 0
			object.state = 1
			object.animation = 1
			object.direction ^= FLIP_X
			FlipSign(object.xvel)
		end if
		break
	case 1
		if object.value3 < 30
			object.value3++
		else
			object.value3 = 0
			object.state = 0
			object.animation = 0
			object.value2 = 0
		end if
		break
	case 2
		if object.value3 < 50
			object.value3++
			if object.value3 == 30
				CreateTempObject(TypeName[Bee Shot], 0, object.xpos, object.ypos)
				object[tempObjectPos].direction = object.direction
				if object.direction == FLIP_NONE
					object[tempObjectPos].xpos += 0xD0000
					object[tempObjectPos].xvel = -0x18000
				else
					object[tempObjectPos].xpos -= 0xD0000
					object[tempObjectPos].xvel = 0x18000
				end if
				object[tempObjectPos].ypos += 0x180000
				object[tempObjectPos].yvel = 0x18000
			end if
		else
			object.value3 = 0
			object.state = 0
			object.animation = 0
			object.value2 = 1
		end if
		break
	case 3
		object.animation = 3
		break
	end switch
	if object.outOfBounds == 1
		temp0 = object.xpos
		object.xpos = object.value1
		if object.value3 == 1
			object.state = 3
		end if
		if object.outOfBounds == 1
			object.xpos = object.value1
			GetBit(temp0, object.propertyValue, 0)
			if temp0 == 0
				object.direction = FLIP_NONE
				object.xvel = -0x10000
			else
				object.direction = FLIP_X
				object.xvel = 0x10000
			end if
			object.value0 = 0
			object.animation = 0
			object.priority = PRIORITY_ACTIVE_BOUNDS
			object.state = 0
		else
			object.xpos = temp0
		end if
	end if
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		if object.state == 0
			if object.value2 == 0
				if object.direction == FLIP_NONE
					BoxCollisionTest(C_TOUCH, object.entityPos, -28, -256, -30, 256, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				else
					BoxCollisionTest(C_TOUCH, object.entityPos, 28, -256, 30, 256, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				end if
				if checkResult == 1
					object.state = 2
					object.animation = 2
				end if
			end if
		end if
		if object.state < 3
			CallFunction(PlayerObject_EnemyPop)
			BoxCollisionTest(C_TOUCH, object.entityPos, -24, -12, 24, 12, currentPlayer, object[currentPlayer].value40, object[currentPlayer].value38, object[currentPlayer].value41, object[currentPlayer].value39)
			if checkResult == 1
				CallFunction(PlayerObject_BadnikBreak)
			end if
		end if
	next
	object.frame = object.animationTimer
	object.frame /= 3
	object.frame += 3
	object.animationTimer++
	if object.animationTimer >= 6
		object.animationTimer = 0
	end if
end event


event ObjectDraw
	switch object.animation
	case 0
		DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
		DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
		break
	case 1
		DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
		break
	case 2
		DrawSpriteFX(1, FX_FLIP, object.xpos, object.ypos)
		if object.value3 > 20
			if object.value3 < 30
				DrawSpriteFX(2, FX_FLIP, object.xpos, object.ypos)
			end if
		end if
		break
	end switch
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone01")
	if checkResult == 1
		LoadSpriteSheet("EHZ/Objects.gif")
		SpriteFrame(-24, -8, 48, 18, 78, 1)
		SpriteFrame(-24, -8, 42, 31, 146, 224)
		SpriteFrame(1, 6, 17, 17, 50, 1)
		SpriteFrame(5, -8, 6, 5, 19, 50)
		SpriteFrame(5, -8, 7, 5, 19, 56)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-24, -8, 48, 18, 78, 1)
		SpriteFrame(-24, -8, 42, 31, 146, 224)
		SpriteFrame(1, 6, 17, 17, 50, 1)
		SpriteFrame(5, -8, 6, 5, 137, 331)
		SpriteFrame(5, -8, 7, 5, 137, 337)
	end if
	foreach (TypeName[Bee], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].value1 = object[arrayPos0].xpos
		GetBit(temp0, object[arrayPos0].propertyValue, 0)
		if temp0 == 0
			object[arrayPos0].direction = FLIP_NONE
			object[arrayPos0].xvel = -0x10000
		else
			object[arrayPos0].direction = FLIP_X
			object[arrayPos0].xvel = 0x10000
		end if
	next
	SetTableValue(TypeName[Bee], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Bee_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Bee_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
