// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Eggman Laser 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.boundsL
private alias object.value2 : object.boundsR
private alias object.value3 : object.coreOffset.y
private alias object.value4 : object.laserFrame
private alias object.value5 : object.laserHeight
private alias object.value6 : object.health
private alias object.value7 : object.invincibilityTimer
private alias object.value8 : object.explodeState
private alias object.value9 : object.flashPaletteIndex

// States
private alias 0  : WFZEGGMANLASER_SETUP
private alias 1  : WFZEGGMANLASER_ENTERDELAY
private alias 2  : WFZEGGMANLASER_ENTERBOSS
private alias 3  : WFZEGGMANLASER_MOVING
private alias 4  : WFZEGGMANLASER_OPENSHELL
private alias 5  : WFZEGGMANLASER_CORESHOWDELAY
private alias 6  : WFZEGGMANLASER_SHOWCORE
private alias 7  : WFZEGGMANLASER_LASERDELAY
private alias 8  : WFZEGGMANLASER_SETUPLASER
private alias 9  : WFZEGGMANLASER_CHARGELASER
private alias 10 : WFZEGGMANLASER_FIRINGLASER
private alias 11 : WFZEGGMANLASER_LASERMOVE
private alias 12 : WFZEGGMANLASER_HIDECORE
private alias 13 : WFZEGGMANLASER_CLOSESHELL
private alias 14 : WFZEGGMANLASER_DESTROYED
private alias 15 : WFZEGGMANLASER_RESETBOUNDS

// Explode States
private alias 0 : WFZEGGMANLASER_BOSS_ACTIVE
private alias 1 : WFZEGGMANLASER_BOSS_EXPLODE
private alias 2 : WFZEGGMANLASER_BOSS_DESTROYED

// WFZEggman States
private alias 2 : WFZEGGMAN_ESCAPE

// WFZEggmanDispenser Aliases
private alias object.value0 : wfzEggmanDispenser.timer
private alias object.value8 : wfzEggmanDispenser.exploding

// WFZEggmanDispenser States
private alias 14 : WFZEGGMANDISPENSER_DESTROYED

// WFZEggmanPlatform States
private alias 2 : WFZEGGMANPLATFORM_DESTROYED

// WFZEggmanBarrier Aliases
private alias object.value0 : wfzEggmanBarrier.timer

// Player Aliases
private alias object.xpos : player.xpos
private alias object.value40 : player.hitboxLeft
private alias object.value38 : player.hitboxTop
private alias object.value41 : player.hitboxRight
private alias object.value39 : player.hitboxBottom


// ========================
// Static Values
// ========================

private value EggmanLaser_laserSheetX = 0


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

