// ----------------------------------
// RSDK Project: Sonic 2
// 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 object.value1 : object.localPos.x // unused, required for halfpipe stuff though
private alias object.value2 : object.localPos.y // unused, required for halfpipe stuff though
private alias object.value3 : object.localPos.z
private alias object.value4 : object.worldPos.x // unused, required for halfpipe stuff though
private alias object.value5 : object.worldPos.y // unused, required for halfpipe stuff though
private alias object.value6 : object.worldPos.z // unused, required for halfpipe stuff though
private alias object.value7 : object.emeraldPos.y
private alias object.value8 : object.emeraldPos.z
private alias object.value9 : object.notEnoughRings

// States
private alias 0 : CHAOSEMERALD_AWAITPLAYER
private alias 1 : CHAOSEMERALD_TRYCOLLECT
private alias 2 : CHAOSEMERALD_HANDLEFINISH

// Text Message Property Values
private alias 1 : TEXTMESSAGE_NOTENOUGH

// Halfpipe Aliases
private alias object.value14 : halfpipe.worldTranslation.x
private alias object.value15 : halfpipe.worldTranslation.y
private alias object.value16 : halfpipe.worldTranslation.z
private alias object.value17 : halfpipe.worldRotation.x
private alias object.value18 : halfpipe.worldRotation.y
private alias object.value19 : halfpipe.worldRotation.z
private alias object.value23 : halfpipe.moveSpeed

// Player Aliases
private alias object.value0  : player.rings
private alias object.value14 : player.localPos.z

// HUD Aliases
private alias object.value1 : HUD.totalRings

// Face Buffer Flag Aliases
private alias 6 : FACE_TEXTURED_C_BLEND
private alias 7 : FACE_SPRITE_3D


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

reserve function ChaosEmerald_HandleDrawing


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

public function ChaosEmerald_HandleDrawing
	MatrixRotateXYZ(MAT_WORLD, halfpipe[0].worldRotation.x, halfpipe[0].worldRotation.y, halfpipe[0].worldRotation.z)
	MatrixTranslateXYZ(MAT_TEMP, halfpipe[0].worldTranslation.x, halfpipe[0].worldTranslation.y, halfpipe[0].worldTranslation.z)
	MatrixMultiply(MAT_WORLD, MAT_TEMP)
	arrayPos0 = scene3D.vertexCount
	arrayPos1 = scene3D.faceCount

	faceBuffer[arrayPos1].flag = FACE_TEXTURED_C_BLEND
	faceBuffer[arrayPos1].a = arrayPos0
	vertexBuffer[arrayPos0].x = 0
	vertexBuffer[arrayPos0].y = -0x3800
	vertexBuffer[arrayPos0].z = object.emeraldPos.z
	vertexBuffer[arrayPos0].u = 116
	vertexBuffer[arrayPos0].v = 50
	arrayPos2 = arrayPos0
	arrayPos0++
	TransformVertices(MAT_WORLD, arrayPos2, arrayPos0)

	faceBuffer[arrayPos1].b = arrayPos0
	vertexBuffer[arrayPos0].x = vertexBuffer[arrayPos2].x
	vertexBuffer[arrayPos0].y = vertexBuffer[arrayPos2].y
	vertexBuffer[arrayPos0].z = vertexBuffer[arrayPos2].z
	vertexBuffer[arrayPos0].u = 0x600
	vertexBuffer[arrayPos0].v = 0x600
	arrayPos0++

	faceBuffer[arrayPos1].c = arrayPos0
	vertexBuffer[arrayPos0].x = vertexBuffer[arrayPos2].x
	vertexBuffer[arrayPos0].y = vertexBuffer[arrayPos2].y
	vertexBuffer[arrayPos0].z = vertexBuffer[arrayPos2].z
	vertexBuffer[arrayPos0].u = 17
	vertexBuffer[arrayPos0].v = 16
	arrayPos0++

	faceBuffer[arrayPos1].d = arrayPos0
	vertexBuffer[arrayPos0].x = vertexBuffer[arrayPos2].x
	vertexBuffer[arrayPos0].y = vertexBuffer[arrayPos2].y
	vertexBuffer[arrayPos0].z = vertexBuffer[arrayPos2].z
	arrayPos0++

	arrayPos1++
	scene3D.vertexCount += 4
	scene3D.faceCount++
	arrayPos2 = arrayPos0
	arrayPos0++

	faceBuffer[arrayPos1].flag = FACE_SPRITE_3D
	faceBuffer[arrayPos1].a = arrayPos2
	vertexBuffer[arrayPos2].x = 0
	vertexBuffer[arrayPos2].y = object.emeraldPos.y
	vertexBuffer[arrayPos2].z = object.emeraldPos.z
	TransformVertices(MAT_WORLD, arrayPos2, arrayPos0)
	vertexBuffer[arrayPos2].u = TypeName[Chaos Emerald]
	vertexBuffer[arrayPos2].v = FX_SCALE
	faceBuffer[arrayPos1].b = arrayPos0

	vertexBuffer[arrayPos0].x = vertexBuffer[arrayPos2].x
	vertexBuffer[arrayPos0].y = vertexBuffer[arrayPos2].y
	vertexBuffer[arrayPos0].z = vertexBuffer[arrayPos2].z
	vertexBuffer[arrayPos0].u = object.frame
	vertexBuffer[arrayPos0].v = 0
	arrayPos0++

	faceBuffer[arrayPos1].c = arrayPos0
	vertexBuffer[arrayPos0].x = vertexBuffer[arrayPos2].x
	vertexBuffer[arrayPos0].y = vertexBuffer[arrayPos2].y
	vertexBuffer[arrayPos0].z = vertexBuffer[arrayPos2].z
	vertexBuffer[arrayPos0].u = object.scale
	vertexBuffer[arrayPos0].v = 0
	arrayPos0++

	faceBuffer[arrayPos1].d = arrayPos0
	vertexBuffer[arrayPos0].x = vertexBuffer[arrayPos2].x
	vertexBuffer[arrayPos0].y = vertexBuffer[arrayPos2].y
	vertexBuffer[arrayPos0].z = vertexBuffer[arrayPos2].z
	scene3D.vertexCount += 4
	scene3D.faceCount++
