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

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

private alias object.value0 : object.startPos.x
private alias object.value1 : object.startPos.y
private alias object.value2 : object.backupPlayer
private alias object.value3 : object.targetPlayer
private alias object.value4 : object.targetDistance
private alias object.value5 : object.timer
private alias object.value6 : object.unused // it's set, but never actually *used*

private alias 0 : BURROBOT_AWAITPLAYER
private alias 1 : BURROBOT_JUMPED
private alias 2 : BURROBOT_MOVING
private alias 3 : BURROBOT_STOPPED

private alias 0 : BURROBOT_ANI_HIDDEN
private alias 1 : BURROBOT_ANI_JUMPED
private alias 2 : BURROBOT_ANI_MOVING
private alias 3 : BURROBOT_ANI_STOPPED

// Player Aliases
private alias object.xpos : player.xpos
private alias object.ypos : player.ypos

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 Burrobot_DebugDraw
reserve function Burrobot_DebugSpawn


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

private function Burrobot_DebugDraw
	DrawSpriteFX(3, FX_FLIP, object.xpos, object.ypos)
end function


private function Burrobot_DebugSpawn
	CreateTempObject(TypeName[Burrobot], 0, object.xpos, object.ypos)
	object[tempObjectPos].direction = object.direction
	if object[tempObjectPos].direction == FLIP_NONE
		object[tempObjectPos].xvel = -0x8000
	else
		object[tempObjectPos].xvel = 0x8000
	end if
	object[tempObjectPos].startPos.x = object.xpos
	object[tempObjectPos].startPos.y = object.ypos
	object[tempObjectPos].yvel = -0x40000
	object[tempObjectPos].animation = BURROBOT_ANI_JUMPED
	object[tempObjectPos].state = BURROBOT_JUMPED
end function


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

event ObjectUpdate
	switch object.state
	case BURROBOT_AWAITPLAYER
		object.targetDistance = 0x7FFFFFFF
		object.backupPlayer = 0
		object.targetPlayer = 0xFFFF
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			temp0 = player[currentPlayer].xpos
			temp0 -= object.xpos
			Abs(temp0)
			if temp0 < object.targetDistance
				object.targetDistance = temp0
				object.backupPlayer = currentPlayer
			end if
			if temp0 < 0x600000
				if player[currentPlayer].ypos <= object.ypos
					if object.targetPlayer != 0xFFFF
						arrayPos0 = object.targetPlayer
						if player[currentPlayer].ypos < player[arrayPos0].ypos
							object.targetPlayer = currentPlayer
						end if
					else
						object.targetPlayer = currentPlayer
					end if
				end if
			end if
		next

		if object.targetPlayer != 0xFFFF
			arrayPos0 = object.targetPlayer
			temp0 = object.ypos
			temp0 -= player[arrayPos0].ypos
			if temp0 >= 0
				if temp0 <= 0x800000
					Rand(temp0, 8)
					if temp0 == 0
						object.state = BURROBOT_JUMPED
						object.priority = PRIORITY_ACTIVE
						if player[arrayPos0].xpos < object.xpos
							object.direction = FLIP_NONE
							object.xvel = -0x8000
						else
							object.direction = FLIP_X
							object.xvel = 0x8000
						end if
						object.yvel = -0x40000
						object.animation = BURROBOT_ANI_JUMPED
					end if
				end if
			end if
		else
			arrayPos0 = object.backupPlayer
			if player[arrayPos0].xpos < object.xpos
				object.direction = FLIP_NONE
			else
				object.direction = FLIP_X
			end if
		end if
		break

	case BURROBOT_JUMPED
		object.xpos += object.xvel
		object.ypos += object.yvel
		object.yvel += 0x1800

		if object.yvel > 0
			ObjectTileCollision(CSIDE_FLOOR, 0, 20, PATH_A)
			if checkResult == true
				arrayPos0 = object.backupPlayer
				if player[arrayPos0].xpos < object.xpos
					object.direction = FLIP_NONE
				else
					object.direction = FLIP_X
				end if

				object.timer = 256
				object.animation = BURROBOT_ANI_MOVING
				object.unused = false
				object.state = BURROBOT_MOVING
			end if
		end if
		break

	case BURROBOT_MOVING
		object.xpos += object.xvel

		temp0 = false
		object.timer--
		if object.timer == 0
			temp0 = true
		end if

		if object.direction == FLIP_NONE
			ObjectTileCollision(CSIDE_FLOOR, -12, 21, PATH_A)
			if checkResult == false
				ObjectTileCollision(CSIDE_FLOOR, -12, 19, PATH_A)
				if checkResult == false
					ObjectTileCollision(CSIDE_FLOOR, -12, 20, PATH_A)
				end if
			end if
		else
			ObjectTileCollision(CSIDE_FLOOR, 12, 21, PATH_A)
			if checkResult == false
				ObjectTileCollision(CSIDE_FLOOR, 12, 19, PATH_A)
				if checkResult == false
					ObjectTileCollision(CSIDE_FLOOR, 12, 20, PATH_A)
				end if
			end if
		end if

		if checkResult == false
			temp0 = true
		end if

		ObjectTileGrip(CSIDE_FLOOR, 0, 20, PATH_A)
		if temp0 == true
			GetBit(temp0, oscillation, 2)
			if temp0 == false
				object.yvel = -0x40000
				object.animation = BURROBOT_ANI_JUMPED
				object.state = BURROBOT_JUMPED
			else
				object.timer = 60
				object.animation = BURROBOT_ANI_STOPPED
				object.state = BURROBOT_STOPPED
			end if
		end if
		break

	case BURROBOT_STOPPED
		object.timer--
		if object.timer == 0
			FlipSign(object.xvel)
			if object.xvel < 0
				object.direction = FLIP_NONE
			else
				object.direction = FLIP_X
			end if
			object.timer = 256
			object.animation = BURROBOT_ANI_MOVING
			object.state = BURROBOT_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.xpos = object.startPos.x
			object.ypos = object.startPos.y
			object.xvel = 0
			object.yvel = 0
			object.animation = BURROBOT_ANI_HIDDEN
			object.animationTimer = 0
			object.priority = PRIORITY_BOUNDS
			object.unused = true
			object.state = BURROBOT_AWAITPLAYER
		else
			object.xpos = temp0
			object.ypos = temp1
		end if
	end if

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

	object.frame = object.animationTimer
	object.frame >>= 2
	switch object.animation
	case BURROBOT_ANI_HIDDEN
	case BURROBOT_ANI_JUMPED
		if object.yvel > 0
			object.frame = 5
		else
			object.frame += 3
		end if
		break

	case BURROBOT_ANI_MOVING
		break

	case BURROBOT_ANI_STOPPED
		object.frame++
		break
		
	end switch

	object.animationTimer++
	object.animationTimer &= 7