event ObjectUpdate
	switch object.state
	case WFZEGGMANLASER_SETUP
		object.boundsL 		= object.xpos
		object.boundsL 		-= 0x600000
		object.boundsR 		= object.xpos
		object.boundsR 		+= 0x600000
		object.laserFrame	= 6
		object.drawOrder 	= 4
		object.inkEffect 	= 3

		CheckCurrentStageFolder("Zone11")
		if checkResult == true
			object.flashPaletteIndex = 196
		else
			object.flashPaletteIndex = 192
		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
		// [Fallthrough]
		
	case WFZEGGMANLASER_ENTERDELAY
		object.timer++
		if object.timer >= 90
			object.timer = 0
			object.state = WFZEGGMANLASER_ENTERBOSS
		end if
		break

	case WFZEGGMANLASER_ENTERBOSS
		object.ypos += 0x4000

		object.timer++
		if object.timer >= 96
			object.timer 	= 0
			object[-1].type = TypeName[Eggman Dispenser]
			object.state 	= WFZEGGMANLASER_MOVING

			if player[0].xpos < object.xpos
				object.xvel = -0x10000
			else
				object.xvel = 0x10000
			end if
		end if
		break

	case WFZEGGMANLASER_MOVING
		object.timer++
		if object.timer > 112
			object.timer = 0
			object.state = WFZEGGMANLASER_OPENSHELL
		else
			object.xpos += object.xvel
			if object.xvel > 0
				if object.xpos >= object.boundsR
					FlipSign(object.xvel)
				end if
			else
				if object.xpos <= object.boundsL
					FlipSign(object.xvel)
				end if
			end if
		end if
		break

	case WFZEGGMANLASER_OPENSHELL
		object.animationTimer++
		if object.animationTimer == 6
			object.animationTimer = 0

			object.frame++
			if object.frame == 3
				object.state = WFZEGGMANLASER_CORESHOWDELAY
			end if
		end if
		break

	case WFZEGGMANLASER_CORESHOWDELAY
		object.timer++
		if object.timer > 26
			object.timer = 0
			object.state = WFZEGGMANLASER_SHOWCORE
		end if
		break

	case WFZEGGMANLASER_SHOWCORE
		object.coreOffset.y += 0x10000

		object.timer++
		if object.timer > 12
			object.timer = 0
			object.state = WFZEGGMANLASER_LASERDELAY
		end if
		break

	case WFZEGGMANLASER_LASERDELAY
		object.timer++
		if object.timer > 60
			object.timer 			= 15
			object.animationTimer 	= 15
			object.laserHeight 		= 8
			object.alpha 			= 0x200
			object.state 			= WFZEGGMANLASER_SETUPLASER
		end if
		break

	case WFZEGGMANLASER_SETUPLASER
		if object.animationTimer > 2
			object.laserFrame = 6
		else
			object.laserFrame = 5
		end if

		object.animationTimer--
		if object.animationTimer == 0
			object.timer--
			if object.timer == 0
				object.state = WFZEGGMANLASER_CHARGELASER
			else
				object.animationTimer = object.timer
				if object.timer == 13
					PlaySfx(SfxName[Laser Charge], false)
				end if
			end if
		end if
		break

	case WFZEGGMANLASER_CHARGELASER
		object.laserFrame -= 5
		object.laserFrame ^= 1
		object.laserFrame += 5

		object.timer++
		if object.timer > 64
			object.timer 		= 0
			object.alpha 		= 0xA0
			object.state 		= WFZEGGMANLASER_FIRINGLASER
			object.laserFrame 	= 5
			PlaySfx(SfxName[Large Laser], false)
		end if
		break

	case WFZEGGMANLASER_FIRINGLASER
		object.laserHeight += 0x20
		if object.laserHeight >= 0x80
			object.alpha 		+= 0x20
			object.laserHeight 	= 167
			object.timer 		= 0

			if player[0].xpos < object.xpos
				object.xvel = -0x10000
			else
				object.xvel = 0x10000
			end if

			object.state = WFZEGGMANLASER_LASERMOVE
		end if
		break

	case WFZEGGMANLASER_LASERMOVE
		object.timer++
		object.alpha--

		if object.timer > 112
			object.timer = 0
			object.state = WFZEGGMANLASER_HIDECORE
		else
			object.xpos += object.xvel
			if object.xvel > 0
				if object.xpos >= object.boundsR
					object.xvel = 0
				end if
			else
				if object.xpos <= object.boundsL
					object.xvel = 0
				end if
			end if
		end if
		break

	case WFZEGGMANLASER_HIDECORE
		object.coreOffset.y	-= 0x10000
		object.alpha 		-= 24

		object.timer++
		if object.timer > 12
			object.timer = 0
			object.animationTimer = 0
			object.state = WFZEGGMANLASER_CLOSESHELL
		end if
		break

	case WFZEGGMANLASER_CLOSESHELL
		object.animationTimer++
		if object.animationTimer == 6
			object.animationTimer = 0

			object.frame--
			if object.frame == 0
				if player[0].xpos < object.xpos
					object.xvel = -0x10000
				else
					object.xvel = 0x10000
				end if
				
				object.state = WFZEGGMANLASER_MOVING
			end if
		end if
		break

	case WFZEGGMANLASER_DESTROYED
		if object.alpha > 0
			object.alpha -= 24
		end if

		object.timer++
		if object.timer >= 180
			object.timer 		= 0
			object.explodeState	= WFZEGGMANLASER_BOSS_DESTROYED
			object.state 		= WFZEGGMANLASER_RESETBOUNDS
		end if
		break

	case WFZEGGMANLASER_RESETBOUNDS
		temp0 = tileLayer[0].ysize
		temp0 <<= 7
		stage.newYBoundary2 = temp0

		temp0 <<= 16
		stage.deathBoundary = temp0
		
#platform: USE_ORIGINS
		if game.oneStageFlag != false
			game.callbackResult = -1
			CallNativeFunction4(NotifyCallback, NOTIFY_STAGE_RETRY, 1, stage.listPos, 0)
			
			// (State 16 doesn't do anything)
			object.state = 16
		end if
#endplatform
		break
		
	end switch

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

		if object.coreOffset.y > 0
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				BoxCollisionTest(C_TOUCH, object.entityPos, -16, 0, 16, 16, currentPlayer, player[currentPlayer].hitboxLeft, player[currentPlayer].hitboxTop, player[currentPlayer].hitboxRight, player[currentPlayer].hitboxBottom)
				if checkResult == true
					if object.invincibilityTimer == 0
						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)
								stage.timeEnabled = false
								CallNativeFunction2(NotifyCallback, NOTIFY_ACT_FINISH, 0)
