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

// Using MZEggman 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.health
private alias object.value2  : object.invincibilityTimer
private alias object.value3  : object.flameAnim
private alias object.value4  : object.flameAnimTimer
private alias object.value5  : object.boundsL
private alias object.value6  : object.boundsR
private alias object.value7  : object.divePos.y
private alias object.value8  : object.originPos.y
private alias object.value9  : object.fireballDropDelay
private alias object.value10 : object.dontRandomizeNextDrop
private alias object.value11 : object.nextState
private alias object.value12 : object.hitDelay

// Eggman States
private alias 0  : MZEGGMAN_AWAITPLAYER
private alias 1  : MZEGGMAN_ENTEREGGMAN
private alias 2  : MZEGGMAN_TAUNT
private alias 3  : MZEGGMAN_MOVE_L
private alias 4  : MZEGGMAN_DROPFIREBALL_L
private alias 5  : MZEGGMAN_DROPPEDFIREBALL_L
private alias 6  : MZEGGMAN_MOVE_R
private alias 7  : MZEGGMAN_DROPFIREBALL_R
private alias 8  : MZEGGMAN_DROPPEDFIREBALL_R
private alias 9  : MZEGGMAN_EXPLODE
private alias 10 : MZEGGMAN_DEFEATFALL
private alias 11 : MZEGGMAN_DEFEATRISE
private alias 12 : MZEGGMAN_FLEE
private alias 13 : MZEGGMAN_ESCAPE
private alias 14 : MZEGGMAN_HITFREEZE

// Eggman Animations
private alias 0 : MZEGGANI_INVISIBLE
private alias 1 : MZEGGANI_IDLE
private alias 2 : MZEGGANI_LAUGH
private alias 3 : MZEGGANI_LAUGH_LOOP
private alias 4 : MZEGGANI_HIT
private alias 5 : MZEGGANI_DEFEATED
private alias 6 : MZEGGANI_TOASTED
private alias 7 : MZEGGANI_PANIC

// Flame Animations
private alias 0 : FLAME_INACTIVE
private alias 1 : FLAME_ACTIVE
private alias 2 : FLAME_EXPLODE

// Marker aliases
private alias object.xpos  : marker.xpos
private alias object.ypos  : marker.ypos
private alias object.ixpos : marker.ixpos
private alias object.iypos : marker.iypos

// Fireball aliases
private alias object.value2 : fireball.fireballDir
private alias object.value3 : fireball.finalY
private alias 1 : FIREBALL_MOVE_GRAVITY

// 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.value18 : player.sortedDrawOrder

// Music Events
private alias 26 : SLOT_MUSICEVENT_BOSS

private alias 0 : MUSICEVENT_FADETOBOSS
private alias 1 : MUSICEVENT_FADETOSTAGE
private alias 2 : MUSICEVENT_TRANSITION


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

reserve function MZEggman_Oscillate
reserve function MZEggman_HandleFireballErupt


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

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


public function MZEggman_HandleFireballErupt
	if object.fireballDropDelay > 0
		object.fireballDropDelay--
	else
		Rand(temp0, 112)
		temp0 -= 56
		temp0 <<= 16
		temp0 += marker[+1].xpos
		temp1 = marker[+1].ypos
		temp1 += 0x200000
		CreateTempObject(TypeName[Fireball], 0, temp0, temp1)
		object[tempObjectPos].state = FIREBALL_MOVE_GRAVITY
		object[tempObjectPos].xvel = 0
		object[tempObjectPos].yvel = -0x40000
		fireball[tempObjectPos].finalY = temp1
		fireball[tempObjectPos].fireballDir = 2
		PlaySfx(SfxName[Small Fireball], false)

		if object.dontRandomizeNextDrop == false
			Rand(temp0, 3)
			if temp0 == 2
				object.fireballDropDelay = 70
				object.dontRandomizeNextDrop = true
			else
				object.fireballDropDelay = 140
			end if
		else
			object.dontRandomizeNextDrop = false
			object.fireballDropDelay = 140
		end if
	end if
end function


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

