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

// Using CNZEggman 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.timer
private alias object.value1  : object.originPos.y
private alias object.value2  : object.oscillateAngle
private alias object.value3  : object.health
private alias object.value4  : object.invincibilityTimer
private alias object.value5  : object.exploding
private alias object.value6  : object.boundsL
private alias object.value7  : object.boundsR
private alias object.value8  : object.boundsT
private alias object.value9  : object.boundsB
private alias object.value10 : object.isClawWide
private alias object.value11 : object.bombDropDisabled
private alias object.value12 : object.sparkSfxTimer
private alias object.value13 : object.paletteTimer
private alias object.value14 : object.isCNZ

// Eggman States
private alias 0  : CNZEGGMAN_AWAITPLAYER
private alias 1  : CNZEGGMAN_ENTERDELAY
private alias 2  : CNZEGGMAN_MOVEH
private alias 3  : CNZEGGMAN_MOVEV_DOWN
private alias 4  : CNZEGGMAN_MOVEV_UP
private alias 5  : CNZEGGMAN_DROPBOMB
private alias 6  : CNZEGGMAN_DROPPEDBOMB
private alias 7  : CNZEGGMAN_EXPLODE
private alias 8  : CNZEGGMAN_DEFEATFALL
private alias 9  : CNZEGGMAN_DEFEATRISE
private alias 10 : CNZEGGMAN_FLEE
private alias 11 : CNZEGGMAN_ESCAPE

// Eggman Animations
private alias 0 : CNZEGGANI_INVISIBLE
private alias 1 : CNZEGGANI_IDLE
private alias 2 : CNZEGGANI_LAUGH
private alias 3 : CNZEGGANI_HIT
private alias 4 : CNZEGGANI_DEFEATED
private alias 5 : CNZEGGANI_TOASTED
private alias 6 : CNZEGGANI_PANIC

// EggmanClaw Aliases
private alias object.value1 : eggmanClaw.parent
private alias object.value2 : eggmanClaw.electricityActive

// EggmanClaw States
private alias 1 : CNZEGGMANCLAW_DEBRIS

// EggmanBomb Aliases
private alias object.value1 : eggmanBomb.parent
private alias object.value2 : eggmanBomb.offset.y

// EggmanBomb States
private alias 1 : CNZEGGMANBOMB_DROPPED

// BossWall States
private alias 1 : CNZBOSSWALL_APPEAR
private alias 3 : CNZBOSSWALL_DELWALL

// Player Aliases
private alias object.xpos : player.xpos
private alias object.ypos : player.ypos
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 CNZEggman_Oscillate


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

private function CNZEggman_Oscillate
	Sin256(object.ypos, object.oscillateAngle)
	object.ypos <<= 10
	object.ypos += object.originPos.y

	object.oscillateAngle += 2
	object.oscillateAngle &= 0xFF
end function


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

event ObjectUpdate
	if object.isCNZ == true
		object.paletteTimer++
		if object.paletteTimer == 4
			object.paletteTimer = 0
			RotatePalette(0, 194, 196, false)
			RotatePalette(0, 197, 202, false)
		end if
	else
		// In MBZ/Egg Gauntlet, where the palette indices used are different
		object.paletteTimer++
		if object.paletteTimer == 4
			object.paletteTimer = 0
			RotatePalette(0, 112, 114, false)
			RotatePalette(0, 115, 120, false)
		end if
	end if

	switch object.state
	case CNZEGGMAN_AWAITPLAYER
		if player[0].xpos > object[-1].xpos
			temp0 = -32
			temp0 *= screen.xsize
			temp0 /= 320
			temp0 -= 192
			temp0 += object[-1].ixpos
			stage.newXBoundary1 = temp0

			temp0 = 32
			temp0 *= screen.xsize
			temp0 /= 320
			temp0 += 192
			temp0 += object[-1].ixpos
			stage.newXBoundary2 = temp0

			object.boundsL = object[-1].xpos
			object.boundsL -= 0x800000

			object.boundsR = object[-1].xpos
			object.boundsR += 0x800000

			object.boundsT = object.ypos
			object.boundsB = object.ypos
			object.boundsB += 0x2C0000

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

			object[-2].type 			= TypeName[Eggman Claw]
			object[-2].propertyValue 	= 0 // left claw
			eggmanClaw[-2].parent 		= object.entityPos

			object[+1].type 			= TypeName[Eggman Claw]
			object[+1].propertyValue 	= 1 // right claw
			eggmanClaw[+1].parent 		= object.entityPos

			
			object[+2].priority = PRIORITY_ACTIVE
			object[+2].state 	= CNZBOSSWALL_APPEAR

			object.xvel 		= -0x18000
			object.originPos.y 	= object.ypos

			CheckCurrentStageFolder("Zone04")
			if checkResult == true
				object.isCNZ = true
			else
				// We're gonna assume that we're in MBZ, then
				object.isCNZ = false
			end if
			
