// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: B08 Eggman 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.originPos.y
private alias object.value2  : object.oscillationAngle
private alias object.value3  : object.health
private alias object.value4  : object.invincibilityTimer
private alias object.value5  : object.exploding
private alias object.value6  : object.boundsM
private alias object.value7  : object.boundsL // unused
private alias object.value8  : object.boundsR // unused
private alias object.value9  : object.boundsT
private alias object.value10 : object.boundsB
private alias object.value11 : object.groundPos
// value12 is unused
private alias object.value13 : object.flameAnimation
private alias object.value14 : object.flameAnimTimer
// value15 is unused
private alias object.value16 : object.shakeTimer
private alias object.value17 : object.trumboneExtendPos
private alias object.value18 : object.trumboneExtendMax
private alias object.value19 : object.trumboneOffset.y
private alias object.value20 : object.showTrumbone
private alias object.value21 : object.trumboneExtendSpeed
private alias object.value22 : object.trumboneVelocity.y
private alias object.value23 : object.mineSlot
private alias object.value24 : object.waterLevel
private alias object.value25 : object.impactCount

// Eggman States
private alias 0  : HPZEGGMAN_AWAITPLAYER
private alias 1  : HPZEGGMAN_ENTERDELAY
private alias 2  : HPZEGGMAN_ENTEREGGMAN
private alias 3  : HPZEGGMAN_EXTENDTRUMBONE
private alias 4  : HPZEGGMAN_DROPSMALLBOMBS
private alias 5  : HPZEGGMAN_DROPPEDSMALLBOMBS
private alias 6  : HPZEGGMAN_RAISEBOMBS
private alias 7  : HPZEGGMAN_PLAYLOUDHORN
private alias 8  : HPZEGGMAN_DROPLARGEBOMB
private alias 9  : HPZEGGMAN_AWAITBOMBSEXPLODE
private alias 10 : HPZEGGMAN_GEYSERHIT
private alias 11 : HPZEGGMAN_GEYSERFALL
private alias 12 : HPZEGGMAN_GEYSERRISE
private alias 13 : HPZEGGMAN_EXPLODE
private alias 14 : HPZEGGMAN_DEFEATFALL
private alias 15 : HPZEGGMAN_DEFEATRISE
private alias 16 : HPZEGGMAN_FLEE
private alias 17 : HPZEGGMAN_ESCAPE

// Eggman Animations
private alias 0 : HPZEGGANI_INVISIBLE
private alias 1 : HPZEGGANI_IDLE
private alias 2 : HPZEGGANI_LAUGH
private alias 3 : HPZEGGANI_HIT
private alias 4 : HPZEGGANI_DEFEATED
private alias 5 : HPZEGGANI_TOASTED
private alias 6 : HPZEGGANI_PANIC

// Flame Animations
private alias 0 : FLAMEANI_INVISIBLE
private alias 1 : FLAMEANI_FALLING
private alias 2 : FLAMEANI_RISE
private alias 3 : FLAMEANI_MOVING

// EggmanHorn Aliases
private alias object.value0 : hpzEggmanHorn.timer

// EggmanHorn States
private alias 0 : HPZEGGMANHORN_IDLE
private alias 1 : HPZEGGMANHORN_PLAYINGNOISE
private alias 2 : HPZEGGMANHORN_FINISHEDNOISE
private alias 3 : HPZEGGMANHORN_DEBRIS

// EggmanValve States
private alias 0 : HPZEGGMANVALVE_RESETBOTH
private alias 1 : HPZEGGMANVALVE_USEL
private alias 2 : HPZEGGMANVALVE_USER
private alias 3 : HPZEGGMANVALVE_USEBOTH
private alias 4 : HPZEGGMANVALVE_DEBRIS

// EggmanMine States
private alias 0 : HPZEGGMANMINE_SETUP
private alias 1 : HPZEGGMANMINE_SMALL_FALL
private alias 2 : HPZEGGMANMINE_SMALL_RISE
private alias 3 : HPZEGGMANMINE_SMALL_FLOAT
private alias 4 : HPZEGGMANMINE_SMALL_EXPLODE
private alias 5 : HPZEGGMANMINE_BIG_FALL
private alias 6 : HPZEGGMANMINE_BIG_FOLLOW
private alias 7 : HPZEGGMANMINE_BIG_EXPLODE
private alias 8 : HPZEGGMANMINE_BIG_EXPLODED

// BreakGem Aliases
private alias object.value1 : breakGem.gravityStrength

