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

// Using SLZEggman as a prefix here because there's like 6 "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.oscillateAngle
private alias object.value4  : object.timer
private alias object.value5  : object.flameAnim
private alias object.value6  : object.flameAnimTimer
private alias object.value7  : object.health
private alias object.value8  : object.invincibilityTimer
private alias object.value9  : object.targetSeeSaw
private alias object.value10 : object.boundsL
private alias object.value11 : object.boundsR
private alias object.value12 : object.targetScreenPos.y
private alias object.value13 : object.usedSpikeBall

// SeeSaw Aliases
private alias object.value0  : object.orbTimer
private alias object.value8  : object.orbTargetPos.x
private alias object.value9  : object.orbTargetPos.y
private alias object.value10 : object.orbFrame
private alias object.value12 : object.stood

private alias 3 : SEESAW_NO_ORB

// Eggman States
private alias 0  : SLZEGGMAN_AWAITPLAYER
private alias 1  : SLZEGGMAN_ENTEREGGMAN
private alias 2  : SLZEGGMAN_MOVEANDDROP
private alias 3  : SLZEGGMAN_DROPPEDBALL
private alias 4  : SLZEGGMAN_EXPLODE
private alias 5  : SLZEGGMAN_DEFEATFALL
private alias 6  : SLZEGGMAN_DEFEATRISE
private alias 7  : SLZEGGMAN_FLEE
private alias 8  : SLZEGGMAN_ESCAPE

// Eggman Animations
private alias 0 : SLZEGGANI_INVISIBLE
private alias 1 : SLZEGGANI_IDLE
private alias 2 : SLZEGGANI_LAUGH
private alias 3 : SLZEGGANI_HIT
private alias 4 : SLZEGGANI_DEFEATED
private alias 5 : SLZEGGANI_TOASTED
private alias 6 : SLZEGGANI_PANIC

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

// Music Events
private alias 26 : SLOT_MUSICEVENT_BOSS

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

// 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

// Achievement Aliases
private alias 7 : ACHIEVEMENT_BOMBSAWAY


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

reserve function SLZEggman_Hit
reserve function SLZEggman_DropSpikeBall
reserve function SLZEggman_Oscillate


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

private table SLZEggman_seesawSlotOffsets
	2, 3, 1
end table

private table SLZEggman_seesawDropPos
	0, 0, 0, 0, 0, 0
end table


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

private function SLZEggman_Hit
	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.flameAnim = FLAME_INACTIVE
		object.animation = SLZEGGANI_DEFEATED
		object.animationTimer = 0
		object.state = SLZEGGMAN_EXPLODE
		if stage.debugMode == false
			if object.usedSpikeBall == false
				// Grant the "Bombs Away" Achievement
				CallNativeFunction2(SetAchievement, ACHIEVEMENT_BOMBSAWAY, 100)
			end if
		end if
	else
		object.animation = SLZEGGANI_HIT
		object.animationTimer = 0
		object.invincibilityTimer = 32
		PlaySfx(SfxName[Boss Hit], false)
	end if
end function


private function SLZEggman_DropSpikeBall
	arrayPos0 = object.entityPos
	temp1 = object.targetSeeSaw
	temp1 >>= 1
	GetTableValue(temp2, temp1, SLZEggman_seesawSlotOffsets)
	arrayPos0 -= temp2
	if object[arrayPos0].state == SEESAW_NO_ORB
		if object[arrayPos0].stood == false
			object.xpos = temp0
			object[arrayPos0].state = 2
			object[arrayPos0].xvel = 0
			object[arrayPos0].yvel = 0
			object[arrayPos0].orbTargetPos.x = object.xpos
			object[arrayPos0].orbTargetPos.y = object.ypos
			object[arrayPos0].orbTargetPos.y += 0x200000
			object[arrayPos0].orbTimer = 240
			object[arrayPos0].orbFrame = 2
			object[arrayPos0].animationTimer = 44
			object.timer = 40
			object.flameAnim = FLAME_INACTIVE
			object.state++
		end if
	end if
end function


private function SLZEggman_Oscillate
	Sin256(temp0, object.oscillateAngle)
	temp0 <<= 10
	object.ypos += temp0
	object.oscillateAngle += 2
	object.oscillateAngle &= 0xFF
end function


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

