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

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

private alias object.value0 : object.swimTimer
private alias object.value1 : object.swimDuration
private alias object.value3 : object.startPos.x

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


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

reserve function Jaws_DebugDraw
reserve function Jaws_DebugSpawn


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

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


private function Jaws_DebugSpawn
	CreateTempObject(TypeName[Jaws], 0, object.xpos, object.ypos)
	object[tempObjectPos].direction = object.direction
	object[tempObjectPos].startPos.x = object.xpos
	if object[tempObjectPos].direction == FLIP_NONE
		object[tempObjectPos].xvel = -0x4000
	else
		object[tempObjectPos].xvel = 0x4000
	end if
	object[tempObjectPos].swimTimer = 120
end function


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

event ObjectUpdate
	object.priority = PRIORITY_ACTIVE
	object.xpos += object.xvel

	object.swimTimer--
	if object.swimTimer == 0
		object.direction ^= FLIP_X
		FlipSign(object.xvel)
		object.swimTimer = object.swimDuration
	end if

	if object.outOfBounds == true
		temp0 = object.xpos
		object.xpos = object.startPos.x
		if object.outOfBounds == true
			object.xpos = object.startPos.x
			object.priority = PRIORITY_BOUNDS
			object.direction = object.propertyValue
			object.direction >>= 7
			if object.direction == FLIP_NONE
				object.xvel = -0x4000
			else
				object.xvel = 0x4000
			end if
			object.swimTimer = object.propertyValue
			object.swimTimer &= 0x7F
			object.swimTimer <<= 6
			object.swimDuration = object.swimTimer
		else
			object.xpos = temp0
		end if
	end if

	// Check interaction with players
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -16, -12, 16, 12, 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 >>= 3
	object.animationTimer++
	object.animationTimer &= 15
end event


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


event ObjectStartup
	// Load sheet and sprites depending on which zone the player is currently in
	CheckCurrentStageFolder("Zone04")
	if checkResult == true
		LoadSpriteSheet("LZ/Objects.gif")
		SpriteFrame(-16, -12, 48, 24, 1, 105)
		SpriteFrame(-16, -12, 45, 24, 1, 130)
		SpriteFrame(-16, -12, 48, 24, 1, 155)
		SpriteFrame(-16, -12, 45, 24, 47, 129)
	end if

	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-16, -12, 48, 24, 1, 264)
		SpriteFrame(-16, -12, 45, 24, 99, 264)
		SpriteFrame(-16, -12, 48, 24, 50, 264)
		SpriteFrame(-16, -12, 45, 24, 145, 264)
	end if

	foreach (TypeName[Jaws], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.x = object[arrayPos0].xpos
		object[arrayPos0].direction = object[arrayPos0].propertyValue
		object[arrayPos0].direction >>= 7
		if object[arrayPos0].direction == FLIP_NONE
			object[arrayPos0].xvel = -0x4000
		else
			object[arrayPos0].xvel = 0x4000
		end if

		object[arrayPos0].swimTimer = object[arrayPos0].propertyValue
		object[arrayPos0].swimTimer &= 0x7F
		object[arrayPos0].swimTimer <<= 6
		object[arrayPos0].swimDuration = object[arrayPos0].swimTimer
	next

	// Add Jaws to the debug object list
	SetTableValue(TypeName[Jaws], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Jaws_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Jaws_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
			break
			
		case 0 // swimDuration
			checkResult = object.propertyValue
			checkResult &= 0x7F
			break
			
		case 1 // direction
			GetBit(checkResult, object.propertyValue, 7)
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // swimDuration
			object.propertyValue &= 0x80
			editor.variableValue &= 0x7F
			
			object.propertyValue |= editor.variableValue
			break
			
		case 1 // direction
			CheckNotEqual(editor.variableValue, false)
			SetBit(object.propertyValue, 7, checkResult)
			break
			
		end switch
	end if
end event


event RSDKDraw
	GetBit(object.direction, object.propertyValue, 7)
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
	
	if editor.showGizmos == true
		editor.drawingOverlay = true
		
		temp0 = object.propertyValue
		temp0 &= 0x7C
		temp0 <<= 20
		GetBit(checkResult, object.propertyValue, 7)
		SetBit(temp0, 31, checkResult)
		temp0 += object.xpos
		DrawLine(object.xpos, object.ypos, temp0, object.ypos, 0xFF, 0xFF, 0x00)
		
		editor.drawingOverlay = false
	end if
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-16, -12, 48, 24, 1, 264)
	else
		LoadSpriteSheet("LZ/Objects.gif")
		SpriteFrame(-16, -12, 48, 24, 1, 105)
	end if

	AddEditorVariable("swimDuration")
	SetActiveVariable("swimDuration")

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