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

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

private alias object.value1 : object.spikeBallPosFG
private alias object.value2 : object.spikeBallFrameFG
private alias object.value3 : object.spikeBallPosBG
private alias object.value4 : object.spikeBallFrameBG

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

reserve function Pendulum_DebugDraw
reserve function Pendulum_DebugSpawn

private function Pendulum_DebugDraw
	DrawSprite(0)
	DrawSprite(1)
	DrawSprite(2)
end function


private function Pendulum_DebugSpawn
	CreateTempObject(TypeName[Pendulum], 0, object.xpos, object.ypos)
end function


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

event ObjectUpdate
	if object.yvel >= 0
		object.yvel += 0x1200
	else
		object.yvel += 0xF00
	end if
	object.spikeBallPosFG += object.yvel

	if object.spikeBallPosFG < -0x340000
		object.spikeBallPosFG = -0x340000
	end if

	if object.spikeBallPosFG >= 0x340000
		object.spikeBallPosFG = 0x340000
		temp0 = object.yvel
		temp0 >>= 4
		object.yvel -= temp0
		FlipSign(object.yvel)
		PlaySfx(SfxName[Ground Impact], false)
	end if

	object.spikeBallPosBG = object.spikeBallPosFG
	FlipSign(object.spikeBallPosBG)
	object.spikeBallFrameFG = 3
	object.spikeBallFrameBG = 6

	temp0 = object.spikeBallPosFG
	temp0 >>= 16
	if temp0 > -48
		object.spikeBallFrameBG = 7
	end if

	if temp0 > -24
		object.spikeBallFrameBG = 8
	end if

	if temp0 > -32
		object.spikeBallFrameFG = 4
	end if

	if temp0 > 24
		object.spikeBallFrameFG = 5
	end if

	if object.spikeBallFrameFG > 3
		temp6 = object.ypos
		object.ypos += object.spikeBallPosFG

		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -16, -16, 16, 16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == true
				CallFunction(Player_Hit)
			end if
		next

		object.ypos = temp6
	end if
end event


event ObjectDraw
	if object.spikeBallPosBG > 0
		temp0 = object.spikeBallPosBG
		temp0 *= 28
		temp0 /= 52
		temp0 += object.ypos
		DrawSpriteXY(15, object.xpos, temp0)

		temp0 = object.spikeBallPosBG
		temp0 *= 36
		temp0 /= 52
		temp0 += object.ypos
		DrawSpriteXY(16, object.xpos, temp0)

		temp0 = object.spikeBallPosBG
		temp0 *= 12
		temp0 /= 52
		temp0 += object.ypos
		DrawSpriteXY(13, object.xpos, temp0)

		temp0 = object.spikeBallPosBG
		temp0 *= 20
		temp0 /= 52
		temp0 += object.ypos
		DrawSpriteXY(14, object.xpos, temp0)
	else
		temp0 = object.spikeBallPosBG
		temp0 *= 32
		temp0 /= 52
		temp0 += object.ypos
		DrawSpriteXY(16, object.xpos, temp0)

		temp0 = object.spikeBallPosBG
		temp0 *= 40
		temp0 /= 52
		temp0 += object.ypos
		DrawSpriteXY(15, object.xpos, temp0)

		temp0 = object.spikeBallPosBG
		temp0 *= 16
		temp0 /= 52
		temp0 += object.ypos
		DrawSpriteXY(14, object.xpos, temp0)

		temp0 = object.spikeBallPosBG
		temp0 *= 24
		temp0 /= 52
		temp0 += object.ypos
		DrawSpriteXY(13, object.xpos, temp0)
	end if

	temp0 = object.spikeBallPosBG
	temp0 += object.ypos
	DrawSpriteXY(object.spikeBallFrameBG, object.xpos, temp0)
	DrawSprite(0)
	DrawSprite(1)
	DrawSprite(2)

	if object.spikeBallPosFG > 0
		temp0 = object.spikeBallPosFG
		temp0 *= 12
		temp0 /= 52
		temp0 += object.ypos
		DrawSpriteXY(9, object.xpos, temp0)

		temp0 = object.spikeBallPosFG
		temp0 *= 20
		temp0 /= 52
		temp0 += object.ypos
		DrawSpriteXY(10, object.xpos, temp0)

		temp0 = object.spikeBallPosFG
		temp0 *= 28
		temp0 /= 52
		temp0 += object.ypos
		DrawSpriteXY(11, object.xpos, temp0)

		temp0 = object.spikeBallPosFG
		temp0 *= 36
		temp0 /= 52
		temp0 += object.ypos
		DrawSpriteXY(12, object.xpos, temp0)
	else
		temp0 = object.spikeBallPosFG
		temp0 *= 12
		temp0 /= 52
		temp0 += object.ypos
		DrawSpriteXY(10, object.xpos, temp0)

		temp0 = object.spikeBallPosFG
		temp0 *= 20
		temp0 /= 52
		temp0 += object.ypos
		DrawSpriteXY(9, object.xpos, temp0)

		temp0 = object.spikeBallPosFG
		temp0 *= 28
		temp0 /= 52
		temp0 += object.ypos
		DrawSpriteXY(12, object.xpos, temp0)

		temp0 = object.spikeBallPosFG
		temp0 *= 36
		temp0 /= 52
		temp0 += object.ypos
		DrawSpriteXY(11, object.xpos, temp0)
	end if

	temp0 = object.spikeBallPosFG
	temp0 += object.ypos
	DrawSpriteXY(object.spikeBallFrameFG, object.xpos, temp0)