// BreakGem States
private alias 1 : BREAKGEM_SHARD

// Music Events
private alias 26 : SLOT_MUSICEVENT_BOSS

private alias  0 : MUSICEVENT_FADETOBOSS
private alias  1 : MUSICEVENT_FADETOSTAGE

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


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

reserve function B08Eggman_Oscillate
reserve function B08Eggman_HandleChildren
reserve function B08Eggman_HandleMovement

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

private table B08Eggman_flameFrames_Falling
	7, 7, 8, 8
end table

private table B08Eggman_flameFrames_Rise
	9, 9, 10, 10
end table

private table B08Eggman_flameFrames_Moving
	11, 11, 12, 12
end table

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

private function B08Eggman_Oscillate
	Sin256(object.ypos, object.oscillationAngle)
	object.ypos <<= 10
	object.ypos += object.originPos.y
	object.oscillationAngle += 2
	object.oscillationAngle &= 0xFF
end function


private function B08Eggman_HandleChildren
	// Horn
	object[+1].xpos = object.xpos
	object[+1].ypos = object.ypos
	object[+1].direction = object.direction

	// Valve
	object[+2].xpos = object.xpos
	object[+2].ypos = object.ypos
	object[+2].direction = object.direction
end function


private function B08Eggman_HandleMovement
	checkResult = false
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		if player[currentPlayer].yvel < 0
			checkResult |= true
		end if
	next

	if checkResult == true
		object.originPos.y -= 0xC000
		if object.originPos.y < object.boundsB
			object.originPos.y = object.boundsB
		end if
		object.flameAnimation = FLAMEANI_MOVING
	else
		object.originPos.y += 0x8000
		if object.originPos.y > object.boundsT
			object.originPos.y = object.boundsT
		end if
		object.flameAnimation = FLAMEANI_FALLING
	end if

	temp0 = player[0].xpos
	temp0 -= 0x80000
	temp1 = player[0].xpos
	temp1 += 0x80000
	if object.xpos < temp0
		object.xpos += 0x10000
		object.direction = FLIP_X
	end if

	if object.xpos > temp1
		object.xpos -= 0x10000
		object.direction = FLIP_NONE
	end if
end function


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

event ObjectUpdate
	switch object.state
	case HPZEGGMAN_AWAITPLAYER
		if player[0].xpos > object[+1].xpos
			temp0 = object[+1].ixpos
			temp0 -= screen.xcenter
			stage.newXBoundary1 = temp0

			temp0 = object[+1].ixpos
			temp0 += screen.xcenter
			stage.newXBoundary2 = temp0

			temp0 = stage.newYBoundary2
			temp0 -= screen.ysize
			stage.newYBoundary1 = temp0

			object.boundsM = object[+1].xpos
			object.boundsL = -112
			object.boundsL *= screen.xsize
			object.boundsL /= 320
			object.boundsL <<= 16
			object.boundsR = object.boundsL
			FlipSign(object.boundsR)

			object.boundsL += object[-1].xpos
			object.boundsR += object[-1].xpos
			object.boundsT = object[+1].ypos
			object.boundsB = object.boundsT
			object.boundsB -= 0x200000
			object.groundPos = object[+2].ypos

			object.xpos = 72
			object.xpos *= screen.xsize
			object.xpos /= 320
			object.xpos <<= 16
			object.xpos += object[+1].xpos
			object.ypos = object.boundsT
			object.ypos += 0xF00000

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

			object[+1].type = TypeName[Eggman Horn]
			object[+2].type = TypeName[Eggman Valve]
			CallFunction(B08Eggman_HandleChildren)
			object.trumboneExtendPos = -0x280000
			object.showTrumbone = true
			object.flameAnimation = FLAMEANI_RISE
			HPZSetup_bossFightActive = true
			object.waterLevel = object[-1].iypos
			stage.waterLevel = object.waterLevel
			stage.waterLevel += 192
			object.priority = PRIORITY_ACTIVE
			object.state = HPZEGGMAN_ENTERDELAY
		end if
		break

	case HPZEGGMAN_ENTERDELAY
		object.timer++
		if object.timer == 60
			object.timer = 0
			object.yvel = 0
			object.state = HPZEGGMAN_ENTEREGGMAN
		end if
		break

	case HPZEGGMAN_ENTEREGGMAN
		object.yvel -= 0x1800
		if object.yvel < -0x20000
			object.yvel = -0x20000
		end if

		object.ypos += object.yvel
		if stage.waterLevel > object.waterLevel
			stage.waterLevel -= 2
		end if

		if object.ypos <= object.boundsT
			object.ypos = object.boundsT
			object.originPos.y = object.ypos
			
