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

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

private alias object.value0 : object.timer

private alias 0 : EMERALD_IDLE
private alias 1 : EMERALD_GRABBED
private alias 2 : EMERALD_EXITSS
private alias 3 : EMERALD_FINISHED

// Player Aliases
private alias object.state : player.state
private alias object.xvel : player.xvel
private alias object.yvel : player.yvel
private alias object.interaction : player.interaction

private alias object.value10 : debugMode.currentSelection


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

reserve function ChaosEmerald_DebugDraw
reserve function ChaosEmerald_DebugSpawn


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

private value ChaosEmerald_startDebugID = 0


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

private function ChaosEmerald_DebugDraw
	temp4 = debugMode[0].currentSelection
	temp4 -= ChaosEmerald_startDebugID
	DrawSprite(temp4)
end function


private function ChaosEmerald_DebugSpawn
	temp4 = debugMode[0].currentSelection
	temp4 -= ChaosEmerald_startDebugID
	temp5 = TypeName[Chaos Emerald]
	CallFunction(DebugMode_PlaceBlock)
	object[arrayPos0].drawOrder = 4
	object[arrayPos0].groupID = GROUP_BLOCKS
end function


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

event ObjectUpdate
	switch object.state
	default
	case EMERALD_IDLE
		break

	case EMERALD_GRABBED
		object.timer++
		if object.timer == 32
			object.timer = 0
			object.state++
		end if
		break

	case EMERALD_EXITSS
		object.timer++
		if object.timer == 2
			player[0].state = Player_State_Static
			object.timer = 0
			if player[0].xvel == 0
				if player[0].yvel == 0
					Sin(player[0].xvel, Player_stageRotation)
					player[0].xvel *= 4

					Cos(player[0].yvel, Player_stageRotation)
					player[0].yvel *= -4
				end if
			end if
			player[0].interaction = false

			ResetObjectEntity(20, TypeName[Special Finish], 0, 0, 0)
			object[20].priority = PRIORITY_ACTIVE
			PlaySfx(SfxName[Exit SS], false)
			object.state++
		end if
		break

	case EMERALD_FINISHED
		break
		
	end switch

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		if object.state == EMERALD_IDLE
			BoxCollisionTest(C_TOUCH, object.entityPos, -8, -8, 8, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == true
#platform: USE_ORIGINS
				CallNativeFunction2(NotifyCallback, NOTIFY_TOUCH_EMERALD, 0)
#endplatform
				object.state = EMERALD_GRABBED
				object.timer = 0
				object.drawOrder = 4
				StopMusic()
				PlaySfx(SfxName[Emerald], false)
				Player_isWinner = true
				options.touchControls = false
				stage.timeEnabled = false
			end if
		end if
	next
end event


event ObjectDraw
	switch object.state
	case EMERALD_IDLE
		CallFunction(SpecialSetup_GetBlockPos)
		object.timer++
		object.timer &= 15
		if object.timer < 8
			DrawSpriteXY(object.propertyValue, temp0, temp1)
		else
			DrawSpriteXY(7, temp0, temp1)
		end if
		break

	case EMERALD_GRABBED
		CallFunction(SpecialSetup_GetBlockPos)
		object.frame = object.timer
		object.frame >>= 3
		object.frame += 8
		DrawSpriteXY(object.frame, temp0, temp1)
		break

	case EMERALD_EXITSS
		break
		
	end switch
end event


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

	// Chaos Emerald frames
	SpriteFrame(-12, -12, 24, 24, 176, 276) // #0 - Emerald 1
	SpriteFrame(-12, -12, 24, 24, 201, 276) // #1 - Emerald 2

	// Third emerald color is determinend by Super Stages setting
	if options.superStates == false
		SpriteFrame(-12, -12, 24, 24, 226, 276) // #2 - Emerald 3
	else
		SpriteFrame(-12, -12, 24, 24, 426, 276) // #2 - Emerald 3
	end if

	SpriteFrame(-12, -12, 24, 24, 251, 276) // #3 - Emerald 4
	SpriteFrame(-12, -12, 24, 24, 276, 276) // #4 - Emerald 5
	SpriteFrame(-12, -12, 24, 24, 301, 276) // #5 - Emerald 6
	SpriteFrame(-12, -12, 24, 24, 401, 276) // #6 - Emerald 7
	SpriteFrame(-12, -12, 24, 24, 326, 276) // #7 - Flashing emerald sprite

	SpriteFrame(-12, -12, 24, 24, 401, 76) // #8 - Grabbed anim frame 1
	SpriteFrame(-12, -12, 24, 24, 426, 76) // #9 - Grabbed anim frame 2
	SpriteFrame(-12, -12, 24, 24, 451, 76) // #10 - Grabbed anim frame 3
	SpriteFrame(-12, -12, 24, 24, 476, 76) // #11 - Grabbed anim frame 4

	// Add the object to the debug object list seven times, for the seven colours of emeralds
	temp0 = 0
	ChaosEmerald_startDebugID = DebugMode_ObjCount
	while temp0 < 7
		SetTableValue(TypeName[Chaos Emerald], DebugMode_ObjCount, DebugMode_TypeTable)
		SetTableValue(ChaosEmerald_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(ChaosEmerald_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 // emeraldID
			checkResult = object.propertyValue
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // emeraldID
			object.propertyValue = editor.variableValue
			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, 176, 276)
	SpriteFrame(-12, -12, 24, 24, 201, 276)
	SpriteFrame(-12, -12, 24, 24, 226, 276)
	SpriteFrame(-12, -12, 24, 24, 251, 276)
	SpriteFrame(-12, -12, 24, 24, 276, 276)
	SpriteFrame(-12, -12, 24, 24, 301, 276)
	SpriteFrame(-12, -12, 24, 24, 401, 276)
	SpriteFrame(-12, -12, 24, 24, 326, 276)
	
	AddEditorVariable("emeraldID")
	SetActiveVariable("emeraldID")
	AddEnumVariable("Emerald 1", 0)
	AddEnumVariable("Emerald 2", 1)
	AddEnumVariable("Emerald 3", 2)
	AddEnumVariable("Emerald 4", 3)
	AddEnumVariable("Emerald 5", 4)
	AddEnumVariable("Emerald 6", 5)
	AddEnumVariable("Emerald 7", 6)
end event
