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

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

private alias object.value0 : object.timer

private alias 0 : BLOCK_ZONE_IDLE
private alias 1 : BLOCK_ZONE_ACTIVATED

// Player aliases
private alias object.value10 : debugMode.currentSelection

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

reserve function ZoneBlock_DebugDraw
reserve function ZoneBlock_DebugSpawn


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

private value ZoneBlock_startDebugID = 0


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

private function ZoneBlock_DebugDraw
	temp4 = debugMode[0].currentSelection
	temp4 -= ZoneBlock_startDebugID
	DrawSprite(temp4)
end function


private function ZoneBlock_DebugSpawn
	temp4 = debugMode[0].currentSelection
	temp4 -= ZoneBlock_startDebugID
	temp5 = TypeName[Zone Block]
	CallFunction(DebugMode_PlaceBlock)
	object[arrayPos0].drawOrder = 4
	object[arrayPos0].groupID = GROUP_BLOCKS
end function


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

event ObjectUpdate
	if object.state == BLOCK_ZONE_ACTIVATED
		object.frame = object.timer
		object.frame >>= 3
		object.timer++
		if object.timer == 32
			object.timer = 0
			object.state = BLOCK_ZONE_IDLE
			object.frame = 0
		end if
	end if

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		CallFunction(SpecialSetup_PlayerBlockCol)
	next
end event


event ObjectDraw
	CallFunction(SpecialSetup_GetBlockPos)
#platform: USE_ORIGINS
	// This object isn't even used normally, but it's cool to see them update it regardless
	if game.playMode == BOOT_PLAYMODE_MIRRORING
		object.direction = FACING_LEFT
		DrawSpriteFX(object.propertyValue, FX_FLIP, temp0, temp1)
	else
		DrawSpriteXY(object.propertyValue, temp0, temp1)
	end if
#endplatform
#platform: USE_STANDALONE
	DrawSpriteXY(object.propertyValue, temp0, temp1)
#endplatform
end event


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

	// Zone block frames
	SpriteFrame(-12, -12, 24, 24, 326, 176)
	SpriteFrame(-12, -12, 24, 24, 351, 176)
	SpriteFrame(-12, -12, 24, 24, 376, 176)
	SpriteFrame(-12, -12, 24, 24, 426, 176)
	SpriteFrame(-12, -12, 24, 24, 451, 176)
	SpriteFrame(-12, -12, 24, 24, 476, 176)
	SpriteFrame(-12, -12, 24, 24, 476, 276)

	foreach (TypeName[Zone Block], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].groupID = GROUP_BLOCKS
	next

	// Add the Zone Block to the debug object list seven times for the seven different zones
	temp0 = 0
	ZoneBlock_startDebugID = DebugMode_ObjCount
	while temp0 < 7
		SetTableValue(TypeName[Zone Block], DebugMode_ObjCount, DebugMode_TypeTable)
		SetTableValue(ZoneBlock_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(ZoneBlock_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 // zoneID
			checkResult = object.propertyValue
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // zoneID
			object.propertyValue = temp0
			break
			
		end switch
	end if
end event


event RSDKDraw
	DrawSprite(object.propertyValue)
end event


event RSDKLoad
	LoadSpriteSheet("Special/Objects.gif")
	SpriteFrame(-12, -12, 24, 24, 326, 176)
	SpriteFrame(-12, -12, 24, 24, 351, 176)
	SpriteFrame(-12, -12, 24, 24, 376, 176)
	SpriteFrame(-12, -12, 24, 24, 426, 176)
	SpriteFrame(-12, -12, 24, 24, 451, 176)
	SpriteFrame(-12, -12, 24, 24, 476, 176)
	SpriteFrame(-12, -12, 24, 24, 476, 276)
	
	AddEditorVariable("zoneID")
	SetActiveVariable("zoneID")
	AddEnumVariable("Zone 1", 0)
	AddEnumVariable("Zone 2", 1)
	AddEnumVariable("Zone 3", 2)
	AddEnumVariable("Zone 4", 3)
	AddEnumVariable("Zone 5", 4)
	AddEnumVariable("Zone 6", 5)
	AddEnumVariable("Zone 7", 6)
end event
