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

// the script is called "SecretPlatform" but the game refers to it as "MovingPlatform"
// we're using the latter name here

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

private alias object.value0 : object.collisionOffset.x

private alias 0 : MOVINGPLATFORM_HIDDEN
private alias 1 : MOVINGPLATFORM_AWAITPLAYER
private alias 2 : MOVINGPLATFORM_MOVING_RIGHT
private alias 3 : MOVINGPLATFORM_FALLING
private alias 4 : MOVINGPLATFORM_STOPPED

// Player Aliases
private alias object.xpos : player.xpos
private alias object.ypos : player.ypos
private alias object.gravity : player.gravity

// Push Button Aliases
private alias object.value0 : pushButton.stood

// Path ID Aliases
private alias 0 : PATH_A

// Achievement Aliases
private alias 5 : ACHIEVEMENT_SECRETOFLZ


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

reserve function MovingBlock_DebugDraw
reserve function MovingBlock_DebugSpawn


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

private function MovingBlock_DebugDraw
	DrawSprite(0)
end function


private function MovingBlock_DebugSpawn
	CreateTempObject(TypeName[Moving Block], 0, object.xpos, object.ypos)
	object[tempObjectPos].priority = PRIORITY_XBOUNDS
	object[tempObjectPos].state = MOVINGPLATFORM_AWAITPLAYER
end function


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

event ObjectUpdate
	switch object.state
	case MOVINGPLATFORM_HIDDEN
		if pushButton[+2].stood == true
			object.state++
		end if
		break

	case MOVINGPLATFORM_AWAITPLAYER
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_SOLID, object.entityPos, -16, -8, 16, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == COL_TOP
				object.state++
				if stage.debugMode == false
					if currentPlayer == 0
						// Grant the "Secret of Labyrinth Zone" Achievement
						CallNativeFunction2(SetAchievement, ACHIEVEMENT_SECRETOFLZ, 100)
					end if
				end if
			end if
		next
		break

	case MOVINGPLATFORM_MOVING_RIGHT
		temp0 = object.xpos
		temp0 &= 0xFFFF0000
		object.xpos += 0x10000

		ObjectTileCollision(CSIDE_LWALL, 16, 0, PATH_A)
		if checkResult == true
			object.state++
		end if

		object.collisionOffset.x = object.xpos
		object.collisionOffset.x &= 0xFFFF0000
		object.collisionOffset.x -= temp0
		temp1 = object.xpos
		object.xpos = temp0

		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_SOLID, object.entityPos, -16, -8, 16, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == COL_TOP
				player[currentPlayer].xpos += object.collisionOffset.x
			end if
		next

		object.xpos = temp1
		break

	case MOVINGPLATFORM_FALLING
		temp0 = object.ypos
		temp0 &= 0xFFFF0000
		object.ypos += object.yvel
		object.yvel += 0x3800

		ObjectTileCollision(CSIDE_FLOOR, 0, 8, PATH_A)
		if checkResult == true
			object.state++
			object.priority = PRIORITY_BOUNDS
		end if

		object.collisionOffset.x = object.ypos
		object.collisionOffset.x &= 0xFFFF0000
		object.collisionOffset.x -= temp0
		temp1 = object.ypos
		object.ypos = temp0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_SOLID, object.entityPos, -16, -8, 16, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == COL_TOP
				player[currentPlayer].ypos += object.collisionOffset.x
			end if

			if player[currentPlayer].gravity == GRAVITY_GROUND
				if checkResult == COL_BOTTOM
					CallFunction(Player_Kill)
				end if
			end if
		next
		object.ypos = temp1
		break

	case MOVINGPLATFORM_STOPPED
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_SOLID, object.entityPos, -16, -8, 16, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		next
		break
		
	end switch
end event


event ObjectDraw
	if object.state > MOVINGPLATFORM_HIDDEN
		DrawSprite(0)
	end if
end event


event ObjectStartup
	LoadSpriteSheet("LZ/Objects.gif")
	SpriteFrame(-16, -8, 32, 16, 1, 1)

	foreach (TypeName[Moving Block], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].priority = PRIORITY_XBOUNDS
	next

	SetTableValue(TypeName[Moving Block], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(MovingBlock_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(MovingBlock_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

event RSDKDraw
	DrawSprite(0)
end event


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