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

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

private alias object.value1 : object.parent // Used by CNZEggman, not here though
private alias object.value2 : object.offset.y

// States
private alias 0 : CNZEGGMANBOMB_HELD
private alias 1 : CNZEGGMANBOMB_DROPPED
private alias 2 : CNZEGGMANBOMB_SHRAPNEL

// Path ID Aliases
private alias 0 : PATH_A


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

event ObjectUpdate
	switch object.state
	case CNZEGGMANBOMB_HELD
		object.frame = 2

		if object.offset.y < 0x2E0000
			object.offset.y += 0x10000
		end if

		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -10, -10, 10, 10, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == true
				CallFunction(Player_Hit)
			end if
		next
		break

	case CNZEGGMANBOMB_DROPPED
		object.ypos += object.yvel
		object.yvel += 0x3800

		ObjectTileCollision(CSIDE_FLOOR, 0, 10, PATH_A)
		if checkResult == true
			object.type = TypeName[Blank Object]

			CreateTempObject(TypeName[Eggman Bomb], 0, object.xpos, object.ypos)
			object[tempObjectPos].state = CNZEGGMANBOMB_SHRAPNEL
			object[tempObjectPos].xvel = -0x10000
			object[tempObjectPos].yvel = -0x30000

			CreateTempObject(TypeName[Eggman Bomb], 0, object.xpos, object.ypos)
			object[tempObjectPos].state = CNZEGGMANBOMB_SHRAPNEL
			object[tempObjectPos].xvel =  0x10000
			object[tempObjectPos].yvel = -0x30000

			PlaySfx(SfxName[Explosion], false)
		end if

		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
		break

	case CNZEGGMANBOMB_SHRAPNEL
		object.frame = object.animationTimer
		object.frame >>= 1
		object.animationTimer++
		object.animationTimer &= 3

		object.xpos += object.xvel
		object.ypos += object.yvel
		object.yvel += 0x3800
		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

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


event ObjectDraw
	DrawSprite(object.frame)
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone04")
	if checkResult == true
		LoadSpriteSheet("CNZ/Objects.gif")
		SpriteFrame(-4, -4, 8, 8, 145, 172)
		SpriteFrame(-4, -4, 8, 8, 154, 172)
		SpriteFrame(-12, -12, 23, 23, 145, 181)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-4, -4, 8, 8, 224, 130)
		SpriteFrame(-4, -4, 8, 8, 224, 139)
		SpriteFrame(-12, -12, 23, 23, 200, 130)
	end if
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone04")
	if checkResult == true
		LoadSpriteSheet("CNZ/Objects.gif")
		SpriteFrame(-4, -4, 8, 8, 145, 172)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-4, -4, 8, 8, 224, 130)
	end if
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
