// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Whisp Object
// Script Author: Christan Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 66 : TYPE_WHISP

// Function declarations
reserve function Whisp_Function145
reserve function Whisp_DebugDraw
reserve function Whisp_DebugSpawn

// Static Values

// Tables

function Whisp_Function145
	object[arrayPos0].xvel = 0
	object[arrayPos0].yvel = 0
	object[arrayPos0].value0 = 16
	object[arrayPos0].value1 = 4
	object[arrayPos0].animationTimer = 0
	object[arrayPos0].inkEffect = INK_ALPHA
	object[arrayPos0].alpha = 192
	object[arrayPos0].priority = PRIORITY_ACTIVE_BOUNDS
	object.state = 0
	object[arrayPos0].drawOrder = 5
end function


function Whisp_DebugDraw
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end function


function Whisp_DebugSpawn
	CreateTempObject(TypeName[Whisp], 0, object.xpos, object.ypos)
	object[tempObjectPos].value2 = object.xpos
	object[tempObjectPos].value3 = object.ypos
	arrayPos0 = object[tempObjectPos].entityPos
	CallFunction(Whisp_Function145)
end function


event ObjectMain
	switch object.state
	case 0
		object.priority = PRIORITY_ACTIVE
		object.value0--
		if object.value0 < 0
			object.value1--
			if object.value1 >= 0
				object.yvel = -0x10000
				object.value0 = 96
				object.state = 1
			else
				object.xvel = -0x20000
				object.yvel = -0x20000
				object.state = 2
			end if
		end if
		break
	case 1
		object.xpos += object.xvel
		object.ypos += object.yvel
		temp2 = 0x7FFFFFFF
		arrayPos0 = 0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			temp0 = object[currentPlayer].xpos
			temp0 -= object.xpos
			if temp0 < 0
				temp1 = 0
				FlipSign(temp0)
			else
				temp1 = 1
			end if
			if temp0 < temp2
				temp2 = temp0
				arrayPos0 = currentPlayer
				object.direction = temp1
			end if
		next
		if object[arrayPos0].xpos < object.xpos
			object.xvel -= 0x1000
			if object.xvel < -0x20000
				object.xvel = -0x20000
			end if
		else
			object.xvel += 0x1000
			if object.xvel > 0x20000
				object.xvel = 0x20000
			end if
		end if
		if object[arrayPos0].ypos < object.ypos
			object.yvel -= 0x1000
			if object.yvel < -0x20000
				object.yvel = -0x20000
			end if
		else
			object.yvel += 0x1000
			if object.yvel > 0x20000
				object.yvel = 0x20000
			end if
		end if
		object.value0--
		if object.value0 < 0
			Rand(object.value0, 32)
			object.state = 0
			object.xvel = 0
			object.yvel = 0
			object.direction = FLIP_NONE
		end if
		break
	case 2
		object.xpos += object.xvel
		object.ypos += object.yvel
		break
	end switch
	object.animationTimer++
	object.animationTimer &= 3
	object.frame = object.animationTimer
	object.frame >>= 1
	object.frame++
	if object.outOfBounds == 1
		temp0 = object.xpos
		temp1 = object.ypos
		object.xpos = object.value2
		object.ypos = object.value3
		if object.outOfBounds == 1
			arrayPos0 = object.entityPos
			CallFunction(Whisp_Function145)
		else
			object.xpos = temp0
			object.ypos = temp1
		end if
	end if
	CallFunction(PlayerObject_EnemyPop)
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -8, -8, 8, 8, currentPlayer, object[currentPlayer].value40, object[currentPlayer].value38, object[currentPlayer].value41, object[currentPlayer].value39)
		if checkResult == 1
			CallFunction(PlayerObject_BadnikBreak)
		end if
	next
end event


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


event ObjectStartup
	CheckCurrentStageFolder("Zone03")
	if checkResult == 1
		LoadSpriteSheet("ARZ/Objects.gif")
		SpriteFrame(-12, -7, 24, 15, 34, 42)
		SpriteFrame(-9, -8, 21, 6, 34, 58)
		SpriteFrame(-7, -7, 18, 7, 34, 65)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-12, -7, 24, 15, 111, 317)
		SpriteFrame(-9, -8, 21, 6, 110, 302)
		SpriteFrame(-7, -7, 18, 7, 110, 309)
	end if
	foreach (TypeName[Whisp], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].value2 = object[arrayPos0].xpos
		object[arrayPos0].value3 = object[arrayPos0].ypos
		CallFunction(Whisp_Function145)
	next
	SetTableValue(TypeName[Whisp], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Whisp_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Whisp_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