#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.state = HPZEGGMAN_EXTENDTRUMBONE
		end if

		CallFunction(B08Eggman_HandleChildren)
		break

	case HPZEGGMAN_EXTENDTRUMBONE
		object.timer++
		if object.trumboneExtendPos < -0x280000
			object.trumboneExtendPos += 0x20000
		end if

		if object.timer == 240
			object.timer = 0
			object.mineSlot = object.entityPos
			object.mineSlot -= 4
			object.trumboneExtendMax = -0x280000
			object.state = 4
		end if

		CallFunction(B08Eggman_HandleMovement)
		CallFunction(B08Eggman_Oscillate)
		CallFunction(B08Eggman_HandleChildren)
		break

	case HPZEGGMAN_DROPSMALLBOMBS
		temp0 = object.timer
		temp0 %= 60
		if temp0 == 0
			object[+1].state = HPZEGGMANHORN_PLAYINGNOISE
			hpzEggmanHorn[+1].timer = 30
			object[+2].state++
			if object[+2].state == HPZEGGMANVALVE_USEBOTH
				object[+2].state = HPZEGGMANVALVE_USEL
			end if

			temp1 = object.timer
			temp1 /= 60
			switch temp1
			case 0
				PlaySfx(SfxName[Horn 1], false)
				break

			case 1
				PlaySfx(SfxName[Horn 2], false)
				break

			case 2
				PlaySfx(SfxName[Horn 3], false)
				break

			case 3
				PlaySfx(SfxName[Horn 4], false)
				break
			end switch
		end if

		if hpzEggmanHorn[+1].timer == 1
			temp1 = screen.xsize
			temp1 *= 224
			temp1 >>= 8
			Rand(temp0, temp1)
			temp0 >>= 6
			temp0 <<= 6
			temp1 >>= 1
			temp0 -= temp1
			temp0 <<= 16
			temp0 += object.boundsM
			foreach (TypeName[Eggman Mine], arrayPos0, ACTIVE_ENTITIES)
				temp1 = object[arrayPos0].xpos
				temp1 -= temp0
				Abs(temp1)
				if temp1 <= 0x180000
					if temp0 < object.boundsM
						temp0 += 0x200000
					else
						temp0 -= 0x200000
					end if
				end if
			next

			arrayPos0 = object.mineSlot
			ResetObjectEntity(object.mineSlot, TypeName[Eggman Mine], 0, temp0, object.boundsB)
			object[arrayPos0].ypos -= 0x400000
			object.mineSlot++
			object.trumboneExtendMax -= 0xA0000
		end if

		if hpzEggmanHorn[+1].timer == 0
			if object.trumboneExtendPos > object.trumboneExtendMax
				object.trumboneExtendPos -= 0x10000
			end if
		end if

		object.timer++
		if object.timer == 240
			object.timer = 0
			object.state = HPZEGGMAN_DROPPEDSMALLBOMBS
			object[+2].state = HPZEGGMANVALVE_RESETBOTH
		end if

		CallFunction(B08Eggman_HandleMovement)
		CallFunction(B08Eggman_Oscillate)
		CallFunction(B08Eggman_HandleChildren)
		break

	case HPZEGGMAN_DROPPEDSMALLBOMBS
		object.timer++
		if object.timer == 160
			object.timer = 0
			object.angle = 256
			object[+1].state = HPZEGGMANHORN_FINISHEDNOISE
			hpzEggmanHorn[+1].timer = 128
			object.state = HPZEGGMAN_RAISEBOMBS
			PlaySfx(SfxName[Horn Sweep], false)
		end if

		CallFunction(B08Eggman_HandleMovement)
		CallFunction(B08Eggman_Oscillate)
		CallFunction(B08Eggman_HandleChildren)
		break

	case HPZEGGMAN_RAISEBOMBS
		object.angle += 4
		Cos(stage.waterLevel, object.angle)
		stage.waterLevel >>= 4
		FlipSign(stage.waterLevel)
		stage.waterLevel -= 32
		temp0 = stage.waterLevel
		temp0 *= 3
		temp0 /= 6
		object.trumboneExtendPos = -80
		object.trumboneExtendPos -= temp0
		object.trumboneExtendPos <<= 16
		stage.waterLevel += object.waterLevel
		if object.angle == 0x300
			object[-1].state = HPZEGGMANMINE_SMALL_EXPLODE
			object[-2].state = HPZEGGMANMINE_SMALL_EXPLODE
			object[-3].state = HPZEGGMANMINE_SMALL_EXPLODE
			object[-4].state = HPZEGGMANMINE_SMALL_EXPLODE
			object[+2].state = HPZEGGMANVALVE_RESETBOTH
			object.state = HPZEGGMAN_PLAYLOUDHORN
		end if

		CallFunction(B08Eggman_HandleMovement)
		CallFunction(B08Eggman_Oscillate)
		CallFunction(B08Eggman_HandleChildren)
		break

	case HPZEGGMAN_PLAYLOUDHORN
		object.timer++
		if object.trumboneExtendPos < -0x280000
			object.trumboneExtendPos += 0x20000
		end if

		if object.timer == 240
			object.timer = 0
			object[+1].state = HPZEGGMANHORN_PLAYINGNOISE
			hpzEggmanHorn[+1].timer = 240
			object[+2].state = HPZEGGMANVALVE_USEBOTH
			object.state = HPZEGGMAN_DROPLARGEBOMB
			PlaySfx(SfxName[Loud Horn], false)
		end if

		CallFunction(B08Eggman_HandleMovement)
		CallFunction(B08Eggman_Oscillate)
		CallFunction(B08Eggman_HandleChildren)
		break

	case HPZEGGMAN_DROPLARGEBOMB
		if object.trumboneExtendPos > -0x500000
			object.trumboneExtendPos -= 0x40000
		end if

		if hpzEggmanHorn[+1].timer == 0
			object.state = HPZEGGMAN_AWAITBOMBSEXPLODE
			object[+2].state = HPZEGGMANVALVE_RESETBOTH
			arrayPos0 = object.entityPos
			arrayPos0--
			ResetObjectEntity(arrayPos0, TypeName[Eggman Mine], 2, player[0].xpos, object.boundsB)
			object[arrayPos0].ypos -= 0x400000
		end if

		temp0 = oscillation
		temp0 &= 7
		if temp0 == 0
			temp0 = oscillation
			temp0 &= 15
			if temp0 == 0
				temp0 = 10
			else
				temp0 = 11
			end if
			
			Rand(temp1, screen.xsize)
			temp1 -= screen.xcenter
			temp1 <<= 16
			temp1 += object.boundsM
			temp2 = object.boundsT
			temp2 -= 0x500000
			CreateTempObject(TypeName[Break Gem], temp0, temp1, temp2)
			breakGem[tempObjectPos].gravityStrength = 0x2800
			object[tempObjectPos].state = BREAKGEM_SHARD
		end if

		CallFunction(B08Eggman_HandleMovement)
		CallFunction(B08Eggman_Oscillate)
		CallFunction(B08Eggman_HandleChildren)
		break

	case HPZEGGMAN_AWAITBOMBSEXPLODE
		if object[-1].type == TypeName[Blank Object]
			if object[-2].type == TypeName[Blank Object]
				object.state = HPZEGGMAN_EXTENDTRUMBONE
			end if
		end if

		CallFunction(B08Eggman_HandleMovement)
		CallFunction(B08Eggman_Oscillate)
		CallFunction(B08Eggman_HandleChildren)
		break

	case HPZEGGMAN_GEYSERHIT
		// is this supposed to be set...?
		// update: is this set from water geyser????
		// why does this use arrayPos0 here lol
		object[arrayPos0].flameAnimation = 0
		switch object.shakeTimer
		case 0
			object.xpos += 0x20000
			break

		case 2
			object.xpos -= 0x20000
			break
			
		end switch

		object.animation = HPZEGGANI_HIT
		object.shakeTimer++
		object.shakeTimer &= 3
		CallFunction(B08Eggman_HandleChildren)
		break

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

		if object.impactCount < 3
			if object.ypos > object.groundPos
				object.ypos = object.groundPos
				object.yvel >>= 1
				FlipSign(object.yvel)
				object.impactCount++
				PlaySfx(SfxName[Ground Impact], false)
			end if
		else
			if object.yvel > 0
				object.impactCount = 0
				object.flameAnimation = FLAMEANI_FALLING
				object.state = HPZEGGMAN_GEYSERRISE
			end if
		end if

		CallFunction(B08Eggman_HandleChildren)
		break

	case HPZEGGMAN_GEYSERRISE
		object.yvel -= 0x1800
		if object.yvel < -0x20000
			object.yvel = -0x20000
			object.flameAnimation = FLAMEANI_RISE
		end if

		object.ypos += object.yvel
		if object.ypos <= object.boundsT
			object.ypos = object.boundsT
			object.originPos.y = object.ypos
			object.oscillationAngle = 0
			object.state = HPZEGGMAN_EXTENDTRUMBONE
		end if

		CallFunction(B08Eggman_HandleChildren)
		break

	case HPZEGGMAN_EXPLODE
		object.timer++
		if object.timer == 30
			object.trumboneExtendSpeed = -0x20000
			object.trumboneVelocity.y = 0
		end if
		
		if object.showTrumbone == true
			object.trumboneExtendPos += object.trumboneExtendSpeed
			if object.trumboneExtendPos < -0x500000
				object.trumboneVelocity.y += 0x3800
				object.trumboneOffset.y += object.trumboneVelocity.y
			end if
		end if

		if object.timer == 60
			if object.direction == FLIP_NONE
				object[+1].xvel = -0x10000
			else
				object[+1].xvel = 0x10000
			end if
			object[+1].yvel = -0x20000
			object[+1].state = HPZEGGMANHORN_DEBRIS
		end if

		if object.timer == 120
			object.showTrumbone = false
			if object.direction == FLIP_NONE
				object[+2].xvel = 0x10000
			else
				object[+2].xvel = -0x10000
			end if
			object[+2].yvel = -0x20000
			object[+2].state = HPZEGGMANVALVE_DEBRIS
		end if

		if object.timer == 180
			object.timer = 0
			object.animation = HPZEGGANI_TOASTED
			object.exploding = false
			object.yvel = 0
			object.flameAnimation = FLAMEANI_INVISIBLE
			HPZSetup_bossFightActive = false
			object.state = HPZEGGMAN_DEFEATFALL
			ResetObjectEntity(SLOT_MUSICEVENT_BOSS, TypeName[Music Event], MUSICEVENT_FADETOSTAGE, 0, 0)
			object[SLOT_MUSICEVENT_BOSS].priority = PRIORITY_ACTIVE
			
			// this should prolly be an animal prison or signpost?
			// in the layout its just a ring
			// a little speculation here - if it was a signpost this state'd be SIGNPOST_IDLE_VS which is kinda weird, but it'd be SIGNPOST_EXITSTAGERIGHT with the S1 ver of the obj
			// if it was an animal prison though, it'd be ANIMALPRISON_HANDLEACTCLEAR in both games, nothing too strange
			object[+3].priority = PRIORITY_ACTIVE
			object[+3].state = 3
			
			temp0 = tileLayer[0].xsize
			temp0 <<= 7
			stage.newXBoundary2 = temp0
		end if
		break

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

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

	case HPZEGGMAN_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 HPZEGGMAN_FLEE
		CallFunction(B08Eggman_Oscillate)
		if object.timer < 8
			object.timer++
		else
			object.timer = 0
			object.animation = HPZEGGANI_PANIC
			object.flameAnimation = FLAMEANI_RISE
			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 HPZEGGMAN_ESCAPE
		object.xpos += 0x40000
		object.originPos.y -= 0x4000
		CallFunction(B08Eggman_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 < 32
				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 != false
					if object.animation != HPZEGGANI_LAUGH
						object.animation = HPZEGGANI_LAUGH
						object.animationTimer = 0
					end if
				end if

				BoxCollisionTest(C_TOUCH, object.entityPos, -24, -24, 24, 20, 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.timer = 0
							object.animation = HPZEGGANI_DEFEATED
							object.flameAnimation = FLAMEANI_FALLING
							object.animationTimer = 0
							object.exploding = true
							object.originPos.y = object.ypos
							object.state = HPZEGGMAN_EXPLODE
							SetPaletteEntry(0, 192, 0x000000)
						else
							object.animation = HPZEGGANI_HIT
							object.animationTimer = 0
							object.invincibilityTimer = 64
							PlaySfx(SfxName[Boss Hit], false)
						end if
					end if
				end if

				if object.flameAnimation == FLAMEANI_MOVING
					BoxCollisionTest(C_TOUCH, object.entityPos, -8, 0, 8, 32, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
					if checkResult == true
						CallFunction(Player_Hit)
					end if
				end if
			end if
		next
	end if

	if object.state != HPZEGGMAN_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
	temp2 = object.trumboneExtendPos
	FlipSign(temp2)
	temp2 >>= 16
	if temp2 < 0
		temp2 = 0
	end if
	if temp2 > 80
		temp2 = 80
	end if

	temp3 = object.trumboneExtendPos
	temp3 >>= 16
	temp3 -= 16
	temp4 = object.trumboneOffset.y
	temp4 += object.ypos

	if object.showTrumbone == true
		EditFrame(13, temp3, 0, temp2, 6, 265, 235)
		DrawSpriteFX(13, FX_FLIP, object.xpos, temp4)
	end if

	DrawSpriteFX(6, FX_FLIP, object.xpos, object.ypos)

	switch object.animation
	default
	case HPZEGGANI_INVISIBLE
		break

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

	case HPZEGGANI_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 = HPZEGGANI_IDLE
			end if
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break

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

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

	case HPZEGGANI_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

	if object.showTrumbone == true
		EditFrame(14, temp3, 6, temp2, 6, 265, 241)
		DrawSpriteFX(14, FX_FLIP, object.xpos, temp4)
	end if

	switch object.flameAnimation
	default
	case FLAMEANI_INVISIBLE
		break

	case FLAMEANI_FALLING
		GetTableValue(temp0, object.flameAnimTimer, B08Eggman_flameFrames_Falling)
		object.flameAnimTimer++
		object.flameAnimTimer &= 3
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
		
	case FLAMEANI_RISE
		GetTableValue(temp0, object.flameAnimTimer, B08Eggman_flameFrames_Rise)
		object.flameAnimTimer++
		object.flameAnimTimer &= 3
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break

	case FLAMEANI_MOVING
		GetTableValue(temp0, object.flameAnimTimer, B08Eggman_flameFrames_Moving)
		object.flameAnimTimer++
		object.flameAnimTimer &= 3
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
		
	end switch
end event


event ObjectStartup
	LoadSpriteSheet("HPZ/Objects.gif")
	SpriteFrame(-28, -28, 60, 20, 265, 139)	// eggman idle 1 - #0
	SpriteFrame(-28, -28, 60, 20, 326, 139)	// eggman idle 2 - #1
	SpriteFrame(-28, -28, 60, 20, 265, 160)	// eggman laugh 1 - #2
	SpriteFrame(-28, -28, 60, 20, 326, 160)	// eggman laugh 2 - #3
	SpriteFrame(-28, -28, 60, 20, 265, 181)	// eggman hit - #4
	SpriteFrame(-28, -28, 60, 20, 326, 181)	// eggman destroyed - #5
	SpriteFrame(-32, -8, 64, 32, 265, 202)	// eggmobile - #6
	SpriteFrame(-4, 24, 16, 8, 432, 178) 	// flame 1 - #7
	SpriteFrame(-4, 24, 16, 12, 415, 170) 	// flame 2 - #8
	SpriteFrame(-4, 24, 16, 8, 432, 178) 	// flame 3 - #9
	SpriteFrame(-4, 24, 16, 24, 432, 153) 	// flame 4 - #10
	SpriteFrame(-4, 24, 16, 12, 415, 157) 	// flame 5 - #11
	SpriteFrame(-4, 24, 16, 24, 398, 157) 	// flame 6 - #12
	SpriteFrame(-96, 0, 80, 6, 265, 235) 	// trumbone (top) - #13
	SpriteFrame(-96, 6, 80, 6, 265, 241) 	// trumbone (bottom) - #14
	SpriteFrame(3, -28, 11, 8, 437, 187) 	// panic sweat - #15
end event


event RSDKDraw
	DrawSprite(6)
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("HPZ/Objects.gif")
	SpriteFrame(-28, -28, 60, 20, 265, 139)
	SpriteFrame(-28, -28, 60, 20, 326, 139)
	SpriteFrame(-28, -28, 60, 20, 265, 160)
	SpriteFrame(-28, -28, 60, 20, 326, 160)
	SpriteFrame(-28, -28, 60, 20, 265, 181)
	SpriteFrame(-28, -28, 60, 20, 326, 181)
	SpriteFrame(-32, -8, 64, 32, 265, 202)
	SpriteFrame(-4, 24, 16, 8, 432, 178)
	SpriteFrame(-4, 24, 16, 12, 415, 170)
	SpriteFrame(-4, 24, 16, 8, 432, 178)
	SpriteFrame(-4, 24, 16, 24, 432, 153)
	SpriteFrame(-4, 24, 16, 12, 415, 157)
	SpriteFrame(-4, 24, 16, 24, 398, 157)
	SpriteFrame(-96, 0, 80, 6, 265, 235)
	SpriteFrame(-96, 6, 80, 6, 265, 241)
	SpriteFrame(3, -28, 11, 8, 437, 187)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
