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

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

private alias object.value1 : object.startPos.x
private alias object.value2 : object.spriteSprX

private alias 0 : LAVAFLOW_AWAITPLAYER
private alias 1 : LAVAFLOW_FLOWING
private alias 2 : LAVAFLOW_FINISHEDFLOWING

// Player Aliases
private alias object.state : player.state
private alias object.xpos  : player.xpos
private alias object.ixpos : player.ixpos
private alias object.collisionLeft: player.collisionLeft

// MZSetup aliases
private alias object.value3 : mzSetup.lavaAnimTimer

// Reserved object slots
private alias 10 : SLOT_ZONESETUP


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

reserve function LavaFlow_DebugDraw
reserve function LavaFlow_DebugSpawn


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

private function LavaFlow_DebugDraw
	DrawSprite(0)
end function


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


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

event ObjectUpdate
	switch object.state
	case LAVAFLOW_AWAITPLAYER
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -128, -33, 160, 32, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == true
				CallFunction(Player_FireHit)
			end if

			BoxCollisionTest(C_SOLID, object.entityPos, -128, -31, 128, 32, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)

			BoxCollisionTest(C_TOUCH, object.entityPos, 256, -32, 288, 32, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == true
				object.state++
				object.priority = PRIORITY_ACTIVE
			end if
		next
		break

	case LAVAFLOW_FLOWING
		if player[0].state != Player_State_Death
			if player[0].state != Player_State_Hurt
				object.xpos += 0x18000
				if object.xpos >= 0x6480000
					object.xpos = 0x6480000
					object.state++
				end if
			end if
		end if

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

			BoxCollisionTest(C_SOLID, object.entityPos, -128, -31, 128, 32, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)

			BoxCollisionTest(C_TOUCH, object.entityPos, -128, -23, 128, 32, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == true
				player[currentPlayer].ixpos = player[currentPlayer].collisionLeft
				FlipSign(player[currentPlayer].xpos)
				if object.xpos < 0x5800000
					player[currentPlayer].xpos += 0x800000
				else
					player[currentPlayer].xpos += 0x600000
				end if

				player[currentPlayer].xpos += object.xpos
			end if
		next
		break

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

			BoxCollisionTest(C_SOLID, object.entityPos, -128, -24, 160, 32, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)

			BoxCollisionTest(C_TOUCH, object.entityPos, -128, -23, 128, 32, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == true
				player[currentPlayer].ixpos = player[currentPlayer].collisionLeft
				FlipSign(player[currentPlayer].xpos)
				player[currentPlayer].xpos += 0x600000
				player[currentPlayer].xpos += object.xpos
			end if
		next

		if object.outOfBounds == true
			temp0 = object.xpos
			object.xpos = object.startPos.x
			if object.outOfBounds == true
				object.state = LAVAFLOW_AWAITPLAYER
			else
				object.xpos = temp0
			end if
		end if
		break
		
	end switch

	Sin(object.spriteSprX, mzSetup[SLOT_ZONESETUP].lavaAnimTimer)
	object.spriteSprX += 512
	object.spriteSprX >>= 2
	object.spriteSprX &= 31
	object.spriteSprX++

	object.frame = object.animationTimer
	object.frame /= 10
	object.frame += 4
	object.animationTimer++
	object.animationTimer %= 40

	// Lava is synced across the entire stage, via the [MZ Setup] Object's frame
	switch object[SLOT_ZONESETUP].frame
	case 0
		EditFrame(0, -160, -32, 160, 32, object.spriteSprX, 405)
		EditFrame(1, 0, -32, 124, 32, object.spriteSprX, 405)
		EditFrame(2, -160, 0, 160, 32, object.spriteSprX, 405)
		EditFrame(3, 0, 0, 156, 32, object.spriteSprX, 405)
		break

	case 1
		EditFrame(0, -160, -32, 160, 32, object.spriteSprX, 438)
		EditFrame(1, 0, -32, 124, 32, object.spriteSprX, 438)
		EditFrame(2, -160, 0, 160, 32, object.spriteSprX, 438)
		EditFrame(3, 0, 0, 156, 32, object.spriteSprX, 438)
		break

	case 2
		EditFrame(0, -160, -32, 160, 32, object.spriteSprX, 471)
		EditFrame(1, 0, -32, 124, 32, object.spriteSprX, 471)
		EditFrame(2, -160, 0, 160, 32, object.spriteSprX, 471)
		EditFrame(3, 0, 0, 156, 32, object.spriteSprX, 471)
		break
		
	end switch
end event


event ObjectDraw
	DrawSprite(0)
	DrawSprite(1)
	DrawSprite(2)
	DrawSprite(3)
	DrawSprite(object.frame)
end event


event ObjectStartup
	LoadSpriteSheet("MZ/Objects.gif")
	SpriteFrame(-128, -32, 128, 32, 1, 405)
	SpriteFrame(0, -32, 128, 32, 1, 405)
	SpriteFrame(-128, 0, 256, 32, 1, 405)
	SpriteFrame(0, 0, 256, 32, 1, 405)
	SpriteFrame(124, -32, 60, 64, 1, 194)
	SpriteFrame(124, -32, 60, 64, 62, 194)
	SpriteFrame(124, -32, 60, 64, 1, 259)
	SpriteFrame(124, -32, 60, 64, 62, 259)

	foreach (TypeName[Lava Flow], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.x = object[arrayPos0].xpos
		object[arrayPos0].drawOrder  = 4
	next

	SetTableValue(TypeName[Lava Flow], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(LavaFlow_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(LavaFlow_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

event RSDKDraw
	DrawSprite(0)
	DrawSprite(1)
	DrawSprite(2)
	DrawSprite(3)
	DrawSprite(object.frame)
end event


event RSDKLoad
	LoadSpriteSheet("MZ/Objects.gif")
	SpriteFrame(-128, -32, 128, 32, 1, 405)
	SpriteFrame(0, -32, 128, 32, 1, 405)
	SpriteFrame(-128, 0, 256, 32, 1, 405)
	SpriteFrame(0, 0, 256, 32, 1, 405)
	SpriteFrame(124, -32, 60, 64, 1, 194)
	SpriteFrame(124, -32, 60, 64, 62, 194)
	SpriteFrame(124, -32, 60, 64, 1, 259)
	SpriteFrame(124, -32, 60, 64, 62, 259)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