end function


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

event ObjectUpdate
	switch object.state
	case CHAOSEMERALD_AWAITPLAYER
		object[11].type = TypeName[Blank Object]
		foreach (TypeName[Player Object], currentPlayer, ACTIVE_ENTITIES)
			temp1 = object.localPos.z
			temp1 -= 0x20000
			temp2 = object.localPos.z
			temp2 += 0x20000
			if player[currentPlayer].localPos.z > temp1
				if player[currentPlayer].localPos.z < temp2
					ResetObjectEntity(11, TypeName[Blank Object], 0, 0, 0)
					object.scale = 0
					object.drawOrder = 5
					object.frame = stage.actNum
					object.state++
					if stage.player2Enabled == true
						temp0 = player[2].rings
						temp0 += player[3].rings

						object[2].controlMode 	= CONTROLMODE_NONE
						object[2].left 			= false
						object[2].right 		= false

						object[3].controlMode 	= CONTROLMODE_NONE
						object[3].left 			= false
						object[3].right 		= false
					else
						temp0 = player[2].rings
						object[2].controlMode = CONTROLMODE_NONE
						object[2].left = 0
						object[2].right = 0
					end if

					GetTableValue(temp1, SpecialSetup_checkpointID, SpecialSetup_ringCountTable)
					if temp0 >= temp1
						object.notEnoughRings = false
					else
						object.notEnoughRings = true
					end if

					object.emeraldPos.z = 0x14000
				end if
			end if
		next

		if stage.actNum == 7
			GetTableValue(temp0, SpecialSetup_checkpointID, SpecialSetup_ringCountTable)
			if HUD[10].totalRings > temp0
				object.timer++
				object.timer &= 1
				if object.timer == 0
					RotatePalette(0, 129, 141, false)
				end if
			end if
		end if
		break

	case CHAOSEMERALD_TRYCOLLECT
		Sin(object.emeraldPos.y, object.angle)
		object.emeraldPos.y -= 0x1C00
		object.angle += 12
		object.angle &= 0x1FF

		music.volume--
		if object.notEnoughRings == false
			if stage.actNum == 7
				object.timer++
				object.timer &= 1
				if object.timer == 0
					RotatePalette(0, 129, 141, false)
				end if
			end if
			
			if object.scale < 0x200
				object.scale += 4
				object.emeraldPos.z -= 0x200
			else
				StopMusic()
				PlaySfx(SfxName[Emerald], false)
				SpecialSetup_gotEmerald = true
				object.state++
#platform: USE_ORIGINS
				CallNativeFunction2(NotifyCallback, NOTIFY_TOUCH_EMERALD, 0)
#endplatform
			end if
		else
			if object.scale < 0x180
				object.scale += 4
				object.emeraldPos.z -= 0x200
			else
				StopMusic()
				CreateTempObject(TypeName[Text Message], TEXTMESSAGE_NOTENOUGH, 0, 0)
				object[tempObjectPos].drawOrder = 5
				object.state++
			end if

			if halfpipe[0].moveSpeed > 4
				if object.animationTimer == 0
					halfpipe[0].moveSpeed--
				end if

				object.animationTimer++
				object.animationTimer &= 7
			end if
		end if
		CallFunction(ChaosEmerald_HandleDrawing)
		break

	case CHAOSEMERALD_HANDLEFINISH
		Sin(object.emeraldPos.y, object.angle)
		object.emeraldPos.y -= 0x1C00
		object.angle += 12
		object.angle &= 511
		CallFunction(ChaosEmerald_HandleDrawing)

		if object.notEnoughRings == true
			if halfpipe[0].moveSpeed > 4
				if object.animationTimer == 0
					halfpipe[0].moveSpeed--
				end if

				object.animationTimer++
				object.animationTimer &= 7
			end if

			if object.scale > 0
				object.scale -= 2
				object.emeraldPos.z += 0x100
			end if
		else
			if stage.actNum == 7
				temp0 = object.timer
				temp0 &= 1
				if temp0 == 0
					RotatePalette(0, 129, 141, false)
				end if
			end if
		end if

		object.timer++
		if object.timer == 180
			ResetObjectEntity(20, TypeName[Special Finish], 0, 0, 0)
			object[20].priority = PRIORITY_ACTIVE
#platform: USE_ORIGINS
			object[20].drawOrder = 6
#endplatform
		end if
		break
	end switch
end event


event ObjectStartup
	LoadSpriteSheet("Special/Objects.gif")
	SpriteFrame(-12, -4, 24, 8, 313, 165)
	SpriteFrame(-10, -10, 20, 20, 1, 239)
	SpriteFrame(-10, -10, 20, 20, 22, 239)
	SpriteFrame(-10, -10, 20, 20, 43, 239)
	SpriteFrame(-10, -10, 20, 20, 64, 239)
	SpriteFrame(-10, -10, 20, 20, 85, 239)
	SpriteFrame(-10, -10, 20, 20, 106, 239)
	SpriteFrame(-10, -10, 20, 20, 127, 239)
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("Special/Objects.gif")
	SpriteFrame(-8, -8, 16, 16, 1, 260)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