end event


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


event ObjectStartup
	CheckCurrentStageFolder("Zone04")
	if checkResult == true
		LoadSpriteSheet("LZ/Objects.gif")
		SpriteFrame(-16, -16, 28, 36, 34, 68)
		SpriteFrame(-16, -16, 28, 36, 63, 68)
		SpriteFrame(-16, -16, 28, 36, 194, 90)
		SpriteFrame(-12, -24, 24, 46, 92, 68)
		SpriteFrame(-12, -24, 24, 46, 143, 90)
		SpriteFrame(-16, -20, 25, 42, 168, 90)
		SpriteFrame(-24, -12, 38, 24, 118, 65)
	end if

	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-16, -16, 28, 36, 1, 227)
		SpriteFrame(-16, -16, 28, 36, 30, 227)
		SpriteFrame(-16, -16, 28, 36, 59, 227)
		SpriteFrame(-12, -24, 24, 46, 88, 217)
		SpriteFrame(-12, -24, 24, 46, 113, 217)
		SpriteFrame(-16, -20, 25, 42, 138, 221)
		SpriteFrame(-24, -12, 38, 24, 215, 229)
	end if

#platform: USE_ORIGINS
	CheckCurrentStageFolder("DLC_Zone06")
	if checkResult == true
		LoadSpriteSheet("LZ/Objects.gif")
		SpriteFrame(-16, -16, 28, 36, 34, 68)
		SpriteFrame(-16, -16, 28, 36, 63, 68)
		SpriteFrame(-16, -16, 28, 36, 194, 90)
		SpriteFrame(-12, -24, 24, 46, 92, 68)
		SpriteFrame(-12, -24, 24, 46, 143, 90)
		SpriteFrame(-16, -20, 25, 42, 168, 90)
		SpriteFrame(-24, -12, 38, 24, 118, 65)
	end if
#endplatform

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

	SetTableValue(TypeName[Burrobot], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Burrobot_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Burrobot_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

event RSDKDraw
	DrawSprite(3)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-16, -16, 28, 36, 1, 227)
		SpriteFrame(-16, -16, 28, 36, 30, 227)
		SpriteFrame(-16, -16, 28, 36, 59, 227)
		SpriteFrame(-12, -24, 24, 46, 88, 217)
		SpriteFrame(-12, -24, 24, 46, 113, 217)
		SpriteFrame(-16, -20, 25, 42, 138, 221)
		SpriteFrame(-24, -12, 38, 24, 215, 229)
	else
		// Both "Zone04" and "DLC_Zone06" will end up here
		LoadSpriteSheet("LZ/Objects.gif")
		SpriteFrame(-16, -16, 28, 36, 34, 68)
		SpriteFrame(-16, -16, 28, 36, 63, 68)
		SpriteFrame(-16, -16, 28, 36, 194, 90)
		SpriteFrame(-12, -24, 24, 46, 92, 68)
		SpriteFrame(-12, -24, 24, 46, 143, 90)
		SpriteFrame(-16, -20, 25, 42, 168, 90)
		SpriteFrame(-24, -12, 38, 24, 118, 65)
	end if
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
