// ----------------------------------
// RSDK Project: Sonic 1
// Script Description: Bonus Points 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.pointsAmount

private alias 0 : BONUSPOINTS_HIDDEN
private alias 1 : BONUSPOINTS_VISIBLE


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

event ObjectUpdate
	if object.state == BONUSPOINTS_HIDDEN
		// Interact with player(s)
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -8, -8, 8, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == true
				// Show the object
				object.state 		= BONUSPOINTS_VISIBLE
				object.drawOrder 	= 5
				object.inkEffect 	= INK_ALPHA
				object.alpha 		= 0x100
				PlaySfx(SfxName[Bonus Points], false)
				player.score += object.pointsAmount
			end if
		next
	else
		object.timer++

		if object.timer > 105
			object.alpha -= 16
		end if

		if object.timer == 120
			object.type = TypeName[Blank Object]
		end if
	end if
end event


event ObjectDraw
	// Only show after the player has touched it
	if object.state == BONUSPOINTS_VISIBLE
#platform: USE_ORIGINS
		if game.playMode == BOOT_PLAYMODE_MIRRORING
			object.direction = FACING_LEFT
			// We lose the fade out in Mirror Mode, oh well
			DrawSpriteFX(object.propertyValue, FX_FLIP, object.xpos, object.ypos)
		else
			DrawSpriteFX(object.propertyValue, FX_INK, object.xpos, object.ypos)
		end if
#endplatform
#platform: USE_STANDALONE
		DrawSpriteFX(object.propertyValue, FX_INK, object.xpos, object.ypos)
#endplatform
	end if
end event


event ObjectStartup
	LoadSpriteSheet("Global/Display.gif")

	foreach (TypeName[Bonus Points], arrayPos0, ALL_ENTITIES)
		// Choose point value based on property value
		
		object[arrayPos0].propertyValue &= 3
		switch object[arrayPos0].propertyValue
		default
		case 0
			// No bonus at all (though, 10000 will still be displayed if this object is touched)
			break

		case 1
			object[arrayPos0].pointsAmount = 10000
			break

		case 2
			object[arrayPos0].pointsAmount = 1000
			break

		case 3
			object[arrayPos0].pointsAmount = 100
			break
			
		end switch
	next

	// Sprite frames
	SpriteFrame(-16, -12, 31, 23, 225, 49) // 10000 Points (No Points, functionality-wise) - #0
	SpriteFrame(-16, -12, 31, 23, 225, 49) // 10000 Points - #1
	SpriteFrame(-16, -12, 31, 23, 225, 25) // 1000  Points - #2
	SpriteFrame(-16, -12, 31, 23, 225, 1)  // 100   Points - #3
end event


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

event RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			checkResult = object.propertyValue
			checkResult &= 3
			break
			
		case 0 // pointsAmount
			checkResult = object.propertyValue
			checkResult &= 3
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			object.propertyValue &= 3
			break
			
		case 0 // pointsAmount
			object.propertyValue = editor.variableValue
			object.propertyValue &= 3
			break
			
		end switch
	end if
end event


event RSDKDraw
	DrawSprite(object.propertyValue)
end event


event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -12, 31, 23, 225, 49)
	SpriteFrame(-16, -12, 31, 23, 225, 49)
	SpriteFrame(-16, -12, 31, 23, 225, 25)
	SpriteFrame(-16, -12, 31, 23, 225, 1)

	AddEditorVariable("pointsAmount")
	SetActiveVariable("pointsAmount")
	AddEnumVariable("No Points", 0)
	AddEnumVariable("10,000 Points", 1)
	AddEnumVariable("1,000 Points", 2)
	AddEnumVariable("100 Points", 3)
end event
