// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Eggman Drill Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

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

private alias 0 : DRILL_ACTIVE
private alias 1 : DRILL_FELLOFF


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

event ObjectUpdate
	if object.state == DRILL_ACTIVE
		if object.interaction == true
			// Object is active, check collision with players

			// Backup the object's position since it'll need to be changed for collision checks
			temp0 = object.xpos
			temp1 = object.ypos

			// First, check for the hitbox at the base of the drill
			Sin(object.xpos, object.angle)
			if object.direction == FLIP_NONE
				object.xpos *= -0xC00
			else
				object.xpos *= 0xC00
			end if
			Cos(object.ypos, object.angle)
			object.ypos *= -0xC00
			object.xpos += temp0
			object.ypos += temp1
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				BoxCollisionTest(C_TOUCH, object.entityPos, -6, -6, 6, 6, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				if checkResult == true
					CallFunction(Player_Hit)
				end if
			next

			// Check for the hitbox at the tip of the drill, too
			Sin(object.xpos, object.angle)
			if object.direction == FLIP_NONE
				object.xpos *= -0x1300
			else
				object.xpos *= 0x1300
			end if
			Cos(object.ypos, object.angle)
			object.ypos *= -0x1300
			object.xpos += temp0
			object.ypos += temp1
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				BoxCollisionTest(C_TOUCH, object.entityPos, -4, -4, 4, 4, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				if checkResult == true
					CallFunction(Player_Hit)
				end if
			next

			// Restore the object's position
			object.xpos = temp0
			object.ypos = temp1
		end if
	else
		// Drills falling off after boss is defeated
		object.xpos += object.xvel
		object.ypos += object.yvel
		object.yvel += 0x3800

		if object.outOfBounds == true
			object.type = TypeName[Blank Object]
		end if
	end if
end event


event ObjectDraw
	DrawSpriteFX(object.frame, FX_ROTATE, object.xpos, object.ypos)
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone06")
	if checkResult == true
		LoadSpriteSheet("MCZ/Objects.gif")
		SpriteFrame(-12, -48, 24, 64, 1, 132)
		SpriteFrame(-12, -48, 24, 64, 26, 132)
		SpriteFrame(-12, -48, 24, 64, 51, 132)
		// Mid-turn frame inbetween the two is unused, the sprite is rotated via script FX instead
		SpriteFrame(-48, -12, 64, 24, 150, 182)
		SpriteFrame(-48, -12, 64, 24, 150, 207)
		SpriteFrame(-48, -12, 64, 24, 150, 232)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-12, -48, 24, 64, 42, 191)
		SpriteFrame(-12, -48, 24, 64, 67, 191)
		SpriteFrame(-12, -48, 24, 64, 92, 191)

		SpriteFrame(-48, -12, 64, 24, 117, 181)
		SpriteFrame(-48, -12, 64, 24, 117, 206)
		SpriteFrame(-48, -12, 64, 24, 117, 231)
	end if
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone06")
	if checkResult == true
		LoadSpriteSheet("MCZ/Objects.gif")
		SpriteFrame(-12, -48, 24, 64, 1, 132)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-12, -48, 24, 64, 42, 191)
	end if

	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