event ObjectUpdate
	switch object.state
	case SLZEGGMAN_AWAITPLAYER
		if player[0].xpos >= object[-3].xpos
			temp0 = object[-3].ixpos
			temp0 -= screen.xcenter
			stage.newXBoundary1 = temp0
			temp0 = object[-3].ixpos
			temp0 += screen.xcenter
			stage.newXBoundary2 = temp0
			if screen.yoffset == object.targetScreenPos.y
				object.state++
				object.priority = PRIORITY_ACTIVE
				ResetObjectEntity(SLOT_MUSICEVENT_BOSS, TypeName[Music Event], MUSICEVENT_FADETOBOSS, 0, 0)
				object[SLOT_MUSICEVENT_BOSS].priority = PRIORITY_ACTIVE
			else
				object.targetScreenPos.y = screen.yoffset
			end if
		end if
		break

	case SLZEGGMAN_ENTEREGGMAN
		object.xpos += object.xvel
		temp0 = object[-3].xpos
		temp0 += 0x800000
		if object.xpos < temp0
			object.state++
			temp1 = screen.xcenter
			temp1 -= 4
			temp1 <<= 8
			temp1 /= 160
			temp1 *= -512
			object.xvel = temp1
		end if
		object.ypos = object.originPos.y
		CallFunction(SLZEggman_Oscillate)
		break

	case SLZEGGMAN_MOVEANDDROP
		object.xpos += object.xvel
		if object.direction == FLIP_NONE
			if object.xpos <= object.boundsL
				object.xpos = object.boundsL
				FlipSign(object.xvel)
				object.direction ^= FLIP_X
			else
				GetTableValue(temp0, object.targetSeeSaw, SLZEggman_seesawDropPos)
				if object.xpos <= temp0
					CallFunction(SLZEggman_DropSpikeBall)
					object.targetSeeSaw--
					if object.targetSeeSaw < 0
						object.targetSeeSaw = 1
					end if
				end if
				object.targetSeeSaw &= 14
			end if
		else
			if object.xpos >= object.boundsR
				object.xpos = object.boundsR
				FlipSign(object.xvel)
				object.direction ^= FLIP_X
			else
				GetTableValue(temp0, object.targetSeeSaw, SLZEggman_seesawDropPos)
				if object.xpos >= temp0
					CallFunction(SLZEggman_DropSpikeBall)
					object.targetSeeSaw++
					if object.targetSeeSaw > 5
						object.targetSeeSaw = 4
					end if
				end if
				object.targetSeeSaw |= 1
			end if
		end if
		object.ypos = object.originPos.y
		CallFunction(SLZEggman_Oscillate)
		break

	case SLZEGGMAN_DROPPEDBALL
		object.timer--
		if object.timer <= 0
			object.flameAnim = FLAME_ACTIVE
			object.state--
		end if
		break

	case SLZEGGMAN_EXPLODE
		object.timer++
		temp0 = object.timer
		temp0 &= 7
		if temp0 == 4
			Rand(temp0, 96)
			temp0 -= 48
			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 = SLZEGGANI_TOASTED
			object.animationTimer = 0
			object.timer = 0
			object.direction = FLIP_X
			ResetObjectEntity(SLOT_MUSICEVENT_BOSS, TypeName[Music Event], MUSICEVENT_FADETOSTAGE, 0, 0)
			object[SLOT_MUSICEVENT_BOSS].priority = PRIORITY_ACTIVE
			object.state++
		end if
		break

	case SLZEGGMAN_DEFEATFALL
		object.ypos += object.yvel
		object.yvel += 0x1800
		object.timer++
		if object.timer == 38
			object.yvel = 0
			object.timer = 0
			object.animationTimer = 0
			object.state++
		end if
		break

	case SLZEGGMAN_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
		break

	case SLZEGGMAN_FLEE
		object.ypos = object.originPos.y
		CallFunction(SLZEggman_Oscillate)
		if object.timer < 8
			object.timer++
		else
			object.timer = 0
			object.animation = SLZEGGANI_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 SLZEGGMAN_ESCAPE
		object.xpos += 0x40000
		object.originPos.y -= 0x4000
		object.ypos = object.originPos.y
		CallFunction(SLZEggman_Oscillate)

		if object.outOfBounds == true
			object.type = TypeName[Blank Object]
			object.priority = PRIORITY_BOUNDS
		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 != SLZEGGANI_LAUGH
						object.animation = SLZEGGANI_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 checkResult == true
						object.usedSpikeBall = true
						CallFunction(SLZEggman_Hit)
					end if
				end if
			end if
		next

		if object.invincibilityTimer == 0
			foreach (TypeName[SeeSaw], arrayPos0, ACTIVE_ENTITIES)
				if object[arrayPos0].state != SEESAW_NO_ORB
					temp0 = object[arrayPos0].xpos
					temp1 = object[arrayPos0].ypos
					object[arrayPos0].xpos = object[arrayPos0].orbTargetPos.x
					object[arrayPos0].ypos = object[arrayPos0].orbTargetPos.y
					BoxCollisionTest(C_TOUCH, object.entityPos, -20, -16, 20, 16, arrayPos0, -7, -7, 7, 7)
					if checkResult == true
						CallFunction(SLZEggman_Hit)
						CreateTempObject(TypeName[Smoke Puff], 0, object[arrayPos0].orbTargetPos.x, object[arrayPos0].orbTargetPos.y)
						PlaySfx(SfxName[Explosion], false)
						object[arrayPos0].state = SEESAW_NO_ORB
						object[arrayPos0].orbTimer = 0
					end if
					object[arrayPos0].xpos = temp0
					object[arrayPos0].ypos = temp1
				end if
			next
		end if
	end if

	if object.state < SLZEGGMAN_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
	case SLZEGGANI_INVISIBLE
		break

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

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

	case SLZEGGANI_HIT
		temp0 = object.animationTimer
		temp0 &= 1
		temp0 += 2
		temp1 = temp0
		temp1 += 17
		object.animationTimer++
		if object.animationTimer == 30
			object.animationTimer = 0
			object.animation = SLZEGGANI_IDLE
		end if
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		DrawSpriteFX(temp1, FX_FLIP, object.xpos, object.ypos)
		break

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

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

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

	switch object.flameAnim
	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
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(-12, 17, 24, 16, 213, 135)
	SpriteFrame(-12, 17, 24, 16, 213, 43)

	foreach (TypeName[Eggman], arrayPos0, ALL_ENTITIES)
		if object[arrayPos0].type == TypeName[Eggman]
			object[arrayPos0].priority = PRIORITY_XBOUNDS
			arrayPos1 = arrayPos0
			arrayPos1 -= 3
			object[arrayPos0].xpos = object[arrayPos1].xpos
			object[arrayPos0].xpos += 0xE80000
			object[arrayPos0].ypos = object[arrayPos1].ypos
			object[arrayPos0].ypos -= 0xAC0000

			temp1 = screen.xcenter
			temp0 -= 4
			temp1 <<= 8
			temp1 /= 160
			temp1 *= -256
			object[arrayPos0].xvel = temp1

			object[arrayPos0].targetSeeSaw = 4
			object[arrayPos0].originPos.x = object[arrayPos0].xpos
			object[arrayPos0].originPos.y = object[arrayPos0].ypos
			object[arrayPos0].animation = SLZEGGANI_IDLE
			
