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

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

// Variables
private alias object.value0 : object.animTimerIncrease
private alias object.value1 : object.frameIncrease
private alias object.value2 : object.initialTrackSpeed
private alias object.value3 : object.initialExtraSpeed
private alias object.value4 : object.eggmanID
private alias object.value5 : object.newXBounds
private alias object.value6 : object.signPostID

// States
private alias 0 : EGGMANSIGNPOST_AWAITEGGMAN
private alias 1 : EGGMANSIGNPOST_AWAITBOSSFIGHT
private alias 2 : EGGMANSIGNPOST_SPEEDUPBOSS
private alias 3 : EGGMANSIGNPOST_DEFEATFALL
private alias 4 : EGGMANSIGNPOST_ESCAPE
private alias 5 : EGGMANSIGNPOST_SPAWNFALLSIGNPOST
private alias 6 : EGGMANSIGNPOST_FINDFALLSIGNPOST
private alias 7 : EGGMANSIGNPOST_MISSIONCOMPLETE
private alias 8 : EGGMANSIGNPOST_FINISHED

// Eggman States
private alias 0 : CPZEGGMAN_AWAITPLAYER
private alias 1 : CPZEGGMAN_BOSSFIGHT
private alias 3 : CPZEGGMAN_DEFEATFALL
private alias 6 : CPZEGGMAN_ESCAPE

// Tipping Floor States
private alias 1 : TIPPINGFLOOR_TIPPING

// Fall Sign Post States
private alias 1 : FALLSIGNPOST_FALLTOGROUND
private alias 2 : FALLSIGNPOST_SPINNINGONGROUND
private alias 4 : FALLSIGNPOST_LANDONGROUND

// Player Aliases
private alias object.state 			: player.state
private alias object.speed 			: player.speed
private alias object.ixpos 			: player.ixpos
private alias object.xvel 			: player.xvel
private alias object.yvel 			: player.yvel
private alias object.animation 		: player.animation
private alias object.controlMode 	: player.controlMode
private alias object.interaction 	: player.interaction
private alias object.outOfBounds 	: player.outOfBounds
private alias object.jumpPress 		: player.jumpPress
private alias object.jumpHold 		: player.jumpHold
private alias object.up 			: player.up
private alias object.down 			: player.down
private alias object.left 			: player.left
private alias object.right 			: player.right

// Chemical Dropper Aliases
private alias object.value14 : object.dropletAnimTimer
private alias object.value15 : object.chemicalFrame
private alias object.value19 : object.dropTrackSpeed
private alias object.value20 : object.extraSpeed

// Tipping Floor Aliases
private alias object.value0 : object.timer
private alias object.value3 : object.bossEnd

// Fall Sign Post Aliases
private alias object.value1 : object.spinCount


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

