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

// Using ARZEggman as a prefix here because there's like 12 "Eggman" objects in the game so its less confusing this way

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

private alias object.value0 : object.originPos.x
private alias object.value1 : object.originPos.y
private alias object.value2 : object.oscillationAngle
private alias object.value4 : object.timer
private alias object.value5 : object.unused1 // Technically, it *is* used... but it's never used anywhere important
private alias object.value6 : object.unused2 // Technically, it *is* used... but it's never used anywhere important
private alias object.value7 : object.health
private alias object.value8 : object.invincibilityTimer
private alias object.value9 : object.exploding

// Eggman States
private alias 0 : ARZEGGMAN_AWAITPLAYER
private alias 1 : ARZEGGMAN_BOSSFIGHT
private alias 2 : ARZEGGMAN_EXPLODE
private alias 3 : ARZEGGMAN_DEFEATFALL
private alias 4 : ARZEGGMAN_DEFEATRISE
private alias 5 : ARZEGGMAN_FLEE
private alias 6 : ARZEGGMAN_ESCAPE

// Eggman Animations
private alias 0 : ARZEGGANI_INVISIBLE
private alias 1 : ARZEGGANI_IDLE
private alias 2 : ARZEGGANI_LAUGH
private alias 3 : ARZEGGANI_HIT
private alias 4 : ARZEGGANI_DEFEATED
private alias 5 : ARZEGGANI_TOASTED
private alias 6 : ARZEGGANI_PANIC

// EggmanHammer Aliases
private alias object.value0  : eggmanHammer.originPos.x
private alias object.value1  : eggmanHammer.originPos.y
private alias object.value2  : eggmanHammer.oscillationAngle
private alias object.value3  : eggmanHammer.timer
private alias object.value4  : eggmanHammer.boundsR
private alias object.value5  : eggmanHammer.boundsL
private alias object.value7  : eggmanHammer.boundsOffset
private alias object.value8  : eggmanHammer.hideFlames
private alias object.value12 : eggmanHammer.arrowVelocity

// EggmanHammer States
private alias 4 : EGGMANHAMMER_DESTROYED
private alias 5 : EGGMANHAMMER_DEBRIS

// EggmanTotemPole Aliases
private alias object.value3 : eggmanTotemPole.timer

// EggmanTotemPole States
private alias 3 : EGGMANTOTEMPOLE_LOWERING

// EggmanArrow States
private alias 2 : EGGMANARROW_FALLING

// Player Aliases
private alias object.xpos : player.xpos
private alias object.xvel : player.xvel
private alias object.speed : player.speed
private alias object.animation : player.animation
private alias object.collisionRight : player.collisionRight

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

// Music Events
private alias 26 : SLOT_MUSICEVENT_BOSS

private alias  0 : MUSICEVENT_FADETOBOSS
private alias  1 : MUSICEVENT_FADETOSTAGE


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

reserve function ARZEggman_Oscillate


// ========================
// Tables
// ========================

// No flames so this isn't used at all
// It's the same table as the one in [CPZ Eggman], likely just here because the entire CPZ script was used as a base
private table ARZEggman_flameAnimFrameTable
	7, 8, 8, 7, 7, 16
end table


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

private function ARZEggman_Oscillate
	object.ypos = object.originPos.y
	Sin256(temp0, object.oscillationAngle)
	temp0 <<= 10
	object.ypos += temp0
	object.oscillationAngle += 2
	object.oscillationAngle &= 255

	object[+1].xpos = object.xpos
	object[+1].ypos = object.ypos
	object[+1].direction = object.direction
end function


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

