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

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

private alias object.value1 : object.viewRotation

// Halfpipe Aliases
private alias object.value7 : halfpipe.halfpipeRotation.x
private alias object.value8 : halfpipe.halfpipeRotation.y
private alias object.value9 : halfpipe.halfpipeRotation.z


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

event ObjectDraw
	temp2 = halfpipe[0].halfpipeRotation.y
	temp2 += 0x40
	temp2 &= 0x1FF
	temp2 >>= 7
	switch temp2
	case 0
		temp0 = halfpipe[0].halfpipeRotation.y
		temp1 = halfpipe[0].halfpipeRotation.x
		FlipSign(temp1)
		object.rotation = halfpipe[0].halfpipeRotation.z
		break

	case 1
		temp0 = halfpipe[0].halfpipeRotation.y
		temp1 = halfpipe[0].halfpipeRotation.z
		FlipSign(temp1)
		object.rotation = halfpipe[0].halfpipeRotation.x
		FlipSign(object.rotation)
		break

	case 2
		temp0 = halfpipe[0].halfpipeRotation.y
		temp1 = halfpipe[0].halfpipeRotation.x
		object.rotation = halfpipe[0].halfpipeRotation.z
		FlipSign(object.rotation)
		break

	case 3
		temp0 = halfpipe[0].halfpipeRotation.y
		temp1 = halfpipe[0].halfpipeRotation.z
		object.rotation = halfpipe[0].halfpipeRotation.x
		FlipSign(object.rotation)
		break
		
	end switch

	temp0 <<= 1
	temp0 &= 0xFF
	temp1 <<= 1
	temp1 &= 0xFF
	object.rotation &= 0x1FF

	temp2 = halfpipe[0].halfpipeRotation.x
	temp2 += halfpipe[0].halfpipeRotation.y
	temp2 += halfpipe[0].halfpipeRotation.z
	temp2 &= 0x7F
	if temp2 == 0
		Sin256(object.viewRotation, object.angle)
		object.angle += 8
		object.angle &= 0xFF
	else
		object.angle = 0
		object.viewRotation >>= 1
	end if

	temp2 = object.viewRotation
	temp2 *= 3
	temp2 >>= 9
	temp1 += temp2
	if object.rotation == 0
		temp0 += screen.xcenter
		temp1 += screen.ycenter
		DrawSpriteScreenXY(0, temp0, temp1)
		DrawSpriteScreenXY(1, temp0, temp1)
		DrawSpriteScreenXY(2, temp0, temp1)
		DrawSpriteScreenXY(3, temp0, temp1)
		DrawSpriteScreenXY(4, temp0, temp1)
		DrawSpriteScreenXY(5, temp0, temp1)
		DrawSpriteScreenXY(6, temp0, temp1)
		DrawSpriteScreenXY(7, temp0, temp1)
		DrawSpriteScreenXY(8, temp0, temp1)
		DrawSpriteScreenXY(9, temp0, temp1)
		DrawSpriteScreenXY(10, temp0, temp1)
		DrawSpriteScreenXY(11, temp0, temp1)
	else
		temp0 += screen.xcenter
		temp1 += screen.ycenter
		DrawSpriteScreenFX(1, FX_ROTATE, temp0, temp1)
		DrawSpriteScreenFX(2, FX_ROTATE, temp0, temp1)
		DrawSpriteScreenFX(3, FX_ROTATE, temp0, temp1)
		DrawSpriteScreenFX(5, FX_ROTATE, temp0, temp1)
		DrawSpriteScreenFX(6, FX_ROTATE, temp0, temp1)
		DrawSpriteScreenFX(7, FX_ROTATE, temp0, temp1)
		DrawSpriteScreenFX(8, FX_ROTATE, temp0, temp1)
		DrawSpriteScreenFX(9, FX_ROTATE, temp0, temp1)
		DrawSpriteScreenFX(10, FX_ROTATE, temp0, temp1)
	end if
end event


event ObjectStartup
	LoadSpriteSheet("Special/Background.gif")
	
	SpriteFrame(-640, -128, 256, 256, 0, 0)
	SpriteFrame(-128, -128, 256, 256, 0, 0)
	SpriteFrame(-384, -128, 256, 256, 0, 0)
	SpriteFrame(128, -128, 256, 256, 0, 0)
	SpriteFrame(-640, -384, 256, 256, 0, 0)
	SpriteFrame(-128, -384, 256, 256, 0, 0)
	SpriteFrame(-384, -384, 256, 256, 0, 0)
	SpriteFrame(128, -384, 256, 256, 0, 0)
	SpriteFrame(-128, -128, 256, 256, 0, 0)
	SpriteFrame(-128, 128, 256, 256, 0, 0)
	SpriteFrame(-384, 128, 256, 256, 0, 0)
	SpriteFrame(128, 128, 256, 256, 0, 0)
	
	object[1].type = TypeName[Special BG]
	object[1].priority = PRIORITY_ACTIVE
	object[1].drawOrder = 0
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("Special/Background.gif")
	SpriteFrame(-128, -128, 256, 256, 0, 0)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
