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

// ========================
// Aliases
// ========================

private alias object.value0 : object.timer
private alias object.value1 : object.startPos.y

// States
private alias 0 : OCTUS_SETUP
private alias 1 : OCTUS_JUMPDELAY
private alias 2 : OCTUS_JUMP
private alias 3 : OCTUS_SHOOT
private alias 4 : OCTUS_FALL

// Player Aliases
private alias object.value40 : player.hitboxLeft
private alias object.value38 : player.hitboxTop
private alias object.value41 : player.hitboxRight
private alias object.value39 : player.hitboxBottom

// Path ID Aliases
private alias 0 : PATH_A


// ========================
// Function Declarations
// ========================

reserve function Octus_DebugDraw
reserve function Octus_DebugSpawn
reserve function Octus_TryPlayShotSfx


// ========================
// Function Definitions
// ========================

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


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


private function Octus_TryPlayShotSfx
	temp0 = screen.xcenter
	temp0 += 32
	CheckCameraProximity(object.ixpos, object.iypos, temp0, 140)
	if checkResult == true
		// Fun fact, this sound effect isn't named correctly for Mission Mode Sky Chase in Origins LOL
		// because of that it just plays the jump sfx (id 0)
		PlaySfx(SfxName[Octus Shot], false)
	end if
end function


// ========================
// Events
// ========================

event ObjectUpdate
	switch object.state
	case OCTUS_SETUP
		if object.priority != PRIORITY_XBOUNDS_DESTROY
			object.priority = PRIORITY_ACTIVE
		end if
		
		ObjectTileGrip(CSIDE_FLOOR, 0, 13, PATH_A)
		
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -128, -512, 128, 512, currentPlayer, 0, 0, 0, 0)
			if checkResult == true
				object.state = OCTUS_JUMPDELAY
			end if
		next

		object.frame = object.animationTimer
		object.frame >>= 4
		object.animationTimer++
		object.animationTimer &= 31
		break

	case OCTUS_JUMPDELAY
		object.frame = 2

		object.timer++
		if object.timer == 32
			object.timer = 0
			object.yvel = -0x20000
			object.animationTimer = 0
			object.state = OCTUS_JUMP
		end if
		break

	case OCTUS_JUMP
		if object.timer < 8
			object.timer++
			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.timer = 0
			object.drawOrder = 4
			object.state = OCTUS_SHOOT
		end if

		object.ypos += object.yvel
		break

	case OCTUS_SHOOT
		if object.timer == 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_TryPlayShotSfx)
		end if

		if object.timer == 15
			object.drawOrder = 3
		end if

		object.frame = object.animationTimer
		object.frame >>= 2
		object.frame += 3
		object.animationTimer++
		object.animationTimer %= 12
		object.timer++
		if object.timer == 60
			object.timer = 0
			object.state = OCTUS_FALL
		end if
		break

	case OCTUS_FALL
		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, PATH_A)
		if checkResult == true
			object.yvel = 0
			object.animationTimer = 0
			object.state = OCTUS_SETUP
		end if
		break

	end switch

	if object.outOfBounds == true
#platform: USE_ORIGINS
		if game.playMode == BOOT_PLAYMODE_MISSION
			object.yvel = 0
		end if
#endplatform
		temp0 = object.ypos
		object.ypos = object.startPos.y
		if object.outOfBounds == true
			if object.priority != PRIORITY_XBOUNDS_DESTROY
				object.priority = PRIORITY_BOUNDS
				object.timer = 0
				object.animationTimer = 0
				object.state = OCTUS_SETUP
			end if
		else
			object.ypos = temp0
		end if
	end if

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -16, -12, 16, 12, currentPlayer, player[currentPlayer].hitboxLeft, player[currentPlayer].hitboxTop, player[currentPlayer].hitboxRight, player[currentPlayer].hitboxBottom)
		if checkResult == true
			CallFunction(Player_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 == true
		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
		// This does mean the entire MBZ sheet is loaded in Origins's WFZ mission
		
		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].startPos.y = 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


// ========================
// Editor Events
// ========================

event RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			checkResult = object.propertyValue
			break
			
		case 0 // direction
			checkResult = object.propertyValue
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // direction
			object.propertyValue = editor.variableValue
			break
			
		end switch
	end if
end event


event RSDKDraw
	GetBit(object.direction, object.propertyValue, 0)
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("OOZ/Objects.gif")
		SpriteFrame(-21, -12, 42, 25, 1, 49)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-21, -12, 42, 25, 667, 256)
	end if

	AddEditorVariable("direction")
	SetActiveVariable("direction")
	AddEnumVariable("Left", 0)
	AddEnumVariable("Right", 1)
end event