event ObjectUpdate
	switch object.state
	case ARZEGGMAN_AWAITPLAYER
		temp0 = object.ixpos
		temp0 -= screen.xcenter
		stage.newXBoundary1 = temp0

		temp0 = object.ixpos
		temp0 += screen.xcenter
		stage.newXBoundary2 = temp0

		if player[0].xpos > object.xpos
			object.animation = ARZEGGANI_IDLE
			ResetObjectEntity(SLOT_MUSICEVENT_BOSS, TypeName[Music Event], MUSICEVENT_FADETOBOSS, 0, 0)
			object[SLOT_MUSICEVENT_BOSS].priority = PRIORITY_ACTIVE

			object[+1].type = TypeName[Eggman Hammer]
			eggmanHammer[+1].timer = 168

			object.ypos -= 0xF80000
			object.originPos.y = object.ypos

			object[+1].xpos = object.xpos
			object[+1].ypos = object.ypos
			eggmanHammer[+1].originPos.x = object.xpos
			eggmanHammer[+1].originPos.y = object.ypos
			object[+1].frame = 3
			object[+1].priority = PRIORITY_ACTIVE

			object[+5].type = TypeName[Eggman Totem]
			object[+5].xpos = object.ixpos
			object[+5].xpos -= screen.xcenter
			object[+5].xpos += 16
			object[+5].xpos <<= 16

			eggmanHammer[+1].boundsR = object[+5].xpos
			eggmanHammer[+1].boundsR += 0x5A0000

			eggmanTotemPole[+5].timer = 136
			object[+5].drawOrder = 2
			object[+5].priority = PRIORITY_ACTIVE

			eggmanHammer[+1].boundsOffset = 0xC800
			eggmanHammer[+1].boundsOffset *= screen.xcenter
			eggmanHammer[+1].boundsOffset /= 160
			eggmanHammer[+1].arrowVelocity = 0x30000
			eggmanHammer[+1].arrowVelocity *= screen.xcenter
			eggmanHammer[+1].arrowVelocity /= 160

			object[+6].type = TypeName[Eggman Totem]
			object[+6].xpos = object.ixpos
			object[+6].xpos += screen.xcenter
			object[+6].xpos -= 16
			object[+6].xpos <<= 16

			eggmanHammer[+1].boundsL = object[+6].xpos
			eggmanHammer[+1].boundsL -= 0x5A0000

			eggmanTotemPole[+6].timer = 136
			object[+6].direction = FLIP_X
			object[+6].drawOrder = 2
			object[+6].priority = PRIORITY_ACTIVE

#platform: USE_STANDALONE
			object.health = 8
#endplatform
#platform: USE_ORIGINS
			if game.bossOneLife == false
				object.health = 8
			else
				object.health = 1
			end if
#endplatform

			object.priority = PRIORITY_ACTIVE
			object.state++
		end if
		break

	case ARZEGGMAN_BOSSFIGHT
		break

	case ARZEGGMAN_EXPLODE
		object.timer++
		if object.timer == 60
			if object.direction == FLIP_NONE
				object[+1].xvel = -0x10000
			else
				object[+1].xvel = 0x10000
			end if

			object[+1].yvel = -0x40000
			object[+1].state++
		end if

		if object.timer == 180
			object.timer = 0
			object.animation = ARZEGGANI_TOASTED
			object.exploding = false
			object.state++

			ResetObjectEntity(SLOT_MUSICEVENT_BOSS, TypeName[Music Event], MUSICEVENT_FADETOSTAGE, 0, 0)
			object[SLOT_MUSICEVENT_BOSS].priority = PRIORITY_ACTIVE

			temp0 = tileLayer[0].xsize
			temp0 <<= 7
			stage.newXBoundary2 = temp0
			eggmanHammer[+1].hideFlames = true
		end if
		break

	case ARZEGGMAN_DEFEATFALL
		object.ypos += object.yvel
		object.yvel += 0x1800

		object.timer++
		if object.timer == 38
			object.yvel = 0
			object.timer = 0
			object.animationTimer = 0
			eggmanHammer[+1].hideFlames = false
			object.state++
		end if

		object[+1].xpos = object.xpos
		object[+1].ypos = object.ypos
		object[+1].direction = object.direction
		break

	case ARZEGGMAN_DEFEATRISE
		if object.timer < 48
			object.ypos += object.yvel
			object.yvel -= 0x800
			object.timer++
		else
			object.timer = 0
			object.yvel = 0
			object.originPos.y = object.ypos
			object.state++
		end if

		object[+1].xpos = object.xpos
		object[+1].ypos = object.ypos
		object[+1].direction = object.direction
		break

	case ARZEGGMAN_FLEE
		object.ypos = object.originPos.y
		CallFunction(ARZEggman_Oscillate)

		if object.timer < 8
			object.timer++
		else
			object.timer = 0
			object.animation = ARZEGGANI_PANIC
			object.animationTimer = 0
			object.unused1 = 2
			object.unused2 = 0
			object.direction = FLIP_X
			object.priority = PRIORITY_ACTIVE
			temp0 = tileLayer[0].xsize
			temp0 <<= 7
			stage.newXBoundary2 = temp0
			object.state++
		end if
		break

	case ARZEGGMAN_ESCAPE
		object.xpos += 0x40000
		object.originPos.y -= 0x4000

		CallFunction(ARZEggman_Oscillate)

		if object.outOfBounds == true
			object.type = TypeName[Blank Object]
			object[+1].type = TypeName[Blank Object]
		end if
		break
		
	end switch

	if object.health != 0
		if object.invincibilityTimer > 0
			object.invincibilityTimer--
			GetBit(temp0, object.invincibilityTimer, 0)
			if temp0 == true
				SetPaletteEntry(0, 192, 0xE0E0E0)
			else
				SetPaletteEntry(0, 192, 0x000000)
			end if
		end if

		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object.invincibilityTimer == 0
				CheckEqual(player[currentPlayer].animation, ANI_HURT)
				temp0 = checkResult
				CheckEqual(player[currentPlayer].animation, ANI_DYING)
				temp0 |= checkResult
				CheckEqual(player[currentPlayer].animation, ANI_DROWNING)
				temp0 |= checkResult
				if temp0 != 0
					if object.animation != ARZEGGANI_LAUGH
						object.animation = ARZEGGANI_LAUGH
						object.animationTimer = 0
					end if
				end if

				BoxCollisionTest(C_TOUCH, object.entityPos, -24, -24, 24, 24, currentPlayer, player[currentPlayer].hitboxLeft, player[currentPlayer].hitboxTop, player[currentPlayer].hitboxRight, player[currentPlayer].hitboxBottom)
				if checkResult == true
					CallFunction(Player_CheckHit)
					if checkResult == true
						object.health--
						if object.health == 0
