// ----------------------------------
// RSDK Project: Sonic 1
// Script Description: Falling Lava 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.splashAnimTimer
private alias object.value2 : object.splashFrame
private alias object.value3 : object.splashPos.y

private alias 0 : FALLINGLAVA_FALLING
private alias 1 : FALLINGLAVA_SPLASHING

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

reserve function FallingLava_DebugDraw
reserve function FallingLava_DebugSpawn

private function FallingLava_DebugDraw
	DrawSprite(0)
end function


private function FallingLava_DebugSpawn
	CreateTempObject(TypeName[Falling Lava], 0, object.xpos, object.ypos)
end function


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

event ObjectUpdate
	if object.propertyValue == 0
		BoxCollisionTest(C_TOUCH, object.entityPos, -256, -256, 256, 128, 0, C_BOX, C_BOX, C_BOX, C_BOX)
		if checkResult == true
			object.timer--

			if object.timer <= 0
				object.timer = 240

				CreateTempObject(TypeName[Falling Lava], 1, object.xpos, object.ypos)
				object[tempObjectPos].drawOrder = 4
				object[tempObjectPos].ypos -= 0x1600000
				object[tempObjectPos].splashPos.y = object.ypos

				PlaySfx(SfxName[Fire Burn], false)
			end if
		end if
	else
		switch object.state
		case FALLINGLAVA_FALLING
			object.yvel += 0x1800
			object.ypos += object.yvel

			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				BoxCollisionTest(C_TOUCH, object.entityPos, -32, -128, 32, 128, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)

				if checkResult == true
					CallFunction(Player_FireHit)
				end if
			next

			if object.ypos >= object.splashPos.y
				CreateTempObject(TypeName[Lava Splash], 0, object.xpos, object.ypos)
				object[tempObjectPos].ypos += 0x800000
				object[tempObjectPos].drawOrder = 5
				object.state++
			end if
			break

		case FALLINGLAVA_SPLASHING
			object.yvel += 0x1800
			object.ypos += object.yvel

			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				BoxCollisionTest(C_TOUCH, object.entityPos, -32, -128, 32, 128, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				if checkResult == true
					CallFunction(Player_FireHit)
				end if
			next

			object.timer++
			if object.timer == 60
				object.type = TypeName[Blank Object]
			end if
			break

		end switch

		object.splashFrame = object.splashAnimTimer
		object.splashFrame /= 3
		object.splashFrame += 2
		object.splashAnimTimer++
		object.splashAnimTimer %= 6

		object.frame = object.animationTimer
		object.frame >>= 3
		object.animationTimer++
		object.animationTimer &= 15
	end if
end event


event ObjectDraw
	if object.propertyValue > 0
		DrawSprite(object.frame)
		DrawSprite(object.splashFrame)

		object.direction = FLIP_Y
		DrawSpriteFX(object.splashFrame, FX_FLIP, object.xpos, object.ypos)
	end if
end event


event ObjectStartup

	LoadSpriteSheet("MZ/Objects.gif")

	// Lava frames
	SpriteFrame(-32, -128, 64, 256, 317, 1)  // Falling Lava - #0
	SpriteFrame(-32, -128, 64, 256, 382, 1)  // Falling Lava - #1
	SpriteFrame(-32, -144, 64, 32, 382, 363) // Lava Splash  - #2
	SpriteFrame(-32, -144, 64, 32, 447, 363) // Lava Splash  - #3

	// Add Lava to the debug mode item list
	SetTableValue(TypeName[Falling Lava], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(FallingLava_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(FallingLava_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad

	LoadSpriteSheet("MZ/Objects.gif")

	// Lava frames
	SpriteFrame(-32, -128, 64, 256, 317, 1)  // Falling Lava - #0
	SpriteFrame(-32, -128, 64, 256, 382, 1)  // Falling Lava - #1
	SpriteFrame(-32, -144, 64, 32, 382, 363) // Lava Splash  - #2
	SpriteFrame(-32, -144, 64, 32, 447, 363) // Lava Splash  - #3
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