#platform: USE_ORIGINS
			if game.bossOneLife == false
				object[arrayPos0].health = 8
			else
				object[arrayPos0].health = 1
			end if
#endplatform
#platform: USE_STANDALONE
			object[arrayPos0].health = 8
#endplatform

			temp0 = screen.xcenter
			temp0 -= 160
			temp0 -= 4
			if temp0 < 0
				temp0 = 0
			end if
			temp0 <<= 16
			object[arrayPos0].xpos += temp0
			temp0 += 0x980000

			object[arrayPos0].boundsL = object[arrayPos1].xpos
			object[arrayPos0].boundsL -= temp0
			object[arrayPos0].boundsR = object[arrayPos1].xpos
			object[arrayPos0].boundsR += temp0

			temp0 -= 0x980000
			temp1 = object[arrayPos1].xpos
			temp1 -= 0x280000
			SetTableValue(temp1, 2, SLZEggman_seesawDropPos)
			temp1 += 0x500000
			SetTableValue(temp1, 3, SLZEggman_seesawDropPos)
			arrayPos1++

			object[arrayPos1].xpos -= temp0
			temp1 = object[arrayPos1].xpos
			temp1 -= 0x280000
			SetTableValue(temp1, 0, SLZEggman_seesawDropPos)
			temp1 += 0x500000
			SetTableValue(temp1, 1, SLZEggman_seesawDropPos)
			arrayPos1++

			object[arrayPos1].xpos += temp0
			temp1 = object[arrayPos1].xpos
			temp1 -= 0x280000
			SetTableValue(temp1, 4, SLZEggman_seesawDropPos)
			temp1 += 0x500000
			SetTableValue(temp1, 5, SLZEggman_seesawDropPos)

			object[arrayPos0].flameAnim = FLAME_ACTIVE
		end if
	next
end event


event RSDKDraw
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
	DrawSpriteFX(1, FX_FLIP, object.xpos, object.ypos)
end event


event RSDKLoad
	LoadSpriteSheet("Global/Eggman.gif")
	SpriteFrame(-28, -32, 64, 56, 1, 1)
	SpriteFrame(-12, 17, 24, 16, 213, 43)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
