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

// Using DEZEggman as a prefix here because there's like 12 "Eggman" objects in the game so its less confusing this way
// (and this object is called "DEZ Eggman" in MBZ)

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

private alias object.value0 : object.sweat.x
private alias object.value1 : object.sweat.y
private alias object.value2 : object.sweat.yvel
private alias object.value3 : object.sweat.visible
private alias object.value4 : object.sweat.timer

private alias 0 : DEZEGGMAN_AWAITPLAYER
private alias 1 : DEZEGGMAN_GRIMACING
private alias 2 : DEZEGGMAN_RUNNING
private alias 3 : DEZEGGMAN_DERJUMP

// Reserved object slots
private alias 0  : SLOT_PLAYER1

// One Way Door aliases
private alias object.state : oneWayDoor.isOpen


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

event ObjectUpdate
	switch object.state
	case DEZEGGMAN_AWAITPLAYER
		oneWayDoor[-1].isOpen = false
		
		// Check to see if Sonic (or whoever P1 is) is approaching
		BoxCollisionTest(C_TOUCH, object.entityPos, -92, -92, 0, 92, 0, 1, 1, 1, 1)
		if checkResult == true
			object.frame = 1
			object.state = DEZEGGMAN_GRIMACING
		end if
		break

	case DEZEGGMAN_GRIMACING
		oneWayDoor[-1].isOpen = false
		object.animationTimer++
		if object.animationTimer >= 24
			object.state = DEZEGGMAN_RUNNING
		end if
		break

	case DEZEGGMAN_RUNNING
		if object[SLOT_PLAYER1].xpos < object[+1].xpos
			oneWayDoor[+1].isOpen = true
		end if

		// Constant speed of at least 2 pixels a frame
		object.xpos += 0x20000

		// Make sure to always stay a few steps ahead of that pesky hedgehog
		temp0 = object.xpos
		temp0 -= 0x500000
		if object[SLOT_PLAYER1].xpos > temp0
			object.xpos = object[SLOT_PLAYER1].xpos
			object.xpos += 0x500000
		end if

		// Animate the skidaddling
		object.animationTimer++
		if object.animationTimer >= 6
			object.animationTimer = 0
			object.frame++
			if object.frame > 4
				object.frame = 2
			end if
		end if

		object.sweat.timer++
		switch object.sweat.timer
		case 1
			object.sweat.x = object.xpos
			object.sweat.x -= 0x70000
			object.sweat.y = object.ypos
			object.sweat.y -= 0x180000
			object.sweat.yvel 		= 0
			object.sweat.visible 	= true
			break

		case 2
		case 3
		case 4
		case 5
		case 6
		case 7
		case 8
			object.sweat.x -= 0x10000
			object.sweat.yvel += 0x1C00
			object.sweat.y += object.sweat.yvel
			break

		case 9
			object.sweat.visible = false
			break

		case 32
			object.sweat.timer = 0
			break
			
		end switch

		// Check if arrived at the Death Egg Robot yet
		// (Object [+9] is DER)
		temp0 = object[+9].xpos
		temp0 -= object.xpos
		if temp0 <= 0x2C0000
			// Hop in to the Death Egg Robot
			object.xpos = object[+9].xpos
			object.xpos -= 0x2C0000
			object.yvel = -0x20000
			object.frame = 2
			object.sweat.visible = false
			object.state = DEZEGGMAN_DERJUMP
		end if
		break

	case DEZEGGMAN_DERJUMP
		object.xpos += 0xA000
		object.ypos += object.yvel
		object.yvel += 0x1000
		if object.ypos >= object[+9].ypos
			// Robotnik's no longer visible at all, so start up the Death Egg Robot and make this object dissappear
			PlaySfx(SfxName[Machine Move], false)
			object[+9].state = 2
			object.type = TypeName[Blank Object]
		end if
		break

	end switch
end event


event ObjectDraw
	if object.sweat.visible == true
		DrawSpriteXY(5, object.sweat.x, object.sweat.y)
	end if

	DrawSprite(object.frame)
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone12")
	if checkResult == true
		LoadSpriteSheet("DEZ/Objects.gif")

		// 0 - Idle Frame
		SpriteFrame(-16, -25, 32, 51, 1, 58)

		// 1 - Panic Frame
		SpriteFrame(-16, -26, 32, 52, 34, 58)

		// 2-4 - Running Frames
		SpriteFrame(-30, -27, 58, 43, 67, 58)
		SpriteFrame(-30, -27, 46, 55, 126, 58)
		SpriteFrame(-30, -27, 51, 52, 173, 58)

		// 5 - Sweat Frame
		SpriteFrame(-5, -2, 12, 5, 256, 52)

		// 6-7 - Laughing Frames
		// (I don't think these are ever actually used?)
		SpriteFrame(-16, -26, 32, 52, 225, 58)
		SpriteFrame(-16, -25, 32, 51, 258, 58)
	else
		LoadSpriteSheet("MBZ/Objects.gif")

		// 0 - Idle Frame
		SpriteFrame(-16, -25, 32, 51, 513, 58)

		// 1 - Panic Frame
		SpriteFrame(-16, -26, 32, 52, 546, 58)

		// 2-4 - Running Frames
		SpriteFrame(-30, -27, 58, 43, 579, 58)
		SpriteFrame(-30, -27, 46, 55, 638, 58)
		SpriteFrame(-30, -27, 51, 52, 685, 58)

		// 5 - Sweat Frame
		SpriteFrame(-5, -2, 12, 5, 768, 52)

		// 6-7 - Laughing Frames
		// (I don't think these are ever actually used?)
		SpriteFrame(-16, -26, 32, 52, 737, 58)
		SpriteFrame(-16, -25, 32, 51, 770, 58)
	end if
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone12")
	if checkResult == true
		LoadSpriteSheet("DEZ/Objects.gif")
		SpriteFrame(-16, -25, 32, 51, 1, 58)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-16, -25, 32, 51, 513, 58)
	end if
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
