// ----------------------------------
// RSDK Project: Sonic 1
// Script Description: Harpoon V 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.hitboxT
private alias object.value2 : object.hitboxB


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

reserve function HarpoonV_DebugDraw
reserve function HarpoonV_DebugSpawn


// ========================
// Tables
// ========================

private table HarpoonV_frameTable
	0, 1, 2, 1
end table

private table HarpoonV_delayTable
	66, 4, 66, 4
end table

private table HarpoonV_sizeTable
	-8, -24, -40, -24
end table


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

private function HarpoonV_DebugDraw
	temp0 = object.direction
	if object.direction == FLIP_X
		object.direction = FLIP_Y
	end if
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
	object.direction = temp0
end function


private function HarpoonV_DebugSpawn
	CreateTempObject(TypeName[Harpoon V], 0, object.xpos, object.ypos)
	object[tempObjectPos].direction = object.direction
	object[tempObjectPos].direction <<= 1
end function


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

event ObjectUpdate
	if object.animationTimer == 0
		object.timer++
		if object.timer >= 4
			object.timer = 0
		end if
		GetTableValue(object.animationTimer, object.timer, HarpoonV_delayTable)
		GetTableValue(object.frame, object.timer, HarpoonV_frameTable)
	else
		object.animationTimer--
	end if

	GetTableValue(object.hitboxT, object.timer, HarpoonV_sizeTable)
	object.hitboxB = 8
	GetBit(temp2, object.direction, 1)
	if temp2 == true // Is FLIP_Y set?
		FlipSign(object.hitboxT)
		FlipSign(object.hitboxB)
		temp0 = object.hitboxB
		object.hitboxB = object.hitboxT
		object.hitboxT = temp0
	end if

	// Check interaction with players
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -4, object.hitboxT, 4, object.hitboxB, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		if checkResult == true
			CallFunction(Player_Hit)
		end if
	next
end event


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


event ObjectStartup
	LoadSpriteSheet("LZ/Objects.gif")
	
	// Harpoon frames
	SpriteFrame(-4, -8, 8, 16, 231, 239)
	SpriteFrame(-4, -24, 8, 32, 222, 223)
	SpriteFrame(-4, -40, 8, 48, 213, 207)

	// Add this object to the debug item list
	SetTableValue(TypeName[Harpoon V], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(HarpoonV_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(HarpoonV_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

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


event RSDKLoad
	LoadSpriteSheet("LZ/Objects.gif")
	SpriteFrame(-4, -40, 8, 48, 213, 207)
	
	// dir is set from scene
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
