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

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

private alias object.value1 : object.drawPos.y
private alias object.value2 : object.collisionOffset.y
private alias object.value3 : object.startPos.y
private alias object.value4 : object.amplitude.y

private alias 0 : FALLINGBLOCK_IDLE
private alias 1 : FALLINGBLOCK_FLOATING
private alias 2 : FALLINGBLOCK_WAITFORPLAYER
private alias 3 : FALLINGBLOCK_FALLING
private alias 4 : FALLINGBLOCK_FLOATING_LAVA

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

private alias object.value17 : debugMode.currentSelection

// Tile Flag Aliases
private alias 1 : TILE_LAVA

// Tile Info ID Aliases
private alias 8 : TILEINFO_ANGLEB

// Path ID Aliases
private alias 0 : PATH_A


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

reserve function FallingBlock_DebugDraw
reserve function FallingBlock_DebugSpawn
reserve function FallingBlock_HandleCollisions

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

private value FallingBlock_startDebugID = 0


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

private function FallingBlock_DebugDraw
	DrawSprite(0)
end function


private function FallingBlock_DebugSpawn
	CreateTempObject(TypeName[Falling Block], 0, object.xpos, object.ypos)
	object[tempObjectPos].state = debugMode[0].currentSelection
	object[tempObjectPos].state -= FallingBlock_startDebugID

	switch object[tempObjectPos].state
	case FALLINGBLOCK_IDLE
		object[tempObjectPos].amplitude.y = 0x400
		break

	case FALLINGBLOCK_FLOATING
	case FALLINGBLOCK_WAITFORPLAYER
		object[tempObjectPos].ypos -= 0x80000
		object[tempObjectPos].amplitude.y = 0x400
		break

	case FALLINGBLOCK_FALLING //Actually FALLINGBLOCK_FLOATING_LAVA but we dont wanna spawn FALLINGBLOCK_FALLING so we just advance
		object[tempObjectPos].state++
		object[tempObjectPos].amplitude.y = 0x200
		break
		
	end switch
end function


private function FallingBlock_HandleCollisions
	Sin(object.collisionOffset.y, object.angle)
	object.collisionOffset.y *= object.amplitude.y
	object.collisionOffset.y += object.ypos
	object.collisionOffset.y &= 0xFFFF0000
	object.collisionOffset.y -= object.drawPos.y
	temp0 = object.ypos

	object.ypos = object.drawPos.y
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_SOLID, object.entityPos, -16, -16, 16, 16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		switch checkResult
		case COL_TOP
			player[currentPlayer].ypos += object.collisionOffset.y
			break
			
		case COL_BOTTOM
			if player[currentPlayer].gravity == GRAVITY_GROUND
				CallFunction(Player_Kill)
			end if
			break
			
		end switch
	next
	object.ypos = temp0

	Sin(object.drawPos.y, object.angle)
	object.drawPos.y *= object.amplitude.y
	object.drawPos.y += object.ypos
	object.drawPos.y &= 0xFFFF0000
end function


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