event ObjectUpdate
	switch object.state
	case MZEGGMAN_AWAITPLAYER
		temp0 = marker[+1].ixpos
		temp0 += screen.xcenter
		stage.newXBoundary2 = temp0
		
		temp0 -= screen.xsize
		stage.newXBoundary1 = temp0
		
		temp0 = stage.curYBoundary2
		temp0 -= screen.ysize
		stage.newYBoundary1 = temp0
		
		if player[0].xpos > marker[+1].xpos
			object.divePos.y = marker[+1].ypos
			object.divePos.y -= 0x900000
			object.originPos.y = marker[+1].ypos
			object.originPos.y -= 0x900000

			temp0 = 0x700000
			temp0 *= screen.xcenter
			temp0 /= 160
			object.speed = 0x20000
			object.speed *= screen.xcenter
			object.speed /= 160

			object.boundsL = marker[+1].xpos
			object.boundsL -= temp0
			object.boundsR = marker[+1].xpos
			object.boundsR += temp0

			object.xvel = -0x10000
			object.flameAnim = FLAME_ACTIVE
			object.fireballDropDelay = 120
			ResetObjectEntity(SLOT_MUSICEVENT_BOSS, TypeName[Music Event], MUSICEVENT_FADETOBOSS, 0, 0)
			// 26 = SLOT_MUSICEVENT_BOSS
			object[26].priority = PRIORITY_ACTIVE
			object.drawOrder = 4
			player[0].sortedDrawOrder = 5
			if object[1].type == TypeName[Player 2 Object]
				player[1].sortedDrawOrder = 5
			end if
			object.state++
		end if
		break

	case MZEGGMAN_ENTEREGGMAN
		object.xpos += object.xvel
		if object.xpos <= object.boundsR
			object.xpos = object.boundsR
			object.flameAnim = FLAME_INACTIVE
			object.state++
		end if
		CallFunction(MZEggman_Oscillate)
		break

	case MZEGGMAN_TAUNT
		CallFunction(MZEggman_Oscillate)

		object.timer++
		if object.timer == 16
			object.timer = 0
			object.xvel = object.speed
			FlipSign(object.xvel)
			object.yvel = 0x10000
			object.flameAnim = FLAME_ACTIVE
			object.state++
		end if
		break

	case MZEGGMAN_MOVE_L
		object.yvel -= 0x400
		object.xpos += object.xvel
		object.ypos += object.yvel

		if object.xpos <= object.boundsL
			object.xpos = object.boundsL
			object.flameAnim = FLAME_INACTIVE
			object.state++
		end if
		CallFunction(MZEggman_HandleFireballErupt)
		break

	case MZEGGMAN_DROPFIREBALL_L
		object.yvel -= 0x400
		object.ypos += object.yvel

		if object.ypos <= object.divePos.y
			object.ypos = object.divePos.y
			object.direction = FACING_LEFT
			object.animation = MZEGGANI_LAUGH_LOOP
			object.animationTimer = 0
			CreateTempObject(TypeName[Boss Fireball], 0, object.xpos, object.ypos)
			object[tempObjectPos].ypos += 0x140000
			object.state++
		end if
		CallFunction(MZEggman_HandleFireballErupt)
		break

	case MZEGGMAN_DROPPEDFIREBALL_L
		object.timer++
		if object.timer == 80
			object.timer = 0
			object.xvel = object.speed
			object.yvel = 0x10000
			object.animation = MZEGGANI_IDLE
			object.flameAnim = FLAME_ACTIVE
			object.state++
		end if

		CallFunction(MZEggman_HandleFireballErupt)
		break

	case MZEGGMAN_MOVE_R
		object.yvel -= 0x400
		object.xpos += object.xvel
		object.ypos += object.yvel

		if object.xpos >= object.boundsR
			object.xpos = object.boundsR
			object.flameAnim = FLAME_INACTIVE
			object.state++
		end if
		CallFunction(MZEggman_HandleFireballErupt)
		break

	case MZEGGMAN_DROPFIREBALL_R
		object.yvel -= 0x400
		object.ypos += object.yvel

		if object.ypos <= object.divePos.y
			object.ypos = object.divePos.y
			object.direction = FACING_RIGHT
			object.animation = MZEGGANI_LAUGH_LOOP
			object.animationTimer = 0
			CreateTempObject(TypeName[Boss Fireball], 0, object.xpos, object.ypos)
			object[tempObjectPos].ypos += 0x140000
			object.state++
		end if
		CallFunction(MZEggman_HandleFireballErupt)
		break

	case MZEGGMAN_DROPPEDFIREBALL_R
		object.timer++
		if object.timer == 80
			object.timer = 0
			object.xvel = object.speed
			FlipSign(object.xvel)
			object.yvel = 0x10000
			object.animation = MZEGGANI_IDLE
			object.flameAnim = FLAME_INACTIVE
			object.state = MZEGGMAN_MOVE_L
		end if

		CallFunction(MZEggman_HandleFireballErupt)
		break

	case MZEGGMAN_EXPLODE
		object.timer++
		temp0 = object.timer
		temp0 &= 7
		if temp0 == 4
			Rand(temp0, 64)
			temp0 -= 32
			temp0 <<= 16
			temp0 += object.xpos
			Rand(temp1, 48)
			temp1 -= 24
			temp1 <<= 16
			temp1 += object.ypos
			CreateTempObject(TypeName[Explosion], 0, temp0, temp1)
			PlaySfx(SfxName[Explosion], false)
		end if

		if object.timer == 180
			object.animation = MZEGGANI_TOASTED
			object.animationTimer = 0
			object.timer = 0
			object.direction = FACING_LEFT
			ResetObjectEntity(SLOT_MUSICEVENT_BOSS, TypeName[Music Event], MUSICEVENT_FADETOSTAGE, 0, 0)
			// 26 = SLOT_MUSICEVENT_BOSS
			object[26].priority = PRIORITY_ACTIVE
			object.state++
		end if
		break

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

		object.timer++
		if object.timer == 32
			object.yvel = 0
			object.timer = 0
			object.state++
		end if
		break

	case MZEGGMAN_DEFEATRISE
		if object.timer < 40
			object.ypos += object.yvel
			object.yvel -= 0x800
			object.timer++
		else
			object.timer = 0
			object.yvel = 0
			object.angle = 0
			object.originPos.y = object.ypos
			object.state++
		end if
		break

	case MZEGGMAN_FLEE
		CallFunction(MZEggman_Oscillate)

		if object.timer < 8
			object.timer++
		else
			object.timer = 0
			object.animation = MZEGGANI_PANIC
			object.animationTimer = 0
			object.flameAnim = FLAME_EXPLODE
			object.flameAnimTimer = 0
			object.priority = PRIORITY_ACTIVE
			temp0 = tileLayer[0].xsize
			temp0 <<= 7
			stage.newXBoundary2 = temp0
			object.state++
		end if
		break

	case MZEGGMAN_ESCAPE
		CallFunction(MZEggman_Oscillate)
		object.xpos += 0x40000
		object.originPos.y -= 0x4000
		if object.outOfBounds == true
			object.type = TypeName[Blank Object]
			object.priority = PRIORITY_BOUNDS
		end if
		break

	case MZEGGMAN_HITFREEZE
		object.hitDelay++
		if object.hitDelay == 16
			object.hitDelay = 0
			object.state = object.nextState
		end if
		break
		
	end switch

	if object.health != 0
		if object.invincibilityTimer > 0
			object.invincibilityTimer--
		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 != false
					if object.animation != MZEGGANI_LAUGH
						object.animation = MZEGGANI_LAUGH
						object.animationTimer = 0
					end if
				end if

				BoxCollisionTest(C_TOUCH, object.entityPos, -20, -16, 20, 16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				if checkResult == true
					CallFunction(Player_CheckHit)
					if object.state > MZEGGMAN_TAUNT
						if object.state != MZEGGMAN_HITFREEZE
							object.nextState = object.state
							object.state = MZEGGMAN_HITFREEZE
						end if
					end if

					if checkResult == true
						object.health--
						if object.health == 0
#platform: USE_ORIGINS
							CallNativeFunction2(NotifyCallback, NOTIFY_KILL_BOSS, 0)
							CallNativeFunction2(NotifyCallback, NOTIFY_BOSS_END, true)
#endplatform
							player.score += 1000
							object.drawOrder = 3
							object.animation = MZEGGANI_DEFEATED
							object.animationTimer = 0
							object.flameAnim = FLAME_INACTIVE
							object.timer = 0
							object.yvel = 0
							object.state = MZEGGMAN_EXPLODE
						else
							object.animation = MZEGGANI_HIT
							object.animationTimer = 0
							object.invincibilityTimer = 32
							PlaySfx(SfxName[Boss Hit], false)
						end if
					end if
				end if
			end if
		next
	end if

	// Handle Right Bounds
	if object.state < MZEGGMAN_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
end event


event ObjectDraw
	switch object.animation
	default
	case MZEGGANI_INVISIBLE
		break

	case MZEGGANI_IDLE
		temp0 = object.animationTimer
		temp0 /= 6
		object.animationTimer++
		object.animationTimer %= 12
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break

	case MZEGGANI_LAUGH
		temp0 = object.animationTimer
		temp0 %= 10
		temp0 /= 5
		temp0 += 9
		object.animationTimer++
		if object.animationTimer == 50
			object.animationTimer = 0
			if player[0].animation != ANI_DYING
				object.animation = MZEGGANI_IDLE
			end if
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break

	case MZEGGANI_LAUGH_LOOP
		temp0 = object.animationTimer
		temp0 /= 5
		temp0 += 9
		object.animationTimer++
		object.animationTimer %= 10
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break

	case MZEGGANI_HIT
		temp0 = object.animationTimer
		temp0 &= 1
		temp0 += 2
		object.animationTimer++
		if object.animationTimer == 30
			object.animationTimer = 0
			object.animation = MZEGGANI_IDLE
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break

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

	case MZEGGANI_TOASTED
		DrawSpriteFX(6, FX_FLIP, object.xpos, object.ypos)
		break

	case MZEGGANI_PANIC
		temp0 = object.animationTimer
		temp0 >>= 2
		temp0 += 7
		object.animationTimer++
		object.animationTimer %= 7
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
		
	end switch

	switch object.flameAnim
	default
	case FLAME_INACTIVE
		break

	case FLAME_ACTIVE
		temp0 = object.flameAnimTimer
		temp0 >>= 2
		temp0 += 11
		object.flameAnimTimer++
		object.flameAnimTimer &= 7
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break

	case FLAME_EXPLODE
		temp0 = object.flameAnimTimer
		temp0 /= 3
		temp0 += 11
		object.flameAnimTimer++
		if object.flameAnimTimer > 21
			object.flameAnimTimer = 18
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
		
	end switch

	DrawSpriteFX(19, FX_FLIP, object.xpos, object.ypos)
end event


event ObjectStartup
	LoadSpriteSheet("Global/Eggman.gif")
	
	SpriteFrame(-28, -32, 64, 56, 1, 1)
	SpriteFrame(-28, -32, 64, 56, 66, 1)
	SpriteFrame(-28, -32, 64, 56, 131, 1)
	SpriteFrame(-28, -32, 64, 56, 1, 58)
	SpriteFrame(-28, -32, 64, 56, 1, 1)
	SpriteFrame(-28, -32, 64, 56, 1, 58)
	SpriteFrame(-28, -32, 64, 56, 66, 58)
	SpriteFrame(-28, -32, 64, 56, 131, 58)
	SpriteFrame(-28, -32, 64, 56, 66, 1)
	SpriteFrame(-28, -32, 64, 56, 1, 115)
	SpriteFrame(-28, -32, 64, 56, 66, 115)
	SpriteFrame(34, 1, 10, 9, 196, 2)
	SpriteFrame(33, 0, 15, 11, 196, 12)
	SpriteFrame(34, -1, 22, 14, 213, 1)
	SpriteFrame(34, -8, 32, 26, 213, 16)
	SpriteFrame(34, -1, 22, 14, 213, 1)
	SpriteFrame(34, -8, 32, 26, 213, 16)
	SpriteFrame(34, 1, 10, 9, 196, 2)
	SpriteFrame(33, 0, 15, 11, 196, 12)
	SpriteFrame(-8, 16, 16, 16, 238, 43)

	// Set up values for every Eggman object
	foreach (TypeName[Eggman], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].animation = MZEGGANI_IDLE
#platform: USE_STANDALONE
		object[arrayPos0].health = 8
#endplatform
#platform: USE_ORIGINS
		if game.bossOneLife == false
			object[arrayPos0].health = 8
		else
			object[arrayPos0].health = 1
		end if
#endplatform
	next

	if stage.actNum == 3
		if screen.xsize == 320
			// Change the boss arena to match the original game's version
			SetTileLayerEntry(348, 0, 50, 5)
			SetTileLayerEntry(349, 0, 51, 5)
		end if
	end if
end event


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

event RSDKDraw
	DrawSprite(0)
	DrawSprite(19)
end event


event RSDKLoad
	LoadSpriteSheet("Global/Eggman.gif")
	SpriteFrame(-28, -32, 64, 56, 1, 1)
	SpriteFrame(-28, -32, 64, 56, 66, 1)
	SpriteFrame(-28, -32, 64, 56, 131, 1)
	SpriteFrame(-28, -32, 64, 56, 1, 58)
	SpriteFrame(-28, -32, 64, 56, 1, 1)
	SpriteFrame(-28, -32, 64, 56, 1, 58)
	SpriteFrame(-28, -32, 64, 56, 66, 58)
	SpriteFrame(-28, -32, 64, 56, 131, 58)
	SpriteFrame(-28, -32, 64, 56, 66, 1)
	SpriteFrame(-28, -32, 64, 56, 1, 115)
	SpriteFrame(-28, -32, 64, 56, 66, 115)
	SpriteFrame(34, 1, 10, 9, 196, 2)
	SpriteFrame(33, 0, 15, 11, 196, 12)
	SpriteFrame(34, -1, 22, 14, 213, 1)
	SpriteFrame(34, -8, 32, 26, 213, 16)
	SpriteFrame(34, -1, 22, 14, 213, 1)
	SpriteFrame(34, -8, 32, 26, 213, 16)
	SpriteFrame(34, 1, 10, 9, 196, 2)
	SpriteFrame(33, 0, 15, 11, 196, 12)
	SpriteFrame(-8, 16, 16, 16, 238, 43)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