end event


event ObjectStartup
	LoadSpriteSheet("HPZ/Objects.gif")
	SpriteFrame(-40, -12, 80, 24, 256, 114) // joint - #0
	SpriteFrame(-40, 12, 8, 56, 256, 139)	// pole L - #1
	SpriteFrame(32, 12, 8, 56, 256, 196)	// pole R - #2
	SpriteFrame(-16, -16, 32, 32, 451, 132) // spikeball (BG) - #3
	SpriteFrame(-20, -20, 40, 40, 402, 66)  // spikeball (FG) - #4
	SpriteFrame(-24, -24, 48, 48, 353, 66)  // spikeball (Active) - #5
	SpriteFrame(-12, -12, 24, 24, 418, 107) // green orb (big) - #6
	SpriteFrame(-10, -10, 20, 20, 378, 115) // green orb (med) - #7
	SpriteFrame(-8, -8, 16, 16, 361, 115) 	// green orb (small) - #8
	SpriteFrame(-3, -4, 6, 8, 349, 115)		// spikeBall bar 1 (top) - #9
	SpriteFrame(-3, -4, 6, 8, 349, 119)		// spikeBall bar 1 (bottom) - #10
	SpriteFrame(-2, -4, 4, 8, 356, 115)		// spikeBall bar 2 (top) - #11
	SpriteFrame(-2, -4, 4, 8, 356, 119)		// spikeBall bar 2 (bottom) - #12
	SpriteFrame(-3, -4, 6, 8, 337, 115)		// spikeBall bar 3 (top) - #13
	SpriteFrame(-3, -4, 6, 8, 337, 119)		// spikeBall bar 3 (bottom) - #14
	SpriteFrame(-2, -4, 4, 8, 344, 115)		// spikeBall bar 4 (top) - #15
	SpriteFrame(-2, -4, 4, 8, 344, 119)		// spikeBall bar 4 (bottom) - #16

	SetTableValue(TypeName[Pendulum], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Pendulum_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Pendulum_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++

	foreach (TypeName[Pendulum], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].spikeBallPosFG = -0x340000
	next
end event


event RSDKDraw
	object.spikeBallPosFG = -0x340000
	object.spikeBallPosBG = object.spikeBallPosFG
	FlipSign(object.spikeBallPosBG)
	object.spikeBallFrameFG = 3
	object.spikeBallFrameBG = 6

	temp0 = object.spikeBallPosFG
	temp0 >>= 16
	if temp0 > -48
		object.spikeBallFrameBG = 7
	end if

	if temp0 > -24
		object.spikeBallFrameBG = 8
	end if

	if temp0 > -32
		object.spikeBallFrameFG = 4
	end if

	if temp0 > 24
		object.spikeBallFrameFG = 5
	end if

	if object.spikeBallPosBG > 0
		temp0 = object.spikeBallPosBG
		temp0 *= 28
		temp0 /= 52
		temp0 += object.ypos
		DrawSpriteXY(15, object.xpos, temp0)

		temp0 = object.spikeBallPosBG
		temp0 *= 36
		temp0 /= 52
		temp0 += object.ypos
		DrawSpriteXY(16, object.xpos, temp0)

		temp0 = object.spikeBallPosBG
		temp0 *= 12
		temp0 /= 52
		temp0 += object.ypos
		DrawSpriteXY(13, object.xpos, temp0)

		temp0 = object.spikeBallPosBG
		temp0 *= 20
		temp0 /= 52
		temp0 += object.ypos
		DrawSpriteXY(14, object.xpos, temp0)
	else
		temp0 = object.spikeBallPosBG
		temp0 *= 32
		temp0 /= 52
		temp0 += object.ypos
		DrawSpriteXY(16, object.xpos, temp0)

		temp0 = object.spikeBallPosBG
		temp0 *= 40
		temp0 /= 52
		temp0 += object.ypos
		DrawSpriteXY(15, object.xpos, temp0)

		temp0 = object.spikeBallPosBG
		temp0 *= 16
		temp0 /= 52
		temp0 += object.ypos
		DrawSpriteXY(14, object.xpos, temp0)

		temp0 = object.spikeBallPosBG
		temp0 *= 24
		temp0 /= 52
		temp0 += object.ypos
		DrawSpriteXY(13, object.xpos, temp0)
	end if

	temp0 = object.spikeBallPosBG
	temp0 += object.ypos
	DrawSpriteXY(object.spikeBallFrameBG, object.xpos, temp0)
	DrawSprite(0)
	DrawSprite(1)
	DrawSprite(2)

	if object.spikeBallPosFG > 0
		temp0 = object.spikeBallPosFG
		temp0 *= 12
		temp0 /= 52
		temp0 += object.ypos
		DrawSpriteXY(9, object.xpos, temp0)

		temp0 = object.spikeBallPosFG
		temp0 *= 20
		temp0 /= 52
		temp0 += object.ypos
		DrawSpriteXY(10, object.xpos, temp0)

		temp0 = object.spikeBallPosFG
		temp0 *= 28
		temp0 /= 52
		temp0 += object.ypos
		DrawSpriteXY(11, object.xpos, temp0)

		temp0 = object.spikeBallPosFG
		temp0 *= 36
		temp0 /= 52
		temp0 += object.ypos
		DrawSpriteXY(12, object.xpos, temp0)
	else
		temp0 = object.spikeBallPosFG
		temp0 *= 12
		temp0 /= 52
		temp0 += object.ypos
		DrawSpriteXY(10, object.xpos, temp0)

		temp0 = object.spikeBallPosFG
		temp0 *= 20
		temp0 /= 52
		temp0 += object.ypos
		DrawSpriteXY(9, object.xpos, temp0)

		temp0 = object.spikeBallPosFG
		temp0 *= 28
		temp0 /= 52
		temp0 += object.ypos
		DrawSpriteXY(12, object.xpos, temp0)

		temp0 = object.spikeBallPosFG
		temp0 *= 36
		temp0 /= 52
		temp0 += object.ypos
		DrawSpriteXY(11, object.xpos, temp0)
	end if

	temp0 = object.spikeBallPosFG
	temp0 += object.ypos
	DrawSpriteXY(object.spikeBallFrameFG, object.xpos, temp0)
end event


event RSDKLoad
	LoadSpriteSheet("HPZ/Objects.gif")
	SpriteFrame(-40, -12, 80, 24, 256, 114)
	SpriteFrame(-40, 12, 8, 56, 256, 139)
	SpriteFrame(32, 12, 8, 56, 256, 196)
	SpriteFrame(-16, -16, 32, 32, 451, 132)
	SpriteFrame(-20, -20, 40, 40, 402, 66)
	SpriteFrame(-24, -24, 48, 48, 353, 66)
	SpriteFrame(-12, -12, 24, 24, 418, 107)
	SpriteFrame(-10, -10, 20, 20, 378, 115)
	SpriteFrame(-8, -8, 16, 16, 361, 115)
	SpriteFrame(-3, -4, 6, 8, 349, 115)
	SpriteFrame(-3, -4, 6, 8, 349, 119)
	SpriteFrame(-2, -4, 4, 8, 356, 115)
	SpriteFrame(-2, -4, 4, 8, 356, 119)
	SpriteFrame(-3, -4, 6, 8, 337, 115)
	SpriteFrame(-3, -4, 6, 8, 337, 119)
	SpriteFrame(-2, -4, 4, 8, 344, 115)
	SpriteFrame(-2, -4, 4, 8, 344, 119)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
