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

//-------Aliases-------//
private alias 55 : TYPE_OCTUS

// Function declarations
reserve function Octus_DebugDraw
reserve function Octus_DebugSpawn
reserve function Octus_Function132

// Static Values

// Tables

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


function Octus_DebugSpawn
	CreateTempObject(TypeName[Octus], 0, object.xpos, object.ypos)
	object[tempObjectPos].direction = object.direction
end function


function Octus_Function132
	temp0 = object.xpos
	temp0 >>= 16
	temp0 -= screen.cameraX
	Abs(temp0)
	temp0 -= 32
	if temp0 < screen.xcenter
		temp0 = object.ypos
		temp0 >>= 16
		temp0 -= screen.cameraY
		Abs(temp0)
		if temp0 < 140
			PlaySfx(SfxName[Octus Shot], 0)
		end if
	end if
end function


event ObjectMain
	switch object.state
	case 0
		if object.priority != PRIORITY_XBOUNDS_DESTROY
			object.priority = PRIORITY_ACTIVE
		end if
		ObjectTileGrip(CSIDE_FLOOR, 0, 13, 0)
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -128, -512, 128, 512, currentPlayer, 0, 0, 0, 0)
			if checkResult == 1
				object.state = 1
			end if
		next
		object.frame = object.animationTimer
		object.frame >>= 4
		object.animationTimer++
		object.animationTimer &= 31
		break
	case 1
		object.frame = 2
		object.value0++
		if object.value0 == 32
			object.value0 = 0
			object.yvel = -0x20000
			object.animationTimer = 0
			object.state = 2
		end if
		break
	case 2
		if object.value0 < 8
			object.value0++
			object.frame = 1
		else
			object.frame = object.animationTimer
			object.frame >>= 2
			object.frame += 3
			object.animationTimer++
			object.animationTimer %= 12
		end if
		object.yvel += 0x1000
		if object.yvel >= 0
			object.value0 = 0
			object.drawOrder = 4
			object.state = 3
		end if
		object.ypos += object.yvel
		break
	case 3
		if object.value0 == 0
			CreateTempObject(TypeName[Octus Shot], 0, object.xpos, object.ypos)
			if object.direction == FLIP_NONE
				object[tempObjectPos].xpos -= 0xE0000
				object[tempObjectPos].xvel = -0x20000
			else
				object[tempObjectPos].xpos += 0xE0000
				object[tempObjectPos].xvel = 0x20000
			end if
			CallFunction(Octus_Function132)
		end if
		if object.value0 == 15
			object.drawOrder = 3
		end if
		object.frame = object.animationTimer
		object.frame >>= 2
		object.frame += 3
		object.animationTimer++
		object.animationTimer %= 12
		object.value0++
		if object.value0 == 60
			object.value0 = 0
			object.state = 4
		end if
		break
	case 4
		object.frame = object.animationTimer
		object.frame >>= 2
		object.frame += 3
		object.animationTimer++
		object.animationTimer %= 12
		object.yvel += 0x1000
		object.ypos += object.yvel
		ObjectTileCollision(CSIDE_FLOOR, 0, 13, 0)
		if checkResult == 1
			object.yvel = 0
			object.animationTimer = 0
			object.state = 0
		end if
		break
	end switch
	if object.outOfBounds == 1
		temp0 = object.ypos
		object.ypos = object.value1
		if object.outOfBounds == 1
			if object.priority != PRIORITY_XBOUNDS_DESTROY
				object.priority = PRIORITY_ACTIVE_BOUNDS
				object.value0 = 0
				object.animationTimer = 0
				object.state = 0
			end if
		else
			object.ypos = temp0
		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)
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone07")
	if checkResult == 1
		LoadSpriteSheet("OOZ/Objects.gif")
		SpriteFrame(-21, -12, 42, 25, 1, 49)
		SpriteFrame(-16, -19, 32, 32, 149, 52)
		SpriteFrame(-21, -12, 42, 25, 1, 75)
		SpriteFrame(-21, -12, 42, 25, 1, 49)
		SpriteFrame(-20, -12, 40, 25, 44, 49)
		SpriteFrame(-21, -12, 42, 25, 85, 49)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-21, -12, 42, 25, 667, 256)
		SpriteFrame(-16, -19, 32, 32, 634, 256)
		SpriteFrame(-21, -12, 42, 25, 794, 256)
		SpriteFrame(-21, -12, 42, 25, 667, 256)
		SpriteFrame(-20, -12, 40, 25, 710, 256)
		SpriteFrame(-21, -12, 42, 25, 751, 256)
	end if
	foreach (TypeName[Octus], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].direction = object[arrayPos0].propertyValue
		object[arrayPos0].direction &= FLIP_X
		object[arrayPos0].value1 = object[arrayPos0].ypos
	next
	SetTableValue(TypeName[Octus], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Octus_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Octus_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
