// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Grounder 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.targetPlayer
private alias object.value4 : object.targetPlayerDistance

// States
private alias 0 : GROUNDER_HIDDEN
private alias 1 : GROUNDER_APPEAR
private alias 2 : GROUNDER_MOVING
private alias 3 : GROUNDER_TURNING

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

private alias object.value17 : debugMode.currentSelection

// Path ID Aliases
private alias 0 : PATH_A


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

reserve function Grounder_DebugDraw
reserve function Grounder_DebugSpawn


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

private value Grounder_startDebugID = 0


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

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


private function Grounder_DebugSpawn
	temp0 = debugMode[0].currentSelection
	temp0 -= Grounder_startDebugID

	CreateTempObject(TypeName[Grounder], temp0, object.xpos, object.ypos)
	object[tempObjectPos].startPos.x = object.xpos
	object[tempObjectPos].startPos.y = object.ypos
end function


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

event ObjectUpdate
	switch object.state
	case GROUNDER_HIDDEN
		object.priority = PRIORITY_ACTIVE
		if object.propertyValue == 1
			ObjectTileGrip(CSIDE_FLOOR, 0, 20, PATH_A)
			object.state = GROUNDER_APPEAR
			object.timer = 17
		end if

		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -96, -256, 96, 256, currentPlayer, -1, -1, 1, 1)
			if checkResult == true
				CreateTempObject(TypeName[Brick], 1, object.xpos, object.ypos)
				object[tempObjectPos].xvel = -0x10000
				object[tempObjectPos].yvel = -0x40000

				CreateTempObject(TypeName[Brick], 3, object.xpos, object.ypos)
				object[tempObjectPos].xvel = 0x40000
				object[tempObjectPos].yvel = -0x30000

				CreateTempObject(TypeName[Brick], 1, object.xpos, object.ypos)
				object[tempObjectPos].xvel = 0x20000
				object[tempObjectPos].yvel = 0

				CreateTempObject(TypeName[Brick], 2, object.xpos, object.ypos)
				object[tempObjectPos].xvel = -0x30000
				object[tempObjectPos].yvel = -0x10000

				CreateTempObject(TypeName[Brick], 1, object.xpos, object.ypos)
				object[tempObjectPos].xvel = -0x30000
				object[tempObjectPos].yvel = -0x30000

				CreateTempObject(TypeName[Brick], 0, object.xpos, object.ypos)
				object[tempObjectPos].ypos -= 0x140000
				object[tempObjectPos].xvel = 0x10000
				object[tempObjectPos].yvel = -0x20000

				CreateTempObject(TypeName[Brick], 0, object.xpos, object.ypos)
				object[tempObjectPos].xpos += 0x100000
				object[tempObjectPos].ypos -= 0x40000
				object[tempObjectPos].xvel = 0x10000
				object[tempObjectPos].yvel = -0x10000

				CreateTempObject(TypeName[Brick], 0, object.xpos, object.ypos)
				object[tempObjectPos].ypos += 0xC0000
				object[tempObjectPos].xvel = -0x10000
				object[tempObjectPos].yvel = -0x20000

				CreateTempObject(TypeName[Brick], 0, object.xpos, object.ypos)
				object[tempObjectPos].xpos -= 0x100000
				object[tempObjectPos].ypos -= 0x40000
				object[tempObjectPos].xvel = -0x10000
				object[tempObjectPos].yvel = -0x10000

				ObjectTileGrip(CSIDE_FLOOR, 0, 20, PATH_A)
				object.state = GROUNDER_APPEAR
			end if
		next
		break

	case GROUNDER_APPEAR
		object.timer++
		if object.timer == 8
			object.frame = 1
		end if

		if object.timer == 18
			object.timer = 0
			object.frame = 2
			object.state = GROUNDER_MOVING

			object.targetPlayerDistance = 0x7FFFFFFF
			object.targetPlayer = 0
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				temp0 = player[currentPlayer].xpos
				temp0 -= object.xpos
				if temp0 < 0
					temp1 = FLIP_NONE
					FlipSign(temp0)
				else
					temp1 = FLIP_X
				end if

				if temp0 < object.targetPlayerDistance
					object.targetPlayerDistance = temp0
					object.targetPlayer 		= currentPlayer
					object.direction 			= temp1
				end if
			next

			if object.direction == FLIP_NONE
				object.xvel = -0x10000
			else
				object.xvel = 0x10000
			end if
		end if
		break

	case GROUNDER_MOVING
		object.xpos += object.xvel

		temp0 = object.ypos
		temp1 = object.ypos
		ObjectTileGrip(CSIDE_FLOOR, 0, 20, PATH_A)
		temp2 = checkResult
		temp1 -= object.ypos

		if temp1 <= -0xC0000
			object.ypos = temp0
			temp2 = 0
		end if

		if temp1 > 0x10000
			object.ypos = temp0
			temp2 = 0
		end if

		if temp2 == 0
			object.timer = 60
			object.state = GROUNDER_TURNING
		end if

		object.animationTimer++
		if object.animationTimer == 12
			object.animationTimer = 0
		end if
		object.frame = object.animationTimer
		object.frame >>= 2
		object.frame += 2
		break

	case GROUNDER_TURNING
		object.timer--
		if object.timer == 0
			object.direction ^= FLIP_X
			FlipSign(object.xvel)
			object.state = GROUNDER_MOVING
		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
			object.state = GROUNDER_HIDDEN
			object.timer = 0
			object.animationTimer = 0
			object.frame = 0
			object.priority = PRIORITY_BOUNDS
		else
			object.xpos = temp0
			object.ypos = temp1
		end if
	end if

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -12, -20, 12, 20, currentPlayer, player[currentPlayer].hitboxLeft, player[currentPlayer].hitboxTop, player[currentPlayer].hitboxRight, player[currentPlayer].hitboxBottom)
		if checkResult == true
			CallFunction(Player_BadnikBreak)
		end if
	next