event ObjectUpdate
	switch object.state
	case EGGMANSIGNPOST_AWAITEGGMAN
		if object.eggmanID == 0
			foreach (TypeName[Eggman], arrayPos0, ACTIVE_ENTITIES)
				object.eggmanID = arrayPos0
			next
			
			if object.eggmanID != 0
				object.state++
				if object.animTimerIncrease < 0
					object.animTimerIncrease = 0
				else
					if object.animTimerIncrease > 18
						object.animTimerIncrease = 18
					end if
				end if
				
				if object.frameIncrease < 0
					object.frameIncrease = 0
				else
					if object.frameIncrease > 12
						object.frameIncrease = 12
					end if
				end if
				
				if object.initialTrackSpeed < 0
					object.initialTrackSpeed = 0
				end if
				
				if object.initialExtraSpeed < 0
					object.initialExtraSpeed = 0
				end if
			end if
		end if
		break
		
	case EGGMANSIGNPOST_AWAITBOSSFIGHT
		arrayPos0 = object.eggmanID
		if object[arrayPos0].state == CPZEGGMAN_BOSSFIGHT
			arrayPos0++
			object[arrayPos0].dropTrackSpeed = object.initialTrackSpeed
			object[arrayPos0].dropTrackSpeed <<= 16
			object[arrayPos0].dropTrackSpeed += 0x10000
			
			object[arrayPos0].extraSpeed = object.initialExtraSpeed
			object[arrayPos0].extraSpeed <<= 16
			object[arrayPos0].extraSpeed += 0x30000
			object.state++
		end if
		break
		
	case EGGMANSIGNPOST_SPEEDUPBOSS
		arrayPos0 = object.eggmanID
		if object[arrayPos0].state == CPZEGGMAN_BOSSFIGHT
			arrayPos1 = object.eggmanID
			arrayPos1++
			if object[arrayPos1].dropletAnimTimer == 0
				object[arrayPos1].dropletAnimTimer = object.animTimerIncrease
				temp0 = object[arrayPos1].chemicalFrame
				temp0 += object.frameIncrease
				if temp0 >= 30
					object[arrayPos1].chemicalFrame = 30
				end if
			end if
		else
			object.type = TypeName[Blank Object]
		end if
		break
		
	case EGGMANSIGNPOST_DEFEATFALL
		arrayPos0 = object.eggmanID
		if object[arrayPos0].state == CPZEGGMAN_DEFEATFALL
			stage.newXBoundary2 = object.newXBounds
			object.state++
		end if
		break
		
	case EGGMANSIGNPOST_ESCAPE
		arrayPos0 = object.eggmanID
		if object[arrayPos0].state == CPZEGGMAN_ESCAPE
			stage.newXBoundary2 = object.newXBounds
			object.state++
		end if
		break
		
	case EGGMANSIGNPOST_SPAWNFALLSIGNPOST
		arrayPos0 = object.eggmanID
		if object[arrayPos0].type == TypeName[Blank Object]
			game.missionCondition = MISSION_CONDITION_CLEAR
			CreateTempObject(TypeName[FallSignPost], 0, 0, 0)
			object[tempObjectPos].frame = 1
			object[tempObjectPos].spinCount = 16
			object[tempObjectPos].priority = PRIORITY_ACTIVE
			object.state++
			object.priority = PRIORITY_ACTIVE
		end if
		break
		
	case EGGMANSIGNPOST_FINDFALLSIGNPOST
		foreach (TypeName[FallSignPost], arrayPos0, ACTIVE_ENTITIES)
			object.signPostID = arrayPos0
		next
		object.state++
		break
		
	case EGGMANSIGNPOST_MISSIONCOMPLETE
		arrayPos2 = 0
		foreach (TypeName[Tipping Floor], arrayPos0, ACTIVE_ENTITIES)
			if object[arrayPos0].bossEnd != false
				object[arrayPos0].state = TIPPINGFLOOR_TIPPING
				object[arrayPos0].timer = 1
				object[arrayPos0].animation = 1
				object[arrayPos0].frame = 0
			else
				if object[arrayPos0].frame == 0
					object[arrayPos0].state = TIPPINGFLOOR_TIPPING
					object[arrayPos0].timer = 1
					object[arrayPos0].animation = 1
					object[arrayPos0].bossEnd = true
				end if
			end if
			
			// Provide Platform collision between the Tipping Floor and the player, regardless of the Floor's actual frame so that the player doesn't fall and die post-boss
			BoxCollisionTest(C_PLATFORM, object[arrayPos0].entityPos, -16, -16, 16, -8, 0, C_BOX, C_BOX, C_BOX, C_BOX)
			arrayPos2 = arrayPos0
		next
		
		if object.signPostID != 0
			arrayPos1 = object.signPostID
			if object[arrayPos1].type == TypeName[FallSignPost]
				if object[arrayPos1].state < FALLSIGNPOST_SPINNINGONGROUND
					temp0 = 48
					temp0 <<= 16
					temp0 += object[arrayPos1].ypos
					if object[arrayPos2].ypos <= temp0
						if object[arrayPos1].state == FALLSIGNPOST_FALLTOGROUND
							object[arrayPos1].state = FALLSIGNPOST_LANDONGROUND
						end if
					end if
				end if
			else
				arrayPos0 = object.signPostID
				CheckEqual(player[0].state, Player_State_Ground)
				temp0 = checkResult
				CheckEqual(player[0].state, Player_State_Static)
				temp0 |= checkResult
				CheckEqual(player[0].outOfBounds, true)
				temp0 |= checkResult
				temp1 = screen.xoffset
				temp1 += screen.xsize
				temp1 += 32
				CheckGreater(player[0].ixpos, temp1)
				temp0 |= checkResult
				CheckGreater(object[arrayPos0].state, FALLSIGNPOST_FALLTOGROUND)
				temp0 &= checkResult
				
				if temp0 == true
					SetBit(object[arrayPos0].value3, 0, false)
					player[0].state = Player_State_Static
					player[0].controlMode = CONTROLMODE_NONE
					player[0].interaction = false
					player[0].up = false
					player[0].down = false
					player[0].left = false
					player[0].right = false
					player[0].jumpHold = false
					player[0].jumpPress = false
					player[0].xvel = 0
					player[0].yvel = 0
					player[0].speed = 0
					player[0].animation = ANI_WAITING
					screen.cameraEnabled = false
				end if
			end if
		end if
		break
		
	case EGGMANSIGNPOST_FINISHED
		break
		
	end switch
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
	
	// TODO: val0, val1, val2, val3, they're all set from editor so they prolly do something
	// (m049)
	// it looks like they're used for controlling robotnik's speed and stuff? that's honestly a really cool way to do it, props to them
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