#platform: USE_ORIGINS
							CallNativeFunction2(NotifyCallback, NOTIFY_KILL_BOSS, 0)
							CallNativeFunction2(NotifyCallback, NOTIFY_BOSS_END, 1)
#endplatform

							player.score += 1000
							object.animation = ARZEGGANI_DEFEATED
							object.animationTimer = 0
							object.unused1 = 0
							object.exploding = true
							object.originPos.y = object.ypos
							object.oscillationAngle = eggmanHammer[+1].oscillationAngle
							object.state++
							object[+1].state = EGGMANHAMMER_DESTROYED

							object[+2].state = EGGMANARROW_FALLING
							object[+3].state = EGGMANARROW_FALLING
							object[+4].state = EGGMANARROW_FALLING

							eggmanTotemPole[+5].timer = 144
							object[+5].state = EGGMANTOTEMPOLE_LOWERING

							eggmanTotemPole[+6].timer = 144
							object[+6].state = EGGMANTOTEMPOLE_LOWERING
						else
							object.animation = ARZEGGANI_HIT
							object.animationTimer = 0
							object.invincibilityTimer = 64
							PlaySfx(SfxName[Boss Hit], false)
						end if
					end if
				end if
			end if
		next
	end if

	if object.state != ARZEGGMAN_ESCAPE
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			temp0 = player[currentPlayer].collisionRight
			temp0 <<= 16
			temp0 += player[currentPlayer].xpos
			temp1 = stage.curXBoundary2
			temp1 <<= 16
			if temp0 > temp1
				player[currentPlayer].xvel = 0
				player[currentPlayer].speed = 0
				player[currentPlayer].xpos = temp1
				temp0 = player[currentPlayer].collisionRight
				temp0 <<= 16
				player[currentPlayer].xpos -= temp0
			end if
		next
	end if

	if object.exploding == true
		temp0 = oscillation
		temp0 &= 7
		if temp0 == 0
			Rand(temp0, 48)
			temp0 -= 24
			temp0 <<= 16
			temp0 += object.xpos
			Rand(temp1, 48)
			temp1 -= 24
			temp1 <<= 16
			temp1 += object.ypos
			CreateTempObject(TypeName[Explosion], 0, temp0, temp1)
			object[tempObjectPos].drawOrder = 5
			PlaySfx(SfxName[Explosion], false)
		end if
	end if
end event