end event


event ObjectDraw
	if object.state == GROUNDER_HIDDEN
		temp0 = object.xpos
		temp1 = object.ypos

		temp1 -= 0x140000
		DrawSpriteFX(5, FX_FLIP, temp0, temp1)

		temp1 += 0x100000
		temp0 -= 0x100000
		DrawSpriteFX(5, FX_FLIP, temp0, temp1)

		temp0 += 0x200000
		DrawSpriteFX(5, FX_FLIP, temp0, temp1)

		temp1 += 0x100000
		temp0 -= 0x100000
		DrawSpriteFX(5, FX_FLIP, temp0, temp1)
	else
		DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
	end if
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone03")
	if checkResult == true
		LoadSpriteSheet("ARZ/Objects.gif")
		
		// Emerging From Wall Frames
		SpriteFrame(-14, -12, 28, 32, 133, 1)
		SpriteFrame(-16, -16, 32, 36, 162, 1)
		
		// Normal Frames
		SpriteFrame(-16, -20, 32, 40, 34, 1)
		SpriteFrame(-16, -20, 32, 40, 67, 1)
		SpriteFrame(-16, -20, 32, 40, 100, 1)
		
		// Brick Cover Frame
		SpriteFrame(-16, -8, 32, 16, 18, 128)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		
		// Emerging From Wall Frames
		SpriteFrame(-14, -12, 28, 32, 374, 256)
		SpriteFrame(-16, -16, 32, 36, 403, 256)
		
		// Normal Frames
		SpriteFrame(-16, -20, 32, 40, 275, 256)
		SpriteFrame(-16, -20, 32, 40, 308, 256)
		SpriteFrame(-16, -20, 32, 40, 341, 256)
		
		// Brick Cover Frame
		SpriteFrame(-16, -8, 32, 16, 436, 306)
	end if

	foreach (TypeName[Grounder], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.x = object[arrayPos0].xpos
		object[arrayPos0].startPos.y = object[arrayPos0].ypos
	next

	temp0 = 0
	Grounder_startDebugID = DebugMode_ObjCount
	Grounder_startDebugID--
	while temp0 < 2
		SetTableValue(TypeName[Grounder], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(Grounder_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(Grounder_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
end event


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

event RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			checkResult = object.propertyValue
			break
			
		case 0 // type
			GetBit(checkResult, object.propertyValue, 0)
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // type
			CheckNotEqual(editor.variableValue, 0)
			SetBit(object.propertyValue, 0, checkResult)
			break
			
		end switch
	end if
end event


event RSDKDraw
	object.frame = object.propertyValue
	object.frame <<= 1

	DrawSprite(object.frame)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone03")
	if checkResult == true
		LoadSpriteSheet("ARZ/Objects.gif")
		SpriteFrame(-14, -12, 28, 32, 133, 1)
		SpriteFrame(-16, -16, 32, 36, 162, 1)
		SpriteFrame(-16, -20, 32, 40, 34, 1)
		SpriteFrame(-16, -20, 32, 40, 67, 1)
		SpriteFrame(-16, -20, 32, 40, 100, 1)
		SpriteFrame(-16, -8, 32, 16, 18, 128)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-14, -12, 28, 32, 374, 256)
		SpriteFrame(-16, -16, 32, 36, 403, 256)
		SpriteFrame(-16, -20, 32, 40, 275, 256)
		SpriteFrame(-16, -20, 32, 40, 308, 256)
		SpriteFrame(-16, -20, 32, 40, 341, 256)
		SpriteFrame(-16, -8, 32, 16, 436, 306)
	end if

	AddEditorVariable("type")
	SetActiveVariable("type")
	AddEnumVariable("Start Hidden", 0)
	AddEnumVariable("Not Hidden", 1)
end event
