// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: DER Bomb 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.originPos.y

private alias 0 : DERBOMB_LAUNCHED
private alias 1 : DERBOMB_LANDED
private alias 2 : DERBOMB_EXPLODING

// Path ID Aliases
private alias 0 : PATH_A


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

event ObjectUpdate
	switch object.state
	case DERBOMB_LAUNCHED
		object.xpos += object.xvel
		object.ypos += object.yvel
		object.yvel += 0x3800
		
		// Only check for collision if lower than where it started from
		// (If there's a roof, we don't want to land on it)
		if object.ypos > object.originPos.y
			ObjectTileCollision(CSIDE_FLOOR, 0, 16, PATH_A)
			if checkResult == true
				object.timer = 64
				object.state = DERBOMB_LANDED
			end if
		end if
		break
		
	case DERBOMB_LANDED
		object.timer--
		if object.timer < 0
			object.timer = 40
			object.frame = 1
			CreateTempObject(TypeName[Explosion], 0, object.xpos, object.ypos)
			object[tempObjectPos].drawOrder = 5
			PlaySfx(SfxName[Explosion], false)
			object.state = DERBOMB_EXPLODING
		end if
		break
		
	case DERBOMB_EXPLODING
		object.timer--
		if object.timer < 0
			object.state = 0
			object.frame = 0
			object.type = TypeName[Blank Object]
		end if
		break
		
	end switch
	
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -12, -16, 12, 16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		if checkResult == true
			CallFunction(Player_Hit)
		end if
	next
end event


event ObjectDraw
	DrawSprite(object.frame)
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone12")
	if checkResult == true
		LoadSpriteSheet("DEZ/Objects.gif")

		// Aerial Bomb Frame
		SpriteFrame(-14, -16, 28, 32, 401, 125)

		// Grounded Bomb Frame
		SpriteFrame(0, 0, 1, 1, 401, 125)

		// Unused Bomb Shrapnel Frames
		SpriteFrame(-4, -4, 8, 8, 356, 187)
		SpriteFrame(-4, -4, 8, 8, 365, 187)
	else
		LoadSpriteSheet("MBZ/Objects.gif")

		// Aerial Bomb Frame
		SpriteFrame(-14, -16, 28, 32, 913, 125)

		// Grounded Bomb Frame
		SpriteFrame(0, 0, 1, 1, 913, 125)

		// Unused Bomb Shrapnel Frames
		SpriteFrame(-4, -4, 8, 8, 868, 187)
		SpriteFrame(-4, -4, 8, 8, 877, 187)
	end if
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone12")
	if checkResult == true
		LoadSpriteSheet("DEZ/Objects.gif")
		SpriteFrame(-14, -16, 28, 32, 401, 125)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-14, -16, 28, 32, 913, 125)
	end if

	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
