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

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

private alias object.value0 : object.bombTime // Set on spawn by master Ball Hog object

// Path ID Aliases
private alias 0 : PATH_A


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

event ObjectUpdate
	object.xpos += object.xvel
	object.ypos += object.yvel
	object.yvel += 0x3800

	if object.yvel > 0
		temp0 = object.ypos

		ObjectTileCollision(CSIDE_FLOOR, 0, 8, PATH_A)
		if checkResult == true
			object.yvel = -0x30000
			object.ypos = temp0
			ObjectTileGrip(CSIDE_FLOOR, -2, 8, PATH_A)
			temp1 = object.ypos
			object.ypos = temp0
			ObjectTileGrip(CSIDE_FLOOR, 2, 8, PATH_A)
			temp2 = object.ypos
			object.ypos = temp0
			if object.xvel > 0
				if temp2 < temp1
					FlipSign(object.xvel)
				end if
			else
				if temp1 < temp2
					FlipSign(object.xvel)
				end if
			end if
		end if
	end if

	// Check collision with players
	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

	// Animate the object
	object.animationTimer++
	if object.animationTimer >= 6
		object.frame ^= 1
		object.animationTimer = 0
	end if

	// Update the bomb's countdown timer
	if object.bombTime > 0
		object.bombTime--
	else
		// Explode the bomb
		ResetObjectEntity(object.entityPos, TypeName[Blank Object], 0, object.xpos, object.ypos)
		CreateTempObject(TypeName[Explosion], 0, object.xpos, object.ypos)
		object[tempObjectPos].drawOrder = 4
		PlaySfx(SfxName[Explosion], false)
	end if
end event


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


event ObjectStartup
	CheckCurrentStageFolder("Zone06")
	if checkResult == true
		LoadSpriteSheet("SBZ/Objects.gif")
		SpriteFrame(-7, -7, 14, 14, 82, 126)
		SpriteFrame(-7, -7, 14, 14, 82, 143)
	end if

	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-7, -7, 14, 14, 165, 248)
		SpriteFrame(-7, -7, 14, 14, 182, 248)
	end if
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-7, -7, 14, 14, 165, 248)
	else
		LoadSpriteSheet("SBZ/Objects.gif")
		SpriteFrame(-7, -7, 14, 14, 82, 126)
	end if
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
