// ----------------------------------
// RSDK Project: Sonic 1
// Script Description: Yadrin 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 0 : YADRIN_MOVING
private alias 1 : YADRIN_IDLE

private alias 0 : YADRIN_ANI_MOVING
private alias 1 : YADRIN_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 Yadrin_DebugDraw
reserve function Yadrin_DebugSpawn


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

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


private function Yadrin_DebugSpawn
	CreateTempObject(TypeName[Yadrin], 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

	object[tempObjectPos].startPos.x = object.xpos
	object[tempObjectPos].startPos.y = object.ypos
end function


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

event ObjectUpdate
	switch object.state
	case YADRIN_MOVING
		object.priority = PRIORITY_ACTIVE
		object.xpos += object.xvel
		temp0 = false
		ObjectTileGrip(CSIDE_FLOOR, 0, 18, PATH_A)
		if checkResult == false
			temp0 = true
		end if

		ObjectTileCollision(CSIDE_RWALL, -20, 0, PATH_A)
		temp0 |= checkResult
		ObjectTileCollision(CSIDE_LWALL, 20, 0, PATH_A)
		temp0 |= checkResult
		if temp0 != false
			object.state = YADRIN_IDLE
			object.animation = YADRIN_ANI_IDLE
			object.frame = 0
		end if
		break

	case YADRIN_IDLE
		if object.timer < 59
			object.timer++
		else
			object.timer = 0
			object.state = YADRIN_MOVING
			object.animation = YADRIN_ANI_MOVING
			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
			object.xpos = object.startPos.x
			object.ypos = object.startPos.y

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

			object.animation = YADRIN_ANI_MOVING
			object.frame = 0
			object.priority = PRIORITY_BOUNDS
			object.state = YADRIN_MOVING
		else
			object.xpos = temp0
			object.ypos = temp1
		end if
	end if

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

		if temp0 == false
			if object.direction == FLIP_NONE
				BoxCollisionTest(C_TOUCH, object.entityPos, -8, -18, 18, -10, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			else
				BoxCollisionTest(C_TOUCH, object.entityPos, -18, -18, 8, -10, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			end if

			if checkResult == true
				CallFunction(Player_Hit)
			end if
		end if
	next

	if object.animation == YADRIN_ANI_MOVING
		object.frame = object.animationTimer
		object.frame >>= 3
		object.animationTimer++
		object.animationTimer &= 63
	end if
end event


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


event ObjectStartup
	CheckCurrentStageFolder("Zone02")
	if checkResult == true
		LoadSpriteSheet("MZ/Objects.gif")
		SpriteFrame(-20, -20, 40, 38, 1, 2)
		SpriteFrame(-20, -20, 40, 38, 1, 42)
		SpriteFrame(-20, -20, 40, 39, 42, 1)
		SpriteFrame(-20, -20, 40, 39, 42, 41)
		SpriteFrame(-20, -20, 40, 38, 1, 2)
		SpriteFrame(-20, -20, 40, 38, 1, 42)
		SpriteFrame(-15, -20, 35, 39, 83, 1)
		SpriteFrame(-15, -20, 35, 39, 83, 41)
	end if

	CheckCurrentStageFolder("Zone03")
	if checkResult == true
		LoadSpriteSheet("SYZ/Objects.gif")
		SpriteFrame(-20, -20, 40, 38, 1, 2)
		SpriteFrame(-20, -20, 40, 38, 1, 42)
		SpriteFrame(-20, -20, 40, 39, 42, 1)
		SpriteFrame(-20, -20, 40, 39, 42, 41)
		SpriteFrame(-20, -20, 40, 38, 1, 2)
		SpriteFrame(-20, -20, 40, 38, 1, 42)
		SpriteFrame(-15, -20, 35, 39, 83, 1)
		SpriteFrame(-15, -20, 35, 39, 83, 41)
	end if

	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-20, -20, 40, 38, 138, 2)
		SpriteFrame(-20, -20, 40, 38, 138, 42)
		SpriteFrame(-20, -20, 40, 39, 179, 1)
		SpriteFrame(-20, -20, 40, 39, 179, 41)
		SpriteFrame(-20, -20, 40, 38, 138, 2)
		SpriteFrame(-20, -20, 40, 38, 138, 42)
		SpriteFrame(-15, -20, 35, 39, 220, 1)
		SpriteFrame(-15, -20, 35, 39, 220, 41)
	end if

	foreach (TypeName[Yadrin], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.x = object[arrayPos0].xpos
		object[arrayPos0].startPos.y = object[arrayPos0].ypos
		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

	SetTableValue(TypeName[Yadrin], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Yadrin_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Yadrin_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
	// Make sure we have a fall-back, in case the current folder isn't actually a valid one
	temp0 = false
	
	CheckCurrentStageFolder("Zone03")
	temp0 |= checkResult
	if checkResult == true
		LoadSpriteSheet("SYZ/Objects.gif")
		SpriteFrame(-20, -20, 40, 38, 1, 2)
	end if

	CheckCurrentStageFolder("Zone07")
	temp0 |= checkResult
	if checkResult == true
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-20, -20, 40, 38, 138, 2)
	end if
	
	if temp0 == false
		// We use MZ's sprites as the fallback, "Zone02" (MZ's folder) will come here too
		LoadSpriteSheet("MZ/Objects.gif")
		SpriteFrame(-20, -20, 40, 38, 1, 2)
	end if
	
	AddEditorVariable("direction")
	SetActiveVariable("direction")
	AddEnumVariable("Left", 0)
	AddEnumVariable("Right", 1)
end event
