// ----------------------------------
// RSDK Project: Sonic 1
// Script Description: Vanish Platform 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.duration
private alias object.value2 : object.intervalOffset
private alias object.value3 : object.interval

// Player Aliases
private alias object.value17 : debugMode.currentSelection


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

reserve function VanishPlatform_DebugDraw
reserve function VanishPlatform_DebugSpawn


// ========================
// Static Values
// ========================

private value VanishPlatform_startDebugID = 0


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

private function VanishPlatform_DebugDraw
	DrawSprite(3)
end function


private function VanishPlatform_DebugSpawn
	CreateTempObject(TypeName[Vanish Platform], 0, object.xpos, object.ypos)
	object[tempObjectPos].priority = PRIORITY_XBOUNDS
	object[tempObjectPos].duration = 127
	object[tempObjectPos].timer = 127

	temp1 = debugMode[0].currentSelection
	temp1 -= VanishPlatform_startDebugID
	temp1 <<= 4
	temp0 += 128
	temp1 *= temp0
	temp1 >>= 8
	object[tempObjectPos].intervalOffset = temp1
	temp0--
	object[tempObjectPos].interval = temp0
end function


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

event ObjectUpdate
	if object.state == 0
		temp0 = SBZSetup_platformTimer
		temp0 -= object.intervalOffset
		temp0 &= object.interval
		if temp0 == 0
			object.state = 1
			object.priority = PRIORITY_ACTIVE
		end if
	end if

	if object.state != 0
		object.timer--
		if object.timer < 0
			if object.animation == 0
				object.timer = object.duration
			else
				object.timer = 127
			end if
			object.animation ^= 1
		end if

		if object.animation == 0
			if object.frame < 3
				object.animationTimer++
				if object.animationTimer == 8
					object.animationTimer = 0
					object.frame++
				end if
			end if
		else
			if object.frame > 0
				object.animationTimer++
				if object.animationTimer == 8
					object.animationTimer = 0
					object.frame--
				end if
			end if
		end if

		GetBit(temp0, object.frame, 1)
		if temp0 != 0
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				BoxCollisionTest(C_PLATFORM, object.entityPos, -16, -8, 16, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			next
		end if
	end if

	temp0 = camera[0].xpos
	temp0 -= object.ixpos
	Abs(temp0)
	temp0 -= 128
	if temp0 > screen.xcenter
		object.state = 0
		object.timer = 127
		object.priority = PRIORITY_XBOUNDS
		object.animation = 0
		object.frame = 0
		object.animationTimer = 0
	end if
end event


event ObjectDraw
	DrawSprite(object.frame)
end event


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

	// Platform frames
	SpriteFrame(0, 0, 1, 1, 449, 198)
	SpriteFrame(-2, -8, 4, 32, 507, 231)
	SpriteFrame(-8, -8, 16, 32, 490, 231)
	SpriteFrame(-16, -8, 32, 32, 457, 231)

	// Do setup for every vanish platform object
	foreach (TypeName[Vanish Platform], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].priority = PRIORITY_XBOUNDS
		temp0 = object[arrayPos0].propertyValue
		temp0 &= 0x0F
		temp0++
		temp0 <<= 7
		object[arrayPos0].duration = temp0
		object[arrayPos0].duration--
		object[arrayPos0].timer = 127

		temp1 = object[arrayPos0].propertyValue
		temp1 &= 0xF0
		temp0 += 0x80
		temp1 *= temp0
		temp1 >>= 8
		object[arrayPos0].intervalOffset = temp1
		temp0--
		object[arrayPos0].interval = temp0
	next

	// Add this object to the debug object list, several times for the different intervals
	temp0 = 0
	VanishPlatform_startDebugID = DebugMode_ObjCount
	while temp0 < 16
		SetTableValue(TypeName[Vanish Platform], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(VanishPlatform_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(VanishPlatform_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
end event


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

event RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			checkResult = object.propertyValue
			break
			
		case 0 // interval
			checkResult = object.propertyValue
			checkResult &= 0xF
			break
			
		case 1 // intervalOffset
			checkResult = object.propertyValue
			checkResult >>= 4
			checkResult &= 0xF
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // interval
			temp0 = editor.variableValue
			temp0 &= 0xF
			object.propertyValue &= 0xF0
			object.propertyValue |= temp0
			break
			
		case 1 // intervalOffset
			temp0 = editor.variableValue
			temp0 &= 0xF
			temp0 <<= 4
			object.propertyValue &= 0xF
			object.propertyValue |= temp0
			break
			
		end switch
	end if
end event


event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("SBZ/Objects.gif")

	// Platform frames
	SpriteFrame(-16, -8, 32, 32, 457, 231)

	AddEditorVariable("interval")
	SetActiveVariable("interval")
	AddEnumVariable("Every 128 Frames", 0)
	AddEnumVariable("Every 256 Frames", 1)
	AddEnumVariable("Every 384 Frames", 2)
	AddEnumVariable("Every 512 Frames", 3)
	AddEnumVariable("Every 640 Frames", 4)
	AddEnumVariable("Every 768 Frames", 5)
	AddEnumVariable("Every 896 Frames", 6)
	AddEnumVariable("Every 1024 Frames", 7)
	AddEnumVariable("Every 1152 Frames", 8)
	AddEnumVariable("Every 1280 Frames", 9)
	AddEnumVariable("Every 1408 Frames", 10)
	AddEnumVariable("Every 1536 Frames", 11)
	AddEnumVariable("Every 1664 Frames", 12)
	AddEnumVariable("Every 1792 Frames", 13)
	AddEnumVariable("Every 1920 Frames", 14)
	AddEnumVariable("Every 2048 Frames", 15)

	AddEditorVariable("intervalOffset")
	SetActiveVariable("intervalOffset")
	AddEnumVariable("0% through the interval", 0)
	AddEnumVariable("6.25% through the interval", 1)
	AddEnumVariable("12.5% through the interval", 2)
	AddEnumVariable("18.75% through the interval", 3)
	AddEnumVariable("25% through the interval", 4)
	AddEnumVariable("31.25% through the interval", 5)
	AddEnumVariable("37.5% through the interval", 6)
	AddEnumVariable("43.75% through the interval", 7)
	AddEnumVariable("50% through the interval", 8)
	AddEnumVariable("56.25% through the interval", 9)
	AddEnumVariable("62.5% through the interval", 10)
	AddEnumVariable("68.75% through the interval", 11)
	AddEnumVariable("75% through the interval", 12)
	AddEnumVariable("81.25% through the interval", 13)
	AddEnumVariable("87.5% through the interval", 14)
	AddEnumVariable("93.75% through the interval", 15)
end event