#endplatform
								
								player.score += 1000
								object.explodeState	= WFZEGGMANLASER_BOSS_EXPLODE
								object.timer 		= 0
								object.state 		= WFZEGGMANLASER_DESTROYED

								object[-4].state 			= WFZEGGMAN_ESCAPE

								wfzEggmanBarrier[-3].timer 	= 240
								wfzEggmanBarrier[-2].timer 	= 240

								wfzEggmanDispenser[-1].exploding	= true
								wfzEggmanDispenser[-1].timer 		= 0
								object[-1].state 					= WFZEGGMANDISPENSER_DESTROYED

								CreateTempObject(TypeName[Explosion], 0, object[+1].xpos, object[+1].ypos)
								object[+1].state = WFZEGGMANPLATFORM_DESTROYED

								CreateTempObject(TypeName[Explosion], 0, object[+2].xpos, object[+2].ypos)
								object[+2].state = WFZEGGMANPLATFORM_DESTROYED

								CreateTempObject(TypeName[Explosion], 0, object[+3].xpos, object[+3].ypos)
								object[+3].state = WFZEGGMANPLATFORM_DESTROYED

								StopSfx(SfxName[Laser Charge])
								StopSfx(SfxName[Large Laser])
							else
								object.invincibilityTimer = 32
								PlaySfx(SfxName[Boss Hit], false)
							end if
						end if
					end if
				else
					if object.state == WFZEGGMANLASER_LASERMOVE
						BoxCollisionTest(C_TOUCH, object.entityPos, -16, 0, 16, 168, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
						if checkResult == true
							CallFunction(Player_Hit)
						end if
					end if
				end if
			next
		end if
	end if

	if object.explodeState == WFZEGGMANLASER_BOSS_EXPLODE
		temp0 = oscillation
		temp0 &= 7
		if temp0 == 0
			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)
			object[tempObjectPos].drawOrder = 5
			PlaySfx(SfxName[Explosion], false)
		end if
	end if
end event


event ObjectDraw
	if object.explodeState < WFZEGGMANLASER_BOSS_DESTROYED
		// Draw Core
		temp1 = object.ypos
		temp1 += object.coreOffset.y
		DrawSpriteXY(4, object.xpos, temp1)

		// Draw Shell
		DrawSprite(object.frame)

		// Draw Laser
		temp0 = 168
		temp0 -= object.laserHeight
		EditFrame(5, -16, 0, 32, object.laserHeight, EggmanLaser_laserSheetX, temp0)
		DrawSpriteFX(object.laserFrame, FX_INK, object.xpos, temp1)
	end if
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone11")
	if checkResult == true
		LoadSpriteSheet("SCZ/Objects.gif")
		SpriteFrame(-32, -8, 64, 23, 311, 1)  	// Shell - Frame 1 	- #0
		SpriteFrame(-32, -8, 64, 22, 311, 25)	// Shell - Frame 2 	- #1
		SpriteFrame(-32, -8, 64, 19, 311, 48)	// Shell - Frame 3 	- #2
		SpriteFrame(-32, -8, 64, 13, 311, 68)	// Shell - Frame 4 	- #3
		SpriteFrame(-16, -8, 32, 16, 376, 1)	// Core 			- #4
		SpriteFrame(-16, 0, 32, 168, 261, 1)	// Laser Beam 		- #5
		SpriteFrame(0, 0, 1, 1, 261, 168)		// Laser Bottom 	- #6
		EggmanLaser_laserSheetX = 261
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-32, -8, 64, 23, 513, 139)	// Shell - Frame 1 	- #0
		SpriteFrame(-32, -8, 64, 22, 578, 139)	// Shell - Frame 2 	- #1
		SpriteFrame(-32, -8, 64, 19, 643, 139)	// Shell - Frame 3 	- #2
		SpriteFrame(-32, -8, 64, 13, 578, 162)	// Shell - Frame 4 	- #3
		SpriteFrame(-16, -8, 32, 16, 397, 1)	// Core 			- #4
		SpriteFrame(-16, 0, 32, 168, 480, 1)	// Laser Beam 		- #5
		SpriteFrame(0, 0, 1, 1, 480, 168)		// Laser Bottom 	- #6
		EggmanLaser_laserSheetX = 480
	end if
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone11")
	if checkResult == true
		LoadSpriteSheet("SCZ/Objects.gif")
		SpriteFrame(-32, -8, 64, 23, 311, 1)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-32, -8, 64, 23, 513, 139)
	end if
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