#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 	= CNZEGGMAN_ENTERDELAY
		end if
		break

	case CNZEGGMAN_ENTERDELAY
		object.timer++
		if object.timer == 60
			object.timer = 0
			object.state = CNZEGGMAN_MOVEH
		end if
		break

	case CNZEGGMAN_MOVEH
		CallFunction(CNZEggman_Oscillate)
		object.xpos += object.xvel

		if object.xvel > 0
			if object.xpos > object.boundsR
				object.xpos = object.boundsR
				object.direction ^= FLIP_X
				FlipSign(object.xvel)
				object.bombDropDisabled = false
			end if
		else
			if object.xpos < object.boundsL
				object.xpos = object.boundsL
				object.direction ^= FLIP_X
				FlipSign(object.xvel)
				object.bombDropDisabled = false
			end if
		end if

		object.timer++
		if object.timer == 60
			object.timer = 0
			eggmanClaw[-2].electricityActive = true
			object.isClawWide ^= true
		end if

		object.sparkSfxTimer++
		if object.sparkSfxTimer == 32
			object.sparkSfxTimer = 0
			PlaySfx(SfxName[BossSpark], false)
		end if

		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object.state == CNZEGGMAN_MOVEH
				if player[currentPlayer].ypos > object.ypos
					temp0 = player[currentPlayer].xpos
					temp0 -= object.xpos
					Abs(temp0)
					if temp0 < 0x100000
						temp0 = player[currentPlayer].ypos
						temp0 -= object.ypos
						Abs(temp0)
						if temp0 < 0x600000
							object.state = CNZEGGMAN_MOVEV_DOWN
						else
							if object.bombDropDisabled == false
								object.timer 		= 0
								object.isClawWide 	= false
								object.state 		= CNZEGGMAN_DROPBOMB
							end if
						end if
					end if
				end if
			end if
		next
		break

	case CNZEGGMAN_MOVEV_DOWN
		object.isClawWide = true
		object.ypos += 0x18000

		if object.ypos > object.boundsB
			object.state = CNZEGGMAN_MOVEV_UP
		end if

		object.sparkSfxTimer++
		if object.sparkSfxTimer == 32
			object.sparkSfxTimer = 0
			PlaySfx(SfxName[BossSpark], false)
		end if
		break

	case CNZEGGMAN_MOVEV_UP
		object.isClawWide = false
		object.ypos -= 0x18000

		if object.ypos < object.boundsT
			object.ypos 		= object.boundsT
			object.originPos.y 	= object.boundsT
			object.state 		= CNZEGGMAN_MOVEH
		end if

		object.sparkSfxTimer++
		if object.sparkSfxTimer == 32
			object.sparkSfxTimer = 0
			PlaySfx(SfxName[BossSpark], false)
		end if
		break

	case CNZEGGMAN_DROPBOMB
		CallFunction(CNZEggman_Oscillate)

		object.timer++
		if object.timer == 1
			eggmanClaw[-2].electricityActive = false
			arrayPos0 = object.entityPos
			arrayPos0--
			ResetObjectEntity(arrayPos0, TypeName[Eggman Bomb], 0, object.xpos, object.ypos)
			eggmanBomb[arrayPos0].parent = object.entityPos
		else
			object[-1].ypos = object.ypos
			object[-1].ypos += eggmanBomb[-1].offset.y
		end if

		if object.timer == 80
			object.timer 		= 0
			object.isClawWide 	= true
			object[-1].state 	= CNZEGGMANBOMB_DROPPED
			object.state 		= CNZEGGMAN_DROPPEDBOMB
		end if
		break

	case CNZEGGMAN_DROPPEDBOMB
		CallFunction(CNZEggman_Oscillate)

		object.timer++
		if object.timer == 20
			object.timer = 0
			object.isClawWide = false
			object.bombDropDisabled = true
			eggmanClaw[-2].electricityActive = true
			object.state = CNZEGGMAN_MOVEH
		end if
		break

	case CNZEGGMAN_EXPLODE
		object.timer++
		if object.timer == 60
			if object.direction == FLIP_NONE
				object[-2].xvel = -0x10000
			else
				object[-2].xvel = 0x10000
			end if
			object[-2].yvel  = 0
			object[-2].state = CNZEGGMANCLAW_DEBRIS
		end if

		if object.timer == 120
			if object.direction == FLIP_NONE
				object[+1].xvel = 0x10000
			else
				object[+1].xvel = -0x10000
			end if
			object[+1].yvel  = 0
			object[+1].state = CNZEGGMANCLAW_DEBRIS
		end if

		if object.timer == 180
			object.timer = 0
			object.animation = CNZEGGANI_TOASTED
			object.exploding = false
			object.state++
			ResetObjectEntity(SLOT_MUSICEVENT_BOSS, TypeName[Music Event], MUSICEVENT_FADETOSTAGE, 0, 0)
			object[SLOT_MUSICEVENT_BOSS].priority = PRIORITY_ACTIVE
			object[+3].priority = PRIORITY_ACTIVE
			object[+3].state = CNZBOSSWALL_DELWALL
			temp0 = tileLayer[0].xsize
			temp0 <<= 7
			stage.newXBoundary2 = temp0
		end if
		break

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

		object.timer++
		if object.timer == 16
			object.yvel = 0
			object.timer = 0
			object.animationTimer = 0
			object.state++
		end if
		break

	case CNZEGGMAN_DEFEATRISE
		if object.timer < 24
			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
		break

	case CNZEGGMAN_FLEE
		CallFunction(CNZEggman_Oscillate)
		if object.timer < 8
			object.timer++
		else
			object.timer = 0
			object.animation = CNZEGGANI_PANIC
			object.animationTimer = 0
			object.direction = FLIP_X
			object.priority = PRIORITY_ACTIVE
			temp0 = tileLayer[0].xsize
			temp0 <<= 7
			stage.newXBoundary2 = temp0
			object.state++
		end if
		break

	case CNZEGGMAN_ESCAPE
		object.xpos += 0x40000
		object.originPos.y -= 0x4000
		CallFunction(CNZEggman_Oscillate)

		if object.outOfBounds == true
			ResetObjectEntity(object.entityPos, TypeName[Blank Object], 0, 0, 0)
		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 != CNZEGGANI_LAUGH
						object.animation = CNZEGGANI_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
							eggmanClaw[-2].electricityActive	= false
							object.timer 						= 0
							object.animation 					= CNZEGGANI_DEFEATED
							object.animationTimer 				= 0
							object.exploding 					= true
							object.originPos.y 					= object.ypos
							object.state 						= CNZEGGMAN_EXPLODE
							if object[-1].type == TypeName[Eggman Bomb]
								temp0 = object.entityPos
								temp0--
								ResetObjectEntity(temp0, TypeName[Explosion], 0, object[-1].xpos, object[-1].ypos)
							end if
						else
							object.animation 			= CNZEGGANI_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 != CNZEGGMAN_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
	DrawSpriteFX(6, FX_FLIP, object.xpos, object.ypos)

	switch object.animation
	default
	case CNZEGGANI_INVISIBLE
		break

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

	case CNZEGGANI_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 = CNZEGGANI_IDLE
			end if
		end if

		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break

	case CNZEGGANI_HIT
		temp0 = object.animationTimer
		temp0 &= 1
		temp0 += 2

		object.animationTimer++
		if object.animationTimer == 50
			object.animationTimer = 0
			object.animation = CNZEGGANI_IDLE
		end if

		DrawSpriteFX(4, FX_FLIP, object.xpos, object.ypos)
		break

	case CNZEGGANI_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 CNZEGGANI_TOASTED
		DrawSpriteFX(5, FX_FLIP, object.xpos, object.ypos)
		break

	case CNZEGGANI_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 == true
			DrawSpriteFX(7, FX_FLIP, object.xpos, object.ypos)
		end if
		break
		
	end switch
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone04")
	if checkResult == true
		LoadSpriteSheet("CNZ/Objects.gif")
		SpriteFrame(-28, -16, 44, 16, 76, 140)	// Idle 1 		- #0
		SpriteFrame(-28, -16, 44, 16, 121, 140)	// Idle 2 		- #1
		SpriteFrame(-28, -16, 44, 16, 166, 140)	// Laugh 1 		- #2
		SpriteFrame(-28, -16, 44, 16, 211, 140)	// Laugh 2 		- #3
		SpriteFrame(-28, -16, 44, 16, 166, 157)	// Hit 			- #4
		SpriteFrame(-28, -16, 44, 16, 211, 157)	// Destroyed 	- #5
		SpriteFrame(-40, -40, 80, 72, 175, 183)	// Eggmobile 	- #6
		SpriteFrame(-3, -18, 11, 8, 154, 157)	// Panic Sweat 	- #7

		if options.vsMode == true
			foreach (TypeName[Eggman], arrayPos0, ALL_ENTITIES)
				object[arrayPos0].type = TypeName[Blank Object]
			next
		end if
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-28, -16, 44, 16, 1, 5)		// Idle 1 		- #0
		SpriteFrame(-28, -16, 44, 16, 62, 5)	// Idle 2 		- #1
		SpriteFrame(-28, -16, 44, 16, 1, 26)	// Laugh 1 		- #2
		SpriteFrame(-28, -16, 44, 16, 62, 26)	// Laugh 2 		- #3
		SpriteFrame(-28, -16, 44, 16, 1, 47)	// Hit 			- #4
		SpriteFrame(-28, -16, 44, 16, 62, 47)	// Destroyed 	- #5
		SpriteFrame(-40, -40, 80, 72, 232, 112)	// Eggmobile 	- #6
		SpriteFrame(-3, -18, 11, 8, 45, 88)		// Panic Sweat 	- #7
	end if
end event


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

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


event RSDKLoad
	CheckCurrentStageFolder("Zone04")
	if checkResult == true
		LoadSpriteSheet("CNZ/Objects.gif")
		SpriteFrame(-28, -16, 44, 16, 76, 140)  // Idle 1
		SpriteFrame(-40, -40, 80, 72, 175, 183) // Eggmobile
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-28, -16, 44, 16, 1, 5)     // Idle 1
		SpriteFrame(-40, -40, 80, 72, 232, 112) // Eggmobile
	end if
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
