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

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

private alias object.value0 : object.startPos.y
private alias object.value1 : object.timer
private alias object.value2 : object.cannonOffset.x

// States
private alias 0 : CLUCKER_HIDDEN
private alias 1 : CLUCKER_APPEAR
private alias 2 : CLUCKER_ADJUSTCANNON
private alias 3 : CLUCKER_ATTACKING

// Player Aliases
private alias object.xpos : player.xpos

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

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

reserve function Clucker_Setup
reserve function Clucker_DebugDraw
reserve function Clucker_DebugSpawn

// ========================
// Static Values
// ========================

private value Clucker_sheetX = 0
private value Clucker_sheetY = 0

private function Clucker_Setup
	object[arrayPos0].ypos 		= object[arrayPos0].startPos.y
	object[arrayPos0].timer 	= 0
	object[arrayPos0].state 	= CLUCKER_HIDDEN
	object[arrayPos0].priority 	= PRIORITY_BOUNDS
end function


private function Clucker_DebugDraw
	EditFrame(0, -16, -16, 32, 32, Clucker_sheetX, Clucker_sheetY)
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end function


private function Clucker_DebugSpawn
	CreateTempObject(TypeName[Clucker], 0, object.xpos, object.ypos)
	object[tempObjectPos].ypos += 0x200000
	object[tempObjectPos].startPos.y 	= object[tempObjectPos].ypos
	object[tempObjectPos].direction 	= object.direction

	arrayPos0 = object[tempObjectPos].entityPos
	CallFunction(Clucker_Setup)
end function


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

event ObjectUpdate
	switch object.state
	case CLUCKER_HIDDEN
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -128, -256, 128, 32, currentPlayer, -1, -1, 1, 1)
			if checkResult == true
				object.timer = 16
				if player[currentPlayer].xpos > object.xpos
					object.direction 		= FLIP_X
					object.cannonOffset.x	= -0x80000
				else
					object.direction 		= FLIP_NONE
					object.cannonOffset.x	= 0x80000
				end if
				object.priority = PRIORITY_ACTIVE
				object.state 	= CLUCKER_APPEAR
			end if
		next
		break

	case CLUCKER_APPEAR
		object.ypos -= 0x20000

		object.timer--
		if object.timer == 0
			object.timer 	= 8
			object.state 	= CLUCKER_ADJUSTCANNON
		end if
		break

	case CLUCKER_ADJUSTCANNON
		if object.direction == FLIP_NONE
			object.cannonOffset.x -= 0x10000
		else
			object.cannonOffset.x += 0x10000
		end if

		object.timer--
		if object.timer == 0
			object.timer 	= 14
			object.state 	= CLUCKER_ATTACKING
		end if
		break

	case CLUCKER_ATTACKING
		switch object.timer
		case 4
			if object.direction == FLIP_NONE
				object.cannonOffset.x += 0x20000
			else
				object.cannonOffset.x -= 0x20000
			end if
			break

		case 0
			PlaySfx(SfxName[Octus Shot], false)
			temp0 = object.xpos
			temp1 = object.ypos
			temp1 += 0xB0000

			if object.direction == FLIP_NONE
				object.cannonOffset.x -= 0x20000
				temp0 -= 0x130000
				CreateTempObject(TypeName[Clucker Shot], 0, temp0, temp1)
				object[tempObjectPos].xvel = -0x20000
			else
				object.cannonOffset.x += 0x20000
				temp0 += 0x130000
				CreateTempObject(TypeName[Clucker Shot], 0, temp0, temp1)
				object[tempObjectPos].xvel = 0x20000
			end if
			object[tempObjectPos].direction = object.direction

			object.timer = 68
			break
			
		end switch

		object.timer--
		break
		
	end switch

	temp0 = object.ypos
	object.ypos = object.startPos.y
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_SOLID, object.entityPos, -16, -8, 16, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
	next

	object.ypos = temp0
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -14, -16, 14, 16, currentPlayer, player[currentPlayer].hitboxLeft, player[currentPlayer].hitboxTop, player[currentPlayer].hitboxRight, player[currentPlayer].hitboxBottom)
		if checkResult == true
			CallFunction(Player_BadnikBreak)
		end if
	next

	if object.outOfBounds == true
		temp1 = object.ypos
		object.ypos = object.startPos.y
		if object.outOfBounds == true
			arrayPos0 = object.entityPos
			CallFunction(Clucker_Setup)
		else
			object.ypos = temp1
		end if
	end if
end event


event ObjectDraw
	if object.state >= CLUCKER_ADJUSTCANNON
		temp0 = object.xpos
		temp0 += object.cannonOffset.x
		DrawSpriteFX(1, FX_FLIP, temp0, object.ypos)
	end if

	temp0 = object.startPos.y
	temp0 -= object.ypos
	temp0 >>= 16
	EditFrame(0, -16, -16, 32, temp0, Clucker_sheetX, Clucker_sheetY)
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone11")
	if checkResult == true
		LoadSpriteSheet("SCZ/Objects.gif")
		SpriteFrame(-16, -16, 32, 32, 9, 223) 	// Clucker 	- #0
		SpriteFrame(-24, 7, 8, 7, 1, 246)		// Cannon 	- #1
		Clucker_sheetX = 9
		Clucker_sheetY = 223
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-16, -16, 32, 32, 845, 256) // Clucker - #0
		SpriteFrame(-24, 7, 8, 7, 837, 279)		// Cannon 	- #1
		Clucker_sheetX = 845
		Clucker_sheetY = 256
	end if

	foreach (TypeName[Clucker], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].ypos += 0x200000
		object[arrayPos0].startPos.y = object[arrayPos0].ypos
		CallFunction(Clucker_Setup)
	next

	SetTableValue(TypeName[Clucker], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Clucker_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Clucker_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

event RSDKDraw
	DrawSprite(1)
	DrawSprite(0)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone11")
	if checkResult == true
		LoadSpriteSheet("SCZ/Objects.gif")
		SpriteFrame(-16, -16, 32, 32, 9, 223) 	// Clucker 	- #0
		SpriteFrame(-24, 7, 8, 7, 1, 246)		// Cannon 	- #1
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-16, -16, 32, 32, 845, 256) // Clucker - #0
		SpriteFrame(-24, 7, 8, 7, 837, 279)		// Cannon 	- #1
	end if
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
