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

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

// Reserved Object Slot Aliases
// (The UFO occupies the slot the Title Card is initially in)
private alias 11 : SLOT_TITLECARD


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

reserve function UFO_DebugDraw
reserve function UFO_DebugSpawn


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

private function UFO_DebugDraw
	DrawSprite(0)
end function


private function UFO_DebugSpawn
	// Place one... and then clear it?
	// The actual UFO placing is done below
	// A fake one is placed because Debug Mode does an operation with tempObjectPos after spawning the object
	CreateTempObject(TypeName[UFO], 0, object.xpos, object.ypos)
	object[tempObjectPos].type = TypeName[Blank Object]

	// UFOs use reserved object slot 11 (yeah the same slot as the title card) so they can't be placed like a normal object
	if object[SLOT_TITLECARD].type == TypeName[UFO]
		// If the UFOs already exist, clear them
		ResetObjectEntity(SLOT_TITLECARD, TypeName[Blank Object], 0, 0, 0)
	else
		// If they don't exist, then spawn them
		ResetObjectEntity(SLOT_TITLECARD, TypeName[UFO], 0, 0, 0)
		object[SLOT_TITLECARD].priority = PRIORITY_ACTIVE
		object[SLOT_TITLECARD].drawOrder = 1 // Place it among the lowest draw order, it's supposed to be drawn as part of the BG
		object[SLOT_TITLECARD].inkEffect = INK_ALPHA
	end if
end function


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

event ObjectUpdate
	// Animate the UFOs
	object.frame = object.animationTimer
	object.frame >>= 3

	object.animationTimer++
	object.animationTimer &= 63
end event


event ObjectDraw
	if screen.yoffset < 640
		temp0 = screen.xoffset
		temp0 >>= 3
		temp0 &= 127
		FlipSign(temp0)
		
		// temp1 is the base pos for the UFOs to draw at
		temp1 = tileLayer[1].scrollPos
		temp1 /= -0x10000
		temp1 += 32

		// First draw the "closer" set of UFOs
		temp2 = screen.xsize
		temp2 += 32
		temp5 = oscillation
		temp5 <<= 1
		while temp0 < temp2
			temp3 = temp0
			temp3 += temp5
			Sin(temp4, temp3)
			temp4 >>= 5
			temp4 += temp1
			Cos(object.alpha, temp3)
			object.alpha >>= 2
			object.alpha += 128
			DrawSpriteScreenFX(object.frame, FX_INK, temp0, temp4)
			temp0 += 64
		loop

		// And then draw the "further" set of UFOs
		temp0 = screen.xoffset
		temp0 >>= 4
		temp0 &= 127
		temp0 += 32
		FlipSign(temp0)
		while temp0 < temp2
			temp3 = temp0
			temp3 += temp5
			Sin(temp4, temp3)
			temp4 >>= 5
			FlipSign(temp4)
			temp4 += temp1
			Cos(object.alpha, temp3)
			object.alpha >>= 2
			FlipSign(object.alpha)
			object.alpha += 128
			DrawSpriteScreenFX(object.frame, FX_INK, temp0, temp4)
			temp0 += 64
		loop
	end if
end event


event ObjectStartup
	LoadSpriteSheet("MZ/Objects.gif")

	// UFO frames
	SpriteFrame(-16, -8, 32, 16, 123, 303)
	SpriteFrame(-16, -8, 32, 16, 156, 230)
	SpriteFrame(-16, -12, 32, 24, 123, 247)
	SpriteFrame(-16, -15, 32, 30, 123, 272)
	SpriteFrame(-16, -16, 32, 32, 130, 355)
	SpriteFrame(-16, -15, 32, 30, 156, 273)
	SpriteFrame(-16, -12, 32, 25, 156, 247)
	SpriteFrame(-16, -8, 32, 16, 123, 230)

	// Add UFOs to the stage's debug object list
	SetTableValue(TypeName[UFO], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(UFO_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(UFO_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("MZ/Objects.gif")
	SpriteFrame(-16, -8, 32, 16, 123, 303)

	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
