// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Rock Crusher 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.origin.y // Unused

private alias 0 : ROCKCRUSHER_RISE
private alias 1 : ROCKCRUSHER_FALL

// Player aliases
private alias object.type    : player.type
private alias object.ypos    : player.ypos
private alias object.gravity : player.gravity


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

reserve function RockCrusher_DebugDraw
reserve function RockCrusher_DebugSpawn


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

private function RockCrusher_DebugDraw
	DrawSprite(0)
end function


private function RockCrusher_DebugSpawn
	CreateTempObject(TypeName[Rock Crusher], 0, object.xpos, object.ypos)
	object[tempObjectPos].origin.y = object[tempObjectPos].ypos
	object[tempObjectPos].yvel = -0x10000
end function


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

event ObjectUpdate
	if object.state == ROCKCRUSHER_RISE
		object.timer++
		if object.timer == 96
			object.timer = 0
			object.yvel = 0x80000
			object.state = ROCKCRUSHER_FALL
		end if
	else
		object.timer++
		if object.timer == 12
			object.timer = 0
			object.yvel = -0x10000
			object.state = ROCKCRUSHER_RISE
		end if

		if object.timer == 6
			PlaySfx(SfxName[Ground Impact], false)
		end if
	end if

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_SOLID2, object.entityPos, -15, -64, 15, 64, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		switch checkResult
		case COL_TOP
			player[currentPlayer].ypos += object.yvel
			break

		case COL_BOTTOM
			if player[currentPlayer].gravity == GRAVITY_GROUND
				if object.yvel > 0
					// If the rock's going down, then crush the player!
					CallFunction(Player_Kill)
#platform: USE_ORIGINS
					// If we're in a mission and P2 was just killed, then force P1 to die too
					// (Part of the Bodyguard mission)
					if game.playMode == BOOT_PLAYMODE_MISSION
						if player[1].type == TypeName[Player 2 Object]
							game.forceKillPlayer = true
						end if
					end if
#endplatform
				end if
			end if
			break
			
		end switch
	next
	
	object.ypos += object.yvel
end event


event ObjectDraw
	DrawSprite(0)
end event


event ObjectStartup
	LoadSpriteSheet("MCZ/Objects.gif")
	SpriteFrame(-15, -74, 30, 148, 201, 0)

	foreach (TypeName[Rock Crusher], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].origin.y = object[arrayPos0].ypos // even if this is stored, it's never referenced again
		object[arrayPos0].yvel = -0x10000
	next

	// Add this object to the debug object list
	SetTableValue(TypeName[Rock Crusher], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(RockCrusher_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(RockCrusher_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

event RSDKDraw
	DrawSprite(0)
	
	if editor.showGizmos == true
		editor.drawingOverlay = true
		
		object.inkEffect = INK_BLEND
		temp0 = object.ypos
		temp0 -= 0x5F0000
		DrawSpriteFX(0, FX_INK, object.xpos, temp0)
		
		editor.drawingOverlay = false
	end if
end event


event RSDKLoad
	LoadSpriteSheet("MCZ/Objects.gif")
	SpriteFrame(-15, -74, 30, 148, 201, 0)

	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