event ObjectUpdate
	switch object.state
	case FALLINGBLOCK_IDLE
		object.drawPos.y = object.ypos
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_SOLID, object.entityPos, -16, -16, 16, 16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		next
		break

	case FALLINGBLOCK_FLOATING
		object.priority = PRIORITY_ACTIVE
		object.angle = oscillation
		object.angle <<= 2
		CallFunction(FallingBlock_HandleCollisions)
		break

	case FALLINGBLOCK_WAITFORPLAYER
		object.priority = PRIORITY_ACTIVE
		object.angle = oscillation
		object.angle <<= 2
		CallFunction(FallingBlock_HandleCollisions)

		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if player[currentPlayer].ypos > object.ypos
				temp0 = object.xpos
				temp0 -= player[currentPlayer].xpos
				Abs(temp0)
				if temp0 < 0x900000
					object.state = FALLINGBLOCK_FALLING
					object.ypos = object.drawPos.y
					object.amplitude.y = 0x200
				end if
			end if
		next
		break

	case FALLINGBLOCK_FALLING
		object.priority = PRIORITY_ACTIVE
		object.yvel += 0x1800
		object.ypos += object.yvel
		object.drawPos.y = object.ypos

		ObjectTileCollision(CSIDE_FLOOR, 0, 16, PATH_A)
		if checkResult == true
			temp1 = object.iypos
			temp1 += 8
			Get16x16TileInfo(temp0, object.ixpos, temp1, TILEINFO_ANGLEB)
			if temp0 == TILE_LAVA
				object.state = FALLINGBLOCK_FLOATING_LAVA
			else
				object.state = FALLINGBLOCK_IDLE
			end if
		end if

		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_SOLID, object.entityPos, -16, -16, 16, 16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == COL_BOTTOM
				if player[currentPlayer].gravity == GRAVITY_GROUND
					CallFunction(Player_Kill)
				end if
			end if
		next
		break

	case FALLINGBLOCK_FLOATING_LAVA
		object.priority = PRIORITY_ACTIVE
		object.angle = oscillation
		object.angle <<= 1
		CallFunction(FallingBlock_HandleCollisions)
		break
		
	end switch

	if object.outOfBounds == true
		temp0 = object.ypos
		object.ypos = object.startPos.y
		if object.outOfBounds == true
			object.yvel = 0
			object.priority = PRIORITY_BOUNDS
			object.state = object.propertyValue
			object.state &= 7
			if object.state > FALLINGBLOCK_FLOATING_LAVA
				object.state = FALLINGBLOCK_IDLE
			end if

			switch object.state
			case FALLINGBLOCK_IDLE
			case FALLINGBLOCK_FLOATING
			case FALLINGBLOCK_WAITFORPLAYER
				object.amplitude.y = 0x400
				break

			case FALLINGBLOCK_FLOATING_LAVA
				object.amplitude.y = 0x200
				break
			end switch
		else
			object.ypos = temp0
		end if
	end if
end event


event ObjectDraw
	DrawSpriteXY(0, object.xpos, object.drawPos.y)
end event


event ObjectStartup
	LoadSpriteSheet("MZ/Objects.gif")
	SpriteFrame(-16, -16, 32, 32, 191, 289)

	foreach (TypeName[Falling Block], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].state = object[arrayPos0].propertyValue
		object[arrayPos0].state &= 7
		if object[arrayPos0].state > FALLINGBLOCK_FLOATING_LAVA
			object[arrayPos0].state = FALLINGBLOCK_IDLE
		end if

		switch object[arrayPos0].state
		case FALLINGBLOCK_IDLE
			object[arrayPos0].amplitude.y = 0x400
			break

		case FALLINGBLOCK_FLOATING
		case FALLINGBLOCK_WAITFORPLAYER
			object[arrayPos0].ypos -= 0x80000
			object[arrayPos0].amplitude.y = 0x400
			break

		case FALLINGBLOCK_FALLING
			break

		case FALLINGBLOCK_FLOATING_LAVA
			object[arrayPos0].amplitude.y = 0x200
			break
			
		end switch
		object[arrayPos0].startPos.y = object[arrayPos0].ypos
	next

	temp0 = 0
	FallingBlock_startDebugID = DebugMode_ObjCount
	while temp0 < 4
		SetTableValue(TypeName[Falling Block], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(FallingBlock_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(FallingBlock_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
			checkResult &= 7
			break
			
		case 0 // type
			checkResult = object.propertyValue
			checkResult &= 7
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			object.propertyValue &= 7
			break
			
		case 0 // type
			object.propertyValue = editor.variableValue
			object.propertyValue &= 7
			break
			
		end switch
	end if
end event


event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("MZ/Objects.gif")
	SpriteFrame(-16, -16, 32, 32, 191, 289)

	AddEditorVariable("type")
	SetActiveVariable("type")
	AddEnumVariable("Idle", 0)
	AddEnumVariable("Floating (Won't Drop)", 1)
	AddEnumVariable("Floating (Will Drop)", 2)
	AddEnumVariable("Floating in Lava", 4)
end event