event ObjectDraw
	switch object.animation
	default
	case ARZEGGANI_INVISIBLE
		break

	case ARZEGGANI_IDLE
		temp0 = object.animationTimer
		temp0 >>= 3
		object.animationTimer++
		object.animationTimer &= 15
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break

	case ARZEGGANI_LAUGH
		temp0 = object.animationTimer
		temp0 &= 15
		temp0 >>= 3
		temp0 += 2
		object.animationTimer++
		if object.animationTimer == 50
			object.animationTimer = 0
			if player[0].animation != ANI_DYING
				object.animation = ARZEGGANI_IDLE
			end if
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break

	case ARZEGGANI_HIT
		temp0 = object.animationTimer
		temp0 &= 1
		temp0 += 2
		object.animationTimer++
		if object.animationTimer == 50
			object.animationTimer = 0
			object.animation = ARZEGGANI_IDLE
		end if
		DrawSpriteFX(4, FX_FLIP, object.xpos, object.ypos)
		break

	case ARZEGGANI_DEFEATED
		temp0 = object.animationTimer
		temp0 >>= 5
		temp0 += 4
		temp0 %= 5
		object.animationTimer++
		object.animationTimer &= 63
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break

	case ARZEGGANI_TOASTED
		DrawSpriteFX(5, FX_FLIP, object.xpos, object.ypos)
		break

	case ARZEGGANI_PANIC
		temp0 = object.animationTimer
		temp0 >>= 3
		object.animationTimer++
		object.animationTimer &= 15
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)

		temp0 = object.animationTimer
		temp0 >>= 2
		temp0 &= 1
		if temp0 == 1
			DrawSpriteFX(15, FX_FLIP, object.xpos, object.ypos)
		end if
		break
		
	end switch
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone03")
	if checkResult == true
		LoadSpriteSheet("Global/EggMobile.gif")
		
		// Eggman Frames
		SpriteFrame(-28, -28, 60, 20, 5, 1)
		SpriteFrame(-28, -28, 60, 20, 66, 1)
		SpriteFrame(-28, -28, 60, 20, 5, 22)
		SpriteFrame(-28, -28, 60, 20, 66, 22)
		SpriteFrame(-28, -28, 60, 20, 5, 43)
		SpriteFrame(-28, -28, 60, 20, 66, 43)
		
		// Eggmobile Body Frame
		SpriteFrame(-32, -8, 64, 29, 1, 64)
		
		// Eggmobile Jet Frames
		SpriteFrame(31, 4, 12, 8, 66, 65)
		SpriteFrame(30, 2, 14, 12, 66, 74)
		SpriteFrame(31, -2, 22, 14, 66, 87)
		SpriteFrame(31, -9, 32, 26, 94, 64)
		SpriteFrame(31, -2, 22, 14, 66, 87)
		SpriteFrame(31, -9, 32, 26, 94, 64)
		SpriteFrame(31, 4, 12, 8, 66, 65)
		SpriteFrame(30, 2, 14, 12, 66, 74)
		
		// Sweat Frames
		SpriteFrame(-3, -28, 11, 8, 79, 65)
		SpriteFrame(0, 0, 0, 0, 7, 2)

		if options.vsMode == true
			foreach (TypeName[Eggman], arrayPos0, ALL_ENTITIES)
				object[arrayPos0].type = TypeName[Blank Object]
			next
		end if
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		
		// Eggman Frames
		SpriteFrame(-28, -28, 60, 20, 1, 1)
		SpriteFrame(-28, -28, 60, 20, 62, 1)
		SpriteFrame(-28, -28, 60, 20, 1, 22)
		SpriteFrame(-28, -28, 60, 20, 62, 22)
		SpriteFrame(-28, -28, 60, 20, 1, 43)
		SpriteFrame(-28, -28, 60, 20, 62, 43)
		
		// Eggmobile Body Frame
		SpriteFrame(-32, -8, 64, 29, 415, 170)
		
		// Eggmobile Jet Frames
		SpriteFrame(31, 4, 12, 8, 57, 90)
		SpriteFrame(30, 2, 14, 12, 57, 99)
		SpriteFrame(31, -2, 22, 14, 34, 97)
		SpriteFrame(31, -9, 32, 26, 72, 86)
		SpriteFrame(31, -2, 22, 14, 34, 97)
		SpriteFrame(31, -9, 32, 26, 72, 86)
		SpriteFrame(31, 4, 12, 8, 57, 90)
		SpriteFrame(30, 2, 14, 12, 57, 99)
		
		// Sweat Frames
		SpriteFrame(-3, -28, 11, 8, 45, 88)
		SpriteFrame(0, 0, 0, 0, 7, 2)
	end if
end event


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

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


event RSDKLoad
	CheckCurrentStageFolder("Zone03")
	if checkResult == true
		LoadSpriteSheet("Global/EggMobile.gif")
		SpriteFrame(-28, -28, 60, 20, 5, 1)
		SpriteFrame(-32, -8, 64, 29, 1, 64)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-28, -28, 60, 20, 1, 1)
		SpriteFrame(-32, -8, 64, 29, 415, 170)
	end if
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
