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

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

private alias object.xpos : player.xpos
private alias object.ypos : player.ypos
private alias object.xvel : player.xvel
private alias object.yvel : player.yvel
private alias object.direction : player.direction
private alias object.drawOrder : player.drawOrder
private alias object.visible : player.visible
private alias object.value8 : player.moveVelocity.x
private alias object.value9 : player.moveVelocity.y


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

event ObjectUpdate
	arrayPos0 = player[0].drawOrder
	screen[arrayPos0].drawListSize = 2
	GetDrawListEntityRef(temp0, arrayPos0, 0)
	SetDrawListEntityRef(object.entityPos, arrayPos0, 0)
	SetDrawListEntityRef(temp0, arrayPos0, 1)
end event


event ObjectDraw
	object.direction = player[0].direction
	object.xvel = player[0].xvel
	object.xvel += player[0].moveVelocity.x
	object.yvel = player[0].yvel
	object.yvel += player[0].moveVelocity.y

	temp0 = object.frame
	temp0 >>= 2
	CheckEqual(object.xvel, 0)
	temp1 = checkResult
	CheckEqual(object.yvel, 0)
	temp1 &= checkResult
	if temp1 == false
		ATan2(object.rotation, object.xvel, object.yvel)
		object.rotation += 16
		temp2 = 512
		temp2 -= Player_stageRotation
		temp2 >>= 1
		object.rotation += temp2
		object.rotation &= 255
		object.rotation >>= 5

		switch object.rotation
		case 0
		case 8
			temp0 += 5
			object.rotation = 0
			break

		case 1
			temp0 += 8
			if object.direction == FACING_RIGHT
				object.rotation = 64
			else
				object.rotation = 0
			end if
			break

		case 2
			temp0 += 5
			object.rotation = 64
			break

		case 3
			temp0 += 8
			if object.direction == FACING_RIGHT
				object.rotation = 128
			else
				object.rotation = 64
			end if
			break

		case 4
			temp0 += 5
			object.rotation = 128
			break

		case 5
			temp0 += 8
			if object.direction == FACING_RIGHT
				object.rotation = 192
			else
				object.rotation = 128
			end if
			break

		case 6
			temp0 += 5
			object.rotation = 192
			break

		case 7
			temp0 += 8
			if object.direction == FACING_RIGHT
				object.rotation = 0
			else
				object.rotation = 192
			end if
			break
			
		end switch

		if object.direction == FACING_LEFT
			object.rotation += 128
		end if
	else
		temp0 += 5
		object.rotation = 0
	end if

	object.rotation <<= 1
	if player[0].visible == true
		DrawSpriteFX(temp0, FX_ROTATE, player[0].xpos, player[0].ypos)
	end if

	object.frame++
	object.frame %= 12
end event


event ObjectStartup
	if stage.playerListPos == PLAYER_TAILS_ALONE
		LoadSpriteSheet("Players/Tails1.gif")
		
		// Place the Tails Object into the scene
		object[1].type = TypeName[Tails Object]
		object[1].drawOrder = 8 // In the official scripts this would be 7, but we're setting it to 8 as a workaround for an Origins bug
		object[1].priority = PRIORITY_ACTIVE
		
		SpriteFrame(-22, -8, 16, 24, 82, 199)
		SpriteFrame(-26, -8, 20, 24, 99, 199)
		SpriteFrame(-26, -8, 20, 24, 120, 199)
		SpriteFrame(-26, -8, 20, 24, 141, 199)
		SpriteFrame(-26, -8, 20, 24, 162, 199)
		SpriteFrame(-35, -8, 24, 16, 231, 166)
		SpriteFrame(-35, -8, 24, 16, 231, 183)
		SpriteFrame(-35, -8, 24, 16, 231, 200)
		SpriteFrame(-25, 9, 20, 16, 235, 217)
		SpriteFrame(-25, 9, 18, 16, 237, 234)
		SpriteFrame(-25, 9, 20, 16, 216, 234)
		SpriteFrame(-30, -6, 24, 16, 231, 166)
		SpriteFrame(-30, -6, 24, 16, 231, 183)
		SpriteFrame(-30, -6, 24, 16, 231, 200)
	end if
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("Players/Tails1.gif")
	SpriteFrame(-22, -8, 16, 24, 82, 199)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
