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

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

// Bit of a note: The less transparency you have, the more opaque you are
private alias object.value1 : object.transparency

private alias 0 : FORCEFIELD_SOLID
private alias 1 : FORCEFIELD_WAITFORPLAYER
private alias 2 : FORCEFIELD_FADEIN
private alias 3 : FORCEFIELD_ACTIVE

// Player Aliases
private alias object.xpos : player.xpos
private alias object.xvel : player.xvel
private alias object.speed : player.speed
private alias object.direction : player.direction


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

reserve function ForceField_DebugDraw
reserve function ForceField_DebugSpawn

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

private function ForceField_DebugDraw
	// Backup a few values
	temp6 = object.inkEffect
	temp7 = object.alpha

	// Turn the object into an alpha blending one
	object.inkEffect = INK_ADD

	temp1 = oscillation
	temp1 <<= 2
	Sin(temp0, temp1)
	object.alpha = temp0
	object.alpha >>= 5
	object.alpha += 0x50
	DrawSpriteFX(0, FX_INK, object.xpos, object.ypos)
	DrawSpriteFX(4, FX_INK, object.xpos, object.ypos)

	object.alpha = temp0
	object.alpha >>= 5
	object.alpha += 0x90
	DrawSpriteFX(1, FX_INK, object.xpos, object.ypos)
	DrawSpriteFX(3, FX_INK, object.xpos, object.ypos)

	object.alpha = temp0
	object.alpha >>= 4
	object.alpha += 0xC0
	DrawSpriteFX(2, FX_INK, object.xpos, object.ypos)

	// Restore those backed up values from earlier
	object.alpha 	 = temp7
	object.inkEffect = temp6
end function


private function ForceField_DebugSpawn
	CreateTempObject(TypeName[Force Field], 0, object.xpos, object.ypos)

	object[tempObjectPos].drawOrder = 5
	object[tempObjectPos].inkEffect = INK_ADD
end function


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

event ObjectUpdate
	switch object.state
	case FORCEFIELD_SOLID
		// Active - simply act as a box
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_SOLID, object.entityPos, -8, -80, 8, 80, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		next
		break

	case FORCEFIELD_WAITFORPLAYER
		// Check if the player is 64 pixels (screen-space-wise) past the right of this
		temp0 = object.xpos
		temp0 += 0x400000
		if player[0].xpos > temp0
			object.state = FORCEFIELD_FADEIN
		end if
		break

	case FORCEFIELD_FADEIN
		// "Fading in" state where force field starts force fielding
		if object.transparency > 0
			object.transparency--
		else
			object.state = FORCEFIELD_ACTIVE
		end if
		break

	case FORCEFIELD_ACTIVE
		temp0 = object.xpos
		temp0 += 0x160000

		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if player[currentPlayer].xpos < temp0
				BoxCollisionTest(C_TOUCH, object.entityPos, -256, -80, 8, 80, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				if checkResult == true
					if player[currentPlayer].xvel < 0x20000
						player[currentPlayer].xvel 		= 0x20000
						player[currentPlayer].speed 	= 0x20000
						player[currentPlayer].direction = FACING_RIGHT
					end if
				end if
			else
				BoxCollisionTest(C_SOLID, object.entityPos, -8, -80, 8, 80, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			end if
		next
		break

	end switch
end event


event ObjectDraw
	temp1 = oscillation
	temp1 <<= 2
	Sin(temp0, temp1)
	object.alpha = temp0
	object.alpha >>= 5
	object.alpha += 0x50
	object.alpha >>= object.transparency
	DrawSpriteFX(0, FX_INK, object.xpos, object.ypos)
	DrawSpriteFX(4, FX_INK, object.xpos, object.ypos)

	object.alpha = temp0
	object.alpha >>= 5
	object.alpha += 0x90
	object.alpha >>= object.transparency
	DrawSpriteFX(1, FX_INK, object.xpos, object.ypos)
	DrawSpriteFX(3, FX_INK, object.xpos, object.ypos)

	object.alpha = temp0
	object.alpha >>= 4
	object.alpha += 0xC0
	object.alpha >>= object.transparency
	DrawSpriteFX(2, FX_INK, object.xpos, object.ypos)
end event


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

	// Force Field frames
	SpriteFrame(-8, -72, 2, 144, 382, 266)
	SpriteFrame(-6, -72, 3, 144, 384, 266)
	SpriteFrame(-3, -72, 6, 144, 387, 266)
	SpriteFrame(3, -72, 3, 144, 393, 266)
	SpriteFrame(6, -72, 2, 144, 396, 266)

	// Set up values for every Force Field object
	foreach (TypeName[Force Field], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].drawOrder = 5
		object[arrayPos0].inkEffect = INK_ADD
		
		if object[arrayPos0].propertyValue == 1
			object[arrayPos0].state  		= FORCEFIELD_WAITFORPLAYER
			object[arrayPos0].transparency 	= 10
		end if
	next

	// Add the object to the debug mode item list
	SetTableValue(TypeName[Force Field], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(ForceField_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(ForceField_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
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 // type
			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 // type
			object.propertyValue = editor.variableValue
			object.propertyValue &= 3
			break
			
		end switch
	end if
end event


event RSDKDraw
	object.inkEffect = INK_ADD
	if object.propertyValue == 1
		object.transparency = 10
	else
		object.transparency = 0
	end if

	Sin(temp0, 0)
	object.alpha = temp0
	object.alpha >>= 5
	object.alpha += 0x50
	object.alpha >>= object.transparency
	DrawSpriteFX(0, FX_INK, object.xpos, object.ypos)
	DrawSpriteFX(4, FX_INK, object.xpos, object.ypos)

	object.alpha = temp0
	object.alpha >>= 5
	object.alpha += 0x90
	object.alpha >>= object.transparency
	DrawSpriteFX(1, FX_INK, object.xpos, object.ypos)
	DrawSpriteFX(3, FX_INK, object.xpos, object.ypos)

	object.alpha = temp0
	object.alpha >>= 4
	object.alpha += 0xC0
	object.alpha >>= object.transparency
	DrawSpriteFX(2, FX_INK, object.xpos, object.ypos)
end event


event RSDKLoad
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-8, -72, 2, 144, 382, 266)
	SpriteFrame(-6, -72, 3, 144, 384, 266)
	SpriteFrame(-3, -72, 6, 144, 387, 266)
	SpriteFrame(3, -72, 3, 144, 393, 266)
	SpriteFrame(6, -72, 2, 144, 396, 266)

	AddEditorVariable("type")
	SetActiveVariable("type")
	AddEnumVariable("Always Active", 0)
	AddEnumVariable("Active On Enter", 1)
end event
