// ----------------------------------
// RSDK Project: Sonic 1
// Script Description: MotobugEX 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.startPos.x
private alias object.value2 : object.startPos.y
private alias object.value3 : object.exhaustTimer

private alias 0 : MotobugEX_FALLING
private alias 1 : MotobugEX_MOVING
private alias 2 : MotobugEX_PAUSED

private alias 0 : MotobugEX_ANI_MOVING
private alias 1 : MotobugEX_ANI_IDLE

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

// Path ID Aliases
private alias 0 : PATH_A


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

reserve function MotobugEX_DebugDraw
reserve function MotobugEX_DebugSpawn


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

private function MotobugEX_DebugDraw
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end function


private function MotobugEX_DebugSpawn
	CreateTempObject(TypeName[MotobugEX], 0, object.xpos, object.ypos)
	GetBit(temp0, object.direction, 0)
	if temp0 == FLIP_NONE
		object[tempObjectPos].direction = FLIP_NONE
		object[tempObjectPos].xvel = -0x10000
	else
		object[tempObjectPos].direction = FLIP_X
		object[tempObjectPos].xvel = 0x10000
	end if
end function


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

event ObjectUpdate
	switch object.state
	case MotobugEX_FALLING
		object.xpos += object.xvel
		ObjectTileGrip(CSIDE_FLOOR, 0, 13, PATH_A)
		if checkResult == false
			object.ypos += object.yvel
    		object.yvel += 0x3800
		else
			object.state = MotobugEX_MOVING
		end if
		break
	case MotobugEX_MOVING
		object.priority = PRIORITY_ACTIVE

		object.xpos += object.xvel

		ObjectTileGrip(CSIDE_FLOOR, 0, 13, PATH_A)
		if checkResult == false
			object.state = MotobugEX_FALLING
		end if

		// Check object collision
		ObjectTileGrip(CSIDE_FLOOR, 0, 15, PATH_A)
		if checkResult == false
			// If on the edge, then pause to turn around
			object.state = MotobugEX_PAUSED
			object.animation = MotobugEX_ANI_IDLE
		end if


		// Update exhaust process
		if object.exhaustTimer < 20
			object.exhaustTimer++
		else
			object.exhaustTimer = 0

			// Create the exhaust object
			CreateTempObject(TypeName[MotobugEX Exhaust], 0, object.xpos, object.ypos)
			
			// Offset the exhaust based on the MotobugEX's direction
			if object.direction == FLIP_NONE
				object[tempObjectPos].xpos += 0x160000
			else
				object[tempObjectPos].xpos -= 0x160000
			end if

			// Set the exhaust to match the MotobugEX's direction
			object[tempObjectPos].direction = object.direction
		end if
		break

	case MotobugEX_PAUSED
		if object.timer < 59
			// Stay still for a moment...
			object.timer++
		else
			// Start moving again
			object.timer = 0

			object.state = MotobugEX_MOVING
			object.animation = MotobugEX_ANI_MOVING

			// Flip the MotobugEX
			object.direction ^= FLIP_X
			FlipSign(object.xvel)
		end if
		break

	end switch

	if object.outOfBounds == true
		temp0 = object.xpos
		temp1 = object.ypos

		object.xpos = object.startPos.x
		object.ypos = object.startPos.y

		if object.outOfBounds == true
			if object.propertyValue == 0
				object.direction = FLIP_NONE
				object.xvel = -0x10000
			else
				object.direction = FLIP_X
				object.xvel = 0x10000
			end if

			object.animation = MotobugEX_ANI_MOVING
			object.priority = PRIORITY_BOUNDS
			object.state = MotobugEX_MOVING
		else
			object.xpos = temp0
			object.ypos = temp1
		end if
	end if

	// Check collision with players
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -14, -14, 14, 14, currentPlayer, player[currentPlayer].hitboxLeft, player[currentPlayer].hitboxTop, player[currentPlayer].hitboxRight, player[currentPlayer].hitboxBottom)
		if checkResult == true
			CallFunction(Player_BadnikBreak)
		end if
	next

	if object.animation == MotobugEX_ANI_MOVING
		object.frame = object.animationTimer
		object.frame >>= 3
		object.animationTimer++
		object.animationTimer &= 31
	end if
end event


event ObjectDraw
	DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
end event


event ObjectStartup

		// MotobugEX in GHZ (or a mission) - load GHZ frames
		LoadSpriteSheet("GHZ/Objects.gif")
		SpriteFrame(-21, -13, 40, 28, 98, 127)
		SpriteFrame(-20, -14, 39, 29, 139, 127)
		SpriteFrame(-20, -13, 39, 28, 98, 156)
		SpriteFrame(-20, -14, 39, 29, 139, 127)
	

	// Setup all MotobugEXs
	foreach (TypeName[MotobugEX], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.x = object[arrayPos0].xpos
		object[arrayPos0].startPos.y = object[arrayPos0].ypos

		// Set their direction based on their propertyValue
		if object[arrayPos0].propertyValue == 0
			object[arrayPos0].direction = FLIP_NONE
			object[arrayPos0].xvel = -0x10000
		else
			object[arrayPos0].direction = FLIP_X
			object[arrayPos0].xvel = 0x10000
		end if
	next

	// Add MotobugEX to the debug item list
	SetTableValue(TypeName[MotobugEX], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(MotobugEX_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(MotobugEX_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

event RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			checkResult = object.propertyValue
			checkResult &= 1
			break
			
		case 0 // direction
			checkResult = object.propertyValue
			checkResult &= 1
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			object.propertyValue &= 1
			break
			
		case 0 // direction
			object.propertyValue = editor.variableValue
			object.propertyValue &= 1
			break
			
		end switch
	end if
end event


event RSDKDraw
	GetBit(object.direction, object.propertyValue, 0)
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end event


event RSDKLoad
	// Check which sprites to load based on current folder
	

		LoadSpriteSheet("GHZ/Objects.gif")
		SpriteFrame(-21, -13, 40, 28, 98, 127)


	AddEditorVariable("direction")
	SetActiveVariable("direction")
	AddEnumVariable("Left", 0)
	AddEnumVariable("Right", 1)
end event
