// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Whisp 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.buzzCount
private alias object.value2 : object.startPos.x
private alias object.value3 : object.startPos.y

// States
private alias 0 : WHISP_BUZZING
private alias 1 : WHISP_FLYTOWARDTARGET
private alias 2 : WHISP_FLYAWAY

// Player Aliases
private alias object.xpos : player.xpos

private alias object.value40 : player.hitboxLeft
private alias object.value38 : player.hitboxTop
private alias object.value41 : player.hitboxRight
private alias object.value39 : player.hitboxBottom


// ========================
// Function Declarations
// ========================

reserve function Whisp_Setup
reserve function Whisp_DebugDraw
reserve function Whisp_DebugSpawn


// ========================
// Function Definitions
// ========================

private function Whisp_Setup
	object[arrayPos0].xvel 			 = 0
	object[arrayPos0].yvel 			 = 0
	object[arrayPos0].timer 		 = 16
	object[arrayPos0].buzzCount		 = 4
	object[arrayPos0].animationTimer = 0
	object[arrayPos0].inkEffect 	 = 2
	object[arrayPos0].alpha 		 = 0xC0
	object[arrayPos0].priority 		 = PRIORITY_BOUNDS

	// This only works by luck, state is 0 (WHISP_BUZZING) by default and the current object is the same as arrayPos0 when off screen resetting
	// This really should be object[arrayPos0].state for safety, it's likely that this is a typo
	object.state = WHISP_BUZZING
end function


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


private function Whisp_DebugSpawn
	CreateTempObject(TypeName[Whisp], 0, object.xpos, object.ypos)
	object[tempObjectPos].startPos.x = object.xpos
	object[tempObjectPos].startPos.y = object.ypos

	arrayPos0 = object[tempObjectPos].entityPos
	CallFunction(Whisp_Setup)
end function


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

event ObjectUpdate
	switch object.state
	case WHISP_BUZZING
		object.priority = PRIORITY_ACTIVE
		object.timer--
		if object.timer < 0
			object.buzzCount--
			if object.buzzCount >= 0
				object.yvel = -0x10000
				object.timer = 96
				object.state = WHISP_FLYTOWARDTARGET
			else
				object.xvel = -0x20000
				object.yvel = -0x20000
				object.state = WHISP_FLYAWAY
			end if
		end if
		break

	case WHISP_FLYTOWARDTARGET
		object.xpos += object.xvel
		object.ypos += object.yvel

		temp2 = 0x7FFFFFFF
		arrayPos0 = 0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			temp0 = player[currentPlayer].xpos
			temp0 -= object.xpos
			if temp0 < 0
				temp1 = FLIP_NONE
				FlipSign(temp0)
			else
				temp1 = FLIP_X
			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.timer--
		if object.timer < 0
			Rand(object.timer, 32)
			object.state = WHISP_BUZZING
			object.xvel = 0
			object.yvel = 0
			object.direction = FLIP_NONE
		end if
		break

	case WHISP_FLYAWAY
		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 == true
		temp0 = object.xpos
		temp1 = object.ypos
		object.xpos = object.startPos.x
		object.ypos = object.startPos.y
		if object.outOfBounds == true
			arrayPos0 = object.entityPos
			CallFunction(Whisp_Setup)
		else
			object.xpos = temp0
			object.ypos = temp1
		end if
	end if

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -8, -8, 8, 8, currentPlayer, player[currentPlayer].hitboxLeft, player[currentPlayer].hitboxTop, player[currentPlayer].hitboxRight, player[currentPlayer].hitboxBottom)
		if checkResult == true
			CallFunction(Player_BadnikBreak)
		end if
	next
end event


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


event ObjectStartup
	CheckCurrentStageFolder("Zone03")
	if checkResult == true
		LoadSpriteSheet("ARZ/Objects.gif")
		
		// Body Frame
		SpriteFrame(-12, -7, 24, 15, 34, 42)
		
		// Wings Up Frame
		SpriteFrame(-9, -8, 21, 6, 34, 58)
		
		// Wings Down Frame
		SpriteFrame(-7, -7, 18, 7, 34, 65)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		
		// Body Frame
		SpriteFrame(-12, -7, 24, 15, 111, 317)
		
		// Wings Up Frame
		SpriteFrame(-9, -8, 21, 6, 110, 302)
		
		// Wings Down Frame
		SpriteFrame(-7, -7, 18, 7, 110, 309)
	end if

	foreach (TypeName[Whisp], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.x = object[arrayPos0].xpos
		object[arrayPos0].startPos.y = object[arrayPos0].ypos
		CallFunction(Whisp_Setup)
	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


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

event RSDKDraw
	DrawSprite(0) // body
	DrawSprite(1) // wings
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone03")
	if checkResult == true
		LoadSpriteSheet("ARZ/Objects.gif")
		SpriteFrame(-12, -7, 24, 15, 34, 42)
		SpriteFrame(-9, -8, 21, 6, 34, 58)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-12, -7, 24, 15, 111, 317)
		SpriteFrame(-9, -8, 21, 6, 110, 302)
	end if
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
